Skip to content

Commit b3b4985

Browse files
use tuple valued state for proper checkpointing
1 parent 01fef75 commit b3b4985

9 files changed

+171
-58
lines changed

FSI/cylinderFlap/OpenFOAM-FEniCS/Solid/cyl-flap.py

+35-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
import matplotlib.pyplot as plt
1111
from fenicsadapter import Adapter
1212
from enum import Enum
13+
import argparse
14+
15+
parser = argparse.ArgumentParser()
16+
parser.add_argument("--wr-tag", "-wr", help="wr tag", choices=["11", "52"], default="11", type=str)
17+
parser.add_argument("--waveform-interpolation-strategy", "-wri", help="interpolation", choices=["linear", "quadratic"], default="linear", type=str)
18+
args = parser.parse_args()
1319

1420
# Beam geometry
1521
dim = 2 # number of dimensions
@@ -71,7 +77,8 @@ def remaining_boundary(x, on_boundary):
7177

7278
# displacement fields
7379
u_np1 = Function(V)
74-
saved_u_old = Function(V)
80+
v_np1 = Function(V)
81+
a_np1 = Function(V)
7582

7683
# function known from previous timestep
7784
u_n = Function(V)
@@ -90,11 +97,11 @@ def remaining_boundary(x, on_boundary):
9097
# get the adapter ready
9198

9299
# read fenics-adapter json-config-file)
93-
adapter_config_filename = "../tools/precice-adapter-config-fsi-s_wr.json"
94-
other_adapter_config_filename = "../tools/precice-adapter-config-excite.json"
100+
adapter_config_filename = "../tools/precice-adapter-config-fsi-s_wr{wr_tag}.json".format(wr_tag=args.wr_tag)
101+
other_adapter_config_filename = "../tools/precice-adapter-config-excite_wr{wr_tag}.json".format(wr_tag=args.wr_tag)
95102

96103
# create Adapter
97-
precice = Adapter(adapter_config_filename, other_adapter_config_filename)
104+
precice = Adapter(adapter_config_filename, other_adapter_config_filename, wr_interpolation_strategy=args.waveform_interpolation_strategy)
98105

99106
# create subdomains used by the adapter
100107
clamped_boundary_domain = AutoSubDomain(left_boundary)
@@ -104,7 +111,7 @@ def remaining_boundary(x, on_boundary):
104111
mesh=mesh,
105112
read_field=f_N_function,
106113
write_field=u_function,
107-
u_n=u_n,
114+
u_n=(u_n, v_n, a_n),
108115
dimension=dim,
109116
dirichlet_boundary=clamped_boundary_domain)
110117

@@ -115,8 +122,8 @@ def remaining_boundary(x, on_boundary):
115122

116123

117124
# generalized alpha method (time stepping) parameters
118-
alpha_m = Constant(0.2)
119-
alpha_f = Constant(0.4)
125+
alpha_m = Constant(0)
126+
alpha_f = Constant(0)
120127
gamma = Constant(0.5 + alpha_f - alpha_m)
121128
beta = Constant((gamma + 0.5)**2 * 0.25)
122129

@@ -187,10 +194,10 @@ def avg(x_old, x_new, alpha):
187194

188195

189196
# residual
190-
a_np1 = update_acceleration(du, u_n, v_n, a_n, ufl=True)
191-
v_np1 = update_velocity(a_np1, u_n, v_n, a_n, ufl=True)
197+
da = update_acceleration(du, u_n, v_n, a_n, ufl=True)
198+
#v_np1 = update_velocity(a_np1, u_n, v_n, a_n, ufl=True)
192199

193-
res = m(avg(a_n, a_np1, alpha_m), v) + k(avg(u_n, du, alpha_f), v)
200+
res = m(avg(a_n, da, alpha_m), v) + k(avg(u_n, du, alpha_f), v)
194201

195202
Forces_x, Forces_y = precice.create_force_boundary_condition(V)
196203

