ExtractVectorField.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 "ExtractFields.hpp"
26 #include "SkdTreeUtils.hpp"
27 
28 namespace mimmo{
29 
34  m_name = "mimmo.ExtractVectorField";
35 }
36 
41 ExtractVectorField::ExtractVectorField(const bitpit::Config::Section & rootXML){
42 
43  std::string fallback_name = "ClassNONE";
44  std::string input_name = rootXML.get("ClassName", fallback_name);
45  input_name = bitpit::utils::string::trim(input_name);
46 
47  m_name = "mimmo.ExtractVectorField";
48 
49  if(input_name == "mimmo.ExtractVectorField"){
50  absorbSectionXML(rootXML);
51  }else{
53  };
54 }
55 
60 
66  m_field = other.m_field;
67  m_result = other.m_result;
68 }
69 
76  m_field = other.m_field;
77  m_result = other.m_result;
78  return *this;
79 };
80 
86 {
87  m_field.swap(x.m_field);
88  m_result.swap(x.m_result);
90 };
91 
92 
96 void
98 
100 
101  bool built = m_arePortsBuilt;
102  built = (built && createPortIn<dmpvecarr3E*, ExtractVectorField>(this, &mimmo::ExtractVectorField::setField, M_VECTORFIELD, true));
103  built = (built && createPortOut<dmpvecarr3E*, ExtractVectorField>(this, &mimmo::ExtractVectorField::getExtractedField, M_VECTORFIELD));
104 
105  m_arePortsBuilt = built;
106 }
107 
114  return &m_result;
115 }
116 
123  return m_field;
124 }
125 
130 void
132  if(!field) return;
133  m_field = *field;
134 }
135 
139 void
141  m_field.clear();
142  m_result.clear();
144 }
145 
149 void
151 
152  m_result.setName("magnitude");
153  write(m_result.getGeometry(), m_result);
154 
155 }
156 
157 
162 bool
164 
165  if (getGeometry() == nullptr || m_field.getGeometry() == nullptr) return false;
166  //checking internal ids coherence of the field.
167  if(!m_field.checkDataIdsCoherence()) return false;
168 
169  mimmo::MPVLocation refLoc = m_field.getDataLocation();
171 
172  m_result.clear();
173  m_result.setDataLocation(refLoc);
174 
175  switch(m_mode){
176  case ExtractMode::ID :
177  extractID(refLoc);
178  m_result.setGeometry(getGeometry());
179  break;
180 
181  case ExtractMode::PID :
182  extractPID(refLoc);
183  m_result.setGeometry(m_field.getGeometry());
184  break;
185 
186  case ExtractMode::MAPPING :
187  extractMapping(refLoc);
188  m_result.setGeometry(getGeometry());
189  break;
190 
191  default :
192  assert(false && "unrecognized field location");
193  break;
194  }
195 
196  return true;
197 
198 }
199 
205 void ExtractVectorField::extractID(mimmo::MPVLocation loc){
206 
207  switch(loc){
209  for (const auto & ID : getGeometry()->getVertices().getIds()){
210  if (m_field.exists(ID)){
211  m_result.insert(ID, m_field[ID]);
212  }
213  }
214  break;
216  for (const auto & ID : getGeometry()->getCells().getIds()){
217  if (m_field.exists(ID)){
218  m_result.insert(ID, m_field[ID]);
219  }
220  }
221  break;
223  getGeometry()->updateInterfaces();
224  for (const auto & ID : getGeometry()->getInterfaces().getIds()){
225  if (m_field.exists(ID)){
226  m_result.insert(ID, m_field[ID]);
227  }
228  }
229  break;
230  default:
231  break;
232  }
233 }
234 
240 void ExtractVectorField::extractPID(mimmo::MPVLocation loc){
241  //Extract by PIDs
242  livector1D commonPID;
243  {
244  std::unordered_set<long> pidTarget = getGeometry()->getPIDTypeList();
245  std::unordered_set<long> pidLinked = m_field.getGeometry()->getPIDTypeList();
246  std::set<long> unionPID(pidTarget.begin(), pidTarget.end());
247  unionPID.insert(pidLinked.begin(), pidLinked.end());
248  for(auto val: unionPID){
249  if(pidLinked.count(val) && pidTarget.count(val)){
250  commonPID.push_back(val);
251  }
252  }
253  }
254  livector1D cellExtracted = m_field.getGeometry()->extractPIDCells(commonPID);
255 
256  switch(loc){
258  {
259  livector1D vertExtracted = m_field.getGeometry()->getVertexFromCellList(cellExtracted);
260  for (const auto & ID : vertExtracted){
261  if (m_field.exists(ID)){
262  m_result.insert(ID, m_field[ID]);
263  }
264  }
265  }
266  break;
268  for (const auto & ID : cellExtracted){
269  if (m_field.exists(ID)){
270  m_result.insert(ID, m_field[ID]);
271  }
272  }
273  break;
275  {
276  livector1D interfExtracted = m_field.getGeometry()->getInterfaceFromCellList(cellExtracted);
277  for (const auto & ID : interfExtracted){
278  if (m_field.exists(ID)){
279  m_result.insert(ID, m_field[ID]);
280  }
281  }
282  }
283  break;
284  default:
285  break;
286  }
287 }
288 
294 void ExtractVectorField::extractMapping(mimmo::MPVLocation loc){
295 
296  m_field.getGeometry()->buildSkdTree();
297  getGeometry()->buildSkdTree();
298 
299 #if MIMMO_ENABLE_MPI
300  livector1D cellExtracted = mimmo::skdTreeUtils::selectByGlobalPatch(m_field.getGeometry()->getSkdTree(), getGeometry()->getSkdTree(), m_tol);
301 #else
302  livector1D cellExtracted = mimmo::skdTreeUtils::selectByPatch(m_field.getGeometry()->getSkdTree(), getGeometry()->getSkdTree(), m_tol);
303 #endif
304 
305  switch(loc){
307  {
308  livector1D vertExtracted = getGeometry()->getVertexFromCellList(cellExtracted);
309  for (const auto & ID : vertExtracted){
310  if (m_field.exists(ID)){
311  m_result.insert(ID, m_field[ID]);
312  }
313  }
314  }
315  break;
317  for (const auto & ID : cellExtracted){
318  if (m_field.exists(ID)){
319  m_result.insert(ID, m_field[ID]);
320  }
321  }
322  break;
324  {
325  livector1D interfExtracted = getGeometry()->getInterfaceFromCellList(cellExtracted);
326  for (const auto & ID : interfExtracted){
327  if (m_field.exists(ID)){
328  m_result.insert(ID, m_field[ID]);
329  }
330  }
331  }
332  break;
333  default:
334  break;
335  }
336 }
337 
338 }
void setField(dmpvecarr3E *field)
void swap(ExtractField &x) noexcept
MPVLocation
Define data location for the MimmoPiercedVector field.
void setName(std::string name)
std::vector< long > livector1D
ExtractField is an abstract executable block class for extracting/restricting an input field defined ...
ExtractVectorField & operator=(const ExtractVectorField &other)
void warningXML(bitpit::Logger *log, std::string name)
#define M_VECTORFIELD
MimmoSharedPointer< MimmoObject > getGeometry() const
void write(MimmoSharedPointer< MimmoObject > geometry)
ExtractField & operator=(const ExtractField &other)
std::vector< long > selectByPatch(bitpit::PatchSkdTree *selection, bitpit::PatchSkdTree *target, double tol)
void setGeometry(MimmoSharedPointer< MimmoObject > geo)
void swap(ExtractVectorField &x) noexcept
void setDataLocation(MPVLocation loc)
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
MimmoSharedPointer< MimmoObject > getGeometry()
ExtractVectorField is specialized derived class of ExtractField to extract a vector field of array<do...