-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Write One Block Phantom Files #83
Open
kn8483
wants to merge
23
commits into
ttricco:main
Choose a base branch
from
kn8483:write_phantom
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f1227f5
Working simple file write tests
kn8483 a66f614
Working tests for simple phantom file in write_phantom
kn8483 75b1a8b
cleaned up read test
kn8483 c2db266
More clean up
kn8483 16be3f3
Reverted reader test
kn8483 fae948b
Some cleanup. Determine default types.
kn8483 193babb
Mostly cleaned version of basic write functionality.
kn8483 f7568f9
Single block write working, but not multiple blocks.
kn8483 0a9f89f
Multiple block Phantom writes (not working)
kn8483 b27d8ea
Buffer error
kn8483 81109fe
Particles and sinks seems to be working.
kn8483 e8ddcfe
Test clean up
kn8483 bf1badb
Some clean up and removed large files.
kn8483 39e8c10
store def_int and def_real
ttricco 107fd4e
bug fix tag size writing
ttricco 2ee9585
use _write_fortran_block where possible
ttricco af9ddb9
preserve header var dtypes in params
ttricco d171328
handle writing of duplicate tags in global header
ttricco 4d7ac8b
Changes to write particles and sinks. Changed write method signature …
kn8483 91f93fc
Merge branch 'main' of https://github.com/ttricco/sarracen into write…
kn8483 bd69844
fixed import
kn8483 dc6e82c
Changes to read nparttot as int64
kn8483 19a5755
Fixed header types. Phantom works.
kn8483 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
import pandas as pd | ||
import numpy as np | ||
import sarracen | ||
import tempfile | ||
|
||
|
||
def _create_capture_pattern(def_int, def_real): | ||
""" Construct capture pattern. """ | ||
|
||
read_tag = np.array([13], dtype='int32') | ||
i1 = np.array([60769], dtype=def_int) | ||
r2 = np.array([60878], dtype=def_real) | ||
i2 = np.array([60878], dtype=def_int) | ||
iversion = np.array([0], dtype=def_int) | ||
i3 = np.array([690706], dtype=def_int) | ||
|
||
capture_pattern = bytearray(read_tag.tobytes()) | ||
capture_pattern += bytearray(i1.tobytes()) | ||
capture_pattern += bytearray(r2.tobytes()) | ||
capture_pattern += bytearray(i2.tobytes()) | ||
capture_pattern += bytearray(iversion.tobytes()) | ||
capture_pattern += bytearray(i3.tobytes()) | ||
capture_pattern += bytearray(read_tag.tobytes()) | ||
|
||
return capture_pattern | ||
|
||
|
||
def _create_file_identifier(): | ||
""" Construct 100-character file identifier. """ | ||
|
||
read_tag = np.array([13], dtype='int32') | ||
file_identifier = "Test of read_phantom".ljust(100) | ||
bytes_file = bytearray(read_tag.tobytes()) | ||
bytes_file += bytearray(map(ord, file_identifier)) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
return bytes_file | ||
|
||
|
||
def _create_global_header(massoftype=1e-6, massoftype_7=None, | ||
def_int=np.int32, def_real=np.float64): | ||
""" Construct global variables. Only massoftype in this example. """ | ||
|
||
read_tag = np.array([13], dtype='int32') | ||
bytes_file = bytearray() | ||
for i in range(8): # loop over 8 dtypes | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
nvars = (i == 5) + (massoftype_7 is not None) | ||
if i == 5: # default real | ||
nvars = np.array([nvars], dtype='int32') | ||
else: | ||
nvars = np.array([0], dtype='int32') | ||
bytes_file += bytearray(nvars.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
if i == 5: # default real | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
bytes_file += bytearray(map(ord, "massoftype".ljust(16))) | ||
if massoftype_7 is not None: | ||
bytes_file += bytearray(map(ord, "massoftype_7".ljust(16))) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
if i == 5: | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
bytes_file += bytearray(np.array([massoftype], dtype=def_real)) | ||
if massoftype_7 is not None: | ||
bytes_file += bytearray(np.array([massoftype_7], dtype=def_real)) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
return bytes_file | ||
|
||
|
||
def _create_particle_array(tag, data, dtype=np.float64): | ||
read_tag = np.array([13], dtype='int32') | ||
bytes_file = bytearray(read_tag.tobytes()) | ||
bytes_file += bytearray(map(ord, tag.ljust(16))) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
bytes_file += bytearray(read_tag.tobytes()) | ||
bytes_file += bytearray(np.array(data, dtype=dtype).tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
return bytes_file | ||
|
||
|
||
def get_one_block_phantom_file(): | ||
bytes_file = _create_capture_pattern(np.int32, np.float64) | ||
bytes_file += _create_file_identifier() | ||
bytes_file += _create_global_header() | ||
|
||
# create 1 block for gas | ||
read_tag = np.array([13], dtype='int32') | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
nblocks = np.array([1], dtype='int32') | ||
bytes_file += bytearray(nblocks.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
# 8 particles storing 4 real arrays (x, y, z, h) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
n = np.array([8], dtype='int64') | ||
nums = np.array([0, 0, 0, 0, 0, 4, 0, 0], dtype='int32') | ||
bytes_file += bytearray(n.tobytes()) | ||
bytes_file += bytearray(nums.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
# write 4 particle arrays | ||
bytes_file += _create_particle_array("x", [0, 0, 0, 0, 1, 1, 1, 1]) | ||
bytes_file += _create_particle_array("y", [0, 0, 1, 1, 0, 0, 1, 1]) | ||
bytes_file += _create_particle_array("z", [0, 1, 0, 1, 0, 1, 0, 1]) | ||
bytes_file += _create_particle_array("h", [1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]) | ||
|
||
return bytes_file | ||
|
||
|
||
def get_gas_dust_sink_particles(): | ||
|
||
bytes_file = _create_capture_pattern(np.int32, np.float64) | ||
bytes_file += _create_file_identifier() | ||
bytes_file += _create_global_header(massoftype_7=1e-4) | ||
|
||
# create 1 block for gas | ||
read_tag = np.array([13], dtype='int32') | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
nblocks = np.array([2], dtype='int32') | ||
bytes_file += bytearray(nblocks.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
# 8 particles storing 4 real arrays (x, y, z, h) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
n = np.array([16], dtype='int64') | ||
nums = np.array([0, 1, 0, 0, 0, 4, 0, 0] , dtype='int32') | ||
bytes_file += bytearray(n.tobytes()) | ||
bytes_file += bytearray(nums.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
bytes_file += bytearray(read_tag.tobytes()) | ||
n = np.array([1], dtype='int64') | ||
nums = np.array([0, 0, 0, 0, 0, 7, 0, 0] , dtype='int32') | ||
bytes_file += bytearray(n.tobytes()) | ||
bytes_file += bytearray(nums.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
# write 5 gas/dust particle arrays | ||
bytes_file += _create_particle_array("itype", [1, 1, 1, 1, 1, 1, 1, 1, | ||
7, 7, 7, 7, 7, 7, 7, 7], np.int8) | ||
bytes_file += _create_particle_array("x", [0, 0, 0, 0, 1, 1, 1, 1, | ||
0.5, 0.5, 0.5, 0.5, 1.5, 1.5, 1.5, 1.5]) | ||
bytes_file += _create_particle_array("y", [0, 0, 1, 1, 0, 0, 1, 1, | ||
0.5, 0.5, 1.5, 1.5, 0.5, 0.5, 1.5, 1.5]) | ||
bytes_file += _create_particle_array("z", [0, 1, 0, 1, 0, 1, 0, 1, | ||
0.5, 1.5, 0.5, 1.5, 0.5, 1.5, 0.5, 1.5]) | ||
bytes_file += _create_particle_array("h", [1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, | ||
1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]) | ||
|
||
# write 7 sink particle arrays | ||
bytes_file += _create_particle_array("x", [0.000305]) | ||
bytes_file += _create_particle_array("y", [-0.035809]) | ||
bytes_file += _create_particle_array("z", [-0.000035]) | ||
bytes_file += _create_particle_array("h", [1.0]) | ||
bytes_file += _create_particle_array("spinx", [-3.911744e-8]) | ||
bytes_file += _create_particle_array("spiny", [-1.326062e-8]) | ||
bytes_file += _create_particle_array("spinz", [0.00058]) | ||
return bytes_file | ||
|
||
def get_gas_sink_particles(): | ||
|
||
bytes_file = _create_capture_pattern(np.int32, np.float64) | ||
bytes_file += _create_file_identifier() | ||
bytes_file += _create_global_header() | ||
|
||
# create 1 block for gas | ||
read_tag = np.array([13], dtype='int32') | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
nblocks = np.array([2], dtype='int32') | ||
bytes_file += bytearray(nblocks.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
# 8 particles storing 4 real arrays (x, y, z, h) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
n = np.array([8], dtype='int64') | ||
nums = np.array([0, 0, 0, 0, 0, 4, 0, 0] , dtype='int32') | ||
bytes_file += bytearray(n.tobytes()) | ||
bytes_file += bytearray(nums.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
bytes_file += bytearray(read_tag.tobytes()) | ||
n = np.array([1], dtype='int64') | ||
nums = np.array([0, 0, 0, 0, 0, 7, 0, 0] , dtype='int32') | ||
bytes_file += bytearray(n.tobytes()) | ||
bytes_file += bytearray(nums.tobytes()) | ||
bytes_file += bytearray(read_tag.tobytes()) | ||
|
||
# write 4 gas particle arrays | ||
bytes_file += _create_particle_array("x", [0, 0, 0, 0, 1, 1, 1, 1]) | ||
bytes_file += _create_particle_array("y", [0, 0, 1, 1, 0, 0, 1, 1]) | ||
bytes_file += _create_particle_array("z", [0, 1, 0, 1, 0, 1, 0, 1]) | ||
bytes_file += _create_particle_array("h", [1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]) | ||
|
||
# write 7 sink particle arrays | ||
bytes_file += _create_particle_array("x", [0.000305]) | ||
bytes_file += _create_particle_array("y", [-0.035809]) | ||
bytes_file += _create_particle_array("z", [-0.000035]) | ||
bytes_file += _create_particle_array("h", [1.0]) | ||
bytes_file += _create_particle_array("spinx", [-3.911744e-8]) | ||
bytes_file += _create_particle_array("spiny", [-1.326062e-8]) | ||
bytes_file += _create_particle_array("spinz", [0.00058]) | ||
|
||
return bytes_file | ||
|
||
|
||
def get_file_content(file_path): | ||
with open(file_path, 'rb') as file: | ||
return file.read() | ||
|
||
|
||
def test_sink_particles_gas_and_dust(): #PASSES | ||
with tempfile.NamedTemporaryFile() as fp: | ||
fp.write(get_gas_dust_sink_particles()) | ||
fp.seek(0) | ||
|
||
test_sdfs = sarracen.read_phantom(fp.name) | ||
phantom_file = sarracen.write_phantom("wp", test_sdfs[0], test_sdfs[1]) | ||
test_sdfs_from_new_file = sarracen.read_phantom(phantom_file.name) | ||
pd.testing.assert_frame_equal(test_sdfs_from_new_file[0], test_sdfs[0]) | ||
pd.testing.assert_frame_equal(test_sdfs_from_new_file[1], test_sdfs[1]) | ||
# | ||
# original_content = get_file_content(fp.name) | ||
# new_content = get_file_content(phantom_file.name) | ||
# | ||
# assert original_content == new_content | ||
|
||
def test_sink_particles(): #PASSES | ||
with tempfile.NamedTemporaryFile() as fp: | ||
fp.write(get_gas_sink_particles()) | ||
fp.seek(0) | ||
|
||
test_sdfs = sarracen.read_phantom(fp.name) | ||
phantom_file = sarracen.write_phantom("wp", test_sdfs[0], test_sdfs[1]) | ||
test_sdfs_from_new_file = sarracen.read_phantom(phantom_file.name) | ||
pd.testing.assert_frame_equal(test_sdfs_from_new_file[0], test_sdfs[0]) | ||
pd.testing.assert_frame_equal(test_sdfs_from_new_file[1], test_sdfs[1]) | ||
|
||
|
||
def test_write_phantom_one_block(): #PASSES | ||
with tempfile.NamedTemporaryFile() as fp: | ||
fp.write(get_one_block_phantom_file()) | ||
fp.seek(0) | ||
|
||
test_sdf = sarracen.read_phantom(fp.name) | ||
phantom_file = sarracen.write_phantom("wp", test_sdf) | ||
test_sdf_from_new_file = sarracen.read_phantom(phantom_file.name) | ||
pd.testing.assert_frame_equal(test_sdf, test_sdf_from_new_file) |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change the version yet. I do that just before I make an official release.