Peano
Loading...
Searching...
No Matches
InsertParticlesAlongCartesianLayoutIntoUnrefinedCells.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3from peano4.solversteps.ActionSet import ActionSet
4
5
6import jinja2
7
8
11 self,
12 particle_set,
13 distance_between_particles,
14 computational_domain_offset,
15 computational_domain_size,
16 initialisation_call="",
17 noise=True,
18 noise_scale=1.0,
19 additional_includes="",
20 guard="true",
21 ):
22 """!
23 Insert particles
24
25
26 :Attibutes:
27
28 particle: ParticleSet
29 I take this as particle set.
30
31 average_distance_between_particles: Float
32 Some positive floating point value.
33
34 initialisation_call: String (C code)
35 Within this code snippet, you have a variable particle which is a pointer to
36 your particle type. You can use it to set the particle attributes.
37
38 noise: Bool
39 If True, add noise to particle coordinates
40
41 noise_scale:
42 If noise=True, the noise added to particle coordinates is +-0.5 * particle
43 distance multiplied by this factor
44
45 computational_domain_offset: [Float]
46 Has to be an array with Dimensions entries that specifies the global domain
47 offset. You can pass in an array of floats, and this is definitely the
48 default use case. However, you can also pass in an arbitrary array of
49 strings, as long as these strings make sense in the generated C++ code
50 and eventually return a double each.
51
52 computational_domain_size: [Float]
53 Defines size of domain.
54
55 guard: String (C code)
56 A simple boolean expression. You can decide if a particle is inserted or not.
57
58 initialisation_call: String (C++ code snippet)
59 Arbitrary code snippet which can work over a pointer particle. The object
60 to which this pointer points to is properly allocated, and its
61 coordinates are set. Furthermore, the object's search radius is
62 initialised with zero. You can alter the particle attributes now, i.e.
63 initialise the domain-specific data. Please also ensure you assign the
64 particle a proper search (interaction) radius if the radius is of
65 relevance for your application.
66
67 Please note that we create a lot of particles and then decide if we
68 actually insert them. Only those particles where we come to the
69 conclusion that we should insert them (as they overlap with the cell)
70 are actually then initialised through this code snippet.
71
72 """
73 super(InsertParticlesAlongCartesianLayoutIntoUnrefinedCells, self).__init__(
74 descend_invocation_order=1, parallel=False
75 )
76
77 self.d = {}
78 self.d["PARTICLE"] = particle_set.particle_model.name
79 self.d["PARTICLES_CONTAINER"] = particle_set.name
80 self.d["INITIALISATION_CALL"] = initialisation_call
81 self.d["DOMAIN_OFFSET"] = computational_domain_offset
82 self.d["DOMAIN_SIZE"] = computational_domain_size
83 self.d["H"] = distance_between_particles
84 self.d["GUARD"] = guard
85 if noise:
86 self.d["NOISE"] = "true"
87 self.d["NOISE_SCALE"] = noise_scale
88 else:
89 self.d["NOISE"] = "false"
90 self.d["NOISE_SCALE"] = "1."
91 self.additional_includes = additional_includes
92
93 __Template_TouchCellFirstTime = jinja2.Template(
94 """
95
96 // Start of template in python/peano4/toolbox/particles/InsertParticlesAlongCartesianLayoutIntoUnrefinedCells.py
97
98 if ( not marker.hasBeenRefined() and not marker.willBeRefined() ) {
99 #if Dimensions==2
100 tarch::la::Vector<2,double> domainOffset = { {{DOMAIN_OFFSET[0]}}, {{DOMAIN_OFFSET[1]}} };
101 tarch::la::Vector<2,double> domainSize = { {{DOMAIN_SIZE[0]}}, {{DOMAIN_SIZE[1]}} };
102 #else
103 tarch::la::Vector<3,double> domainOffset = { {{DOMAIN_OFFSET[0]}}, {{DOMAIN_OFFSET[1]}}, {{DOMAIN_OFFSET[2]}} };
104 tarch::la::Vector<3,double> domainSize = { {{DOMAIN_SIZE[0]}}, {{DOMAIN_SIZE[1]}}, {{DOMAIN_SIZE[2]}} };
105 #endif
106 std::vector< globaldata::{{PARTICLE}}* > newParticles = toolbox::particles::createParticlesAlignedWithGlobalCartesianMesh<globaldata::{{PARTICLE}}>(
107 {{H}},
108 marker.x(),
109 marker.h(),
110 domainOffset,
111 domainSize,
112 {{NOISE}},
113 {{NOISE_SCALE}}
114 );
115 for (auto* particle: newParticles) {
116 if ( {{GUARD}} ) {
117 // See documentation of constructor on initialisation snippet
118 {{INITIALISATION_CALL}}
119 toolbox::particles::insertParticleIntoCell(
120 marker,
121 particle,
122 fineGridVertices{{PARTICLES_CONTAINER}},
123 _spacetreeId
124 );
125 }
126 else {
127 delete particle;
128 }
129 }
130 }
131
132 // End of template in python/peano4/toolbox/particles/InsertParticlesAlongCartesianLayoutIntoUnrefinedCells.py
133"""
134 )
135
136 def get_body_of_operation(self, operation_name):
137 result = "\n"
138 if operation_name == ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
139 result = self.__Template_TouchCellFirstTime.render(**self.d)
140 return result
141
143 return " return std::vector< peano4::grid::GridControlEvent >();\n"
144
146 return __name__.replace(".py", "").replace(".", "_")
147
149 return False
150
151 def get_includes(self):
152 result = jinja2.Template(
153 """
154#include "vertexdata/{{PARTICLES_CONTAINER}}.h"
155#include "globaldata/{{PARTICLE}}.h"
156#include "toolbox/particles/particles.h"
157#include "toolbox/particles/ParticleFactory.h"
158"""
159 )
160 return result.render(**self.d) + self.additional_includes
161
162 def get_attributes(self):
163 return f"""
164 int _spacetreeId;
165"""
166
168 return """
169 _spacetreeId = treeNumber;
170"""
Action set (reactions to events)
Definition ActionSet.py:6
__init__(self, particle_set, distance_between_particles, computational_domain_offset, computational_domain_size, initialisation_call="", noise=True, noise_scale=1.0, additional_includes="", guard="true")
Insert particles.