ioofoam_example_00001.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_iogeneric.hpp"
26 #include "IOOFOAM.hpp"
27 #include "mimmo_manipulators.hpp"
28 
29 // =================================================================================== //
30 
61 void OFOAM_manip() {
62 
63  /* read a OpenFoam mesh, expose bulk volume and boundary mesh */
64  mimmo::IOOFOAM * reader = new mimmo::IOOFOAM(false);
65  reader->setDir("geodata/OFOAM");
66 
67  /* Define a FFDLattice manipulation shaped as a box. Default dimension are 2x2x2 */
70 
71  /* set up origin and span of the lattice box */
72  darray3E origin = {{0.367, 0., 0.}};
73  darray3E span = {{0.2, 0.1, 0.1}};
74  /* define displacements of the lattice nodes */
75  dvecarr3E displ(12,{{0.0,0.0,0.0}});
76  displ[4][1] = 0.045;
77  displ[5][1] = 0.045;
78  displ[6][1] = -0.045;
79  displ[7][1] = -0.045;
80 
81  ffd->setOrigin(origin);
82  ffd->setSpan(span);
83  ffd->setDimension(ivector1D({{3,2,2}}));
84  ffd->setDegrees(iarray3E({{2,2,2}}));
85  ffd->setDisplacements(displ);
86  ffd->setPlotInExecution(true);
87 
88  /* Define Apply block to apply deformation field from ffd to the target volume mesh*/
89  mimmo::Apply * applier = new mimmo::Apply();
90 
91  /* In the same openfoam case folder, the block will write the new modified vertices only*/
92  mimmo::IOOFOAM * writer = new mimmo::IOOFOAM(true);
93  writer->setDir("geodata/OFOAM");
94  writer->setWritePointsOnly(true);
95  writer->setOverwrite(false);
96 
97  /* define block connections */
98  mimmo::pin::addPin(reader, ffd, M_GEOMOFOAM, M_GEOM);
99  mimmo::pin::addPin(reader, applier, M_GEOMOFOAM, M_GEOM);
100  mimmo::pin::addPin(ffd, applier, M_GDISPLS, M_GDISPLS);
101  mimmo::pin::addPin(applier, writer, M_GEOM, M_GEOMOFOAM);
102 
103  /*Define chain */
104  mimmo::Chain c0;
105  c0.addObject(reader);
106  c0.addObject(ffd);
107  c0.addObject(applier);
108  c0.addObject(writer);
109  c0.exec(true);
110 
111  /*clean up */
112  delete reader;
113  delete ffd;
114  delete applier;
115  delete writer;
116 }
117 
118 // =================================================================================== //
119 
120 void OFOAM_sensi() {
121 
122  /* read a OpenFoam mesh, expose bulk volume and boundary mesh */
123  mimmo::IOOFOAM * reader = new mimmo::IOOFOAM(false);
124  reader->setDir("geodata/OFOAM");
125 
126  /* read a scalar field p, defined onto the OpenFoam boundary mesh */
127  mimmo::IOOFOAMScalarField * fieldreader = new mimmo::IOOFOAMScalarField(false);
128  fieldreader->setDir("geodata/OFOAM");
129  fieldreader->setFieldName("p");
130 
131  /*
132  Create apply block. Use p field to define a "deformation field" onto the surface
133  boundary mesh and using the surface normals. Scaling is used to
134  globally modulate the p value */
135  mimmo::Apply * applier = new mimmo::Apply();
136  applier->setScaling(0.1);
137 
138  /*
139  Write the modified boundary mesh to vtu file
140  */
142  writer->setWriteDir(".");
143  writer->setWriteFileType(FileType::SURFVTU);
144  writer->setWriteFilename("ofoam_sensi_output");
145 
146  /* Define block pin connections */
147  mimmo::pin::addPin(reader, fieldreader, M_GEOMOFOAM2, M_GEOMOFOAM2);
148  mimmo::pin::addPin(reader, fieldreader, M_UMAPIDS, M_UMAPIDS);
149  mimmo::pin::addPin(reader, applier, M_GEOMOFOAM2, M_GEOM);
150  mimmo::pin::addPin(fieldreader, applier, M_SCALARFIELD2, M_SCALARFIELD);
151  mimmo::pin::addPin(applier, writer, M_GEOM, M_GEOM);
152 
153  /* Define the execution chain*/
154  mimmo::Chain c0;
155  c0.addObject(reader);
156  c0.addObject(fieldreader);
157  c0.addObject(applier);
158  c0.addObject(writer);
159  c0.exec(true);
160 
161  /*Clean up */
162  delete reader;
163  delete fieldreader;
164  delete applier;
165  delete writer;
166 }
167 
168 // =================================================================================== //
169 
170 int main( int argc, char *argv[] ) {
171 
172  BITPIT_UNUSED(argc);
173  BITPIT_UNUSED(argv);
174 
175 #if MIMMO_ENABLE_MPI
176  MPI_Init(&argc, &argv);
177 #endif
178 
179  try{
180  OFOAM_sensi() ;
181  }
182  catch(std::exception & e){
183  std::cout<<"test_ioofoam_00001 PART1 exit with the following errors :"<<e.what()<<std::endl;
184  return 1;
185  }
186  try{
187  OFOAM_manip() ;
188  }
189  catch(std::exception & e){
190  std::cout<<"test_ioofoam_00001 PART2 exit with the following errors :"<<e.what()<<std::endl;
191  return 1;
192  }
193 
194 #if MIMMO_ENABLE_MPI
195  MPI_Finalize();
196 #endif
197 
198  return 0;
199 }
void exec(bool debug=false)
Definition: Chain.cpp:284
#define M_GDISPLS
std::array< int, 3 > iarray3E
#define M_UMAPIDS
void setDimension(ivector1D dim)
#define M_GEOMOFOAM
Chain is the class used to manage the chain execution of multiple executable blocks (manipulation obj...
Definition: Chain.hpp:48
#define M_GEOM
Apply is the class that applies the deformation resulting from a manipulation object to the geometry.
Definition: Apply.hpp:89
std::vector< darray3E > dvecarr3E
void setDisplacements(dvecarr3E displacements)
Definition: FFDLattice.cpp:284
void setScaling(double alpha)
Definition: Apply.cpp:158
void setWriteFileType(FileType type)
void setOrigin(darray3E origin)
void setWriteDir(std::string dir)
void setDegrees(iarray3E curveDegrees)
Definition: FFDLattice.cpp:273
void setShape(ShapeType type=ShapeType::CUBE)
void setWriteFilename(std::string filename)
std::vector< int > ivector1D
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...
#define M_SCALARFIELD
std::array< double, 3 > darray3E
void setSpan(double, double, double)
#define M_SCALARFIELD2
Free Form Deformation of a 3D surface and point clouds, with structured lattice.
Definition: FFDLattice.hpp:133
#define M_GEOMOFOAM2
int addObject(BaseManipulation *obj, int id_=-1)
Definition: Chain.cpp:170