-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunscript_energy_decay_space_refinement.py
276 lines (222 loc) · 8.08 KB
/
runscript_energy_decay_space_refinement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
import porepy as pp
sys.path.append("../")
import plotting.plot_utils as pu
import run_models.run_linear_model as rlm
from models import DynamicMomentumBalanceABCLinear
from utils import u_v_a_wrap
# Prepare path for generated output files
folder_name = "energy_values"
script_dir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(script_dir, folder_name)
os.makedirs(output_dir, exist_ok=True)
# Coarse/Fine variables and plotting (save figure)
coarse = True
save_figure = True
# Model class for setting up and running the simulation from here and onwards.
class BoundaryConditionsEnergyDecayAnalysis:
def initial_condition_bc(self, bg: pp.BoundaryGrid) -> np.ndarray:
dt = self.time_manager.dt
vals_0 = self.initial_condition_value_function(bg=bg, t=0)
vals_1 = self.initial_condition_value_function(bg=bg, t=0 - dt)
data = self.mdg.boundary_grid_data(bg)
# The values for the 0th and -1th time step are to be stored
pp.set_solution_values(
name="boundary_displacement_values",
values=vals_1,
data=data,
time_step_index=1,
)
pp.set_solution_values(
name="boundary_displacement_values",
values=vals_0,
data=data,
time_step_index=0,
)
return vals_0
def initial_condition_value_function(self, bg, t):
"""Assigning initial bc values."""
sd = bg.parent
x = sd.face_centers[0, :]
y = sd.face_centers[1, :]
boundary_sides = self.domain_boundary_sides(sd)
inds_north = np.where(boundary_sides.north)[0]
inds_south = np.where(boundary_sides.south)[0]
inds_west = np.where(boundary_sides.west)[0]
inds_east = np.where(boundary_sides.east)[0]
bc_vals = np.zeros((sd.dim, sd.num_faces))
displacement_function = u_v_a_wrap(model=self)
# North
bc_vals[0, :][inds_north] = displacement_function[0](
x[inds_north], y[inds_north], t
)
bc_vals[1, :][inds_north] = displacement_function[1](
x[inds_north], y[inds_north], t
)
# East
bc_vals[0, :][inds_east] = displacement_function[0](
x[inds_east], y[inds_east], t
)
bc_vals[1, :][inds_east] = displacement_function[1](
x[inds_east], y[inds_east], t
)
# West
bc_vals[0, :][inds_west] = displacement_function[0](
x[inds_west], y[inds_west], t
)
bc_vals[1, :][inds_west] = displacement_function[1](
x[inds_west], y[inds_west], t
)
# South
bc_vals[0, :][inds_south] = displacement_function[0](
x[inds_south], y[inds_south], t
)
bc_vals[1, :][inds_south] = displacement_function[1](
x[inds_south], y[inds_south], t
)
bc_vals = bc_vals.ravel("F")
bc_vals = bg.projection(self.nd) @ bc_vals.ravel("F")
return bc_vals
class SourceValuesEnergyDecayAnalysis:
def evaluate_mechanics_source(self, f: list, sd: pp.Grid, t: float) -> np.ndarray:
vals = np.zeros((self.nd, sd.num_cells))
return vals.ravel("F")
class Geometry:
def nd_rect_domain(self, x, y) -> pp.Domain:
box: dict[str, pp.number] = {"xmin": 0, "xmax": x}
box.update({"ymin": 0, "ymax": y})
return pp.Domain(box)
def set_domain(self) -> None:
x = 1.0 / self.units.m
y = 1.0 / self.units.m
self._domain = self.nd_rect_domain(x, y)
def meshing_arguments(self) -> dict:
cell_size = self.units.convert_units(self.cell_size_value, "m")
mesh_args: dict[str, float] = {"cell_size": cell_size}
return mesh_args
class ExportEnergy:
def data_to_export(self):
data = super().data_to_export()
for sd in self.mdg.subdomains(dim=self.nd):
vel_op = self.velocity_time_dep_array([sd]) * self.velocity_time_dep_array(
[sd]
)
vel_op_int = self.volume_integral(integrand=vel_op, grids=[sd], dim=self.nd)
vel_op_int_val = vel_op_int.value(self.equation_system)
vel = self.velocity_time_dep_array([sd]).value(self.equation_system)
data.append((sd, "energy", vel_op_int_val))
data.append((sd, "velocity", vel))
with open(os.path.join(output_dir, f"energy_values_{i}.txt"), "a") as file:
file.write(f"{np.sum(vel_op_int_val)},")
return data
class RotationAngle:
@property
def rotation_angle(self) -> float:
return np.pi / 4
class ModelSetupEnergyDecayAnalysis(
BoundaryConditionsEnergyDecayAnalysis,
SourceValuesEnergyDecayAnalysis,
Geometry,
ExportEnergy,
RotationAngle,
DynamicMomentumBalanceABCLinear,
):
def write_pvd_and_vtu(self) -> None:
"""Override method such that pvd and vtu files are not created."""
self.data_to_export()
# This is where the simulation actually is run. We loop through different space
# refinements and run the model class once per refinement.
if coarse:
dxs = np.array([1 / 2**i for i in range(5, 7)])
else:
dxs = np.array([1 / 2**i for i in range(5, 10)])
i = 8
for dx in dxs:
tf = 15.0
time_steps = 300
dt = tf / time_steps
time_manager = pp.TimeManager(
schedule=[0.0, tf],
dt_init=dt,
constant_dt=True,
)
solid_constants = pp.SolidConstants(lame_lambda=0.01, shear_modulus=0.01)
material_constants = {"solid": solid_constants}
params = {
"time_manager": time_manager,
"grid_type": "simplex",
"manufactured_solution": "diagonal_wave",
"progressbars": True,
"material_constants": material_constants,
}
model = ModelSetupEnergyDecayAnalysis(params)
model.cell_size_value = dx
model.index = i
with open(os.path.join(output_dir, f"energy_values_{i}.txt"), "w") as file:
pass
rlm.run_linear_model(model, params)
i += 1
# Plotting from here and down
# Tuple value in dictionary:
# * Legend text
# * Color
# * Dashed/not dashed line
# * Logarithmic y scale/not logarithmic y scale.
if save_figure:
plt.figure(figsize=(7, 5))
if coarse:
index_dx_dict = {
9: ("$\Delta x = 1/64$", pu.RGB(255, 193, 7), True, True),
8: ("$\Delta x = 1/32$", pu.RGB(0, 0, 0), True, True),
}
else:
index_dx_dict = {
12: ("$\Delta x = 1/512$", pu.RGB(0, 0, 0), False, True),
11: ("$\Delta x = 1/256$", pu.RGB(216, 27, 96), False, True),
10: ("$\Delta x = 1/128$", pu.RGB(30, 136, 229), True, True),
9: ("$\Delta x = 1/64$", pu.RGB(255, 193, 7), True, True),
8: ("$\Delta x = 1/32$", pu.RGB(0, 0, 0), True, True),
}
for key, value in index_dx_dict.items():
filename = os.path.join(output_dir, f"energy_values_{key}.txt")
energy_values = (
pu.read_float_values(filename=filename)
/ pu.read_float_values(filename=filename)[0]
)
final_time = 15
time_values = np.linspace(0, final_time, len(energy_values))
plt.yscale("log" if value[3] else "linear")
plt.plot(
time_values,
energy_values,
label=value[0],
color=value[1],
linestyle="-" if not value[2] else "--",
)
plt.axvline(
x=10 * np.sqrt(6) / 3,
ymin=0,
ymax=5,
color=(0.65, 0.65, 0.65),
linestyle="--",
linewidth=1,
)
plt.axhline(
y=0,
xmin=0,
xmax=12,
color=(0, 0, 0),
linewidth=0.5,
)
plt.xlabel("Time [s]", fontsize=12)
plt.ylabel("$\\frac{E}{E_0}$", fontsize=16)
plt.title("Energy evolution with respect to time")
plt.legend()
folder_name = "figures"
script_dir = os.path.dirname(os.path.abspath(__file__))
output_dir = os.path.join(script_dir, folder_name)
os.makedirs(output_dir, exist_ok=True)
plt.savefig(os.path.join(output_dir, "energy_decay_space_refinement.png"))