@@ -214,7 +221,8 @@ def avg(x_old, x_new, alpha):
214221
u_np1.rename("Displacement", "")
215222
displacement_out << u_n
216223

217-
224+
u_tip_buffer = []
225+
t_buffer = []
218226
# time loop for coupling
219227
while precice.is_coupling_ongoing():
220228
A, b = assemble_system(a_form, L_form, bc)
@@ -229,19 +237,25 @@ def avg(x_old, x_new, alpha):
229237
assert(b is not b_forces)
230238
solve(A, u_np1.vector(), b_forces)
231239

232-
t, n, precice_timestep_complete, precice_dt, Forces_x, Forces_y = precice.advance(u_np1, u_np1, u_n, t, float(dt), n)
240+
a_np1 = project(update_acceleration(u_np1, u_n, v_n, a_n, ufl=False), V)
241+
v_np1 = project(update_velocity(a_np1, u_n, v_n, a_n, ufl=False), V)
242+
243+
t, n, precice_timestep_complete, precice_dt, Forces_x, Forces_y = precice.advance(u_np1, (u_np1, v_np1, a_np1), (u_n, v_n, a_n), t, float(dt), n)
233244
dt = Constant(np.min([precice_dt, fenics_dt]))
234245

235-
if precice_timestep_complete:
236-
237-
update_fields(u_np1, saved_u_old, v_n, a_n)
238-
246+
u_tip_buffer.append(u_n(0.6, 0.2)[1])
247+
t_buffer.append(t)
248+
249+
if precice_timestep_complete:
239250
if n % 20==0:
240251
displacement_out << (u_n, t)
241252

242-
u_tip.append(u_n(0.6, 0.2)[1])
253+
u_tip += u_tip_buffer
254+
u_tip_buffer = []
255+
time += t_buffer
256+
t_buffer = []
243257
print(u_n(0.6, 0.2))
244-
time.append(t)
258+
245259

246260

247261
# Plot tip displacement evolution
@@ -252,6 +266,8 @@ def avg(x_old, x_new, alpha):
252266
plt.ylabel("Tip displacement")
253267
plt.show()
254268

255-
output_file = open("subiteration_out.txt", "a")
269+
output_file = open("wr11_out.txt", "a")
256270
output_file.write("{};{}\n".format(u_tip[-1], dt(0)))
257271
precice.finalize()
272+
print(np.array2string(np.array(time), separator=','))
273+
print(np.array2string(np.array(u_tip), separator=','))

