Peano
Loading...
Searching...
No Matches
test_periodic_BC-1D.py
Go to the documentation of this file.
1import os
2import argparse
3import numpy as np
4
5import peano4.api
6import exaparty
7import dastgen2
8
9import shutil
10
11
14
15parser = argparse.ArgumentParser(description="Test periodic boundary conditions")
16parser.add_argument(
17 "-j", "--parallel-builds", dest="j", type=int, default=-1, help="Parallel builds"
18)
19parser.add_argument(
20 "-v",
21 "--verbose",
22 dest="verbose",
23 action="store_true",
24 default=False,
25 help="Verbose",
26)
27parser.add_argument(
28 "-c",
29 "--clean",
30 dest="clean_temporary_directories",
31 action="store_true",
32 help="Remove temporary directories before script starts",
33)
34parser.add_argument(
35 "-et",
36 "--end-time",
37 dest="end_time",
38 type=float,
39 default=1.0,
40 help="End of simulation",
41)
42parser.add_argument(
43 "-dt",
44 "--timestep-size",
45 dest="timestep_size",
46 type=float,
47 default=1e-3,
48 help="Global timestep size",
49)
50parser.add_argument(
51 "-plot",
52 "--plot-delta",
53 dest="plot_delta",
54 type=float,
55 default=0.01,
56 help="Plot delta (0 switches off)",
57)
58parser.add_argument(
59 "-cs",
60 "--cell-size",
61 dest="cell_size",
62 type=float,
63 default=0.1,
64 help="Cell size (default 0.1)",
65)
66parser.add_argument(
67 "-pd",
68 "--peano-dir",
69 dest="peanodir",
70 default="../../../../",
71 help="Peano4 directory",
72)
73parser.add_argument(
74 "-a",
75 "--asserts",
76 dest="asserts",
77 action="store_true",
78 default=False,
79 help="Switch on assertions",
80)
81parser.add_argument(
82 "-d",
83 "--debug",
84 dest="debug",
85 action="store_true",
86 default=False,
87 help="Switch on debug assertions",
88)
89args = parser.parse_args()
90
91
92
95
96# peano doesn't like 1D grids
97dimensions = 2
98hydro_dimensions = 1
99
100
102 namespace=["tests", "exaparty", "periodicBoundaryConditions1D"],
103 project_name="periodicBoundaryConditions1D",
104 executable="periodicBCTest1D",
105)
106
107
109 name="GenericParticle",
110 cfl_factor=0.999,
111 initial_time_step_size=args.timestep_size,
112 particles_per_cell=0,
113 min_h=args.cell_size,
114 max_h=0.3,
115 add_partid=(not args.asserts),
116)
117particle_set = project.add_particle_species(particle)
118
119
120if args.debug:
121 build_mode = peano4.api.output.CompileMode.DEBUG
122elif args.asserts:
123 build_mode = peano4.api.output.CompileMode.ASSERTS
124else:
125 build_mode = peano4.api.output.CompileMode.RELEASE
126
127offset = [0, 0, 0]
128domain_size = [1, 1, 1]
129periodic_boundary_conditions = [True, False, False] # Periodic BC
130
131
132project.set_global_simulation_parameters(
133 dimensions=dimensions,
134 offset=offset,
135 domain_size=domain_size,
136 min_end_time=args.end_time,
137 max_end_time=0.0,
138 first_plot_time_stamp=0.0,
139 time_in_between_plots=args.plot_delta,
140 periodic_BC=periodic_boundary_conditions,
141 plotter_precision=8, # plotter precision
142)
143
144project.set_load_balancing(
145 "toolbox::loadbalancing::strategies::SpreadOutHierarchically",
146 "new toolbox::loadbalancing::DefaultConfiguration()",
147)
148
149project.set_Peano4_installation(args.peanodir, build_mode)
150
151
152
157
158particle_force = ""
159
160# particle.cell_kernel = particle_force
161
162
163
173search_radius = args.cell_size / 100.0
174initialisation_snippet = (
175 """
176 /* Internal search radius used by Peano.
177 * Not really used here. */
178 particle->setSearchRadius("""
179 + str(search_radius)
180 + """);
181 particle->setA( {0.} );
182
183 if( particle->getX(0) > 0.5 ){
184 particle->setV( {-1.} );
185 } else {
186 particle->setV( {1.} );
187 }
188
189 // hack to get a somewhat flexible, but always unique
190 // and reproducible particle ID: Assume we have a 1600^3
191 // uniform grid particle distribution. (1600^3 is close)
192 // to the numeric limits of integers.
193
194 double boxsize_x = """
195 + str(domain_size[0])
196 + """;
197 double offset_x = """
198 + str(offset[0])
199 + """;
200
201 double dx = boxsize_x / 1600.;
202
203 int id = (int) ((particle->getX(0) - offset_x) / dx + 0.5);
204 particle->setPartid(id);
205
206"""
207)
208
209coords = np.array([[0.21, 0.5, 0.0], [0.81, 0.5, 0.0]])
210
211project.algorithm_step_initial_conditions.add_action_set(
213 particle_set=particle_set,
214 initialisation_call=initialisation_snippet,
215 coordinates=coords,
216 )
217)
218
219
220
244 "particles",
245 particle_set,
246 "globaldata::{}::getSpecies().getMinTimeStamp()".format(particle.name),
247)
248particle_plotter.add_attribute_to_plot(particle.velocity, dimensions)
249particle_plotter.add_attribute_to_plot(particle.partid, 1)
250project.algorithm_step_plot.add_action_set(particle_plotter)
251
252
253
262peano4_project = project.generate_Peano4_project(verbose=args.verbose)
263peano4_project.output.makefile.add_CXX_flag("-D" + "HYDRO_DIMENSION=1")
264
265peano4_project.constants.export_const_with_type(
266 "GLOBAL_TIME_STEP_SIZE", str(args.timestep_size), "double"
267)
268
269# If we want to start a project from scratch, we may want to remove some of the generated code
270if args.clean_temporary_directories:
271 dirs = ["vertexdata/", "repositories/", "globaldata/", "observers/"]
272 for dir in dirs:
273 shutil.rmtree(dir)
274
275peano4_project.generate(
276 overwrite=peano4.api.output.Overwrite.Always, throw_away_data_after_generation=True
277)
278peano4_project.build(make_clean_first=True, number_of_parallel_builds=args.j)
279
280# remove output files from previous runs
281output_files = [
282 f
283 for f in os.listdir(".")
284 if f.endswith(".peano-patch-file") or f.endswith(".vtu") or f.endswith(".pvd")
285]
286for f in output_files:
287 os.remove(f)
ExaParty project.
Definition Project.py:27