Lattice.cpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \ *---------------------------------------------------------------------------*/
24 
25 #include "Lattice.hpp"
26 #include "customOperators.hpp"
27 #include <bitpit_operators.hpp>
28 #include <bitpit_common.hpp>
29 
30 namespace mimmo{
31 
36  m_np = 0;
37  m_intMapDOF.clear();
38  m_name = "mimmo.Lattice";
39 };
40 
45 Lattice::Lattice(const bitpit::Config::Section & rootXML){
46  m_np = 0;
47  m_intMapDOF.clear();
48  m_name = "mimmo.Lattice";
49  std::string fallback_name = "ClassNONE";
50  std::string input = rootXML.get("ClassName", fallback_name);
51  input = bitpit::utils::string::trim(input);
52  if(input == "mimmo.Lattice"){
53  absorbSectionXML(rootXML);
54  }else{
56  };
57 }
58 
61 
65 Lattice::Lattice(const Lattice & other):BaseManipulation(other), UStructMesh(other){
66  m_intMapDOF = other.m_intMapDOF;
67  m_np = other.m_np;
68 };
69 
74 void Lattice::swap(Lattice & x) noexcept
75 {
76  std::swap(m_intMapDOF, x.m_intMapDOF);
77  std::swap(m_np, x.m_np);
78 
81 }
82 
87 
88  bool built = true;
89  built = (built && createPortIn<MimmoSharedPointer<MimmoObject>, Lattice>(&m_geometry, M_GEOM, true));
90  built = (built && createPortIn<iarray3E, Lattice>(this, &mimmo::Lattice::setDimension, M_DIMENSION));
91  built = (built && createPortIn<darray3E, Lattice>(this, &mimmo::Lattice::setInfLimits, M_INFLIMITS));
92  built = (built && createPortIn<dmatrix33E, Lattice>(this, &mimmo::Lattice::setRefSystem, M_AXES));
93  built = (built && createPortIn<darray3E, Lattice>(this, &mimmo::Lattice::setSpan, M_SPAN));
94  built = (built && createPortIn<darray3E, Lattice>(this, &mimmo::Lattice::setOrigin, M_POINT));
95  built = (built && createPortIn<mimmo::ShapeType, Lattice>(this, &mimmo::Lattice::setShape, M_SHAPE));
96  built = (built && createPortIn<const BasicShape *, Lattice>(this, &mimmo::Lattice::setShape, M_COPYSHAPE));
97  built = (built && createPortIn<int, Lattice>(this, &mimmo::Lattice::setShape, M_SHAPEI));
98 
99  // creating output ports
100  built = (built && createPortOut<dvecarr3E, Lattice>(this, &mimmo::Lattice::getGlobalCoords, M_GLOBAL));
101  built = (built && createPortOut<dvecarr3E, Lattice>(this, &mimmo::Lattice::getLocalCoords, M_LOCAL));
102  built = (built && createPortOut<darray3E, Lattice>(this, &mimmo::Lattice::getOrigin, M_POINT));
103  built = (built && createPortOut<dmatrix33E, Lattice>(this, &mimmo::Lattice::getRefSystem, M_AXES));
104  built = (built && createPortOut<darray3E, Lattice>(this, &mimmo::Lattice::getSpan, M_SPAN));
105  built = (built && createPortOut<iarray3E, Lattice>(this, &mimmo::Lattice::getDimension, M_DIMENSION));
106  built = (built && createPortOut<BasicShape *, Lattice>(this, &mimmo::Lattice::getShape, M_COPYSHAPE));
107  built = (built && createPortOut<MimmoSharedPointer<MimmoObject>, Lattice>(this, &mimmo::Lattice::getGeometry, M_GEOM));
108 
109  m_arePortsBuilt = built;
110 };
111 
116  clear(); //base manipulation stuff clear
117  clearMesh(); // structured mesh cleaned
118  m_intMapDOF.clear();
119 };
120 
126  return(m_np);
127 }
128 
134 dvecarr3E
136  int np = (getNNodes());
137  dvecarr3E coords(np);
138  int index, i0, i1, i2;
139  for (int i=0; i<np; i++){
140  index = accessGridFromDOF(i);
141  accessPointIndex(index,i0,i1,i2);
142  coords[i] = getGlobalPoint(i0,i1,i2);
143  }
144  return(coords);
145 };
146 
152 dvecarr3E
154  int np = (getNNodes());
155  dvecarr3E coords(np);
156  int index, i0, i1, i2;
157  for (int i=0; i<np; i++){
158  index = accessGridFromDOF(i);
159  accessPointIndex(index,i0,i1,i2);
160  coords[i] = getLocalPoint(i0,i1,i2);
161  }
162  return(coords);
163 };
164 
171 int
173  return(m_intMapDOF[index]);
174 }
175 
182 int
184  return(posVectorFind(m_intMapDOF, index));
185 }
186 
193 ivector1D
195  ivector1D result(gNindex.size());
196  int counter = 0;
197  for(auto &val : gNindex){
198  result[counter] = accessDOFFromGrid(val);
199  ++counter;
200  }
201  return(result);
202 };
203 
210 ivector1D
212  ivector1D result(dofIndex.size());
213  int counter = 0;
214  for(auto &val : dofIndex){
215  result[counter] = accessGridFromDOF(val);
216  ++counter;
217  }
218  return(result);
219 };
220 
229 void
230 Lattice::plotGrid(std::string directory, std::string filename,int counter, bool binary){
231  dvecarr3E* pnull = nullptr;
232 
233  iarray3E n = getDimension();
234  int size = n[0]*n[1]*n[2];
235  ivector1D labels(size);
236  for(int i=0; i<size; ++i){
237  labels[i] = accessDOFFromGrid(i);
238  }
239  UStructMesh::plotGrid(directory, filename, counter, binary, labels, pnull);
240 };
241 
250 void
251 Lattice::plotCloud(std::string directory, std::string filename, int counter, bool binary){
252  dvecarr3E* pnull = nullptr;
253 
254  iarray3E n = getDimension();
255  int size = n[0]*n[1]*n[2];
256  ivector1D labels(size);
257  for(int i=0; i<size; ++i){
258  labels[i] = accessDOFFromGrid(i);
259  }
260  UStructMesh::plotCloud(directory, filename, counter, binary, labels, pnull);
261 };
262 
269 bool
271  bool check = UStructMesh::build();
272  resizeMapDof();
273  return check;
274 };
275 
279 void
281  if(!build()){
282  throw std::runtime_error (m_name + " : cannot build the current mesh.");
283  };
284 };
285 
286 
292 void Lattice::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
293 
294  BITPIT_UNUSED(name);
295 
296  BaseManipulation::absorbSectionXML(slotXML, name);
297 
298  if(slotXML.hasOption("Shape")){
299  std::string input = slotXML.get("Shape");
300  input = bitpit::utils::string::trim(input);
301 
302  if(input == "CYLINDER"){
304  }else if(input =="SPHERE"){
306  }else if(input =="WEDGE"){
308  }else{
310  }
311  };
312 
313  if(slotXML.hasOption("Origin")){
314  std::string input = slotXML.get("Origin");
315  input = bitpit::utils::string::trim(input);
316  darray3E temp = {{0.0,0.0,0.0}};
317  if(!input.empty()){
318  std::stringstream ss(input);
319  for(auto &val : temp) ss>>val;
320  }
321  setOrigin(temp);
322  };
323 
324  if(slotXML.hasOption("Span")){
325  std::string input = slotXML.get("Span");
326  input = bitpit::utils::string::trim(input);
327  darray3E temp = {{0.0,0.0,0.0}};
328  if(!input.empty()){
329  std::stringstream ss(input);
330  for(auto &val : temp) ss>>val;
331  }
332  setSpan(temp);
333  };
334 
335  if(slotXML.hasSection("RefSystem")){
336  const bitpit::Config::Section & rfXML = slotXML.getSection("RefSystem");
337  std::string rootAxis = "axis";
338  std::string axis;
339  dmatrix33E temp;
340  temp[0].fill(0.0); temp[0][0] = 1.0;
341  temp[1].fill(0.0); temp[1][1] = 1.0;
342  temp[2].fill(0.0); temp[2][2] = 1.0;
343  for(int i=0; i<3; ++i){
344  axis = rootAxis + std::to_string(i);
345  std::string input = rfXML.get(axis);
346  input = bitpit::utils::string::trim(input);
347  if(!input.empty()){
348  std::stringstream ss(input);
349  for(auto &val : temp[i]) ss>>val;
350  }
351  }
352  setRefSystem(temp);
353  };
354 
355  if(slotXML.hasOption("InfLimits")){
356  std::string input = slotXML.get("InfLimits");
357  input = bitpit::utils::string::trim(input);
358  darray3E temp = {{0.0,0.0,0.0}};
359  if(!input.empty()){
360  std::stringstream ss(input);
361  for(auto &val : temp) ss>>val;
362  }
363  setInfLimits(temp);
364  };
365 
366  if(slotXML.hasOption("Dimension")){
367  std::string input = slotXML.get("Dimension");
368  input = bitpit::utils::string::trim(input);
369  iarray3E temp = {{2,2,2}};
370  if(!input.empty()){
371  std::stringstream ss(input);
372  for(auto &val : temp) ss>>val;
373  }
374  setDimension(temp);
375  };
376 }
377 
383 void Lattice::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
384 
385  BITPIT_UNUSED(name);
386 
387  BaseManipulation::flushSectionXML(slotXML, name);
388 
389  std::string towrite = "CUBE";
390 
392  towrite = "CYLINDER";
393  } else if(getShapeType() == ShapeType::SPHERE){
394  towrite = "SPHERE";
395  } else if(getShapeType() == ShapeType::WEDGE){
396  towrite = "WEDGE";
397  }
398  slotXML.set("Shape", towrite);
399 
400  {
401  std::stringstream ss;
402  ss<<std::scientific<<getOrigin()[0]<<'\t'<<getOrigin()[1]<<'\t'<<getOrigin()[2];
403  slotXML.set("Origin", ss.str());
404  }
405 
406  {
407  std::stringstream ss;
408  ss<<std::scientific<<getSpan()[0]<<'\t'<<getSpan()[1]<<'\t'<<getSpan()[2];
409  slotXML.set("Span", ss.str());
410  }
411 
412  {
413  auto rs = getRefSystem();
414  bitpit::Config::Section & rsXML = slotXML.addSection("RefSystem");
415  std::string rootAxis = "axis";
416  std::string localAxis;
417  int counter=0;
418  for(auto &axis : rs){
419  localAxis = rootAxis+std::to_string(counter);
420  std::stringstream ss;
421  ss<<std::scientific<<axis[0]<<'\t'<<axis[1]<<'\t'<<axis[2];
422  rsXML.set(localAxis, ss.str());
423  ++counter;
424  }
425  }
426 
427  {
428  std::stringstream ss;
429  ss<<std::scientific<<getInfLimits()[0]<<'\t'<<getInfLimits()[1]<<'\t'<<getInfLimits()[2];
430  slotXML.set("InfLimits", ss.str());
431  }
432 
433  {
434  std::stringstream ss;
435  ss<<getDimension()[0]<<'\t'<<getDimension()[1]<<'\t'<<getDimension()[2];
436  slotXML.set("Dimension", ss.str());
437  }
438 };
439 
440 
441 
447  std::string dir = m_outputPlot;
448  std::string nameGrid = m_name+"GRID";
449  plotGrid(dir, nameGrid, getId(), true );
450 }
451 
459 int
460 Lattice::reduceDimToDOF(int nx, int ny, int nz, bvector1D & info){
461 
462  int delta = 0;
463  double dval;
464  switch(getShapeType()){
465 
466  case ShapeType::WEDGE :
467  delta = ny*(1 -nz);
468  info.push_back(true);
469  break;
470 
471  case ShapeType::CYLINDER :
472  if(!(getShape()->getLocalOrigin()[0] >0.0 )){
473  info.push_back(false);
474  }else{
475  delta += nz;
476  nx--;
477  info.push_back(true);
478  }
479  if(getCoordType(1) == CoordType::PERIODIC) ny--;
480 
481 
482  info.push_back(getCoordType(1) == CoordType::PERIODIC);
483  break;
484 
485  case ShapeType::SPHERE :
486  if(!(getShape()->getLocalOrigin()[0] >0.0 )){
487  info.push_back(false);
488  }else{
489  delta ++;
490  nx--;
491  info.push_back(true);
492  }
493  if(getCoordType(1) == CoordType::PERIODIC) ny--;
494  dval = getInfLimits()[2];
495  if(dval == 0.0) {
496  nz--;
497  delta += nx;
498  }
499  if((dval + getLocalSpan()[2]) == BITPIT_PI){
500  nz--;
501  delta += nx;
502  }
503 
504  info.push_back(getCoordType(1) == CoordType::PERIODIC);
505  info.push_back(dval==0.0);
506  info.push_back((dval + getLocalSpan()[2]) == BITPIT_PI);
507  break;
508 
509  default:
510  //doing nothing
511  break;
512  }
513 
514  int result = nx*ny*nz + delta;
515  return(result);
516 };
517 
523 void
525  //reallocate your displacement node
526  m_intMapDOF.clear();
527  m_intMapDOF.resize((m_nx+1)*(m_ny+1)*(m_nz+1), -1);
528  ivector1D::iterator itMapBegin = m_intMapDOF.begin();
529  ivector1D::iterator itMap = itMapBegin;
530  ivector1D::iterator itMapEnd = m_intMapDOF.end();
531  bvector1D info;
532  m_np = reduceDimToDOF(m_nx+1,m_ny+1,m_nz+1, info);
533 
534  //set m_intMapDOF
535 
536  int target;
537  int index;
538  ivector1D dummy;
539 
540  int i0,i1,i2;
541  switch(getShapeType()){
542 
543  case ShapeType::WEDGE :
544 
545  target=0;
546  while(itMap != itMapEnd){
547 
548  *itMap = target;
549  index = std::distance(itMapBegin, itMap);
550  accessPointIndex(index,i0,i1,i2);
551 
552  if(info[0] && i0 == m_nx){
553  for(int k=0; k<=m_nz;++k){
554  m_intMapDOF[accessPointIndex(i0,i1,k)] = target;
555  }
556  }
557 
558  itMap = find(m_intMapDOF.begin(), itMapEnd,-1);
559  target++;
560  }
561  break;
562 
563  case ShapeType::CYLINDER :
564  target=0;
565  while(itMap != itMapEnd){
566 
567  *itMap = target;
568  index = std::distance(itMapBegin, itMap);
569  accessPointIndex(index,i0,i1,i2);
570 
571  if(info[0] && i0 == 0){
572  for(int k=0; k<=m_ny;++k){
573  m_intMapDOF[accessPointIndex(i0,k,i2)] = target;
574  }
575  }
576  if(info[1] && i1 == 0){
577  m_intMapDOF[accessPointIndex(i0,m_ny,i2)] = target;
578  }
579 
580  itMap = find(m_intMapDOF.begin(), itMapEnd,-1);
581  target++;
582  }
583  break;
584 
585  case ShapeType::SPHERE :
586 
587  target = 0;
588  while(itMap != itMapEnd){
589 
590  *itMap = target;
591  index = std::distance(itMapBegin, itMap);
592  accessPointIndex(index,i0,i1,i2);
593 
594  if(info[0] && i0 == 0){
595  for(int k1=0; k1<=m_ny;++k1){
596  for(int k2=0; k2<=m_nz; ++k2){
597  m_intMapDOF[accessPointIndex(i0,k1,k2)] = target;
598  }
599  }
600  }
601 
602  if(info[1] && i1 == 0){
603  m_intMapDOF[accessPointIndex(i0,m_ny-1,i2)] = target;
604  }
605 
606  if(info[2] && i2 == 0){
607  for(int k1=0; k1<=m_ny; ++k1){
608  m_intMapDOF[accessPointIndex(i0,k1,i2)] = target;
609  }
610  }
611 
612  if(info[3] && i2 == (m_nz)){
613  for(int k1=0; k1<=m_ny;++k1){
614  m_intMapDOF[accessPointIndex(i0,k1,i2)] = target;
615  }
616  }
617 
618  itMap = find(m_intMapDOF.begin(), itMapEnd,-1);
619  target++;
620  }
621  break;
622 
623 
624  case ShapeType::CUBE :
625  target = 0;
626  while(itMap != itMapEnd){
627 
628  *itMap = target;
629  itMap = find(m_intMapDOF.begin(), itMapEnd,-1);
630  target++;
631  }
632  break;
633 
634  default: //doing nothing
635  break;
636  }//end switch
637 
638 }
639 
640 
641 }
void execute()
Definition: Lattice.cpp:280
darray3E getLocalSpan()
std::array< int, 3 > iarray3E
void setDimension(ivector1D dim)
darray3E getGlobalPoint(int)
virtual bool build()
darray3E getLocalPoint(int)
void resizeMapDof()
Definition: Lattice.cpp:524
#define M_GEOM
void plotGrid(std::string directory, std::string filename, int counter, bool binary)
Definition: Lattice.cpp:230
void buildPorts()
Definition: Lattice.cpp:86
void setRefSystem(darray3E, darray3E, darray3E)
dvecarr3E getGlobalCoords()
Definition: Lattice.cpp:135
std::vector< bool > bvector1D
std::vector< darray3E > dvecarr3E
virtual bool build()
Definition: Lattice.cpp:270
void setOrigin(darray3E origin)
ivector1D m_intMapDOF
Definition: Lattice.hpp:107
int accessPointIndex(int i, int j, int k)
const BasicShape * getShape() const
#define M_POINT
#define M_DIMENSION
void warningXML(bitpit::Logger *log, std::string name)
BaseManipulation is the base class of any manipulation object of the library.
void setShape(ShapeType type=ShapeType::CUBE)
#define M_SHAPE
#define M_LOCAL
int getNNodes()
Definition: Lattice.cpp:125
#define M_SHAPEI
#define M_INFLIMITS
std::array< CoordType, 3 > getCoordType()
int accessDOFFromGrid(int index)
Definition: Lattice.cpp:172
std::vector< int > ivector1D
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
std::array< darray3E, 3 > dmatrix33E
#define M_SPAN
#define M_AXES
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
Definition: Lattice.cpp:383
iarray3E getDimension()
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer< MimmoObject > m_geometry
int posVectorFind(std::vector< T > &vect, T &target)
int accessGridFromDOF(int index)
Definition: Lattice.cpp:183
void swap(Lattice &) noexcept
Definition: Lattice.cpp:74
virtual ~Lattice()
Definition: Lattice.cpp:60
void plotCloud(std::string directory, std::string filename, int counter, bool binary)
Definition: Lattice.cpp:251
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
Definition: Lattice.cpp:292
dvecarr3E getLocalCoords()
Definition: Lattice.cpp:153
void plotCloud(std::string &, std::string, int, bool, const ivector1D &labels, dvecarr3E *extPoints=nullptr)
ShapeType getShapeType()
Class for 3D uniform structured mesh.
Definition: BasicMeshes.hpp:44
#define M_COPYSHAPE
std::array< double, 3 > darray3E
void clearLattice()
Definition: Lattice.cpp:115
void setSpan(double, double, double)
void swap(UStructMesh &x) noexcept
#define M_GLOBAL
MimmoSharedPointer< MimmoObject > getGeometry()
virtual void plotOptionalResults()
Definition: Lattice.cpp:446
darray3E getOrigin()
void swap(BaseManipulation &x) noexcept
darray3E getInfLimits()
void plotGrid(std::string &, std::string, int, bool, const ivector1D &labels, dvecarr3E *extPoints=nullptr)
void setInfLimits(double val, int dir)
Structured 3D Cartesian Mesh.
Definition: Lattice.hpp:103
dmatrix33E getRefSystem()