Peano
Loading...
Searching...
No Matches
InsertParticlesFromFile.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
5import jinja2
6
8
10
11import numpy as np
12
13
15 def __init__(self, particle_set, filename, scale_factor=1.0):
16 """
17
18 particle_set: ParticleSet
19
20 filename: String
21 Name of the sample point table, currently we have: t-design (948 points), Gauss_Legendre_quadrature (800 points)
22
23 scale_factor
24 scale factor for all coordinates, used for scale up/down the sample table from unit sphere to certain radius.
25 set to 1 if your table is exact.
26
27 """
28
29 self.d = {}
30 self.d["PARTICLE"] = particle_set.particle_model.name
31 self.d["PARTICLES_CONTAINER"] = particle_set.name
32 self._filename = filename
33 self._scale = scale_factor
34
35 __Template_TouchCellFirstTime = jinja2.Template(
36 """
37 if (not marker.willBeRefined()) {
38 tarch::multicore::Lock lock( _semaphore );
39
40 std::list< tarch::la::Vector<Dimensions,double> > coords = _fileReader.getParticlesWithinVoxel(marker.x(), marker.h(), true);
41 std::list< tarch::la::Vector<Dimensions,double> >::iterator p = coords.begin();
42 //for (auto* p: coords) {
43 while ( p!=coords.end() ) {
44 globaldata::{{PARTICLE}}* newParticle = new globaldata::{{PARTICLE}}();
45
46 newParticle->setNumber(0, _spacetreeId);
47 newParticle->setNumber(1, _particleNumberOnThisTree);
48 _particleNumberOnThisTree++;
49
50 logInfo( "touchVertexFirstTime(...)", "insert particle " << newParticle->toString() << " into " << marker.toString() );
51
52 toolbox::particles::init(*newParticle,*p,0.0);
53
54 toolbox::particles::insertParticleIntoCell(
55 marker,
56 newParticle,
57 fineGridVertices{{PARTICLES_CONTAINER}}
58 );
59 p++;
60 }
61
62 logDebug( "touchVertexFirstTime(...)", "assigned " << coords.size() << " particle(s) to vertex " << marker.toString() << " on tree " << _spacetreeId );
63 }
64"""
65 )
66
67 __Template_EndTraversal = jinja2.Template(
68 """
69 if (_particleNumberOnThisTree==0) {
70 logWarning( "endIteration()", "inserted " << _particleNumberOnThisTree << " particle(s) on tree " << _spacetreeId );
71 }
72 else {
73 logInfo( "endIteration()", "inserted " << _particleNumberOnThisTree << " particle(s) on tree " << _spacetreeId << " (boundary vertices might host redundant particle data)" );
74 }
75 #if !defined(Parallel) and !defined(SharedMemoryParallelisation)
76 assertion( _particleNumberOnThisTree>0 );
77 #endif
78"""
79 )
80
81 def get_body_of_operation(self, operation_name):
82 result = "\n"
83 if operation_name == ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
84 result = self.__Template_TouchCellFirstTime.render(**self.d)
85 if operation_name == ActionSet.OPERATION_END_TRAVERSAL:
86 result = self.__Template_EndTraversal.render(**self.d)
87 return result
88
90 return " return std::vector< peano4::grid::GridControlEvent >();\n"
91
93 return __name__.replace(".py", "").replace(".", "_")
94
96 return False
97
98 def get_includes(self):
99 result = jinja2.Template(
100 """
101#include "tarch/multicore/Lock.h"
102#include "tarch/multicore/multicore.h"
103#include "vertexdata/{{PARTICLES_CONTAINER}}.h"
104#include "globaldata/{{PARTICLE}}.h"
105#include "toolbox/particles/FileReader.h"
106#include "toolbox/particles/particles.h"
107#include "toolbox/particles/ParticleFactory.h"
108#include <fstream>
109#include <string>
110#include <vector>
111"""
112 )
113 return result.render(**self.d)
114
115 def get_static_initialisations(self, full_qualified_classname):
116 return (
117 """
118tarch::multicore::BooleanSemaphore """
119 + full_qualified_classname
120 + """::_semaphore;
121toolbox::particles::FileReader """
122 + full_qualified_classname
123 + """::_fileReader;
124"""
125 )
126
128 # return super( InsertParticlesFromFile ).get_constructor_body() + """
129 return (
130 f"""
131 _particleNumberOnThisTree = 0;
132 _spacetreeId = treeNumber;
133
134 tarch::multicore::Lock lock( _semaphore );
135
136 if ( _fileReader.empty() )
137 _fileReader.readDatFile( \""""
138 + self._filename
139 + """\", """
140 + str(self._scale)
141 + """);
142"""
143 )
144
145 # def get_destructor_body(self):
146 # return """
147 # """
148
149 def get_attributes(self):
150 # return super( InsertParticlesFromFile ).get_attributes() + """
151 return """
152 static tarch::multicore::BooleanSemaphore _semaphore;
153
154 int _particleNumberOnThisTree;
155 int _spacetreeId;
156 static toolbox::particles::FileReader _fileReader;
157"""
__init__(self, particle_set, filename, scale_factor=1.0)
particle_set: ParticleSet
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
get_attributes(self)
Return attributes as copied and pasted into the generated class.
user_should_modify_template(self)
Is the user allowed to modify the output.
Default superclass for any data model in Peano which is stored within the grid.
Definition DaStGen2.py:47
Action set (reactions to events)
Definition ActionSet.py:6