pvade 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. pvade/IO/DataStream.py +304 -0
  2. pvade/IO/Parameters.py +485 -0
  3. pvade/IO/Utilities.py +156 -0
  4. pvade/IO/__init__.py +9 -0
  5. pvade/IO/input_schema.yaml +719 -0
  6. pvade/IO/verbosity.py +81 -0
  7. pvade/__init__.py +9 -0
  8. pvade/__main__.py +4 -0
  9. pvade/application.py +232 -0
  10. pvade/cli.py +69 -0
  11. pvade/fluid/FlowManager.py +1972 -0
  12. pvade/fluid/__init__.py +1 -0
  13. pvade/fluid/boundary_conditions.py +688 -0
  14. pvade/fsi/FSI.py +98 -0
  15. pvade/fsi/__init__.py +9 -0
  16. pvade/geometry/MeshManager.py +1389 -0
  17. pvade/geometry/__init__.py +1 -0
  18. pvade/geometry/cylinder2d/DomainCreation.py +49 -0
  19. pvade/geometry/cylinder2d/__init__.py +1 -0
  20. pvade/geometry/cylinder3d/DomainCreation.py +111 -0
  21. pvade/geometry/cylinder3d/__init__.py +1 -0
  22. pvade/geometry/flag2d/DomainCreation.py +205 -0
  23. pvade/geometry/flag2d/__init__.py +1 -0
  24. pvade/geometry/heliostats3d/DomainCreation.py +1072 -0
  25. pvade/geometry/heliostats3d/__init__.py +1 -0
  26. pvade/geometry/panels2d/DomainCreation.py +362 -0
  27. pvade/geometry/panels2d/__init__.py +1 -0
  28. pvade/geometry/panels3d/DomainCreation.py +2438 -0
  29. pvade/geometry/panels3d/__init__.py +1 -0
  30. pvade/geometry/template/TemplateDomainCreation.py +236 -0
  31. pvade/geometry/template/__init__.py +1 -0
  32. pvade/structure/ElasticityAnalysis.py +1143 -0
  33. pvade/structure/ModalAnalysis.py +780 -0
  34. pvade/structure/StructureMain.py +447 -0
  35. pvade/structure/__init__.py +1 -0
  36. pvade/structure/boundary_conditions.py +457 -0
  37. pvade-1.1.0.dist-info/METADATA +87 -0
  38. pvade-1.1.0.dist-info/RECORD +43 -0
  39. pvade-1.1.0.dist-info/WHEEL +5 -0
  40. pvade-1.1.0.dist-info/entry_points.txt +2 -0
  41. pvade-1.1.0.dist-info/licenses/LICENSE.txt +28 -0
  42. pvade-1.1.0.dist-info/top_level.txt +2 -0
  43. pvade_main.py +14 -0
