Skip to content

Commit

Permalink
Use export in demos
Browse files Browse the repository at this point in the history
  • Loading branch information
ddundo committed Oct 14, 2024
1 parent 594d64b commit 5aa111d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
18 changes: 6 additions & 12 deletions demos/gray_scott.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,12 @@ def qoi():

# Finally, plot the outputs to be viewed in Paraview. ::

ic = mesh_seq.get_initial_condition()
for field, sols in solutions.items():
fwd_outfile = VTKFile(f"gray_scott/{field}_forward.pvd")
adj_outfile = VTKFile(f"gray_scott/{field}_adjoint.pvd")
fwd_outfile.write(*ic[field].subfunctions)
for i in range(num_subintervals):
for sol in sols["forward"][i]:
fwd_outfile.write(*sol.subfunctions)
for sol in sols["adjoint"][i]:
adj_outfile.write(*sol.subfunctions)
adj_end = Function(ic[field]).assign(0.0)
adj_outfile.write(*adj_end.subfunctions)
solutions.export(
"gray_scott/forward.pvd",
export_field_types="forward",
initial_condition=mesh_seq.get_initial_condition(),
)
solutions.export("gray_scott/adjoint.pvd", export_field_types="adjoint")

# In the `next demo <./gray_scott_split.py.html>`__, we consider solving the same
# problem, but splitting the solution field into multiple components.
Expand Down
17 changes: 6 additions & 11 deletions demos/gray_scott_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,11 @@ def qoi():
)
solutions = mesh_seq.solve_adjoint()

ic = mesh_seq.get_initial_condition()
for field, sols in solutions.items():
fwd_outfile = VTKFile(f"gray_scott_split/{field}_forward.pvd")
adj_outfile = VTKFile(f"gray_scott_split/{field}_adjoint.pvd")
fwd_outfile.write(ic[field])
for i in range(num_subintervals):
for sol in sols["forward"][i]:
fwd_outfile.write(sol)
for sol in sols["adjoint"][i]:
adj_outfile.write(sol)
adj_outfile.write(Function(ic[field]).assign(0.0))
solutions.export(
"gray_scott_split/forward.pvd",
export_field_types="forward",
initial_condition=mesh_seq.get_initial_condition(),
)
solutions.export("gray_scott_split/adjoint.pvd", export_field_types="adjoint")

# This tutorial can be dowloaded as a `Python script <gray_scott_split.py>`__.
11 changes: 3 additions & 8 deletions demos/solid_body_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,9 @@ def qoi():
# use Paraview. To save all adjoint solution components in Paraview format, use the
# following. ::

for field, sols in solutions.items():
fwd_outfile = VTKFile(f"solid_body_rotation/{field}_forward.pvd")
adj_outfile = VTKFile(f"solid_body_rotation/{field}_adjoint.pvd")
for i in range(len(mesh_seq)):
for sol in sols["forward"][i]:
fwd_outfile.write(sol)
for sol in sols["adjoint"][i]:
adj_outfile.write(sol)
solutions.export(
"solid_body_rotation/solutions.pvd", export_field_types=["forward", "adjoint"]
)

# In the `next demo <./gray_scott.py.html>`__, we increase the complexity by considering
# two concentration fields in an advection-diffusion-reaction problem.
Expand Down
6 changes: 3 additions & 3 deletions test_adjoint/test_demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
"burgers-hessian.py": {""""maxiter": 35""": """"maxiter": 3"""},
"gray_scott.py": {
"end_time = 2000.0": "end_time = 10.0",
r"ic = mesh_seq.get_initial_condition\(\)\n.*?adj_outfile.write\(\*adj_end.subfunctions\)": "",
r"solutions\.export\((.*?)\)\s*solutions\.export\((.*?)\)": "",
},
"gray_scott_split.py": {
"end_time = 2000.0": "end_time = 10.0",
r"ic = mesh_seq.get_initial_condition\(\)\n.*?adj_outfile.write\(Function\(ic\[field\]\).assign\(0.0\)\)": "",
r"solutions\.export\((.*?)\)\s*solutions\.export\((.*?)\)": "",
},
"point_discharge2d-hessian.py": {""""maxiter": 35""": """"maxiter": 3"""},
"point_discharge2d-goal_oriented.py": {""""maxiter": 35""": """"maxiter": 3"""},
"solid_body_rotation.py": {
r"for field, sols in solutions.items\(\):\n.*?adj_outfile.write\(sol\)": "",
r"solutions\.export\((.*?)\)": "",
},
}

Expand Down

0 comments on commit 5aa111d

Please sign in to comment.