Debian Codegraph @

src/num/aslLevelSetLinear.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 "aslLevelSetLinear.h"
25 #include <data/aslDataWithGhostNodes.h>
26 #include "acl/acl.h"
27 #include "acl/aclHardware.h"
28 #include "acl/Kernels/aclKernel.h"
29 #include "acl/Kernels/aclKernelConfigurationTemplates.h"
30 #include "acl/Kernels/aclKernelMerger.h"
31 #include "acl/aclGenerators.h"
32 #include "acl/aclMath/aclVectorOfElements.h"
33 #include "acl/aclMath/aclBarycentric.h"
34 #include "acl/aclMath/aclMathAlg.h"
35 #include <math/aslTemplates.h>
36 #include <math/aslTemplateVE.h>
37 #include <math/aslTemplatesExtras.h>
38 #include <math/aslDistanceFunction.h>
39
40
41 namespace asl
42 {
43
44 LevelSetLinear::~LevelSetLinear()
45 {}
46
47 LevelSetLinear::LevelSetLinear()
48 {
49 }
50
51 LevelSetLinear::LevelSetLinear(Data df):
52 LevelSet(df)
53 {
54 }
55
56 /// computes gradient and distance for a cell i.
57 /// val is the corresponding template with values
58 /// vel is velocity templates each element contains velocity vector for corresponding node
59 /// k is kernel working with the data
60 /// grad is the private variable where the corresponding value is stored
61 /// d is the distance (constant of the corresponding lenear function)
62 /// isBoundary set of booleans corresponding to noundary existence in the current cell
63 void computeLinearFunction(unsigned int i,
64 TemplateVE & val,
65 vector<acl::VectorOfElements> & vel,
66 acl::Kernel & k,
67 const acl::VectorOfElements & grad,
68 const acl::VectorOfElements & d,
69 const acl::VectorOfElements & isBoundary)
70 {
71 typedef acl::VectorOfElements VE;
72 auto vt(val.vectorTemplate);
73 auto vto(vtObject(vt));
74 unsigned int nv(vto->elementaryCells[0].getSize());
75 VE lval(nv);
76 vector<VE> lpoints(nv);
77 static acl::Barycentric bac;
78
79 for(unsigned int ii(0); ii < nv; ++ii)
80 {
81 unsigned int j(vto->elementaryCells[i][ii]);
82 lval[ii]=val.values[j];
83 copy(vel[j]+vt->vectors[j], lpoints[ii]);
84 }
85
86 bac.init(lpoints);
87 k<<bac.initTInv;
88 k<<(grad = bac.gradient(lval));
89 k<<(d = bac.interpolate(acl::generateVEConstant(vt->vectors[0]), lval));
90 k<<(isBoundary =
91 acl::differentSign(lval) &&
92 bac.in(-d*grad/l2(grad)));
93 k<<(d = select(10.*sign(subVE(val.values,0)),
94 d*rsqrt(l2(grad)),
95 isBoundary,
96 getElementType(val.values)));
97 }
98
99 /// This function compute new value of the distance function for the zerow node of the template
100 /// i is the number of vector (direction)
101 /// val i is the corresponding template with values
102 /// vel is the corresponding velocity vector template x y and z components
103
104 void computeLinearDistance(unsigned int i,
105 TemplateVE & val,
106 vector<acl::VectorOfElements> & vel,
107 acl::Kernel & k,
108 const acl::VectorOfElements & pd,
109 const acl::VectorOfElements & d,
110 const acl::VectorOfElements & isBoundary)
111 {
112 typedef acl::VectorOfElements VE;
113 auto vt(val.vectorTemplate);
114 vector<VE> lval(2);
115 vector<VE> lpoints(2);
116
117 copy(subVE(val.values, 0), lval[0]);
118 copy(vel[0], lpoints[0]);
119 copy(subVE(val.values, i), lval[1]);
120 copy(vel[i]+vt->vectors[i], lpoints[1]);
121
122 k<<(pd = lval[0]/(lval[0]-lval[1])*(lpoints[1]-lpoints[0])+lpoints[0]);
123 k<<(isBoundary = acl::differentSign(acl::cat(lval[0], lval[1])));
124 k<<(d = select(10.*sign(lval[0]),
125 sqrt(l2(pd))*sign(lval[0])*sign(pd*vt->vectors[i]),
126 isBoundary,
127 getElementType(val.values)));
128
129 }
130
131 void LevelSetLinear::initDistancesComputation()
132 {
133 auto & kk(*kernel);
134 acl::TypeID type(getElementType(distanceField->getEContainer()));
135 unsigned int nd(nD(*vectorTemplate));
136 auto vto(vtObject(vectorTemplate));
137
138 auto newDistance(generateVEPrivateVariable(1,type));
139 auto newDistanceCounter(generateVEPrivateVariable(1,type));
140 auto newDistanceD(generateVEPrivateVariable(1,type));
141 auto newDistanceDCounter(generateVEPrivateVariable(1,type));
142 auto grad(generateVEPrivateVariable(nd,type));
143 auto d(generateVEPrivateVariable(1,type));
144 auto isBoundary(generateVEPrivateVariable(1,acl::TYPE_SELECT[type]));
145
146 kk << (newDistance = acl::generateVEConstant(0.));//large number
147 kk << (newDistanceCounter = acl::generateVEConstant(0.));//large number
148
149 /* for(unsigned int i(0); i < vto->elementaryCells.size(); ++i)
150 {
151 computeLinearFunction(i, *distanceTVE, lVelocities, kk, grad, d, isBoundary);
152 // kk << (newDistance = minAbs(newDistance, d));
153 kk << (newDistance += select(acl::generateVEConstant(0.),
154 d,
155 fabs(d) < 5.,
156 type));
157 kk << (newDistanceCounter += select(acl::generateVEConstant(0.),
158 acl::generateVEConstant(1.),
159 fabs(d) < 5.,
160 type));
161 }*/
162 kk << (newDistanceD = acl::generateVEConstant(0.));//large number
163 kk << (newDistanceDCounter = acl::generateVEConstant(0.));//large number
164
165 for(unsigned int i(1); i < vectorTemplate->vectors.size(); ++i)
166 {
167 computeLinearDistance(i, *distanceTVE, lVelocities, kk, grad, d, isBoundary);
168 kk << (newDistanceD = select(acl::generateVEConstant(0.),
169 d,
170 fabs(d) < 5.,
171 type));
172 kk << (newDistanceDCounter += select(acl::generateVEConstant(0.),
173 acl::generateVEConstant(1.),
174 fabs(d) < 5.,
175 type));
176 }
177 kk << (newDistance = select(newDistanceD/max(newDistanceDCounter,
178 acl::generateVEConstant(.1),
179 type),
180 newDistance/newDistanceCounter,
181 newDistanceCounter > .1,
182 type));// /
183 //DistFNormalization::scaleFactor);
184 kk << (newDistance = select(newDistance/DistFNormalization::scaleFactor,
185 newDistance/(1. - newDistance),
186 newDistance*subVE(distanceTVE->values, 0) < 0));
187
188 // kk << assignmentSafe (distanceFieldInternalData->getSubContainer(),
189 // select( newDistance,
190 // sign(newDistance),
191 // fabs(newDistance) > 1.,
192 // type));
193 kk << assignmentSafe (distanceFieldInternalData->getSubContainer(),
194 select( newDistance,
195 sign(subVE(distanceTVE->values,0)),
196 newDistanceCounter + newDistanceDCounter < .1,
197 type));
198
199 }
200
201 } // asl
202