manipulators_example_00004.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 "mimmo_manipulators.hpp"
26 #include "mimmo_iogeneric.hpp"
27 #include <bitpit_common.hpp>
28 #include <random>
29 
30 // =================================================================================== //
44 void test00004() {
45 
46  /*
47  Read a sphere from STL file. Convert mode is to save the just read geometry in
48  another file with name manipulators_output_00004.0000.stl
49  */
51  mimmo0->setReadDir("geodata");
52  mimmo0->setReadFileType(FileType::STL);
53  mimmo0->setReadFilename("sphere2");
54  mimmo0->setWriteDir(".");
55  mimmo0->setWriteFileType(FileType::STL);
56  mimmo0->setWriteFilename("manipulators_output_00004.0000");
57 
58  /*
59  write the deformed geometry to file
60  */
62  mimmo1->setWriteDir(".");
63  mimmo1->setWriteFileType(FileType::STL);
64  mimmo1->setWriteFilename("manipulators_output_00004.0001");
65 
66  /*
67  Instantiation of a FFDobject with spherical shape.
68  Setup of sphere dimensions and number of lattice nodes/nurbs degrees
69  for each spherical coordinate.
70  */
71  mimmo::FFDLattice* lattice = new mimmo::FFDLattice();
72  darray3E origin = {0.0, 0.0,0.0};
73  darray3E span;
74  span[0]= 3.01;
75  span[1]= 2*BITPIT_PI;
76  span[2]= BITPIT_PI;
77 
78  /* Set number of nodes of the mesh (dim) and degree of nurbs functions (deg).
79  */
80  iarray3E dim, deg;
81  dim[0] = 30;
82  dim[1] = 30;
83  dim[2] = 30;
84  deg[0] = 2;
85  deg[1] = 2;
86  deg[2] = 2;
87 
88  lattice->setLattice(origin,span,mimmo::ShapeType::SPHERE,dim, deg);
89 
90  /*
91  Change reference system to work in local spherical coordinates.
92  Set coordinates as CLAMPED (continuity in origins of angles).
93  */
94  lattice->setRefSystem(2, darray3E{0,1,0});
96  lattice->setPlotInExecution(true);
97 
98  /*
99  Build mesh of lattice outside the execution chain
100  to use it during setup the displacements.
101  */
102  lattice->build();
103 
104  /* Creation of displacements for control nodes of the lattice.
105  * Use random values to set the displacements of the control nodes at a longitude
106  * angle smaller than PI and expansion on radius direction for nodes with longitude
107  * angle greater than PI.
108  * As exercise GenericInput/Output blocks are used to write them down to file
109  */
110  int ndeg = lattice->getNNodes();
111  dvecarr3E displ(ndeg, darray3E{0,0,0});
112  std::minstd_rand rgen;
113  rgen.seed(16);
114  double distRand = (rgen.max()-rgen.min());
115  for (int i=0; i<ndeg; i++){
116  int l1,l2,l3;
117  int index = lattice->accessGridFromDOF(i);
118  lattice->accessPointIndex(index,l1,l2,l3);
119  if(l1 > 0 && lattice->getLocalPoint(l1,l2,l3)[1] < BITPIT_PI){
120  displ[i][0] = 1.0*( double( rgen() - rgen.min() ) / distRand );
121  }
122  if( (l1 > 0 && lattice->getLocalPoint(l1,l2,l3)[1] >= BITPIT_PI)
123  || lattice->getLocalPoint(l1,l2,l3)[1] == 0){
124  displ[i][0] = 1.25;
125  }
126 
127  }
128 
129  /*
130  Set Generic input block with the
131  displacements defined above.
132  */
134  input->setInput(displ);
135 
136  /*
137  Set Generic output block to write the
138  displacements defined above.
139  */
141  output->setFilename("manipulators_output_00004.csv");
142  output->setCSV(true);
143 
144  /*
145  Create applier block.
146  It applies the deformation displacements to the original input geometry.
147  */
148  mimmo::Apply* applier = new mimmo::Apply();
149 
150  /*
151  Setup pin connections.
152  */
153  mimmo::pin::addPin(mimmo0, lattice, M_GEOM, M_GEOM);
154  mimmo::pin::addPin(mimmo0, applier, M_GEOM, M_GEOM);
155  mimmo::pin::addPin(input, lattice, M_DISPLS, M_DISPLS);
156  mimmo::pin::addPin(input, output, M_DISPLS, M_DISPLS);
157  mimmo::pin::addPin(lattice, applier, M_GDISPLS, M_GDISPLS);
158  mimmo::pin::addPin(applier, mimmo1, M_GEOM, M_GEOM);
159 
160  /*
161  Setup execution chain.
162  */
163  mimmo::Chain ch0;
164  ch0.addObject(input);
165  ch0.addObject(output);
166  ch0.addObject(applier);
167  ch0.addObject(lattice);
168  ch0.addObject(mimmo1);
169  ch0.addObject(mimmo0);
170 
171  /* Execute the chain.
172  * Use debug flag false to avoid printing intermediate results of the execution steps.
173  */
174  ch0.exec(false);
175 
176  /*
177  Clean up & exit;
178  */
179  delete lattice;
180  delete applier;
181  delete input;
182  delete output;
183  delete mimmo0;
184  delete mimmo1;
185 
186  return;
187 }
188 
189 int main( int argc, char *argv[] ) {
190 
191  BITPIT_UNUSED(argc);
192  BITPIT_UNUSED(argv);
193 
194 #if MIMMO_ENABLE_MPI
195  MPI_Init(&argc, &argv);
196 #endif
197  try{
199  test00004() ;
200  }
201  catch(std::exception & e){
202  std::cout<<"manipulators_example_00004 exited with an error of type : "<<e.what()<<std::endl;
203  return 1;
204  }
205 #if MIMMO_ENABLE_MPI
206  MPI_Finalize();
207 #endif
208 
209  return 0;
210 }
void exec(bool debug=false)
Definition: Chain.cpp:284
#define M_GDISPLS
std::array< int, 3 > iarray3E
Chain is the class used to manage the chain execution of multiple executable blocks (manipulation obj...
Definition: Chain.hpp:48
darray3E getLocalPoint(int)
#define M_DISPLS
#define M_GEOM
Apply is the class that applies the deformation resulting from a manipulation object to the geometry.
Definition: Apply.hpp:89
void setRefSystem(darray3E, darray3E, darray3E)
std::vector< darray3E > dvecarr3E
void setWriteFileType(FileType type)
void setCoordType(CoordType, int)
void setFilename(std::string filename)
void setWriteDir(std::string dir)
int accessPointIndex(int i, int j, int k)
void setInput(T *data)
GENERICINPUT////////////////////////////////////////////////////////////////////////////.
void setWriteFilename(std::string filename)
GenericOutput is the class that write generic data in a file output.
int getNNodes()
Definition: Lattice.cpp:125
GenericInput is the class that set the initialization of a generic input data.
void setReadFilename(std::string filename)
void setCSV(bool csv)
bool addPin(BaseManipulation *objSend, BaseManipulation *objRec, PortID portS, PortID portR, bool forced)
MimmoGeometry is an executable block class wrapping(linking or internally instantiating) a Mimmo Obje...
int accessGridFromDOF(int index)
Definition: Lattice.cpp:183
void setLattice(darray3E &origin, darray3E &span, ShapeType, iarray3E &dimensions, iarray3E &degrees)
Definition: FFDLattice.cpp:316
std::array< double, 3 > darray3E
void setReadFileType(FileType type)
Free Form Deformation of a 3D surface and point clouds, with structured lattice.
Definition: FFDLattice.hpp:133
void setReadDir(std::string dir)
int addObject(BaseManipulation *obj, int id_=-1)
Definition: Chain.cpp:170
virtual bool build()