Apply.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 #include "Apply.hpp"
25 
26 
27 namespace mimmo{
28 
33  m_name = "mimmo.Apply";
34  m_input.clear();
36  m_factor = 1.;
37  m_annotation = false;
38  m_annotationThres = 1.0E-18;
39  m_annCellLabel = "DeformedCells";
40  m_annVertexLabel = "DeformedVertices";
41 };
42 
47 Apply::Apply(const bitpit::Config::Section & rootXML){
48 
49  m_name = "mimmo.Apply";
50  m_factor = 1.;
51  m_annotation = false;
52  m_annotationThres = 1.0E-18;
53  m_annCellLabel = "DeformedCells";
54  m_annVertexLabel = "DeformedVertices";
55 
56  std::string fallback_name = "ClassNONE";
57  std::string input = rootXML.get("ClassName", fallback_name);
58  input = bitpit::utils::string::trim(input);
59  if(input == "mimmo.Apply"){
60  absorbSectionXML(rootXML);
61  }else{
63  };
64 }
65 
69 
73 Apply::Apply(const Apply & other):BaseManipulation(other){
74  m_input = other.m_input;
76  m_factor = other.m_factor;
77  m_annotation = other.m_annotation;
81  //annotation structures are not copied, they are filled each time in execution if m_annotation is true.
82 };
83 
88 void Apply::swap(Apply & x) noexcept
89 {
90  //std::swap(m_input, x.m_input);
91  m_input.swap(x.m_input);
92  m_scalarinput.swap(x.m_scalarinput);
93  std::swap(m_factor, x.m_factor);
94  std::swap(m_annotation, x.m_annotation);
95  std::swap(m_annotationThres, x.m_annotationThres);
96  std::swap(m_annCellLabel, x.m_annCellLabel);
97  std::swap(m_annVertexLabel, x.m_annVertexLabel);
98 
99  m_cellAnnotation.swap(x.m_cellAnnotation);
100  m_vertexAnnotation.swap(x.m_vertexAnnotation);
101 
103 }
104 
107 void
109  bool built = true;
110  built = (built && createPortIn<dmpvecarr3E*, Apply>(this, &Apply::setInput, M_GDISPLS, true, 1));
111  built = (built && createPortIn<dmpvector1D*, Apply>(this, &Apply::setScalarInput, M_SCALARFIELD, true, 1));
112  built = (built && createPortIn<dmpvector1D*, Apply>(this, &Apply::setFilter, M_FILTER));
113  built = (built && createPortIn<MimmoSharedPointer<MimmoObject>, Apply>(this, &BaseManipulation::setGeometry, M_GEOM, true));
114 
115  built = (built && createPortOut<MimmoSharedPointer<MimmoObject>, Apply>(this, &BaseManipulation::getGeometry, M_GEOM));
116  built = (built && createPortOut<dmpvecarr3E*, Apply>(this, &Apply::getOutput, M_GDISPLS));
117  built = (built && createPortOut<MimmoPiercedVector<long>*, Apply>(this, &Apply::getAnnotatedVertices, M_LONGFIELD));
118  built = (built && createPortOut<MimmoPiercedVector<long>*, Apply>(this, &Apply::getAnnotatedCells, M_LONGFIELD2));
119 
120  m_arePortsBuilt = built;
121 };
122 
123 
127 void
129  if(!input) return;
130  m_input = *input;
131 };
132 
137 void
139  if(!input) return;
140  m_scalarinput = *input;
141 };
142 
147 void
149  if(!input) return;
150  m_filter = *input;
151 };
152 
157 void
158 Apply::setScaling(double alpha){
159  m_factor = alpha;
160 };
161 
168 void
169 Apply::setAnnotation(bool activate){
171 }
172 
178 void
180  m_annotationThres = std::max(1.0E-18, threshold);
181 }
182 
188 void
189 Apply::setCellsAnnotationName(const std::string & label){
190  if(label.empty()){
191  m_annCellLabel = "DeformedCells";
192  }else{
193  m_annCellLabel = label;
194  }
195 }
196 
202 void
203 Apply::setVerticesAnnotationName(const std::string & label){
204  if(label.empty()){
205  m_annVertexLabel = "DeformedVertices";
206  }else{
207  m_annVertexLabel = label;
208  }
209 
210 }
211 
217  return &m_output;
218 };
219 
227  return &m_vertexAnnotation;
228 }
236  return &m_cellAnnotation;
237 }
238 
244 void
246  if(!getGeometry()){
247  (*m_log)<<m_name + " : nullptr pointer to linked geometry found"<<std::endl;
248  return;
249  }
250 
251  checkInput();
252 
253  m_output = m_input;
254 
255  darray3E vertexcoords;
256  long int ID;
257  for (const auto & vertex : m_geometry->getVertices()){
258  vertexcoords = vertex.getCoords();
259  ID = vertex.getId();
260  std::array<double,3> val = m_factor*m_input[ID];
261  m_output[ID] = val;
262  vertexcoords += val;
263  getGeometry()->modifyVertex(vertexcoords, ID);
264  }
265 
266  //step 2: produce annotations.
267  if(m_annotation){
268  //-->get the list of vertices involved, whose deformation norm is greater
269  // then prescribed tolerance;
270  double tol = m_annotationThres;
275  m_vertexAnnotation.reserve(m_output.size());
276 
277  for(auto it = m_output.begin(); it!= m_output.end(); ++it){
278  if(norm2(*it) < tol) continue;
279  m_vertexAnnotation.insert(it.getId(),it.getId());
280  }
281 
282  m_vertexAnnotation.shrinkToFit();
283 
284  //cell round
285  livector1D celllist = getGeometry()->getCellFromVertexList(m_vertexAnnotation.getIds(), false); //false! any cell sharing a moved vertex
290  m_cellAnnotation.reserve(celllist.size());
291 
292  for (long id: celllist){
293  m_cellAnnotation.insert(id,id);
294  }
295  //no need to shrink cellAnnotation.
296  }
297 
298  // Update geometry
299  getGeometry()->update();
300 
301 };
302 
303 
309 void
310 Apply::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
311 
312  BITPIT_UNUSED(name);
313  BaseManipulation::absorbSectionXML(slotXML, name);
314 
315  if(slotXML.hasOption("Scaling")){
316  std::string input = slotXML.get("Scaling");
317  input = bitpit::utils::string::trim(input);
318  double val = 1.;
319  if(!input.empty()){
320  std::stringstream ss(input);
321  ss>>val;
322  }
323  setScaling(val);
324  }
325 
326  if(slotXML.hasOption("Annotation")){
327  std::string input = slotXML.get("Annotation");
328  input = bitpit::utils::string::trim(input);
329  bool val = false;
330  if(!input.empty()){
331  std::stringstream ss(input);
332  ss>>val;
333  }
334  setAnnotation(val);
335  }
336 
337  if(slotXML.hasOption("AnnotationThreshold")){
338  std::string input = slotXML.get("AnnotationThreshold");
339  input = bitpit::utils::string::trim(input);
340  double val = 1.0E-18;
341  if(!input.empty()){
342  std::stringstream ss(input);
343  ss>>val;
344  }
346  }
347 
348  if(slotXML.hasOption("CellsAnnotationName")){
349  std::string input = slotXML.get("CellsAnnotationName");
350  input = bitpit::utils::string::trim(input);
351  setCellsAnnotationName(input);
352  }
353 
354  if(slotXML.hasOption("VerticesAnnotationName")){
355  std::string input = slotXML.get("VerticesAnnotationName");
356  input = bitpit::utils::string::trim(input);
358  }
359 
360 
361 };
362 
368 void
369 Apply::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
370 
371  BITPIT_UNUSED(name);
372  BaseManipulation::flushSectionXML(slotXML, name);
373 
374  slotXML.set("Scaling", std::to_string(m_factor));
375  slotXML.set("Annotation", std::to_string(m_annotation));
376  slotXML.set("AnnotationThreshold", std::to_string(m_annotationThres));
377  slotXML.set("CellsAnnotationName", m_annCellLabel);
378  slotXML.set("VerticesAnnotationName", m_annVertexLabel);
379 };
380 
385 void
387 
388  bool check = false;
389  if (m_input.size()){
391  check = check && m_input.getGeometry() == m_geometry;
392  check = check && m_input.completeMissingData({{0.0,0.0,0.0}});
393  }
394  if (m_scalarinput.size()){
396  check = check && m_geometry->getType() == 1;
397  check = check && m_scalarinput.getGeometry() == m_geometry;
398  check = check && m_scalarinput.completeMissingData(0.0);
399  if (check){
400  m_geometry->updateAdjacencies();
401  bitpit::SurfaceKernel* skernel = static_cast<bitpit::SurfaceKernel*>(m_geometry->getPatch());
402  bitpit::PiercedVector<darray3E> vNormals;
403  bitpit::ConstProxyVector<long> verts;
404  std::size_t size;
405  long idN;
406  for(const auto & cell: m_geometry->getCells()){
407  verts = cell.getVertexIds();
408  size = verts.size();
409  for(std::size_t i=0; i<size; ++i){
410  idN = verts[i];
411  if(!vNormals.exists(idN)){
412  vNormals.insert(idN, skernel->evalVertexNormal(cell.getId(), i));
413  }
414  }
415  }
416  m_input.clear();
417  for(const auto & vertex: m_geometry->getVertices()){
420  long id = vertex.getId();
421  m_input.insert(id, darray3E(m_scalarinput[id]*vNormals[id]));
422  }
423  }
424  }
425  if (m_filter.size()){
427  check = check && m_geometry->getType() == 1;
428  // Force filter to link to the morphing geometry
429  bool geometry_check = m_filter.getGeometry() == m_geometry;
430  if (!geometry_check){
432  }
433  // Complete missing data force filter to 0.
434  check = check && m_filter.completeMissingData(0.0);
435  if (check){
436  // Apply filter to deformation field
437  for (const auto & vertex : m_geometry->getVertices()){
438  long ID = vertex.getId();
439  m_input[ID] *= m_filter[ID];
440  }
441  }
442  }
443  if (!check){
444  m_log->setPriority(bitpit::log::Verbosity::DEBUG);
445  (*m_log)<<"Not valid input found in "<<m_name<<". Proceeding with default zero field"<<std::endl;
446  m_log->setPriority(bitpit::log::Verbosity::NORMAL);
447  m_input.clear();
450  m_input.reserve(m_geometry->getNVertices());
451  for (const auto & vertex : m_geometry->getVertices()){
452  m_input.insert(vertex.getId(), {{0.0,0.0,0.0}});
453  }
454  }
455 };
456 
457 }
std::string m_annCellLabel
Definition: Apply.hpp:131
#define M_LONGFIELD
#define M_GDISPLS
dmpvecarr3E m_input
Definition: Apply.hpp:124
MimmoPiercedVector< long > * getAnnotatedVertices()
Definition: Apply.cpp:226
void setAnnotationThreshold(double threshold)
Definition: Apply.cpp:179
#define M_GEOM
Apply is the class that applies the deformation resulting from a manipulation object to the geometry.
Definition: Apply.hpp:89
void setCellsAnnotationName(const std::string &label)
Definition: Apply.cpp:189
void execute()
Definition: Apply.cpp:245
void setScaling(double alpha)
Definition: Apply.cpp:158
void setName(std::string name)
MimmoPiercedVector< long > m_vertexAnnotation
Definition: Apply.hpp:135
std::vector< long > livector1D
dmpvecarr3E m_output
Definition: Apply.hpp:128
bool completeMissingData(const mpv_t &defValue)
#define M_LONGFIELD2
void warningXML(bitpit::Logger *log, std::string name)
bool m_annotation
Definition: Apply.hpp:130
BaseManipulation is the base class of any manipulation object of the library.
MimmoSharedPointer< MimmoObject > getGeometry() const
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
Definition: Apply.cpp:310
void setInput(dmpvecarr3E *input)
Definition: Apply.cpp:128
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
double m_factor
Definition: Apply.hpp:127
double m_annotationThres
Definition: Apply.hpp:133
void setAnnotation(bool activate)
Definition: Apply.cpp:169
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer< MimmoObject > m_geometry
void checkInput()
Definition: Apply.cpp:386
void setGeometry(MimmoSharedPointer< MimmoObject > geo)
MimmoPiercedVector< long > * getAnnotatedCells()
Definition: Apply.cpp:235
dmpvector1D m_filter
Definition: Apply.hpp:126
void setDataLocation(MPVLocation loc)
#define M_SCALARFIELD
void setScalarInput(dmpvector1D *input)
Definition: Apply.cpp:138
std::string m_annVertexLabel
Definition: Apply.hpp:132
void setGeometry(MimmoSharedPointer< MimmoObject > geometry)
MimmoPiercedVector< long > m_cellAnnotation
Definition: Apply.hpp:134
std::array< double, 3 > darray3E
void swap(Apply &x) noexcept
Definition: Apply.cpp:88
MimmoSharedPointer< MimmoObject > getGeometry()
dmpvecarr3E * getOutput()
Definition: Apply.cpp:216
void setFilter(dmpvector1D *input)
Definition: Apply.cpp:148
void buildPorts()
Definition: Apply.cpp:108
void swap(BaseManipulation &x) noexcept
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
Definition: Apply.cpp:369
dmpvector1D m_scalarinput
Definition: Apply.hpp:125
void setVerticesAnnotationName(const std::string &label)
Definition: Apply.cpp:203
#define M_FILTER