Debian Codegraph @

src/aslGeomInc.cxx

raw
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
24 #include "aslGeomInc.h"
25 #include <math/aslDistanceFunction.h>
26 #include "acl/aclMath/aclVectorOfElements.h"
27 #include <acl/aclGenerators.h>
28 #include <acl/acl.h>
29 #include <data/aslBlocks.h>
30 #include <aslGenerators.h>
31 #include <math/aslIndex2Position.h>
32 #include <acl/Kernels/aclKernelConfigurationTemplates.h>
33 #include <acl/Kernels/aclKernel.h>
34 #include <data/aslDataWithGhostNodes.h>
35
36 namespace asl
37 {
38
39 SPDistanceFunction operator&(SPDistanceFunction a, SPDistanceFunction b)
40 {
41 if ((a.get() == 0) && (b.get() == 0))
42 errorMessage("DistanceFunction::operator& - both operands are not initialized");
43
44 if (a.get() == 0)
45 return b;
46
47 if (b.get() == 0)
48 return a;
49
50 return SPDistanceFunction(new DistFOperationAnd(a, b));
51 }
52
53
54 SPDistanceFunction operator|(SPDistanceFunction a, SPDistanceFunction b)
55 {
56 if ((a.get() == 0) && (b.get() == 0))
57 errorMessage("DistanceFunction::operator| - both operands are not initialized");
58
59 if (a.get() == 0)
60 return b;
61
62 if (b.get() == 0)
63 return a;
64
65 return SPDistanceFunction(new DistFOperationOr(a, b));
66 }
67
68
69 SPDistanceFunction operator-(SPDistanceFunction a)
70 {
71 if (a.get() == 0)
72 errorMessage("DistanceFunction::operator- - operand is not initialized");
73
74 return SPDistanceFunction(new DistFOperationInversion(a));
75 }
76
77
78 SPDistanceFunction normalize(SPDistanceFunction a, double dx)
79 {
80 if (a.get() == 0)
81 errorMessage("DistanceFunction::normalize - argument is not initialized");
82
83 return SPDistanceFunction(new DistFNormalization(a, dx));
84 }
85
86 SPDistanceFunction generateDFSphere(double r, const AVec<double> & c)
87 {
88 return SPDistanceFunction(new DistFSphere(r,c));
89 }
90
91 SPDistanceFunction generateDFCylinderInf(double r,
92 const AVec<double> & l,
93 const AVec<double> & c)
94 {
95 return SPDistanceFunction(new DistFCylinder(r, l, c));
96 }
97
98 SPDistanceFunction generateDFCylinder(double r,
99 const AVec<double> & l,
100 const AVec<double> & c)
101 {
102 auto cyl(generateDFCylinderInf(r, l, c));
103 auto p1(generateDFPlane(l, c + l * .5));
104 auto p2(generateDFPlane(-l, c - l * .5));
105 return cyl & p1 & p2;
106 }
107
108 SPDistanceFunction generateDFCone(double r,
109 const AVec<double> & l,
110 const AVec<double> & a)
111 {
112 auto cone(SPDistanceFunction(new DistFCone(r/sqrt(l2(l)), l, a)));
113 auto p1(generateDFPlane(l, a + l));
114 auto p2(generateDFPlane(-l, a));
115 return cone & p1 & p2;
116 }
117
118 SPDistanceFunction generateDFPlane(const AVec<double> & n, const AVec<double> & p0)
119 {
120 return SPDistanceFunction(new DistFPlane(n,p0));
121 }
122
123 SPDistanceFunction generateDFConvexPolygonPrism(vector<AVec<double>> points)
124 {
125 AVec<double> center(points[0]);
126 for(unsigned int i(1); i < points.size(); ++i)
127 center+=points[i];
128 center = center / double(points.size());
129
130 auto axis(crossProduct(points[0]-center, points[1]-center));
131
132
133 auto res(generateDFPlane(crossProduct(points[1]-points[0], axis),
134 points[0]));
135 for(unsigned int i(1); i < points.size(); ++i)
136 res = res & generateDFPlane(crossProduct(points[(i+1) % points.size()]-points[i], axis),
137 points[i]);
138 return res;
139 }
140
141 SPDistanceFunction generateDFConvexPolygonPyramid(vector<AVec<double>> points,
142 AVec<double> a)
143 {
144 AVec<double> center(points[0]);
145 for(unsigned int i(1); i < points.size(); ++i)
146 center+=points[i];
147 center = center / double(points.size());
148
149 auto axis(crossProduct(points[0]-center, points[1]-center));
150
151
152 auto res(generateDFPlane(crossProduct(points[1]-points[0], a - points[0]),
153 points[0]));
154 for(unsigned int i(1); i < points.size(); ++i)
155 res = res & generateDFPlane(crossProduct(points[(i+1) % points.size()]-points[i],
156 a - points[i]),
157 points[i]);
158 res = res & generateDFPlane(-axis,center);
159 return res;
160 }
161
162 SPDistanceFunction generateDFInBlock(const Block & b, unsigned int nG)
163 {
164 unsigned int nd(nD(b));
165 AVec<> n(nd, 0.);
166
167 AVec<> nc(n);
168 nc[0] = 1.;
169 AVec<> offs(nd, (double(nG) - .5) * b.dx);
170 AVec<> p0(b.position + offs);
171 AVec<> pE(b.getBPosition() - offs);
172 auto res(generateDFPlane(nc, p0) | generateDFPlane(-nc, pE));
173 if(nd >= 2 )
174 {
175 nc = n; nc[1] = 1;
176 res = res | generateDFPlane(nc, p0) | generateDFPlane(-nc, pE);
177 }
178 if(nd >= 3 )
179 {
180 nc = n; nc[2] = 1;
181 res = res | generateDFPlane(nc, p0) | generateDFPlane(-nc, pE);
182 }
183
184 return res;
185 }
186
187 SPAbstractDataWithGhostNodes generateDataContainer_SP(const Block &b,
188 SPDistanceFunction df,
189 unsigned int gN,
190 acl::TypeID t)
191 {
192 asl::Index2PositionACL i2p(offset(b, gN), t);
193
194 return generateDataContainer_SP(b, df->getDistance(i2p.positionWithInit), gN);
195 }
196
197
198 void initData(SPAbstractDataWithGhostNodes d,
199 SPDistanceFunction f)
200 {
201 initData(d, f, acl::KERNEL_SIMD);
202 }
203
204
205 void initData(SPAbstractDataWithGhostNodes d,
206 SPDistanceFunction f,
207 const acl::KernelConfiguration & kernelConfig)
208 {
209 if (f.get() == 0)
210 errorMessage("DistanceFunction::initData - argument is not initialized");
211
212 auto type(getElementType(d->getEContainer()));
213 asl::Index2PositionACL i2p(d->getBlock(),type);
214
215 acl::Kernel k(kernelConfig);
216 k << i2p.initPosition;
217 k << acl::assignmentSafe(d->getEContainer(), f->getDistance(i2p.position));
218 k.setup();
219 k.compute();
220 }
221
222
223 } // asl