Peano
Loading...
Searching...
No Matches
testLeapfrogFixedTimeStepSize.py
Go to the documentation of this file.
1# This file is part of the SWIFT2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3from swift2.particle.Particle import Particle
4from swift2.particle.AlgorithmStep import AlgorithmStep
5
6import peano4
7import dastgen2
9
10
12 """
13
14 Simple particle with a fixed interaction radius h which moves according
15 to leapfrog KDK scheme.
16 By default, it uses global time stepping,
17 i.e. the combination of maximum velocity and minimal mesh size determines
18 the time step size of the subsequent time step. Besides the default
19 variables x and h, the particle has the following properties:
20
21 - a vector a which is the acceleration;
22 - a vector v which is the velocity.
23
24 You can add further properties via
25
26 myparticle.data.add_attribute( peano4.dastgen2.Peano4DoubleArray("myFancyArray","Dimensions") )
27
28 in your code. Or you can create a subclass which adds additional fields
29 after it has called the baseline constructor.
30
31 You will need to add further properties for any SPH project.
32
33 ## Force calculation
34
35 particle_particle_interaction_over_particle_sets_kernel is a C++ string which defines a force between two
36 particles. It has access to three important objects:
37
38 - localParticles is a container (list) over pointers to local particles
39 - activeParticles is a container (list) over pointers to active particles
40 - marker is a cell marker which identifies the current cell.
41
42 Please consult the guidebook for a definition of local and active particles
43 but take into account that the local particles always are a subset of the
44 active particles.
45
46 Besides the actual particle-to-particle calculation, i.e. a force
47 calculation, users can also provide kernels that kick in when you touch
48 particles for the first time before you actually compute any
49 particle-particle interaction, and there is a plug-in point what you do
50 just once the particle-particle interaction has terminated. The latter
51 point is reached before we do the actual time stepping. In both plug-in
52 points, you have a vertex marker which gives you the position of the
53 vertex to which a particular is associated, and you have the localParticles.
54 This is a vector of pointers in this particular case.
55
56 @see peano4::datamanagement::CellMarker
57 @see peano4::datamanagement::VertexMarker
58
59 """
60
62 self,
63 name,
64 cfl_factor,
65 initial_time_step_size,
66 constant_time_step_size,
67 particle_particle_interaction_over_particle_sets_kernel="",
68 touch_particles_of_set_first_time_kernel="",
69 touch_particles_of_set_last_time_kernel="",
70 particles_per_cell=0, # makes no sense, and should likely be forbidden
71 min_h=0.005,
72 max_h=0.3,
73 ):
74 super(testLeapfrogFixedTimeStepSize, self).__init__(
75 name,
76 particles_per_cell,
77 min_h,
78 max_h,
79 )
80
81 self._cfl_factor = cfl_factor
82 self._initial_time_step_size = initial_time_step_size
83
84 if constant_time_step_size == True:
85 adjustTimeStepSize = "true"
86 else:
87 adjustTimeStepSize = "false"
88 self._adjustTimeStepSize = adjustTimeStepSize
89
90 # Particle attributes
93 self.data.add_attribute(self._velocity)
94 self.data.add_attribute(self._accelerator)
95
96 # Dummy SPH arrays for compilation. Not relevant for non-SPH
97 # Required by the Kick2 SPH compute kernel
98 # Altrnatively, we might want to introduce a separate version of this compute kernel
99 self._v_full = peano4.dastgen2.Peano4DoubleArray("v_full", "Dimensions")
107 self.data.add_attribute(self._u)
108 self.data.add_attribute(self._density)
109 self.data.add_attribute(self._pressure)
110 self.data.add_attribute(self._v_full)
111 self.data.add_attribute(self._u_full)
112 self.data.add_attribute(self._soundSpeed)
113 self.data.add_attribute(self._v_sig_AV)
114 self.data.add_attribute(self._is_boundary_part)
115
117 particle_particle_interaction_over_particle_sets_kernel
118 )
120 touch_particles_of_set_first_time_kernel
121 )
123 touch_particles_of_set_last_time_kernel
124 )
125
126 # now set global parameters as dastgen attributes
127 self.set_parameters()
128
129 return
130
131 def set_parameters(self):
132 """
133 This function translates "global" particle parameters which are
134 constant throughout the simulation (like CFL factor, minimal time step
135 size, viscosity parameters...) into dastgen attributes of the C++
136 particle class.
137 If you modify any of the attributes manually outside of the particle
138 initialisation, e.g. by invoking
139
140 ```
141 particle = SPHLeapfrogFixedSearchRadius(initial_time_step_size=ABC, ...)
142 particle.initial_time_step_size = XYZ
143 ```
144
145 you need to call this function manually so your changes propagate
146 into the generated C++ files.
147 """
148
149 const_static = dastgen2.attributes.Attribute.Qualifier.CONST_STATIC
150
152 "cfl", qualifier=const_static, initval=self._cfl_factor
153 )
154 self.data.add_or_replace_attribute(cfl_attr)
155
156 initial_time_step_size_attr = dastgen2.attributes.Double(
157 "initialTimeStepSize",
158 qualifier=const_static,
159 initval=self._initial_time_step_size,
160 )
161 self.data.add_or_replace_attribute(initial_time_step_size_attr)
162
163 adjust_time_step_size_attr = dastgen2.attributes.Boolean(
164 "adjustTimeStepSize",
165 qualifier=const_static,
166 initval=self._adjustTimeStepSize,
167 )
168 self.data.add_or_replace_attribute(adjust_time_step_size_attr)
169
170 return
171
173 """
174
175 Leapfrog consists basically of four steps per particle. We first
176 determine the force. Then we update the velocity by half a timestep and move
177 the particle by a full timestep. Then the force is re-computed and the
178 second half of the velocity update is done.
179 Some variations of this KDK form re-arrange the steps executed per timestep
180 to avoid a second force loop.
181 """
182 return [
184 name="ForceCalculation",
185 dependencies=AlgorithmStep.Dependencies.NEIGHBOURS,
186 effect=AlgorithmStep.Effect.ALTER_LOCAL_STATE,
187 touch_vertex_first_time_kernel=self.touch_particles_of_set_first_time_kernel,
188 cell_kernel="""
189 ::swift2::kernels::genericInteraction<globaldata::{}>(
190 marker,
191 localParticles,
192 activeParticles,
193 [&] (globaldata::{}* localParticle, const globaldata::{} * const globalParticle) -> void {{
194 {}
195 }}
196 );
197 """.format(
199 self.namenamenamename,
200 self.namenamenamename,
202 ),
203 touch_vertex_last_time_kernel=self.touch_particles_of_set_last_time_kernel,
204 includes="""
205 #include "Constants.h"
206 #include "swift2/kernels/ParticleParticleInteraction.h"
207 """,
208 ),
210 name="Kick1",
211 dependencies=AlgorithmStep.Dependencies.SELF,
212 effect=AlgorithmStep.Effect.ALTER_LOCAL_STATE,
213 touch_vertex_first_time_kernel="""::swift2::timestepping::resetMovedParticleMarker(assignedParticles);""",
214 touch_vertex_last_time_kernel="""::swift2::timestepping::computeLeapfrogKickWithGlobalTimeStepSize(
215 assignedParticles);""",
216 prepare_traversal_kernel="""::swift2::timestepping::computeCFLTimeStepSizeSPH<globaldata::{}>();""".format(
217 self.namenamenamename,
218 ),
219 includes="""
220 #include "Constants.h"
221 #include "swift2/timestepping/Leapfrog.h"
222 #include "swift2/timestepping/GlobalTimeStepping.h"
223 #include "swift2/timestepping/TimeStepping.h"
224
225 #include "swift2/kernels/kernel_hydro.h"
226 #include "swift2/kernels/equation_of_state.h"
227 """,
228 ),
230 name="Drift",
231 dependencies=AlgorithmStep.Dependencies.SELF,
232 effect=AlgorithmStep.Effect.CHANGE_POSITION_OR_INTERACTION_RADIUS_BUT_NEVER_RERUN,
233 touch_vertex_first_time_kernel="""::swift2::timestepping::resetMovedParticleMarker(assignedParticles);""",
234 touch_vertex_last_time_kernel="""::swift2::timestepping::computeLeapfrogDriftWithGlobalTimeStepSize( assignedParticles );""",
235 includes="""
236 #include "Constants.h"
237 #include "swift2/timestepping/Leapfrog.h"
238 #include "swift2/timestepping/GlobalTimeStepping.h"
239 #include "swift2/timestepping/TimeStepping.h"
240 """,
241 ),
243 name="ForceCalculation",
244 dependencies=AlgorithmStep.Dependencies.NEIGHBOURS,
245 effect=AlgorithmStep.Effect.ALTER_LOCAL_STATE,
246 touch_vertex_first_time_kernel=self.touch_particles_of_set_first_time_kernel,
247 cell_kernel="""
248 ::swift2::kernels::genericInteraction<globaldata::{}>(
249 marker,
250 localParticles,
251 activeParticles,
252 [&] (globaldata::{}* localParticle, const globaldata::{} * const globalParticle) -> void {{
253 {}
254 }}
255 );
256 """.format(
257 self.namenamenamename,
258 self.namenamenamename,
259 self.namenamenamename,
261 ),
262 touch_vertex_last_time_kernel=self.touch_particles_of_set_last_time_kernel,
263 includes="""
264 #include "Constants.h"
265 #include "swift2/kernels/ParticleParticleInteraction.h"
266 """,
267 ),
269 name="Kick2",
270 dependencies=AlgorithmStep.Dependencies.SELF,
271 effect=AlgorithmStep.Effect.ALTER_LOCAL_STATE,
272 touch_vertex_first_time_kernel="""::swift2::timestepping::resetMovedParticleMarker(assignedParticles);""",
273 touch_vertex_last_time_kernel="""::swift2::timestepping::computeLeapfrogKickWithGlobalTimeStepSize(
274 assignedParticles);""",
275 includes="""
276 #include "Constants.h"
277 #include "swift2/timestepping/Leapfrog.h"
278 #include "swift2/timestepping/GlobalTimeStepping.h"
279 #include "swift2/timestepping/TimeStepping.h"
280
281 #include "swift2/kernels/kernel_hydro.h"
282 #include "swift2/kernels/equation_of_state.h"
283 """,
284 ),
286 name="ReduceGlobalQuantities",
287 dependencies=AlgorithmStep.Dependencies.SELF,
288 effect=AlgorithmStep.Effect.ALTER_GLOBAL_STATE,
289 cell_kernel="""::swift2::statistics::reduceVelocityAndSearchRadius( localParticles );""",
290 prepare_traversal_kernel="""
291 globaldata::{PARTICLE}::getSpecies().clearSearchRadius();
292 globaldata::{PARTICLE}::getSpecies().clearVelocity();
293 """.replace(
294 "{PARTICLE}", self.namenamenamename
295 ),
296 unprepare_traversal_kernel="""
297 globaldata::{PARTICLE}::getSpecies().setTimeStamp( globaldata::{PARTICLE}::getSpecies().getMinTimeStamp() + globaldata::{PARTICLE}::getSpecies().getMinTimeStepSize(), false );
298 ::swift2::statistics::reportSearchRadiusVTDt<globaldata::{PARTICLE}>( "{PARTICLE}" );
299 """.replace(
300 "{PARTICLE}", self.namenamenamename
301 ),
302 includes="""
303 #include "Constants.h"
304 #include "swift2/statistics/Reports.h"
305 #include "swift2/statistics/Statistics.h"
306 """,
307 ),
308 ]
309
311 """!
312
313 No particular initialisation required
314
315 """
316 return []
cardinality is a string (you can thus use symbols as well as long as they will be defined at compile ...
Definition Double.py:6
Specialisation of dastgen2.attributes.DoubleArray which relies on Peano's tarch.
Defines the meta data around one algorithmic step per particle.
Base class for any particle in the project.
Definition Particle.py:10
Simple particle with a fixed interaction radius h which moves according to leapfrog KDK scheme.
set_parameters(self)
This function translates "global" particle parameters which are constant throughout the simulation (l...
__init__(self, name, cfl_factor, initial_time_step_size, constant_time_step_size, particle_particle_interaction_over_particle_sets_kernel="", touch_particles_of_set_first_time_kernel="", touch_particles_of_set_last_time_kernel="", particles_per_cell=0, min_h=0.005, max_h=0.3)
Initialise the particle.