BaseManipulation.tpp
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 namespace mimmo{
25 
33 template<typename T, typename O>
34 bool
36  bool check = false;
37 
38  //checking if portS containerTAG and dataTAG are registered in mimmo::PortManager
39 
40  if(!mimmo::PortManager::instance().containsPort(portS) || m_portOut.count(portS) != 0 ){
41  (*m_log)<<"Unable to physically create the Output Port."<<std::endl;
42  (*m_log)<<"Port name was not regularly registered or a port with the same name was already instantiated in the current class."<<std::endl;
43  return check;
44  }
45 
46  auto info = mimmo::PortManager::instance().getPortData(portS);
47  DataType datat(info.container, info.datatype);
48  PortOutT<T, O>* portOut = new PortOutT<T, O>(var_, datat);
49  m_portOut[portS] = portOut;
50  check = true;
51  return(check);
52 }
53 
62 template<typename T, typename O>
63 bool
64 BaseManipulation::createPortOut(O* obj_, T (O::*getVar_)(), PortID portS){
65  bool check = false;
66 
67  //checking if portS containerTAG and dataTAG are registered in mimmo::PortManager
68 
69  if(!mimmo::PortManager::instance().containsPort(portS) || m_portOut.count(portS) != 0 ){
70  (*m_log)<<"Unable to physically create the Output Port."<<std::endl;
71  (*m_log)<<"Port name was not regularly registered or a port with the same name was already instantiated in the current class."<<std::endl;
72  return check;
73  }
74 
75  auto info = mimmo::PortManager::instance().getPortData(portS);
76  DataType datat(info.container, info.datatype);
77  PortOutT<T, O>* portOut = new PortOutT<T, O>(obj_, getVar_, datat);
78  m_portOut[portS] = portOut;
79  check = true;
80  return(check);
81 }
82 
92 template<typename T, typename O>
93 bool
94 BaseManipulation::createPortIn(T* var_, PortID portR, bool mandatory, int family ){
95  bool check = false;
96 
97  //checking if portR containerTAG and dataTAG are registered in mimmo::PortManager
98 
99  if(!mimmo::PortManager::instance().containsPort(portR) || m_portOut.count(portR) != 0 ){
100  (*m_log)<<"Unable to physically create the Input Port."<<std::endl;
101  (*m_log)<<"Port name was not regularly registered or a port with the same name was already instantiated in the current class."<<std::endl;
102  return check;
103  }
104 
105  auto info = mimmo::PortManager::instance().getPortData(portR);
106  DataType datat(info.container, info.datatype);
107  PortInT<T, O>* portIn = new PortInT<T, O>(var_, datat, mandatory, family);
108  m_portIn[portR] = portIn;
109  check = true;
110  return(check);
111 }
112 
123 template<typename T, typename O>
124 bool
125 BaseManipulation::createPortIn(O* obj_, void (O::*setVar_)(T), PortID portR, bool mandatory, int family ){
126  bool check = false;
127 
128  //checking if portR containerTAG and dataTAG are registered in mimmo::PortManager
129  if(!mimmo::PortManager::instance().containsPort(portR) || m_portOut.count(portR) != 0 ){
130  (*m_log)<<"Unable to physically create the Input Port."<<std::endl;
131  (*m_log)<<"Port name was not regularly registered or a port with the same name was already instantiated in the current class."<<std::endl;
132  return check;
133  }
134 
135  auto info = mimmo::PortManager::instance().getPortData(portR);
136  DataType datat(info.container, info.datatype);
137  PortInT<T, O>* portIn = new PortInT<T, O>(obj_, setVar_, datat, mandatory, family);
138  m_portIn[portR] = portIn;
139  check = true;
140  return(check);
141 }
142 
151 template<typename mpv_t>
152 void
154 {
155 
156  //Check data with geometry linked
157  if (data.getGeometry() != geometry){
158  (*m_log) << " Warning: data to write not consistent with geometry in " << m_name << "; skip data" << std::endl;
159  write(geometry);
160  return;
161  }
162  if (geometry == nullptr){
163  (*m_log) << " Warning: geometry null during writing " << m_name << std::endl;
164  return;
165  }
166 
167  //Recover location of data
168  bitpit::VTKLocation loc = bitpit::VTKLocation::UNDEFINED;
169  switch(data.getDataLocation()){
170  case MPVLocation::POINT :
171  loc = bitpit::VTKLocation::POINT;
172  break;
173  case MPVLocation::CELL :
174  loc = bitpit::VTKLocation::CELL;
175  break;
176  default:
177  (*m_log)<<" Warning: Undefined Reference Location in plotOptionalResults of "<<m_name<<std::endl;
178  (*m_log)<<" Interface or Undefined locations are not supported in VTU writing." <<std::endl;
179  return;
180  break;
181  }
182 
183  //check size of field and adjust missing values to zero for writing purposes only.
184  if(!data.completeMissingData(mpv_t())){
185  (*m_log) << " Warning: error during complete missing data to write in " << m_name << "; skip data" << std::endl;
186  write(geometry);
187  return;
188  }
189 
190  //Deduce data type and add data to vtk object
191  bitpit::VTKFieldType fieldtype;
192  if (std::is_integral<mpv_t>::value == true || std::is_floating_point<mpv_t>::value == true){
193  fieldtype = bitpit::VTKFieldType::SCALAR;
194  }
195  else if (std::is_same<mpv_t, std::array<double,3>>::value == true){
196  fieldtype = bitpit::VTKFieldType::VECTOR;
197  }
198  else{
199  (*m_log) << " Warning: data type to write not allowed in " << m_name << "; skip data" << std::endl;
200  write(geometry);
201  return;
202  }
203 
204  std::vector<mpv_t> field = data.getDataAsVector();
205 
206  geometry->getPatch()->getVTK().addData(data.getName(), fieldtype, loc, field);
207 
208  write(geometry);
209 
210  geometry->getPatch()->getVTK().removeData(data.getName());
211 
212 }
213 
224 template<typename mpv_t, typename... Args>
225 void
227 {
228  //Check data with geometry linked
229  if (data.getGeometry() != geometry){
230  (*m_log) << " Warning: data to write not consistent with geometry in " << m_name << "; skip data" << std::endl;
231  write(geometry, args...);
232  return;
233  }
234  if (geometry == nullptr){
235  (*m_log) << " Warning: geometry null during writing " << m_name << std::endl;
236  return;
237  }
238 
239  bitpit::VTKLocation loc = bitpit::VTKLocation::UNDEFINED;
240  switch(data.getDataLocation()){
241  case MPVLocation::POINT :
242  loc = bitpit::VTKLocation::POINT;
243  break;
244  case MPVLocation::CELL :
245  loc = bitpit::VTKLocation::CELL;
246  break;
247  default:
248  (*m_log)<<" Warning: Undefined Reference Location in plotOptionalResults of "<<m_name<<std::endl;
249  (*m_log)<<" Interface or Undefined locations are not supported in VTU writing." <<std::endl;
250  return;
251  break;
252  }
253 
254  //check size of field and adjust missing values to zero for writing purposes only.
255  if(!data.completeMissingData(mpv_t())){
256  (*m_log) << " Warning: error during complete missing data to write in " << m_name << "; skip data" << std::endl;
257  write(geometry, args...);
258  return;
259  }
260 
261  //Deduce data type and add data to vtk object
262  bitpit::VTKFieldType fieldtype;
263  if (std::is_integral<mpv_t>::value == true || std::is_floating_point<mpv_t>::value == true){
264  fieldtype = bitpit::VTKFieldType::SCALAR;
265  }
266  else if (std::is_same<mpv_t, std::array<double,3>>::value == true){
267  fieldtype = bitpit::VTKFieldType::VECTOR;
268  }
269  else{
270  (*m_log) << " Warning: data type to write not allowed in " << m_name << "; skip data" << std::endl;
271  write(geometry, args...);
272  return;
273  }
274 
275  std::vector<mpv_t> field = data.getDataAsVector();
276 
277  geometry->getPatch()->getVTK().addData(data.getName(), fieldtype, loc, field);
278 
279  write(geometry, args...);
280 
281  geometry->getPatch()->getVTK().removeData(data.getName());
282 
283 }
284 
285 
295 template<typename mpv_t>
296 void
298 {
299  // Instantiate fields as vector structure
300  std::vector<std::vector<mpv_t>> fields(vdata.size());
301 
302  //Deduce data type and add data to vtk object
303  bitpit::VTKFieldType fieldtype;
304  if (std::is_integral<mpv_t>::value == true || std::is_floating_point<mpv_t>::value == true){
305  fieldtype = bitpit::VTKFieldType::SCALAR;
306  }
307  else if (std::is_same<mpv_t, std::array<double,3>>::value == true){
308  fieldtype = bitpit::VTKFieldType::VECTOR;
309  }
310  else{
311  (*m_log) << " Warning: data type to write not allowed in " << m_name << "; exit" << std::endl;
312  return;
313  }
314 
315  //Loop on all input data
316  std::size_t ifield = 0;
317  for (MimmoPiercedVector<mpv_t> & data : vdata)
318  {
319 
320  //Check data with geometry linked
321  if (data.getGeometry() != geometry){
322  (*m_log) << " Warning: data to write not consistent with geometry in " << m_name << "; skip data" << std::endl;
323  continue;
324  }
325  if (geometry == nullptr){
326  (*m_log) << " Warning: geometry null during writing " << m_name << std::endl;
327  return;
328  }
329 
330  bitpit::VTKLocation loc = bitpit::VTKLocation::UNDEFINED;
331  switch(data.getDataLocation()){
332  case MPVLocation::POINT :
333  loc = bitpit::VTKLocation::POINT;
334  break;
335  case MPVLocation::CELL :
336  loc = bitpit::VTKLocation::CELL;
337  break;
338  default:
339  (*m_log)<<" Warning: Undefined Reference Location in plotOptionalResults of "<<m_name<<std::endl;
340  (*m_log)<<" Interface or Undefined locations are not supported in VTU writing." <<std::endl;
341  return;
342  break;
343  }
344 
345  //check size of field and adjust missing values to zero for writing purposes only.
346  if(!data.completeMissingData(mpv_t())){
347  (*m_log) << " Warning: error during complete missing data to write in " << m_name << "; skip data" << std::endl;
348  continue;
349  }
350 
351  fields[ifield] = data.getDataAsVector();
352 
353  geometry->getPatch()->getVTK().addData(data.getName(), fieldtype, loc, fields[ifield]);
354 
355  ifield++;
356 
357  } // End loop on data fields
358 
359  write(geometry);
360 
361  //Loop on all input data to remove fields from vtk object
362  for (MimmoPiercedVector<mpv_t> & data : vdata)
363  {
364  geometry->getPatch()->getVTK().removeData(data.getName());
365  }
366 
367 }
368 
382 template<typename mpv_t, typename... Args>
383 void
385 {
386  // Instantiate fields as vector structure
387  std::vector<std::vector<mpv_t>> fields(vdata.size());
388 
389  //Deduce data type and add data to vtk object
390  bitpit::VTKFieldType fieldtype;
391  if (std::is_integral<mpv_t>::value == true || std::is_floating_point<mpv_t>::value == true){
392  fieldtype = bitpit::VTKFieldType::SCALAR;
393  }
394  else if (std::is_same<mpv_t, std::array<double,3>>::value == true){
395  fieldtype = bitpit::VTKFieldType::VECTOR;
396  }
397  else{
398  (*m_log) << " Warning: data type to write not allowed in " << m_name << "; skip vector of data fields" << std::endl;
399  write(geometry, args...);
400  return;
401  }
402 
403  //Loop on all input data
404  std::size_t ifield = 0;
405  for (MimmoPiercedVector<mpv_t> & data : vdata)
406  {
407 
408  //Check data with geometry linked
409  if (data.getGeometry() != geometry){
410  (*m_log) << " Warning: data to write not consistent with geometry in " << m_name << "; skip data" << std::endl;
411  continue;
412  }
413  if (geometry == nullptr){
414  (*m_log) << " Warning: geometry null during writing " << m_name << std::endl;
415  return;
416  }
417 
418  bitpit::VTKLocation loc = bitpit::VTKLocation::UNDEFINED;
419  switch(data.getDataLocation()){
420  case MPVLocation::POINT :
421  loc = bitpit::VTKLocation::POINT;
422  break;
423  case MPVLocation::CELL :
424  loc = bitpit::VTKLocation::CELL;
425  break;
426  default:
427  (*m_log)<<" Warning: Undefined Reference Location in plotOptionalResults of "<<m_name<<std::endl;
428  (*m_log)<<" Interface or Undefined locations are not supported in VTU writing." <<std::endl;
429  return;
430  break;
431  }
432 
433  //check size of field and adjust missing values to zero for writing purposes only.
434  if(!data.completeMissingData(mpv_t())){
435  (*m_log) << " Warning: error during complete missing data to write in " << m_name << "; skip data" << std::endl;
436  continue;
437  }
438 
439  fields[ifield] = data.getDataAsVector();
440 
441  geometry->getPatch()->getVTK().addData(data.getName(), fieldtype, loc, fields[ifield]);
442 
443  ifield++;
444 
445  } // End loop on data fields
446 
447  write(geometry, args...);
448 
449  //Loop on all input data to remove fields from vtk object
450  for (MimmoPiercedVector<mpv_t> & data : vdata)
451  {
452  geometry->getPatch()->getVTK().removeData(data.getName());
453  }
454 
455 }
456 
457 
458 
468 template<typename mpv_t>
469 void
471 {
472  // Instantiate fields as vector structure
473  std::vector<std::vector<mpv_t>> fields(vdata.size());
474 
475  //Deduce data type and add data to vtk object
476  bitpit::VTKFieldType fieldtype;
477  if (std::is_integral<mpv_t>::value == true || std::is_floating_point<mpv_t>::value == true){
478  fieldtype = bitpit::VTKFieldType::SCALAR;
479  }
480  else if (std::is_same<mpv_t, std::array<double,3>>::value == true){
481  fieldtype = bitpit::VTKFieldType::VECTOR;
482  }
483  else{
484  (*m_log) << " Warning: data type to write not allowed in " << m_name << "; exit" << std::endl;
485  return;
486  }
487 
488  //Loop on all input data
489  std::size_t ifield = 0;
490  for (MimmoPiercedVector<mpv_t>* data : vdata)
491  {
492 
493  //Check data with geometry linked
494  if (data->getGeometry() != geometry){
495  (*m_log) << " Warning: data to write not consistent with geometry in " << m_name << "; skip data" << std::endl;
496  continue;
497  }
498  if (geometry == nullptr){
499  (*m_log) << " Warning: geometry null during writing " << m_name << std::endl;
500  return;
501  }
502 
503  bitpit::VTKLocation loc = bitpit::VTKLocation::UNDEFINED;
504  switch(data->getDataLocation()){
505  case MPVLocation::POINT :
506  loc = bitpit::VTKLocation::POINT;
507  break;
508  case MPVLocation::CELL :
509  loc = bitpit::VTKLocation::CELL;
510  break;
511  default:
512  (*m_log)<<" Warning: Undefined Reference Location in plotOptionalResults of "<<m_name<<std::endl;
513  (*m_log)<<" Interface or Undefined locations are not supported in VTU writing." <<std::endl;
514  return;
515  break;
516  }
517 
518  //check size of field and adjust missing values to zero for writing purposes only.
519  if(!data->completeMissingData(mpv_t())){
520  (*m_log) << " Warning: error during complete missing data to write in " << m_name << "; skip data" << std::endl;
521  continue;
522  }
523 
524  fields[ifield] = data->getDataAsVector();
525 
526  geometry->getPatch()->getVTK().addData(data->getName(), fieldtype, loc, fields[ifield]);
527 
528  ifield++;
529 
530  } // End loop on data fields
531 
532  write(geometry);
533 
534  //Loop on all input data to remove fields from vtk object
535  for (MimmoPiercedVector<mpv_t> * data : vdata)
536  {
537  geometry->getPatch()->getVTK().removeData(data->getName());
538  }
539 
540 }
541 
552 template<typename mpv_t, typename... Args>
553 void
555 {
556  // Instantiate fields as vector structure
557  std::vector<std::vector<mpv_t>> fields(vdata.size());
558 
559  //Deduce data type and add data to vtk object
560  bitpit::VTKFieldType fieldtype;
561  if (std::is_integral<mpv_t>::value == true || std::is_floating_point<mpv_t>::value == true){
562  fieldtype = bitpit::VTKFieldType::SCALAR;
563  }
564  else if (std::is_same<mpv_t, std::array<double,3>>::value == true){
565  fieldtype = bitpit::VTKFieldType::VECTOR;
566  }
567  else{
568  (*m_log) << " Warning: data type to write not allowed in " << m_name << "; skip vector of data fields" << std::endl;
569  write(geometry, args...);
570  return;
571  }
572 
573  //Loop on all input data
574  std::size_t ifield = 0;
575  for (MimmoPiercedVector<mpv_t> * data : vdata)
576  {
577 
578  //Check data with geometry linked
579  if (data->getGeometry() != geometry){
580  (*m_log) << " Warning: data to write not consistent with geometry in " << m_name << "; skip data" << std::endl;
581  continue;
582  }
583  if (geometry == nullptr){
584  (*m_log) << " Warning: geometry null during writing " << m_name << std::endl;
585  return;
586  }
587 
588  bitpit::VTKLocation loc = bitpit::VTKLocation::UNDEFINED;
589  switch(data->getDataLocation()){
590  case MPVLocation::POINT :
591  loc = bitpit::VTKLocation::POINT;
592  break;
593  case MPVLocation::CELL :
594  loc = bitpit::VTKLocation::CELL;
595  break;
596  default:
597  (*m_log)<<" Warning: Undefined Reference Location in plotOptionalResults of "<<m_name<<std::endl;
598  (*m_log)<<" Interface or Undefined locations are not supported in VTU writing." <<std::endl;
599  return;
600  break;
601  }
602 
603  //check size of field and adjust missing values to zero for writing purposes only.
604  if(!data->completeMissingData(mpv_t())){
605  (*m_log) << " Warning: error during complete missing data to write in " << m_name << "; skip data" << std::endl;
606  continue;
607  }
608 
609  fields[ifield] = data->getDataAsVector();
610 
611  geometry->getPatch()->getVTK().addData(data->getName(), fieldtype, loc, fields[ifield]);
612 
613  ifield++;
614 
615  } // End loop on data fields
616 
617  write(geometry, args...);
618 
619  //Loop on all input data to remove fields from vtk object
620  for (MimmoPiercedVector<mpv_t> * data : vdata)
621  {
622  geometry->getPatch()->getVTK().removeData(data->getName());
623  }
624 
625 }
626 
627 };
Class DataType defines the container and the type of data communicated by ports.
Definition: InOut.hpp:53
static PortManager & instance()
Definition: portManager.hpp:85
bool completeMissingData(const mpv_t &defValue)
bool createPortIn(T *var_, PortID portR, bool mandatory=false, int family=0)
MimmoPiercedVector is the basic data container for mimmo library.
MimmoSharedPointer< MimmoObject > getGeometry() const
PortOutT is the PIN class to exchange output data from an object to others.
Definition: InOut.hpp:155
void write(MimmoSharedPointer< MimmoObject > geometry)
InfoPort getPortData(const std::string name)
bool createPortOut(O *obj, T(O::*getVar_)(), PortID portS)
std::unordered_map< PortID, PortOut * > m_portOut
std::unordered_map< PortID, PortIn * > m_portIn
std::vector< mpv_t > getDataAsVector(bool ordered=false)
MimmoSharedPointer is a custom implementation of shared pointer.
PortInT is the PIN class to get input data arriving to an object from other objects.
Definition: InOut.hpp:264