Skip to content

Commit

Permalink
testing the copy, deepcopy, serialize etc. of Repeat with thickness C…
Browse files Browse the repository at this point in the history
…alculation intact
  • Loading branch information
bmaranville committed Oct 29, 2024
1 parent d00a569 commit 327d386
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/refl1d/stack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,36 @@ def test_stack_serialization():
assert thickness_plus.value == 160
sample[0].thickness.value += 40
assert thickness_plus.value == 200


def test_repeat_serialization():
"""test that stack can be serialized and deserialized with all the methods we use,
preserving the functioning of the Calculation object for the total thickness"""
unit_cell = Slab(SLD(rho=10), thickness=10) | Slab(SLD(rho=10), thickness=20) | Slab(SLD(rho=10), thickness=30)
# This creates a Repeat object from the Stack:
sample = unit_cell * 4
thickness_plus = sample.thickness + 100 # expression

ser_t, ser_s = deserialize(serialize([thickness_plus, sample]))
assert ser_t.value == 340 # (10+20+30 * 4) + 100
ser_s.stack[0].thickness.value += 40
assert ser_t.value == 500 # (50+20+30 * 4) + 100

dc_t, dc_s = deepcopy([thickness_plus, sample])
assert dc_t.value == 340
dc_s.stack[0].thickness.value += 40
assert dc_t.value == 500

pickle_t, pickle_s = pickle.loads(pickle.dumps([thickness_plus, sample]))
assert pickle_t.value == 340
pickle_s.stack[0].thickness.value += 40
assert pickle_t.value == 500

dill_t, dill_s = dill.loads(dill.dumps([thickness_plus, sample]))
assert dill_t.value == 340
dill_s.stack[0].thickness.value += 40
assert dill_t.value == 500

assert thickness_plus.value == 340
sample.stack[0].thickness.value += 40
assert thickness_plus.value == 500

0 comments on commit 327d386

Please sign in to comment.