ScaleGeometry.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 "ScaleGeometry.hpp"
25 
26 namespace mimmo{
27 
28 
33  m_scaling = scaling;
34  m_origin.fill(0.0);
35  m_meanP = false;
36  m_name = "mimmo.ScaleGeometry";
37 };
38 
43 ScaleGeometry::ScaleGeometry(const bitpit::Config::Section & rootXML){
44 
45  m_scaling.fill(1.0);
46  m_origin.fill(0.0);
47  m_meanP = false;
48  m_name = "mimmo.ScaleGeometry";
49 
50  std::string fallback_name = "ClassNONE";
51  std::string input = rootXML.get("ClassName", fallback_name);
52  input = bitpit::utils::string::trim(input);
53  if(input == "mimmo.ScaleGeometry"){
54  absorbSectionXML(rootXML);
55  }else{
57  };
58 }
59 
63 
67  m_scaling = other.m_scaling;
68  m_origin = other.m_origin;
69  m_meanP = other.m_meanP;
70  m_filter = other.m_filter;
71 };
72 
77  swap(other);
78  return *this;
79 }
80 
86 {
87  std::swap(m_scaling, x.m_scaling);
88  std::swap(m_origin, x.m_origin);
89  std::swap(m_meanP, x.m_meanP);
90  m_filter.swap(x.m_filter);
91  m_displ.swap(x.m_displ);
93 }
94 
95 
98 void
100  bool built = true;
101  built = (built && createPortIn<darray3E, ScaleGeometry>(&m_origin, M_POINT));
102  built = (built && createPortIn<darray3E, ScaleGeometry>(&m_scaling, M_SPAN));
103  built = (built && createPortIn<dmpvector1D*, ScaleGeometry>(this, &mimmo::ScaleGeometry::setFilter, M_FILTER));
104  built = (built && createPortIn<MimmoSharedPointer<MimmoObject>, ScaleGeometry>(&m_geometry, M_GEOM, true));
105  built = (built && createPortOut<dmpvecarr3E*, ScaleGeometry>(this, &mimmo::ScaleGeometry::getDisplacements, M_GDISPLS));
106  built = (built && createPortOut<MimmoSharedPointer<MimmoObject>, ScaleGeometry>(this, &BaseManipulation::getGeometry, M_GEOM));
107  m_arePortsBuilt = built;
108 };
109 
113 void
115  m_origin = origin;
116 }
117 
121 void
123  m_meanP = meanP;
124 }
125 
129 void
131  m_scaling = scaling;
132 }
133 
138 void
140  if(!filter) return;
141  m_filter = *filter;
142 }
143 
150  return &m_displ;
151 };
152 
156 void
158 
159  if(getGeometry() == nullptr){
160  (*m_log)<<m_name + " : nullptr pointer to linked geometry found"<<std::endl;
161  throw std::runtime_error(m_name + "nullptr pointer to linked geometry found");
162  }
163 
164  checkFilter();
165  m_displ.clear();
167  m_displ.reserve(getGeometry()->getNVertices());
168  m_displ.setGeometry(getGeometry());
169 
170 
171  long ID;
172  darray3E value;
173  //computing centroid
174  int nV = m_geometry->getNVertices();
175  darray3E center = m_origin;
176  if (m_meanP){
177  center.fill(0.0);
178  for (const auto & vertex : m_geometry->getVertices()){
179  center += vertex.getCoords();
180  }
181  center /=double(nV);
182  }
183  for (const auto & vertex : m_geometry->getVertices()){
184  ID = vertex.getId();
185 
186  darray3E coords = vertex.getCoords();
187  value = (( m_scaling*(coords - center) + center ) - coords) * m_filter[ID] ;
188  m_displ.insert(ID, value);
189  }
190 };
191 
195 void
197  _apply(m_displ);
198 }
199 
204 void
206  bool check = m_filter.getDataLocation() == mimmo::MPVLocation::POINT;
207  check = check && m_filter.completeMissingData(0.0);
208  check = check && m_filter.getGeometry() == getGeometry();
209 
210  if (!check){
211  m_log->setPriority(bitpit::log::Verbosity::DEBUG);
212  (*m_log)<<"Not valid filter found in "<<m_name<<". Proceeding with default unitary field"<<std::endl;
213  m_log->setPriority(bitpit::log::Verbosity::NORMAL);
214 
215  m_filter.clear();
216  m_filter.setGeometry(m_geometry);
218  m_filter.reserve(getGeometry()->getNVertices());
219  for (const auto & vertex : getGeometry()->getVertices()){
220  m_filter.insert(vertex.getId(), 1.0);
221  }
222  }
223 }
224 
230 void
231 ScaleGeometry::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
232 
233  BITPIT_UNUSED(name);
234 
235  BaseManipulation::absorbSectionXML(slotXML, name);
236 
237  if(slotXML.hasOption("MeanPoint")){
238  std::string input = slotXML.get("MeanPoint");
239  bool value = false;
240  if(!input.empty()){
241  std::stringstream ss(bitpit::utils::string::trim(input));
242  ss >> value;
243  }
244  setMeanPoint(value);
245  };
246 
247 
248  if(slotXML.hasOption("Origin")){
249  std::string input = slotXML.get("Origin");
250  input = bitpit::utils::string::trim(input);
251  darray3E temp = {{0.0,0.0,0.0}};
252  if(!input.empty()){
253  std::stringstream ss(input);
254  for(auto &val : temp) ss>>val;
255  }
256  setOrigin(temp);
257  }
258 
259  if(slotXML.hasOption("Scaling")){
260  std::string input = slotXML.get("Scaling");
261  input = bitpit::utils::string::trim(input);
262  darray3E temp = {{0.0,0.0,0.0}};
263  if(!input.empty()){
264  std::stringstream ss(input);
265  for(auto &val : temp) ss>>val;
266  }
267  setScaling(temp);
268  }
269 
270 };
271 
277 void
278 ScaleGeometry::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
279 
280  BITPIT_UNUSED(name);
281 
282  BaseManipulation::flushSectionXML(slotXML, name);
283  {
284  std::stringstream ss;
285  ss<<std::scientific<<m_origin[0]<<'\t'<<m_origin[1]<<'\t'<<m_origin[2];
286  slotXML.set("Origin", ss.str());
287  }
288 
289  {
290  bool value = m_meanP;
291  std::string towrite = std::to_string(value);
292  slotXML.set("MeanPoint", towrite);
293  }
294 
295  {
296  std::stringstream ss;
297  ss<<std::scientific<<m_scaling[0]<<'\t'<<m_scaling[1]<<'\t'<<m_scaling[2];
298  slotXML.set("Scaling", ss.str());
299  }
300 
301 
302 };
303 
304 }
dmpvecarr3E * getDisplacements()
#define M_GDISPLS
ScaleGeometry is the class that applies a scaling to a given geometry patch in respect to the mean po...
void setFilter(dmpvector1D *filter)
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
#define M_GEOM
void swap(ScaleGeometry &x) noexcept
bool completeMissingData(const mpv_t &defValue)
#define M_POINT
void warningXML(bitpit::Logger *log, std::string name)
BaseManipulation is the base class of any manipulation object of the library.
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
ScaleGeometry & operator=(ScaleGeometry other)
MimmoSharedPointer< MimmoObject > getGeometry() const
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
#define M_SPAN
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer< MimmoObject > m_geometry
void setGeometry(MimmoSharedPointer< MimmoObject > geo)
void _apply(MimmoPiercedVector< darray3E > &displacements)
void setDataLocation(MPVLocation loc)
ScaleGeometry(darray3E scaling={ {1.0, 1.0, 1.0} })
std::array< double, 3 > darray3E
void setMeanPoint(bool meanP)
MimmoSharedPointer< MimmoObject > getGeometry()
void swap(BaseManipulation &x) noexcept
void setOrigin(darray3E origin)
void setScaling(darray3E scaling)
#define M_FILTER