BaseManipulation.hpp
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 #ifndef __BASEMANIPULATION_HPP__
25 #define __BASEMANIPULATION_HPP__
26 
27 #include "MimmoNamespace.hpp"
28 #include "MimmoObject.hpp"
29 #include "InOut.hpp"
30 #include "MimmoPiercedVector.hpp"
31 
32 #include <factory.hpp>
33 #include <portManager.hpp>
34 
35 #include <bitpit_common.hpp>
36 
37 #include <string>
38 #include <functional>
39 #include <unordered_map>
40 #include <typeinfo>
41 #include <type_traits>
42 
43 #if MIMMO_ENABLE_MPI
44  #include <mpi.h>
45 #endif
46 
47 #if defined(_WIN32)
48  #define uint unsigned int
49 #endif
50 
51 namespace mimmo{
52 
103 
107  friend bool pin::addPin(BaseManipulation* objSend, BaseManipulation* objRec, PortID portS, PortID portR, bool forced);
111  friend void pin::removePin(BaseManipulation* objSend, BaseManipulation* objRec, PortID portS, PortID portR);
115  friend void pin::removeAllPins(BaseManipulation* objSend, BaseManipulation* objRec);
119  friend bool pin::checkCompatibility(BaseManipulation* objSend, BaseManipulation* objRec, PortID portS, PortID portR);
123  friend void PortOut::exec();
127  friend void mimmo::setLogger(std::string log);
131  friend void mimmo::setLoggerDirectory(std::string dir);
132 
133 public:
134  //type definitions
136  typedef std::unordered_map<BaseManipulation*, int> bmumap;
138  typedef std::string PortID;
140 protected:
141  uint m_priority;
142  std::string m_name;
143  int m_counter;
152  std::unordered_map<PortID, PortIn*> m_portIn;
153  std::unordered_map<PortID, PortOut*> m_portOut;
156  bool m_active;
157  bool m_execPlot;
158  bool m_apply;
159  std::string m_outputPlot;
161  bitpit::Logger* m_log;
163  //static members
166 #if MIMMO_ENABLE_MPI
167  int m_nprocs;
168  int m_rank;
169  MPI_Comm m_communicator;
170 #endif
171 
172 
173 public:
175  virtual ~BaseManipulation();
176 
177  BaseManipulation(const BaseManipulation & other);
179 
180  bitpit::Logger& getLog();
181 
182  bool arePortsBuilt();
183 
184  uint getPriority();
185  std::string getName();
188  int getNParent();
189  BaseManipulation* getParent(int i = 0);
190  bool isParent(BaseManipulation *, int&);
191  int getNChild();
192  BaseManipulation* getChild(int i = 0);
193  bool isChild(BaseManipulation *, int&);
195  int getNPortsIn();
196  int getNPortsOut();
197 
198 #if MIMMO_ENABLE_MPI
199  int getProcessorCount();
200  int getRank();
201  MPI_Comm & getCommunicator();
202 #endif
203 
204 
205  std::unordered_map<PortID, PortIn*> getPortsIn();
206  std::unordered_map<PortID, PortOut*>getPortsOut();
207 
208  bool isPlotInExecution();
209  bool isActive();
210  bool isApply();
211  int getId();
212 
213  void setLog(bitpit::Logger& log);
214  void setPriority(uint priority);
215  void setName(std::string name);
217  void setPlotInExecution(bool);
218  void setOutputPlot(std::string path);
219  void setId(int );
220  void setApply(bool flag = true);
221 
222  void activate();
223  void disable();
224 
225  void unsetGeometry();
226  void removePins();
227  void removePinsIn();
228  void removePinsOut();
229  void clear();
230 
231  void exec();
232 
233  virtual void absorbSectionXML(const bitpit::Config::Section & slotXML, std::string name = "");
234  virtual void flushSectionXML(bitpit::Config::Section & slotXML, std::string name= "");
235 
236  virtual std::vector<BaseManipulation*> getSubBlocksEmbedded();
237 
238 protected:
239 
240  void swap(BaseManipulation & x) noexcept;
241  void initializeLogger(bool logexists);
242 
247  virtual void buildPorts() = 0;
248  void deletePorts();
249 
250  template<typename T, typename O>
251  bool createPortOut(O* obj, T (O::*getVar_)(), PortID portS);
252 
253  template<typename T, typename O>
254  bool createPortOut(T* var_, PortID portS);
255 
256  template<typename T, typename O>
257  bool createPortIn(T* var_, PortID portR, bool mandatory = false, int family = 0);
258 
259  template<typename T, typename O>
260  bool createPortIn(O* obj, void (O::*setVar_)(T), PortID portR, bool mandatory = false, int family = 0);
261 
262  void setBufferIn(PortID port, mimmo::IBinaryStream& input);
263  void readBufferIn(PortID port);
264  void cleanBufferIn(PortID port);
265 
266  void addParent(BaseManipulation* parent);
267  void addChild(BaseManipulation* child);
268  void unsetParent(BaseManipulation * parent);
269  void unsetChild(BaseManipulation * child);
270 
271  PortID findPinIn(PortIn& pin);
272  PortID findPinOut(PortOut& pin);
273 
274  void addPinIn(BaseManipulation* objIn, PortID portR);
275  void addPinOut(BaseManipulation* objOut, PortID portS, PortID portR);
276 
277  void removePinIn(BaseManipulation* objIn, PortID portR);
278  void removePinOut(BaseManipulation* objOut, PortID portS);
279 
280  void removePinIn(PortID portR, int j);
281  void removePinOut(PortID portS, int j);
282 
286  virtual void execute() = 0;
287  virtual void plotOptionalResults();
288  virtual void apply();
289  void _apply(MimmoPiercedVector<darray3E> & displacements);
290 
291 
292  void write(MimmoSharedPointer<MimmoObject> geometry);
293 
294  template<typename mpv_t, typename... Args>
295  void write(MimmoSharedPointer<MimmoObject> geometry, MimmoPiercedVector<mpv_t> & data, Args ... args);
296 
297  template<typename mpv_t>
299 
300  template<typename mpv_t, typename... Args>
301  void write(MimmoSharedPointer<MimmoObject> geometry, std::vector<MimmoPiercedVector<mpv_t>> & data, Args ... args);
302 
303  template<typename mpv_t>
304  void write(MimmoSharedPointer<MimmoObject> geometry, std::vector<MimmoPiercedVector<mpv_t>> & data);
305 
306  template<typename mpv_t, typename... Args>
307  void write(MimmoSharedPointer<MimmoObject> geometry, std::vector<MimmoPiercedVector<mpv_t>*> & data, Args ... args);
308 
309  template<typename mpv_t>
310  void write(MimmoSharedPointer<MimmoObject> geometry, std::vector<MimmoPiercedVector<mpv_t>*> & data);
311 
312 };
313 
314 
315 };
316 
317 #include "BaseManipulation.tpp"
318 
319 
320 #endif /* __BASEMANIPULATION_HPP__ */
PortID findPinOut(PortOut &pin)
void setLogger(std::string log)
void addPinOut(BaseManipulation *objOut, PortID portS, PortID portR)
void removePin(BaseManipulation *objSend, BaseManipulation *objRec, PortID portS, PortID portR)
BaseManipulation * getChild(int i=0)
virtual void plotOptionalResults()
ConnectionType getConnectionType()
bool checkCompatibility(BaseManipulation *objSend, BaseManipulation *objRec, PortID portS, PortID portR)
void setLog(bitpit::Logger &log)
void addParent(BaseManipulation *parent)
void setName(std::string name)
virtual void execute()=0
void removePinIn(BaseManipulation *objIn, PortID portR)
std::unordered_map< PortID, PortOut * > getPortsOut()
void setBufferIn(PortID port, mimmo::IBinaryStream &input)
void setOutputPlot(std::string path)
bool createPortIn(T *var_, PortID portR, bool mandatory=false, int family=0)
BaseManipulation is the base class of any manipulation object of the library.
void addPinIn(BaseManipulation *objIn, PortID portR)
BaseManipulation & operator=(const BaseManipulation &other)
void unsetChild(BaseManipulation *child)
BaseManipulation * getParent(int i=0)
bitpit::Logger & getLog()
void exec()
Definition: InOut.cpp:168
ConnectionType
Type of allowed connections of the object: bidirectional, only input or only output.
void write(MimmoSharedPointer< MimmoObject > geometry)
virtual void absorbSectionXML(const bitpit::Config::Section &slotXML, std::string name="")
PortIn is the abstract PIN base class dedicated to carry data to a target class from other ones (inpu...
Definition: InOut.hpp:201
PortOut is the abstract PIN base class dedicated to exchange data from a target class to other ones (...
Definition: InOut.hpp:96
void addChild(BaseManipulation *child)
bool addPin(BaseManipulation *objSend, BaseManipulation *objRec, PortID portS, PortID portR, bool forced)
virtual void flushSectionXML(bitpit::Config::Section &slotXML, std::string name="")
virtual void buildPorts()=0
MimmoSharedPointer< MimmoObject > m_geometry
bool isParent(BaseManipulation *, int &)
void initializeLogger(bool logexists)
bool createPortOut(O *obj, T(O::*getVar_)(), PortID portS)
PortID findPinIn(PortIn &pin)
std::unordered_map< PortID, PortOut * > m_portOut
void cleanBufferIn(PortID port)
void readBufferIn(PortID port)
pin::ConnectionType ConnectionType
void _apply(MimmoPiercedVector< darray3E > &displacements)
void removePinOut(BaseManipulation *objOut, PortID portS)
void setGeometry(MimmoSharedPointer< MimmoObject > geometry)
mimmo custom derivation of bitpit IBinaryStream (see relative doc)
std::unordered_map< PortID, PortIn * > m_portIn
bool isChild(BaseManipulation *, int &)
std::unordered_map< PortID, PortIn * > getPortsIn()
void setPriority(uint priority)
MimmoSharedPointer is a custom implementation of shared pointer.
MimmoSharedPointer< MimmoObject > getGeometry()
void removeAllPins(BaseManipulation *objSend, BaseManipulation *objRec)
void setApply(bool flag=true)
MimmoSharedPointer< MimmoObject > & getGeometryReference()
void swap(BaseManipulation &x) noexcept
std::unordered_map< BaseManipulation *, int > bmumap
virtual std::vector< BaseManipulation * > getSubBlocksEmbedded()
void setLoggerDirectory(std::string dir)
void unsetParent(BaseManipulation *parent)