ASL  0.1.7
Advanced Simulation Library
testDistanceFunction.cc
Go to the documentation of this file.
1 /*
2  * Advanced Simulation Library <http://asl.org.il>
3  *
4  * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5  *
6  *
7  * This file is part of Advanced Simulation Library (ASL).
8  *
9  * ASL is free software: you can redistribute it and/or modify it
10  * under the terms of the GNU Affero General Public License as
11  * published by the Free Software Foundation, version 3 of the License.
12  *
13  * ASL is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 
28 #include "math/aslVectors.h"
29 #include "aslGenerators.h"
31 #include "aslGeomInc.h"
32 #include <stdlib.h> /* srand, rand */
33 #include <time.h>
35 #include "num/aslDFOptimizer.h"
36 #include "math/aslTemplates.h"
37 
38 //typedef float FlT;
39 typedef double FlT;
40 
41 using asl::AVec;
42 using asl::makeAVec;
43 
45 {
46  // Geometry description
47  // Radius
48  FlT r(10.);
49 
50  // Generates a sphere with radius r and center at (50., 50.)
51  auto df1(generateDFSphere(r, asl::makeAVec(50., 50.)));
52  auto df2(generateDFSphere(r, asl::makeAVec(40., 40.)));
53  auto df3(generateDFSphere(2. * r, asl::makeAVec(50., 50.)));
54  // Resulting geometry: union of the spheres df1 and df2 intersected by df3
55  auto resultGeometry((df1 | df2) & df3);
56 
57  // Geometry to Data conversion
58  // Grid size (= discrete size of the simulated domain)
59  asl::AVec<int> size(asl::makeAVec(100., 100.));
60  // Grid resolution (= space step)
61  FlT dx(1.);
62  // Creates a Block which describes the grid
63  asl::Block block(size, dx);
64  // Allocates memory for the data that corresponds to
65  // the nodes of the grid desribed by the \p block.
66  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
67 
68  // Initializes the \p data with the values of the distance function
69  // extracted from all points of the \p resultGeometry.
70  asl::initData(data, resultGeometry);
71 
72  // Writes the \p data into the file.
73  asl::writeVTKXML("distFOperation2D.vti", *data, "data");
74 
75  return true;
76 }
77 
78 
80 {
81  FlT r(10.);
82  FlT dx(1.);
83  asl::AVec<int> size(asl::makeAVec(50.,50.,50.));
84 
85  asl::Block block(size, dx);
86  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
87 
88  auto df1(generateDFSphere(r, asl::AVec<FlT>(size)*.5));
89  auto df2(generateDFSphere(r, asl::AVec<FlT>(size)*.4));
90  auto df3(generateDFSphere(1.5*r, asl::AVec<FlT>(size)*.5));
91  asl::initData(data, ((df1 | df2) & df3));
92 
93  asl::writeVTKXML("distFOperation3D.vti", *data, "data");
94 
95  return true;
96 }
97 
98 
100 {
101  FlT r(3.);
102  FlT spacing(4.);
103  FlT dx(1.);
104  asl::AVec<int> size(asl::makeAVec(50., 50., 50.));
105 
106  asl::Block block(size, dx);
107  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
108 
109  vector<asl::SPDistanceFunction> cylinders;
110  asl::SPDistanceFunction resultGeometry;
111  asl::AVec<FlT> orientation(asl::makeAVec(0., 0., 1.));
112  for (int i = 0; i < size[0] / (2 * r + spacing); ++i)
113  {
114  for (int j = 0; j < size[1] / (2 * r + spacing); ++j)
115  {
116  cylinders.push_back(generateDFCylinderInf(r, orientation, asl::makeAVec(i * (2. * r + spacing) + r + spacing / 2., j * (2. * r + spacing) + r + spacing / 2., 0.)));
117  resultGeometry = resultGeometry | cylinders.back();
118  }
119  }
120 
121  asl::initData(data, resultGeometry);
122 
123  asl::writeVTKXML("distFOrderedCylinders.vti", *data, "data");
124 
125  return true;
126 }
127 
128 
130 {
131  FlT r(3.);
132  FlT spacing(4.);
133  FlT dx(1.);
134  asl::AVec<int> size(asl::makeAVec(100., 100., 100.));
135 
136  asl::Block block(size, dx);
137  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
138 
139  vector<asl::SPDistanceFunction> cylinders;
140  asl::SPDistanceFunction resultGeometry;
141  asl::AVec<FlT> orientation(asl::makeAVec(0., 0., 1.));
142  srand (time(NULL));
143  for (unsigned int i = 0; i < size[0] / (2 * r + spacing); ++i)
144  {
145  for (unsigned int j = 0; j < size[1] / (2 * r + spacing); ++j)
146  {
147  for (unsigned int d = 0; d < orientation.getSize(); ++d)
148  orientation[d] = rand() % size[d];
149 
150  cylinders.push_back(generateDFCylinderInf(r, orientation, asl::makeAVec(i * (2. * r + spacing) + r + spacing / 2., j * (2. * r + spacing) + r + spacing / 2., (FlT) (rand() % size[2]))));
151  resultGeometry = resultGeometry | cylinders.back();
152  }
153  }
154 
155  asl::initData(data, resultGeometry);
156 
157  asl::writeVTKXML("distFUnorderedCylinders.vti", *data, "data");
158 
159  return true;
160 }
161 
162 
164 {
165  FlT r(10.);
166  FlT dx(1.);
167  asl::AVec<int> size(asl::makeAVec(100.,100.));
168 
169  asl::Block block(size, dx);
170  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
171 
172  auto df1(generateDFSphere(r, asl::AVec<FlT>(size)*.5));
173  asl::initData(data, normalize(df1, dx));
174 
175  asl::writeVTKXML("distFNormalization2D.vti", *data, "data");
176 
177  return true;
178 }
179 
180 
182 {
183  FlT r(10.);
184  FlT dx(1.);
185  asl::AVec<int> size(asl::makeAVec(50.,50.,50.));
186 
187  asl::Block block(size, dx);
188  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
189 
190  auto df1(generateDFSphere(r, asl::AVec<FlT>(size)*.5));
191  asl::initData(data, normalize(df1,dx));
192 
193  asl::writeVTKXML("distFNormalization3D.vti", *data, "data");
194 
195  return true;
196 }
197 
198 
200 {
201  FlT r(10.);
202  FlT dx(1.);
203  asl::AVec<int> size(asl::makeAVec(50.,50.,50.));
204 
205  asl::Block block(size, dx);
206  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
207 
208  auto center(asl::AVec<FlT>(size)*.5);
209  auto df1(generateDFSphere(r, center));
210  auto df2(asl::generateDFConvexPolygonPrism({center+asl::makeAVec(4.,0.,0.),
211  center+asl::makeAVec(4.,4.,0.),
212  center+asl::makeAVec(-4.,0.,0.),
213  center+asl::makeAVec(-4.,-4.,0.)}));
214  asl::initData(data, (df1 & (-df2)));
215 
216  asl::writeVTKXML("distFOperation3DPrism.vti", *data, "data");
217 
218  return true;
219 }
220 
221 
223 {
224  FlT r(10.);
225  FlT dx(1.);
226  asl::AVec<int> size(asl::makeAVec(50.,50.,50.));
227 
228  asl::Block block(size, dx);
229  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
230 
231  auto center(asl::AVec<FlT>(size)*.5);
232  auto df1(generateDFSphere(r, center));
233  auto df2(generateDFInBlock(block, 1));
234 
235  asl::initData(data, (df1 | df2));
236 
237  asl::writeVTKXML("distFOperation3DBlock.vti", *data, "data");
238 
239  return true;
240 }
241 
243 {
244 
245 
246 // FlT hBath(2.);
247  FlT rBath(1.);
248  FlT rDisk(.9);
249  FlT hDisk(0.1);
250  FlT dx(.02);
251 
252  FlT rAxis(0.05);
253  FlT hAxis(.5);
254 
255  FlT wPillar(.2);
256  FlT dPillar(.1);
257 
258  FlT aCrystal(.5);
259  FlT hCrystalBase(.5);
260  FlT hCrystalPyramid(.5);
261 
262  asl::AVec<int> size(asl::makeAVec(105.,105.,100.));
263 
264  asl::AVec<>center(.5*dx*AVec<>(size));
265 
266  vector<asl::AVec<>> pillar1{asl::makeAVec(wPillar*.5, dPillar*.5,0.),
267  asl::makeAVec(-wPillar*.5, dPillar*.5,0.),
268  asl::makeAVec(-wPillar*.5, -dPillar*.5,0.),
269  asl::makeAVec(wPillar*.5, -dPillar*.5,0.)};
270 
271  vector<asl::AVec<>> pillar2{asl::makeAVec(dPillar*.5, wPillar*.5,0.),
272  asl::makeAVec(-dPillar*.5, wPillar*.5,0.),
273  asl::makeAVec(-dPillar*.5, -wPillar*.5,0.),
274  asl::makeAVec(dPillar*.5, -wPillar*.5,0.)};
275 
276  vector<asl::AVec<>> pillarC{asl::makeAVec(center[0]+rDisk-dPillar*.5, center[1], 0.),
277  asl::makeAVec(center[0]-rDisk+dPillar*.5, center[1], 0.),
278  asl::makeAVec(center[0], center[1]+rDisk-dPillar*.5,0.),
279  asl::makeAVec(center[0], center[1]-rDisk+dPillar*.5,0.)};
280  vector<vector<asl::AVec<>>> pillarsPoints(4);
281  for(unsigned int i(0); i<4; ++i)
282  pillarsPoints[i].resize(4);
283 
284  for(unsigned int i(0); i<4; ++i)
285  {
286  pillarsPoints[0][i] = pillar2[i] + pillarC[0];
287  pillarsPoints[1][i] = pillar2[i] + pillarC[1];
288  pillarsPoints[2][i] = pillar1[i] + pillarC[2];
289  pillarsPoints[3][i] = pillar1[i] + pillarC[3];
290  }
291 
292 
293  asl::Block block(size, dx);
294  auto mBath(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
295  auto mPlatform(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
296  auto mCrystal(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
297 
298  auto bath(-generateDFCylinderInf(rBath, asl::makeAVec(0.,0.,1.),
299  dx*asl::AVec<FlT>(size)*.5));
300 
301  auto diskBottom(generateDFCylinder(rDisk,
302  asl::makeAVec(0., 0., hDisk),
303  asl::makeAVec(center[0], center[1], .5*hDisk)));
304  auto diskTop(generateDFCylinder(rDisk,
305  asl::makeAVec(0., 0., hDisk),
306  asl::makeAVec(center[0], center[1], -.5*hDisk - hAxis + dx*size[2])));
307  auto axis(generateDFCylinder(rAxis,
308  asl::makeAVec(0., 0., hAxis+hDisk*.5),
309  asl::makeAVec(center[0], center[1], - .5*hAxis - hDisk*.25 + dx*size[2])));
310  auto dfPillar1(generateDFConvexPolygonPrism(pillarsPoints[0]));
311  auto dfPillar2(generateDFConvexPolygonPrism(pillarsPoints[1]));
312  auto dfPillar3(generateDFConvexPolygonPrism(pillarsPoints[2]));
313  auto dfPillar4(generateDFConvexPolygonPrism(pillarsPoints[3]));
314  auto dfPillars((dfPillar1 | dfPillar2 | dfPillar3 | dfPillar4) &
315  generateDFPlane(makeAVec(0.,0.,-1.), makeAVec(0.,0.,.5*hDisk)) &
316  generateDFPlane(makeAVec(0.,0.,1.), makeAVec(0.,0.,-.5*hDisk - hAxis + dx*size[2])));
317 
318 
319  auto crystalB(asl::generateDFConvexPolygonPrism({center+makeAVec( aCrystal, aCrystal,0.),
320  center+makeAVec(-aCrystal, aCrystal,0.),
321  center+makeAVec(-aCrystal, -aCrystal,0.),
322  center+makeAVec( aCrystal, -aCrystal,0.)}) &
323  generateDFPlane(makeAVec(0.,0.,-1.), makeAVec(0.,0., hDisk)) &
324  generateDFPlane(makeAVec(0.,0., 1.), makeAVec(0.,0., hDisk + hCrystalBase)));
325  auto cCrPyrBase(makeAVec(center[0],center[1],hDisk+hCrystalBase-.01));
326  auto crystalT(asl::generateDFConvexPolygonPyramid({cCrPyrBase+makeAVec( aCrystal, aCrystal,0.),
327  cCrPyrBase+makeAVec(-aCrystal, aCrystal,0.),
328  cCrPyrBase+makeAVec(-aCrystal, -aCrystal,0.),
329  cCrPyrBase+makeAVec( aCrystal, -aCrystal,0.)},
330  cCrPyrBase+makeAVec(0.,0.,hCrystalPyramid)));
331 
332  asl::initData(mBath, normalize(bath, dx));
333  asl::initData(mPlatform, normalize(diskBottom | diskTop | axis | dfPillars, dx));
334  asl::initData(mCrystal, normalize(crystalB | crystalT, dx));
335 
336 // asl::writeVTKXML("distFAdvanced3D.vti", *data, "data");
337  asl::WriterVTKXML writer("distFAdvanced3D");
338  writer.addScalars("Bath", *mBath);
339  writer.addScalars("Platform", *mPlatform);
340  writer.addScalars("Crystal", *mCrystal);
341  writer.write();
342 
343  return true;
344 }
345 
346 
348 {
349  FlT r(10.);
350  FlT dx(1.);
351  asl::AVec<int> size(asl::makeAVec(50.,50.,50.));
352 
353  asl::Block block(size, dx);
354  auto data(asl::generateDataContainerACL_SP<FlT>(block, 1, 1u));
355 
356  auto center(asl::AVec<FlT>(size)*.5);
357  auto df1(generateDFSphere(r, center));
358  auto df2(generateDFSphere(r, .6*center));
359 
360  asl::initData(data, normalize((df1 | df2),dx));
361  optimizeMap(data, &asl::d3q15());
362 
363  asl::writeVTKXML("distDistFOptimizer.vti", *data, "data");
364 
365  return true;
366 }
367 
368 
369 int main()
370 {
381 
382  return 0;
383 }
bool testDistFOperations3DBlock()
bool testDistFOperations3DPrism()
std::shared_ptr< DistanceFunction > SPDistanceFunction
Definition: aslGeomInc.h:44
float FlT
SPDistanceFunction generateDFSphere(double r, const AVec< double > &c)
generates sphere
AVec< T > makeAVec(T a1)
bool testDistFAdvanced3D()
void optimizeMap(SPDataWithGhostNodesACLData c, const VectorTemplate *vt)
const AVec normalize(const AVec< T > &a)
AVec< T > makeAVec(T a1)
SPDistanceFunction generateDFConvexPolygonPrism(std::vector< AVec< double >> points)
generates infinite prism with convex polygon at its base
bool testDistFNormalization2D()
SPDistanceFunction generateDFCylinderInf(double r, const AVec< double > &l, const AVec< double > &c)
generates infinite cylinder
SPDistanceFunction generateDFPlane(const AVec< double > &n, const AVec< double > &p0)
bool testDistFUnorderedCylinders()
bool testDistFOptimizer()
bool testDistFNormalization3D()
const VectorTemplate & d3q15()
Vector template.
acl::VectorOfElements dx(const TemplateVE &a)
differential operator
int main()
SPDistanceFunction generateDFConvexPolygonPyramid(std::vector< AVec< double >> points, AVec< double > a)
generates pyramid with convex polygon at its base and apex a
bool testDistFOperations2D()
void initData(SPAbstractData d, double a)
void addScalars(std::string name, AbstractData &data)
void writeVTKXML(const std::string &fileName, const AbstractData &data, const std::string &name)
definition of class АVec<T>
bool testDistFOperations3D()
bool testDistFOrderedCylinders()
const unsigned int & getSize() const
double FlT
SPDistanceFunction generateDFCylinder(double r, const AVec< double > &l, const AVec< double > &c)
generates cylinder
SPDistanceFunction generateDFInBlock(const Block &b, unsigned int nG)
generates map corresponding to external (ghost) part of the block