ProjSegmentOnSurface.cpp
1 /*----------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Optimad Engineering S.r.l. ("COMPANY") CONFIDENTIAL
6  * Copyright (c) 2015-2021 Optimad Engineering S.r.l., All Rights Reserved.
7  *
8  * --------------------------------------------------------------------------
9  *
10  * NOTICE: All information contained herein is, and remains the property
11  * of COMPANY. The intellectual and technical concepts contained herein are
12  * proprietary to COMPANY and may be covered by Italian and Foreign Patents,
13  * patents in process, and are protected by trade secret or copyright law.
14  * Dissemination of this information or reproduction of this material is
15  * strictly forbidden unless prior written permission is obtained from
16  * COMPANY. Access to the source code contained herein is hereby forbidden
17  * to anyone except current COMPANY employees, managers or contractors who
18  * have executed Confidentiality and Non-disclosure agreements explicitly
19  * covering such access.
20  *
21  * The copyright notice above does not evidence any actual or intended
22  * publication or disclosure of this source code, which includes information
23  * that is confidential and/or proprietary, and is a trade secret, of
24  * COMPANY. ANY REPRODUCTION, MODIFICATION, DISTRIBUTION, PUBLIC PERFORMANCE,
25  * OR PUBLIC DISPLAY OF OR THROUGH USE OF THIS SOURCE CODE WITHOUT THE
26  * EXPRESS WRITTEN CONSENT OF COMPANY IS STRICTLY PROHIBITED, AND IN
27  * VIOLATION OF APPLICABLE LAWS AND INTERNATIONAL TREATIES. THE RECEIPT OR
28  * POSSESSION OF THIS SOURCE CODE AND/OR RELATED INFORMATION DOES NOT CONVEY
29  * OR IMPLY ANY RIGHTS TO REPRODUCE, DISCLOSE OR DISTRIBUTE ITS CONTENTS, OR
30  * TO MANUFACTURE, USE, OR SELL ANYTHING THAT IT MAY DESCRIBE, IN WHOLE OR
31  * IN PART.
32  *
33 \*----------------------------------------------------------------------------*/
34 
35 #include "ProjSegmentOnSurface.hpp"
36 #include <SkdTreeUtils.hpp>
37 #if MIMMO_ENABLE_MPI
38  #include "Partition.hpp"
39 #endif
40 namespace mimmo{
41 
46  m_name = "mimmo.ProjSegmentOnSurface";
47  m_topo = 1;
48  m_pointA.fill(0.0);
49  m_pointB.fill(1.0);
50 };
51 
56 ProjSegmentOnSurface::ProjSegmentOnSurface(const bitpit::Config::Section & rootXML){
57 
58  m_name = "mimmo.ProjSegmentOnSurface";
59  m_topo = 1;
60  m_pointA.fill(0.0);
61  m_pointB.fill(1.0);
62 
63  std::string fallback_name = "ClassNONE";
64  std::string input = rootXML.get("ClassName", fallback_name);
65  input = bitpit::utils::string::trim(input);
66  if(input == "mimmo.ProjSegmentOnSurface"){
67  absorbSectionXML(rootXML);
68  }else{
70  };
71 }
72 
77  clear();
78 };
79 
84  m_pointA = other.m_pointA;
85  m_pointB = other.m_pointB;
86 };
87 
92  swap(other);
93  return *this;
94 };
95 
101 {
102  std::swap(m_pointA, x.m_pointA);
103  std::swap(m_pointB, x.m_pointB);
105 }
106 
110 void
113  m_pointA.fill(0.0);
114  m_pointB.fill(1.0);
115 }
116 
122 void
124 
125  if(norm2(pointB - pointA) < 1.E-18) return;
126 
127  m_pointA = pointA;
128  m_pointB = pointB;
129 }
130 
137 void
139  setSegment(origin, origin+length*dir);
140 }
141 
147 void ProjSegmentOnSurface::absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name){
148 
149  BITPIT_UNUSED(name);
150 
151  std::string input;
152  BaseManipulation::absorbSectionXML(slotXML, name);
153 
154  if(slotXML.hasOption("Segment")){
155  input = slotXML.get("Segment");
156  darray3E p1,p2;
157  p1.fill(0.0);
158  p2.fill(1.0);
159  if(!input.empty()){
160  std::stringstream ss(bitpit::utils::string::trim(input));
161  ss >> p1[0]>>p1[1]>>p1[2]>>p2[0]>>p2[1]>>p2[2];
162  }
163  setSegment(p1,p2);
164  };
165  if(slotXML.hasOption("nCells")){
166  input = slotXML.get("nCells");
167  int value = 1000;
168  if(!input.empty()){
169  std::stringstream ss(bitpit::utils::string::trim(input));
170  ss >> value;
171  }
173  };
174 
175 
176  if(slotXML.hasOption("BvTree")){
177  input = slotXML.get("BvTree");
178  bool value = false;
179  if(!input.empty()){
180  std::stringstream ss(bitpit::utils::string::trim(input));
181  ss >> value;
182  }
183  setBuildSkdTree(value);
184  };
185 
186  if(slotXML.hasOption("SkdTree")){
187  input = slotXML.get("SkdTree");
188  bool value = false;
189  if(!input.empty()){
190  std::stringstream ss(bitpit::utils::string::trim(input));
191  ss >> value;
192  }
193  setBuildSkdTree(value);
194 };
195 
196  if(slotXML.hasOption("KdTree")){
197  input = slotXML.get("KdTree");
198  bool value = false;
199  if(!input.empty()){
200  std::stringstream ss(bitpit::utils::string::trim(input));
201  ss >> value;
202  }
203  setBuildKdTree(value);
204  };
205 };
206 
212 void ProjSegmentOnSurface::flushSectionXML(bitpit::Config::Section & slotXML, std::string name){
213 
214  BITPIT_UNUSED(name);
215 
216  BaseManipulation::flushSectionXML(slotXML, name);
217  std::string output;
218 
219  {
220  std::stringstream ss;
221  ss<<m_pointA[0]<<m_pointA[1]<<m_pointA[2]<<m_pointB[0]<<m_pointB[1]<<m_pointB[2];
222  slotXML.set("Segment", ss.str());
223  }
224  {
225  output = std::to_string(m_nC);
226  slotXML.set("nCells", output);
227  }
228 
229  if(m_buildSkdTree){
230  output = std::to_string(m_buildSkdTree);
231  slotXML.set("SkdTree", output);
232  }
233 
234  if(m_buildKdTree){
235  output = std::to_string(m_buildKdTree);
236  slotXML.set("KdTree", output);
237  }
238 };
239 
240 
244 void
246 
247  //create points on segment first ...
248  dvecarr3E points;
249 #if MIMMO_ENABLE_MPI
250  if(m_rank == 0)
251 #endif
252  {
253  //create the vertices array, ordered from pointA to pointB
254  points.resize(m_nC+1);
255  darray3E dx = (m_pointB - m_pointA);
256  dx /= double(m_nC);
257  int counter = 0;
258  for( darray3E & ele : points){
259  ele = m_pointA + double(counter)*dx;
260  ++counter;
261  }
262  }
263 
264  //...and projecting them onto target surface
265  getGeometry()->buildSkdTree();
266 
267  std::size_t npoints = points.size();
268  dvecarr3E projs(npoints);
269  livector1D ids(npoints);
270  ivector1D ranks(npoints,0);
271 #if MIMMO_ENABLE_MPI
272  double radius = std::numeric_limits<double>::max();
273  skdTreeUtils::projectPointGlobal(npoints, points.data(), getGeometry()->getSkdTree(), projs.data(), ids.data(), ranks.data(), radius, false);
274 #else
275  skdTreeUtils::projectPoint(npoints, points.data(), getGeometry()->getSkdTree(), projs.data(), ids.data());
276 #endif
277 
278  //you have now on master 0 ranks where points project, and projected values.
279  //istantiate object and fill data of 3D curve
280  {
282  m_patch = dum;
283  }
284 
285 #if MIMMO_ENABLE_MPI
286  //take track of a partition map using ranks of projection.
287  std::unordered_map<long, int> partMap;
288  if (m_rank ==0) //with rank 0 only in MPI
289 #endif
290  {
291  //reserving memory
292  m_patch->getPatch()->reserveVertices(m_nC+1);
293  m_patch->getPatch()->reserveCells(m_nC);
294 
295  //storing the projected points
296  long id(0);
297  for(darray3E & vv : projs){
298  m_patch->addVertex(vv, id);
299  ++id;
300  }
301 
302 
303  //start filling connectivity of your object.
304  bitpit::ElementType eltype = bitpit::ElementType::LINE;
305  id = 0;
306  for(int i=0; i<m_nC; ++i){
307  m_patch->addConnectedCell(livector1D({{i, i+1}}), eltype, 0, id, 0);
308 #if MIMMO_ENABLE_MPI
309  partMap[id]= std::min(ranks[i], ranks[i+1]);
310 #endif
311  ++id;
312  }
313  }
314  m_patch->update();
315 
316 #if MIMMO_ENABLE_MPI
317  std::unique_ptr<mimmo::Partition> part(new mimmo::Partition);
318  part->setPartitionMethod(mimmo::PartitionMethod::PARTGEOM);
319  part->setGeometry(m_patch);
320  part->setPartition(partMap);
321  part->execute();
322  //m_patch will return partitioned and operative.
323 #endif
324 
325 };
326 
327 }
void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
ProjSegmentOnSurface & operator=(ProjSegmentOnSurface other)
MimmoObject is the basic geometry container for mimmo library.
MimmoSharedPointer< MimmoObject > m_patch
std::vector< darray3E > dvecarr3E
std::vector< long > livector1D
void warningXML(bitpit::Logger *log, std::string name)
void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
std::vector< int > ivector1D
void swap(ProjSegmentOnSurface &x) noexcept
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
Class for projecting 1D/2D primitives on a target 3D surface mesh.
void swap(ProjPrimitivesOnSurfaces &x) noexcept
Executable block class capable of projecting an elemental segment on a 3D surface mesh defined by a M...
std::array< double, 3 > darray3E
darray3E projectPoint(const std::array< double, 3 > *point, const bitpit::PatchSkdTree *skdtree, double r)
void setSegment(darray3E pointA, darray3E pointB)
MimmoSharedPointer is a custom implementation of shared pointer.
MimmoSharedPointer< MimmoObject > getGeometry()