pvade/IO/DataStream.py ADDED
@@ -0,0 +1,304 @@
1
+ """Input/output utilities for PVade simulations.
2
+
3
+ This module provides:
4
+
5
+ * :func:`start_print_and_log` – redirect ``stdout`` and ``stderr`` to a log
6
+ file while still echoing output to the terminal.
7
+ * :class:`DataStream` – manages XDMF solution files, writing initial and
8
+ per-timestep snapshots of velocity, pressure, displacement, stress, and
9
+ optional temperature fields.
10
+ """
11
+
12
+ import dolfinx
13
+ import ufl
14
+ import sys
15
+
16
+ import numpy as np
17
+
18
+ from datetime import datetime
19
+ from pvade.IO.verbosity import (
20
+ get_verbosity_level,
21
+ should_emit_terminal_message,
22
+ parse_verbosity_print,
23
+ )
24
+
25
+ # from dolfinx.fem import create_nonmatching_meshes_interpolation_data
26
+ # import logging
27
+
28
+
29
+ def start_print_and_log(rank, logfile_name):
30
+ """Redirect ``stdout`` and ``stderr`` to a timestamped log file.
31
+
32
+ Replaces the process ``sys.stdout`` and ``sys.stderr`` with
33
+ :class:`PrintAndLog` instances that tee output to both the terminal and
34
+ the file *logfile_name*. The log file is initialised as an empty file
35
+ before redirection begins.
36
+
37
+ Args:
38
+ rank (int): MPI rank of the calling process.
39
+ logfile_name (str): Full path to the log file that will be created (or
40
+ overwritten if it already exists).
41
+ """
42
+
43
+ class PrintAndLog:
44
+ """
45
+ A class to capture normal print statements in a log file
46
+ along with displaying them to the terminal as usual.
47
+ """
48
+
49
+ def __init__(self, logfile_name, rank, message_type):
50
+ self.logfile_name = logfile_name
51
+ self.rank = rank
52
+ self.message_type = message_type
53
+ self.verbosity_level = get_verbosity_level()
54
+
55
+ if message_type == "INFO":
56
+ self.terminal = sys.__stdout__
57
+ elif message_type == "ERROR":
58
+ self.terminal = sys.__stdout__
59
+ else:
60
+ raise ValueError(f"Type {message_type} not recognized")
61
+
62
+ def write(self, message):
63
+ # Write to both the command line and save to the logfile
64
+ cleaned_message = message.rstrip()
65
+
66
+ # Can include a test like `len(message) > 0 and self.rank == 0`
67
+ # to limit this to only printing from rank 0, but for parallel
68
+ # debugging, it is often helpful to see messages from all ranks
69
+ # so we omit this check for now.
70
+ if len(cleaned_message) > 0:
71
+
72
+ required_level, cleaned_message = parse_verbosity_print(cleaned_message)
73
+
74
+ terminal_message = cleaned_message
75
+ if self.verbosity_level >= 2 and self.message_type == "INFO":
76
+ terminal_message = f"[rank {self.rank}] {terminal_message}"
77
+
78
+ if should_emit_terminal_message(
79
+ self.rank,
80
+ self.message_type,
81
+ self.verbosity_level,
82
+ required_level=required_level,
83
+ ):
84
+ self.terminal.write(f"{terminal_message}\n")
85
+
86
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
87
+
88
+ with open(self.logfile_name, "a") as fp:
89
+ fp.write(
90
+ f"{timestamp} [rank {self.rank}] [{self.message_type}] {cleaned_message}\n"
91
+ )
92
+
93
+ def flush(self):
94
+ """Flush the output stream (no-op; required by the file-like interface)."""
95
+ pass
96
+
97
+ with open(logfile_name, "w") as fp:
98
+ # Start with an empty file
99
+ pass
100
+
101
+ # Redirect stdout and stderr
102
+ sys.stdout = PrintAndLog(logfile_name, rank, message_type="INFO")
103
+ sys.stderr = PrintAndLog(logfile_name, rank, message_type="ERROR")
104
+
105
+ if rank == 0:
106
+ print("Starting PVade Run")
107
+
108
+
109
+ class DataStream:
110
+ """Input/Output and file writing class
111
+
112
+ This class manages the generation and updating of the saved xdmf files
113
+ from each run along with command line outputs and log files.
114
+
115
+ Attributes:
116
+ comm (MPI Communicator): An MPI communicator used by all PVade objects
117
+ ndim (int): The number of dimensions in the problem
118
+ num_procs (int): The total number of processors being used to solve the problem
119
+ rank (int): A unique ID for each process in the range `[0, 1, ..., num_procs-1]`
120
+ results_filename_fluid (str): The full path to the directory in which all saved files will be written.
121
+
122
+ """
123
+
124
+ def __init__(self, domain, flow, structure=None, params=None):
125
+ """Initialize the DataStream object
126
+
127
+ This initializes an object that manages the I/O for all of PVade.
128
+
129
+ Args:
130
+ domain (:obj:`pvade.geometry.MeshManager.Domain`): A Domain object
131
+ flow (:obj:`pvade.fluid.FlowManager.Flow`): A Flow object
132
+ params (:obj:`pvade.Parameters.SimParams`): A SimParams object
133
+
134
+ """
135
+
136
+ self.comm = params.comm
137
+ self.rank = params.rank
138
+ self.num_procs = params.num_procs
139
+ self.ndim = domain.fluid.msh.topology.dim
140
+ self.thermal_analysis = params.general.thermal_analysis
141
+
142
+ # If doing a fluid simulation, start a fluid solution file
143
+ if params.general.fluid_analysis:
144
+ self.results_filename_fluid = (
145
+ f"{params.general.output_dir_sol}/solution_fluid.xdmf"
146
+ )
147
+
148
+ with dolfinx.io.XDMFFile(
149
+ self.comm, self.results_filename_fluid, "w"
150
+ ) as xdmf_file:
151
+ tt = 0.0
152
+ xdmf_file.write_mesh(domain.fluid.msh)
153
+ xdmf_file.write_function(flow.u_k, 0.0)
154
+ xdmf_file.write_function(flow.p_k, 0.0)
155
+ xdmf_file.write_function(flow.panel_stress, 0.0)
156
+ xdmf_file.write_function(domain.total_mesh_displacement, 0.0)
157
+
158
+ if params.general.thermal_analysis:
159
+ xdmf_file.write_function(flow.theta_k, 0.0)
160
+
161
+ # If doing a structure simulation, start a structure solution file
162
+ if params.general.structural_analysis:
163
+ self.results_filename_structure = (
164
+ f"{params.general.output_dir_sol}/solution_structure.xdmf"
165
+ )
166
+
167
+ # Since this is the first call, build solvers that project forms on an as-saved basis
168
+ # Set up the linear problem used for the projection, cg solver and jacobi pc
169
+ petsc_options = {"ksp_type": "cg", "pc_type": "jacobi"}
170
+ self.prob_k_nominal = dolfinx.fem.petsc.LinearProblem(
171
+ ufl.lhs(structure.elasticity.k_nominal_proj),
172
+ ufl.rhs(structure.elasticity.k_nominal_proj),
173
+ bcs=[],
174
+ petsc_options=petsc_options,
175
+ )
176
+
177
+ # Solve for the stress tensor
178
+ sol_k_nominal = self.prob_k_nominal.solve()
179
+ structure.elasticity.internal_stress.x.array[:] = sol_k_nominal.x.array[:]
180
+ structure.elasticity.internal_stress.x.scatter_forward()
181
+
182
+ with dolfinx.io.XDMFFile(
183
+ self.comm, self.results_filename_structure, "w"
184
+ ) as xdmf_file:
185
+ tt = 0.0
186
+ xdmf_file.write_mesh(domain.structure.msh)
187
+ xdmf_file.write_function(structure.elasticity.u, 0.0)
188
+ xdmf_file.write_function(structure.elasticity.v_old, 0.0)
189
+ xdmf_file.write_function(structure.elasticity.a_old, 0.0)
190
+ xdmf_file.write_function(structure.elasticity.stress, 0.0)
191
+ xdmf_file.write_function(structure.elasticity.internal_stress, 0.0)
192
+
193
+ if self.comm.rank == 0 and self.comm.size > 1 and params.general.test:
194
+ self.log_filename_structure = f"{params.general.output_dir_sol}/log_str.txt"
195
+
196
+ with open(self.log_filename_structure, "w") as fp:
197
+ fp.write(f"start , size of vec is {vec_check.size}\n")
198
+ fp.close()
199
+
200
+ for n in range(solution_vec.size):
201
+ with open(self.log_filename_structure, "a") as fp:
202
+ fp.write(
203
+ f"row {n}, {vec_check[n]} compared to {solution_vec[n]}, diff is {abs(vec_check[n] - solution_vec[n])}\n"
204
+ )
205
+ if abs(vec_check[n] - solution_vec[n]) > 1e-6:
206
+ fp.write("flag \n")
207
+ fp.close()
208
+ print(
209
+ f"solution vec incorrect in {n}th entry \n {vec_check[n]} compared to {solution_vec[n]} np = {self.comm.size} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
210
+ )
211
+ exit()
212
+ print(f"all values match with np = {self.comm.size}")
213
+
214
+ # def store_vec_rank1(functionspace, vector):
215
+ # imap = vector.function_space.dofmap.index_map
216
+ # local_range = (
217
+ # np.asarray(imap.local_range, dtype=np.int32)
218
+ # * vector.function_space.dofmap.index_map_bs
219
+ # )
220
+ # size_global = imap.size_global * vector.function_space.dofmap.index_map_bs
221
+
222
+ # # Communicate ranges and local data
223
+ # ranges = MPI.COMM_WORLD.gather(local_range, root=0)
224
+ # data = MPI.COMM_WORLD.gather(vector.vector.array, root=0)
225
+ # # print(data)
226
+ # # Communicate local dof coordinates
227
+ # x = functionspace.tabulate_dof_coordinates()[: imap.size_local]
228
+ # x_glob = MPI.COMM_WORLD.gather(x, root=0)
229
+
230
+ # # Declare gathered parallel arrays
231
+ # global_array = np.zeros(size_global)
232
+ # global_x = np.zeros((size_global, 3))
233
+ # u_from_global = np.zeros(global_array.shape)
234
+
235
+ # x0 = functionspace.tabulate_dof_coordinates()
236
+
237
+ # if self.comm.rank == 0:
238
+ # # Create array with all values (received)
239
+ # for r, d in zip(ranges, data):
240
+ # global_array[r[0] : r[1]] = d
241
+
242
+ # return global_array
243
+
244
+ # exit()
245
+
246
+ def save_XDMF_files(self, fsi_object, domain, tt):
247
+ """Write additional timestep to XDMF file
248
+
249
+ This function appends the state of the flow at time `tt` to an existing XDMF file.
250
+
251
+ Args:
252
+ flow (:obj:`pvade.fluid.FlowManager.Flow`): A Flow object
253
+ tt (float): The time at which the current solution exists
254
+
255
+ """
256
+
257
+ if fsi_object.name == "fluid":
258
+ with dolfinx.io.XDMFFile(
259
+ self.comm, self.results_filename_fluid, "a"
260
+ ) as xdmf_file:
261
+ xdmf_file.write_function(fsi_object.u_k, tt)
262
+ xdmf_file.write_function(fsi_object.p_k, tt)
263
+ xdmf_file.write_function(fsi_object.panel_stress, tt)
264
+ xdmf_file.write_function(domain.total_mesh_displacement, tt)
265
+ if self.thermal_analysis:
266
+ xdmf_file.write_function(fsi_object.theta_k, tt)
267
+
268
+ elif fsi_object.name == "structure":
269
+ # Solve for the stress tensor
270
+ sol_k_nominal = self.prob_k_nominal.solve()
271
+ fsi_object.elasticity.internal_stress.x.array[:] = sol_k_nominal.x.array[:]
272
+ fsi_object.elasticity.internal_stress.x.scatter_forward()
273
+
274
+ with dolfinx.io.XDMFFile(
275
+ self.comm, self.results_filename_structure, "a"
276
+ ) as xdmf_file:
277
+ xdmf_file.write_function(fsi_object.elasticity.u, tt)
278
+ xdmf_file.write_function(fsi_object.elasticity.v_old, tt)
279
+ xdmf_file.write_function(fsi_object.elasticity.a_old, tt)
280
+ xdmf_file.write_function(fsi_object.elasticity.stress, tt)
281
+ xdmf_file.write_function(fsi_object.elasticity.internal_stress, tt)
282
+
283
+ else:
284
+ raise ValueError(
285
+ f"Got found fsi object name = {fsi_object.name}, not recognized."
286
+ )
287
+
288
+ # def fluid_struct(self, domain, flow, elasticity, params):
289
+ # # print("tst")
290
+
291
+ # elasticity.stress_old.x.array[:] = elasticity.stress.x.array
292
+ # elasticity.stress_old.x.scatter_forward()
293
+
294
+ # elasticity.stress.interpolate(flow.panel_stress_undeformed)
295
+ # elasticity.stress.x.scatter_forward()
296
+
297
+ # beta = params.structure.beta_relaxation
298
+
299
+ # elasticity.stress.x.array[:] = (
300
+ # beta * elasticity.stress.x.array
301
+ # + (1.0 - beta) * elasticity.stress_predicted.x.array
302
+ # )
303
+
304
+ # elasticity.stress.x.scatter_forward()