Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
Simplex.cpp
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_Simplex_cpp_
9#define model_Simplex_cpp_
10
11#include <memory> // shared_ptr
12#include <set>
13#include <map>
14#include <vector>
15#include <float.h>
16#include <Eigen/Dense>
17
18#include <MeshModule.h>
19
20namespace model
21{
22
23
24
25
26
27
28template<short int dim>
29Simplex<dim,0>::Simplex(SimplicialMesh<dim>* const m,
30 const SimplexIDType& vIN) :
31/* init list */ SimplexBase<dim,order>(m,vIN),
32// /* init list */ P0(get_P0())
33/* init list */ P0(m->simplexReader().get_P0(this->xID))
34{
38}
39
40
41template<short int dim>
46
47
48template<short int dim>
49std::set<const Simplex<dim,0>*> Simplex<dim,0>::boundaryNeighbors() const // boundary spouse
50{
51 return std::set<const Simplex<dim,0>*>();
52}
53
54
55template<short int dim>
56Eigen::Matrix<double,dim,1> Simplex<dim,0>::outNormal() const
57{
59}
60
61
62template<short int dim>
63Eigen::Matrix<double,dim,1> Simplex<dim,0>::outNormal(const size_t& rID) const
64{
66}
67
68
69
70
71template<short int dim, short int order>
72Simplex<dim,order>::Simplex(SimplicialMesh<dim>* const m,
73 const SimplexIDType& vIN) :
74/* init */ SimplexParent<dim,order>(m,vIN)
75/* init */,SimplexBase<dim,order>(m,vIN)
76// /* init */ vol0(SimplexVolume<dim,order>::volume(this->vertexPositionMatrix())) // THIS GIVES SEGMENTATION FAULT, WHY?
77{
81
82
84}
85
86
87template<short int dim, short int order>
93
94//
95// template<short int dim, short int order>
96// BaseArrayType& Simplex<dim,order>::children()
97// {
98// return *this;
99// }
100//
101//
102// template<short int dim, short int order>
103// const BaseArrayType& Simplex<dim,order>::children() const
104// {
105// return *this;
106// }
108//
109// template<short int dim, short int order>
110// ChildSimplexType& Simplex<dim,order>::child(const int& n)
111// {
112// return *(children()[n].get());
113// }
116// template<short int dim, short int order>
117// const ChildSimplexType& Simplex<dim,order>::child(const int& n) const
118// {
119// return *(children()[n].get());
120// }
121
122//
123// template<short int dim, short int order>
124// const std::shared_ptr<ChildSimplexType>& Simplex<dim,order>::child(const ChildIDType& xID) const
125// {
126// size_t n=nFaces;
127// for (size_t k=0;k<nFaces;++k)
128// {
129// if(children()[k]->xID==xID)
130// {
131// n=k;
132// break;
133// }
134// }
135// assert(n!=nFaces && "CHILD NOT FOUND");
136// return children()[n];
137// }
138
139
140template<short int dim, short int order>
141size_t Simplex<dim,order>::childOrder(const ChildIDType& childID) const
142{
143 return SimplexTraits<dim,order>::faceOrder(this->xID,childID);
144}
145
146
147template<short int dim, short int order>
148std::vector<int> Simplex<dim,order>::boundaryFaces() const
149{
150 std::vector<int> temp;
151 temp.reserve(nFaces);
152 for (int n=0;n<nFaces;++n)
153 {
154 if(this->child(n).isBoundarySimplex())
155 {
156 temp.push_back(n);
157 }
158 }
159 return temp;
160}
161
162
163
164template<short int dim, short int order>
165std::set<const Simplex<dim,order>*> Simplex<dim,order>::spouses() const
166{
168 std::set<const Simplex<dim,order>*> temp;
169 for(const auto& child : this->children())
170 {
171 for(const auto& parent : child->parents())
172 {
173 if(parent.second.get()!=this)
174 {
175 temp.insert(parent.second.get());
176 }
177 }
178 }
179 return temp;
180}
181
182
183template<short int dim, short int order>
184std::set<const Simplex<dim,order>*> Simplex<dim,order>::boundaryNeighbors() const // boundary spouse
185{
186 std::set<const Simplex<dim,order>*> temp;
187 for(const auto& child : this->children())
188 {
189 if(child->isBoundarySimplex())
190 {
191 for(const auto& parent : child->parents())
192 {
193 if(parent.second->isBoundarySimplex())
194 {
195 temp.insert(parent.second.get());
196 }
197 }
198 }
199 }
200 return temp;
201}
202
203
204template<short int dim, short int order>
205std::set<const Simplex<dim,order>*> Simplex<dim,order>::regionBoundaryNeighbors() const // region boundary spouse
206{
207 std::set<const Simplex<dim,order>*> temp;
208 for(const auto& child : this->children())
209 {
210 if(child->isRegionBoundarySimplex())
211 {
212 for(const auto& parent : child->parents())
213 {
214 if(parent.second->isRegionBoundarySimplex())
215 {
216 temp.insert(parent.second.get());
217 }
218 }
219 }
220 }
221 return temp;
222}
223
224
225
226template<short int dim, short int order>
227Eigen::Matrix<double,dim,1> Simplex<dim,order>::outNormal() const
228{
230}
231
232
233template<short int dim, short int order>
234Eigen::Matrix<double,dim,1> Simplex<dim,order>::outNormal(const size_t& rID) const
235{
237}
238
239
240
241
242template<short int dim>
243const Eigen::Matrix<double,dim,dim+1> Simplex<dim,dim>::get_nda() const
244{
245 Eigen::Matrix<double,dim,dim+1> vP(this->vertexPositionMatrix());
246 Eigen::Matrix<double,dim,dim> F(vP.template block<dim,dim>(0,0));
247 F.colwise() -= vP.col(dim);
248 const double jFabs(std::fabs(F.determinant()));
249 if(jFabs<DBL_EPSILON)
250 {
251 std::cout<<this->xID<<", volume="<<jFabs<<std::endl;
252 std::cout<<F<<std::endl;
253 assert(0 && "SIMPLEX HAS ZERO VOLUME");
254 }
255 return jFabs*F.inverse().transpose()*BarycentricTraits<dim>::NdA;
256}
257
258
259
260template<short int dim>
261Simplex<dim,dim>::Simplex(SimplicialMesh<dim>* const m,
262 const SimplexIDType& vIN, const int regionID) :
263/* init */ SimplexParent<dim,order>(m,vIN)
264/* init */,SimplexBase<dim,order>(m,vIN)
265/* init */,region(m->getSharedRegion(regionID))
266/* init */,p2b(this->b2p.fullPivLu().solve(Eigen::Matrix<double,dim+1,dim+1>::Identity()))
267/* init */,nda(get_nda())
268/* init */,vol0(SimplexVolume<dim,order>::volume(this->vertexPositionMatrix()))
269{
271
272
273
274 region->simplices().emplace(this);
275
276}
277
278
279template<short int dim>
281{
283
284
285
286 region->simplices().erase(this);
287
288}
289
290//
291// template<short int dim>
292// BaseArrayType& Simplex<dim,dim>::children()
293// {
294// return *this;
295// }
296//
297//
298// template<short int dim>
299// const BaseArrayType& Simplex<dim,dim>::children() const
300// {
301// return *this;
302// }
303
304//
305// template<short int dim>
306// ChildSimplexType& Simplex<dim,dim>::child(const int& n)
307// {
308// return *(children()[n].get());
309// }
310//
311//
312// template<short int dim>
313// const ChildSimplexType& Simplex<dim,dim>::child(const int& n) const
314// {
315// return *(children()[n].get());
316// }
317//
318//
319// template<short int dim>
320// const std::shared_ptr<ChildSimplexType>& Simplex<dim,dim>::child(const ChildIDType& xID) const
321// {
322// size_t n=nFaces;
323// for (size_t k=0;k<nFaces;++k)
324// {
325// if(this->operator[](k)->xID==xID)
326// {
327// n=k;
328// break;
329// }
330// }
331// assert(n!=nFaces && "CHILD NOT FOUND");
332// return this->operator[](n);
333// }
334
335
336template<short int dim>
337size_t Simplex<dim,dim>::childOrder(const ChildIDType& childID) const
338{
339 return SimplexTraits<dim,dim>::faceOrder(this->xID,childID);
340}
341
342
343template<short int dim>
344std::vector<int> Simplex<dim,dim>::boundaryFaces() const
345{
346 std::vector<int> temp;
347 temp.reserve(nFaces);
348 for (int n=0;n<nFaces;++n)
349 {
350 if(this->child(n).isBoundarySimplex())
351 {
352 temp.push_back(n);
353 }
354 }
355 return temp;
356}
357
358
359template<short int dim>
364
365
366template<short int dim>
371
372
373template<short int dim>
374Eigen::Matrix<double,dim+1,1> Simplex<dim,dim>::pos2bary(const Eigen::Matrix<double,dim,1>& P) const
375{
376 return p2b*(Eigen::Matrix<double,dim+1,1>()<<P,1.0).finished();
377}
378
379
380template<short int dim>
381void Simplex<dim,dim>::convexDelaunaynSearch(const bool& searchAllRegions,
382 const Eigen::Matrix<double,dim,1>& P,
383 std::pair<bool,const Simplex<dim,dim>*>& lastSearched,
384 std::set<const Simplex<dim,dim>*>& searchSet) const // TO DO: searchSet is not necessary, because baryMin changes sign in next Simplex
385{
386 if(searchSet.find(this)==searchSet.end())
387 {// this simplex has not been searched yet
388 searchSet.insert(this);
389 lastSearched.second=this;
390
391 int kMin;
392 const double baryMin=pos2bary(P).minCoeff(&kMin);
393
394#ifdef _MODEL_BENCH_BARYSEARCH_
395 std::cout<<"Searching "<<this->xID<<std::endl;
396 std::cout<<"bary= "<<pos2bary(P)<<std::endl;
397 searchFile<<this->bary2pos(Eigen::Matrix<double,dim+1,1>::Ones()/(dim+1)).transpose()<<" "
398 /* */<<this->xID<<"\n";
399#endif
400
401 if (baryMin>=-FLT_EPSILON)
402 {
403 lastSearched.first=true;
404 }
405 else
406 {
407
408 for(auto& pParent : this->child(kMin).parents())
409 {
410 if(pParent.second->region->regionID==region->regionID || searchAllRegions)
411 {
412 pParent.second->convexDelaunaynSearch(searchAllRegions,P,lastSearched,searchSet);
413 if (lastSearched.first)
414 {
415 break;
416 }
417 }
418 }
419 }
420 }
421}
422
423template<short int dim>
424Eigen::Matrix<double,dim+1,1> Simplex<dim,dim>::faceLineIntersection(const Eigen::Matrix<double,dim+1,1>& bary0,
425 const Eigen::Matrix<double,dim+1,1>& bary1,
426 const int& faceID) const
427{
434
435 assert((faceID>=0) && (faceID<=dim) && "0 <= faceID <= dim");
436
437 //
438 // Check that baricentric coordinates sum to 1
439 assert(std::fabs(bary0.sum()-1.0)<=FLT_EPSILON && "bary0 must sum to 1");
440 assert(std::fabs(bary1.sum()-1.0)<=FLT_EPSILON && "bary1 must sum to 1");
441
442 Eigen::Matrix<double,dim+1,1> temp(bary0*0.5+bary1*0.5);
443 const double den(bary1(faceID)-bary0(faceID));
444 if (std::fabs(den)>DBL_EPSILON) // non-parallel points
445 {
446 double u(-bary0(faceID)/den); // interpolate linearly
447
448 temp=bary0*(1.0-u)+bary1*u;
449 temp(faceID)=0.0; // make sure
450 }
451 else
452 {
453 std::cout<<"Parallel faceLineIntersection"<<std::endl;
454 }
455 return temp;
456
457}
458
459template<short int dim>
460std::set<size_t> Simplex<dim,dim>::regionIDs() const
461{
462 std::set<size_t> temp;
463 temp.insert(region->regionID);
464 return temp;
465}
466
467template class Simplex<1,0>;
468template class Simplex<1,1>;
469template class Simplex<2,0>;
470template class Simplex<2,1>;
471template class Simplex<2,2>;
472template class Simplex<3,0>;
473template class Simplex<3,1>;
474template class Simplex<3,2>;
475template class Simplex<3,3>;
476
477} // close namespace
478#endif
bool isBoundarySimplex() const
Definition Simplex.cpp:360
SimplexTraits< dim, order-1 >::SimplexIDType ChildIDType
Definition Simplex.h:144
Eigen::Matrix< double, dim+1, 1 > pos2bary(const Eigen::Matrix< double, dim, 1 > &P) const
Definition Simplex.cpp:374
const std::shared_ptr< MeshRegionType > region
Shared pointer to MeshRegioin containing *this.
Definition Simplex.h:150
const Eigen::Matrix< double, dim+1, dim+1 > p2b
The position to barycentric-coordinate transformation matrix.
Definition Simplex.h:153
ParentContainerType parents() const
bool isBoundarySimplex() const
bool isRegionBoundarySimplex() const
std::set< size_t > regionIDs() const
Eigen::Matrix< double, dim, 1 > outNormal() const
Definition Simplex.cpp:227
static constexpr int nFaces
Definition Simplex.h:88
std::set< const Simplex< dim, order > * > boundaryNeighbors() const
Definition Simplex.cpp:184
std::set< const Simplex< dim, order > * > regionBoundaryNeighbors() const
Definition Simplex.cpp:205
double vol0
Definition Simplex.h:95
size_t childOrder(const ChildIDType &childID) const
Definition Simplex.cpp:141
SimplexTraits< dim, order-1 >::SimplexIDType ChildIDType
Definition Simplex.h:93
std::set< const Simplex< dim, order > * > spouses() const
Definition Simplex.cpp:165
std::vector< int > boundaryFaces() const
Definition Simplex.cpp:148
static const MatrixDimHigherDimD NdA
static Eigen::Matrix< double, dim, 1 > outNormal(const Simplex< dim, dim-dmo > &simplexChild)
static bool isBoundarySimplex(const SimplexChild< dim, dim-dmo > &simplexChild)
static bool isRegionBoundarySimplex(const SimplexChild< dim, dim-dmo > &simplexChild)
static constexpr short int dim
Definition SimplexBase.h:25
ChildSimplexType & child(const int &n)
Eigen::Matrix< double, dim, SimplexTraits< dim, order >::nVertices > vertexPositionMatrix() const
Eigen::Matrix< double, dim, 1 > bary2pos(const Eigen::Matrix< double, SimplexTraits< dim, order >::nVertices, 1 > &bary) const
BaseArrayType & children()
static size_t faceOrder(const SimplexIDType &parentID, const typename SimplexTraits< dim, order-1 >::SimplexIDType &childID)
static double volume(const Eigen::Matrix< double, dim, SimplexTraits< dim, order >::nVertices > &posM)
int parent
REAL u[4]
b order
vertex temp
void t struct mesh * m