FSI/cylinderFlap/OpenFOAM-FEniCS/tools/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ You will have to replace `adapter_config_filename = "precice-adapter-config-fsi-
88
Run the following commands from `tutorials/FSI/cylinderFlap/OpenFOAM-FEniCS`:
99

1010
* `python3 Solid/cyl-flap.py`
11-
* `python3 tools/excitement.py tools/precice-config.xml`
11+
* `python3 tools/excitement.py`
1212

13-
A file `out.txt` will be created in this folder. The result of the simulation is written to this folder
13+
A file `out*.txt` will be created in this folder. The result of the simulation is written to this folder
1414

15-
## Using multirate / subiterations
15+
## Using multirate
1616

1717
Depending on `precice_dt` and `fenics_dt` the computations are done using multirate timeintegration or not.
1818

19-
* *no multirate:* the input force is provided for every single timestep
20-
* *multirate:* the input force is only provided for the timestep size `precice_dt`, if `fenics_dt < precice_dt` the same force will be applied for all fenics timesteps inside of a window.
19+
* *no multirate:* the input force is provided for every single timestep append `-wr 11` to the commands for running
20+
* *multirate:* the input force is only provided for some timesteps, this grid does not match the grid of the structure solver. We interpolate between force evaluations. Use `-wr 52`, additionally, you can choose the interpolation strategy via `-wri linear` or `-wri quadratic`.
2121

2222
# Convergence Study
2323

FSI/cylinderFlap/OpenFOAM-FEniCS/tools/excitement.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55
import precice_future as precice
66

7-
omega_excitement = 1
7+
omega_excitement = 4
88

99
# beam geometry
1010
L = 0.35 # length
@@ -24,7 +24,7 @@ def compute_force(time, force_excitement):
2424

2525

2626
parser = argparse.ArgumentParser()
27-
parser.add_argument("configurationFileName", help="Name of the xml config file.", type=str)
27+
parser.add_argument("--wr-tag", "-wr", help="wr tag", choices=["11", "52"], default="11", type=str)
2828

2929
try:
3030
args = parser.parse_args()
@@ -33,11 +33,9 @@ def compute_force(time, force_excitement):
3333
print("Usage: python ./solverdummy precice-config participant-name mesh-name")
3434
quit()
3535

36-
configuration_file_name = args.configurationFileName
36+
configuration_file_name = "tools/precice-config_wr{wr_tag}.xml".format(wr_tag=args.wr_tag)
3737
participant_name = "Excitement"
3838
mesh_name = "Excitement-Mesh"
39-
write_data_name = "Forces0"
40-
read_data_name = "Displacements0"
4139

4240
n_vertices = 1
4341

@@ -59,8 +57,6 @@ def compute_force(time, force_excitement):
5957
vertices[:, 1] = y_attack
6058

6159
vertex_ids = interface.set_mesh_vertices(mesh_id, vertices)
62-
write_data_id = interface.get_data_id(write_data_name, mesh_id)
63-
read_data_id = interface.get_data_id(read_data_name, mesh_id)
6460

6561
dt = interface.initialize()
6662
t = 0
@@ -78,11 +74,20 @@ def compute_force(time, force_excitement):
7874
print("DUMMY: Writing iteration checkpoint")
7975
interface.fulfilled_action(precice.action_write_iteration_checkpoint())
8076

81-
forces = compute_force(t+dt, force_excitement)
82-
print("sending forces: {}".format(forces))
83-
interface.write_block_vector_data(write_data_id, vertex_ids, forces)
77+
if args.wr_tag == "11":
78+
forces = compute_force(t+dt, force_excitement)
79+
print("sending forces: {}".format(forces))
80+
interface.write_block_vector_data(interface.get_data_id("Forces1", mesh_id), vertex_ids, forces)
81+
elif args.wr_tag == "52":
82+
forces = compute_force(t+.5 * dt, force_excitement)
83+
print("sending forces: {}".format(forces))
84+
interface.write_block_vector_data(interface.get_data_id("Forces1", mesh_id), vertex_ids, forces)
85+
86+
forces = compute_force(t+dt, force_excitement)
87+
print("sending forces: {}".format(forces))
88+
interface.write_block_vector_data(interface.get_data_id("Forces2", mesh_id), vertex_ids, forces)
89+
8490
dt = interface.advance(dt)
85-
displacement = interface.read_block_vector_data(read_data_id, vertex_ids)
8691

8792
if interface.is_action_required(precice.action_read_iteration_checkpoint()):
8893
print("DUMMY: Reading iteration checkpoint")

FSI/cylinderFlap/OpenFOAM-FEniCS/tools/plot_convergence_study.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def outfile_to_data(filename):
1717
uu.append(u)
1818
dtdt.append(dt)
1919

20-
u_values = np.array(uu[1:], dtype=float)
21-
dt_values = np.array(dtdt[1:], dtype=float)
20+
u_values = np.array(uu, dtype=float)
21+
dt_values = np.array(dtdt, dtype=float)
2222

2323

2424
def unzip(iterable):
@@ -31,7 +31,7 @@ def unzip(iterable):
3131
return [u for u, _ in paired], [dt for _, dt in paired]
3232

3333

34-
u_values, dt_values = outfile_to_data("../out.txt")
34+
u_values, dt_values = outfile_to_data("wr11_out.txt")
3535

3636
reference_value = u_values[0]
3737

@@ -40,18 +40,29 @@ def unzip(iterable):
4040
u_values = np.array(u_values)
4141
dt_values = np.array(dt_values)
4242

43-
plt.loglog(dt_values[1:], errors[1:], '.', label='num')
43+
plt.loglog(dt_values, errors, '.', label='newmark')
4444

45-
u_values, dt_values = outfile_to_data("../subiteration_out.txt")
45+
u_values, dt_values = outfile_to_data("wr52_lin_out.txt")
4646

4747
errors = np.abs(u_values - reference_value)
4848

4949
u_values = np.array(u_values)
5050
dt_values = np.array(dt_values)
5151

52-
plt.loglog(dt_values, errors, '.', label='num w subiterations')
53-
plt.loglog(dt_values[1:], dt_values[1:], '--', label='O(h^1)')
54-
plt.loglog(dt_values[1:], dt_values[1:]**2, ':', label='O(h^2)')
52+
plt.loglog(dt_values[1:], errors[1:], '1', label='wr52_lin')
53+
54+
u_values, dt_values = outfile_to_data("wr52_quad_out.txt")
55+
56+
errors = np.abs(u_values - reference_value)
57+
58+
u_values = np.array(u_values)
59+
dt_values = np.array(dt_values)
60+
61+
plt.loglog(dt_values[1:], errors[1:], '2', label='wr52_quad')
62+
63+
64+
plt.loglog(dt_values[1:], errors[1]/dt_values[1]*dt_values[1:], '--', label='O(h^1)')
65+
plt.loglog(dt_values[1:], errors[1]/dt_values[1]**2*dt_values[1:]**2, ':', label='O(h^2)')
5566
plt.legend()
5667
plt.show()
5768

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"solver_name": "fenics",
3+
"config_file_name": "../tools/precice-config_wr11.xml",
4+
"interface": {
5+
"coupling_mesh_name": "Excitement",
6+
"write_data_name": "Forces",
7+
"read_data_name": "Displacements"
8+
},
9+
"waveform": {
10+
"n_substeps": 1
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"solver_name": "fenics",
3-
"config_file_name": "../tools/precice-config.xml",
3+
"config_file_name": "../tools/precice-config_wr52.xml",
44
"interface": {
55
"coupling_mesh_name": "Excitement",
66
"write_data_name": "Forces0",
77
"read_data_name": "Displacements0"
88
},
99
"waveform": {
10-
"n_substeps": 1
10+
"n_substeps": 2
1111
}
1212
}

FSI/cylinderFlap/OpenFOAM-FEniCS/tools/precice-adapter-config-fsi-s_wr.json

-12
This file was deleted.

FSI/cylinderFlap/OpenFOAM-FEniCS/tools/precice-config_wr.xml FSI/cylinderFlap/OpenFOAM-FEniCS/tools/precice-config_wr11.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<coupling-scheme:serial-explicit>
4343
<participants first="Excitement" second="fenics" />
4444
<max-time value="1"/>
45-
<timestep-length value="0.1" />
45+
<timestep-length value="0.001" />
4646
<exchange data="Forces1" mesh="Solid" from="Excitement" to="fenics"/>
4747
<exchange data="Displacements1" mesh="Solid" from="fenics" to="Excitement" initialize="0"/>
4848
</coupling-scheme:serial-explicit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0"?>
2+
3+
<precice-configuration>
4+
5+
<log>
6+
<sink type="stream" output="stdout" filter= "%Severity% > debug" format="preCICE:%ColorizedSeverity% %Message%" enabled="true" />
7+
</log>
8+
9+
<solver-interface dimensions="2" >
10+
11+
<data:vector name="Forces1"/>
12+
<data:vector name="Forces2"/>
13+
<data:vector name="Displacements1"/>
14+
<data:vector name="Displacements2"/>
15+
<data:vector name="Displacements3"/>
16+
<data:vector name="Displacements4"/>
17+
<data:vector name="Displacements5"/>
18+
19+
<mesh name="Excitement-Mesh">
20+
<use-data name="Forces1"/>
21+
<use-data name="Forces2"/>
22+
<use-data name="Displacements1"/>
23+
<use-data name="Displacements2"/>
24+
<use-data name="Displacements3"/>
25+
<use-data name="Displacements4"/>
26+
<use-data name="Displacements5"/>
27+
</mesh>
28+
29+
<mesh name="Solid">
30+
<use-data name="Forces1"/>
31+
<use-data name="Forces2"/>
32+
<use-data name="Displacements1"/>
33+
<use-data name="Displacements2"/>
34+
<use-data name="Displacements3"/>
35+
<use-data name="Displacements4"/>
36+
<use-data name="Displacements5"/>
37+
</mesh>
38+
39+
<participant name="Excitement">
40+
<use-mesh name="Excitement-Mesh" provide="yes"/>
41+
<use-mesh name="Solid" from="fenics"/>
42+
<write-data name="Forces1" mesh="Excitement-Mesh"/>
43+
<write-data name="Forces2" mesh="Excitement-Mesh"/>
44+
<read-data name="Displacements1" mesh="Excitement-Mesh"/>
45+
<read-data name="Displacements2" mesh="Excitement-Mesh"/>
46+
<read-data name="Displacements3" mesh="Excitement-Mesh"/>
47+
<read-data name="Displacements4" mesh="Excitement-Mesh"/>
48+
<read-data name="Displacements5" mesh="Excitement-Mesh"/>
49+
<mapping:petrbf-thin-plate-splines direction="write" from="Excitement-Mesh" to="Solid" constraint="conservative" />
50+
<mapping:petrbf-thin-plate-splines direction="read" from="Solid" to="Excitement-Mesh" constraint="consistent" />
51+
</participant>
52+
53+
<participant name="fenics">
54+
<use-mesh name="Solid" provide="yes"/>
55+
<read-data name="Forces1" mesh="Solid"/>
56+
<read-data name="Forces2" mesh="Solid"/>
57+
<write-data name="Displacements1" mesh="Solid"/>
58+
<write-data name="Displacements2" mesh="Solid"/>
59+
<write-data name="Displacements3" mesh="Solid"/>
60+
<write-data name="Displacements4" mesh="Solid"/>
61+
<write-data name="Displacements5" mesh="Solid"/>
62+
<watch-point mesh="Solid" name="point1" coordinate="0.6;0.2;0." />
63+
</participant>
64+
65+
<m2n:sockets from="Excitement" to="fenics" distribution-type="gather-scatter"/>
66+
67+
<coupling-scheme:serial-explicit>
68+
<participants first="Excitement" second="fenics" />
69+
<max-time value="1"/>
70+
<timestep-length value="0.5" />
71+
<exchange data="Forces1" mesh="Solid" from="Excitement" to="fenics"/>
72+
<exchange data="Forces2" mesh="Solid" from="Excitement" to="fenics"/>
73+
<exchange data="Displacements1" mesh="Solid" from="fenics" to="Excitement" initialize="0"/>
74+
<exchange data="Displacements2" mesh="Solid" from="fenics" to="Excitement" initialize="0"/>
75+
<exchange data="Displacements3" mesh="Solid" from="fenics" to="Excitement" initialize="0"/>
76+
<exchange data="Displacements4" mesh="Solid" from="fenics" to="Excitement" initialize="0"/>
77+
<exchange data="Displacements5" mesh="Solid" from="fenics" to="Excitement" initialize="0"/>
78+
</coupling-scheme:serial-explicit>
79+
</solver-interface>
80+
81+
</precice-configuration>

0 commit comments

Comments
 (0)