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.api.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 Dimensions==2
99 tarch::la::Vector<2,double> domainOffset = { {{DOMAIN_OFFSET[0]}}, {{DOMAIN_OFFSET[1]}} };
100 tarch::la::Vector<2,double> domainSize = { {{DOMAIN_SIZE[0]}}, {{DOMAIN_SIZE[1]}} };
101 #else
102 tarch::la::Vector<3,double> domainOffset = { {{DOMAIN_OFFSET[0]}}, {{DOMAIN_OFFSET[1]}}, {{DOMAIN_OFFSET[2]}} };
103 tarch::la::Vector<3,double> domainSize = { {{DOMAIN_SIZE[0]}}, {{DOMAIN_SIZE[1]}}, {{DOMAIN_SIZE[2]}} };
104 #endif
105 std::vector< globaldata::{{PARTICLE}}* > newParticles = toolbox::particles::createParticlesAlignedWithGlobalCartesianMesh<globaldata::{{PARTICLE}}>(
106 {{H}},
107 marker.x(),
108 marker.h(),
109 domainOffset,
110 domainSize,
111 {{NOISE}},
112 {{NOISE_SCALE}}
113 );
114 for (auto* particle: newParticles) {
115 if ( {{GUARD}} ) {
116 // See documentation of constructor on initialisation snippet
117 {{INITIALISATION_CALL}}
118 toolbox::particles::insertParticleIntoCell(
119 marker,
120 particle,
121 fineGridVertices{{PARTICLES_CONTAINER}},
122 _spacetreeId
123 );
124 }
125 else {
126 delete particle;
127 }
128 }
129
130 // End of template in python/peano4/toolbox/particles/InsertParticlesAlongCartesianLayoutIntoUnrefinedCells.py
131"""
132 )
133
134 def get_body_of_operation(self, operation_name):
135 result = "\n"
136 if operation_name == ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
137 result = self.__Template_TouchCellFirstTime.render(**self.d)
138 return result
139
140 def get_guard_of_operation(self, operation_name):
141 if operation_name == ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
142 return "not marker.hasBeenRefined() and not marker.willBeRefined()"
143 return "true"
144
146 return " return std::vector< peano4::grid::GridControlEvent >();\n"
147
149 return __name__.replace(".py", "").replace(".", "_")
150
152 return False
153
154 def get_includes(self):
155 result = jinja2.Template(
156 """
157#include "vertexdata/{{PARTICLES_CONTAINER}}.h"
158#include "globaldata/{{PARTICLE}}.h"
159#include "toolbox/particles/particles.h"
160#include "toolbox/particles/ParticleFactory.h"
161"""
162 )
163 return result.render(**self.d) + self.additional_includes
164
165 def get_attributes(self):
166 return f"""
167 int _spacetreeId;
168"""
169
171 return """
172 _spacetreeId = treeNumber;
173"""
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.