MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
extforce.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/extforce.h,v 1.30 2017/01/12 14:46:09 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 2007-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /* Forza */
33 
34 #ifndef EXTFORCE_H
35 #define EXTFORCE_H
36 
37 #include <vector>
38 #include <string>
39 
40 #include "mbsleep.h"
41 #include "force.h"
42 #include "converged.h"
43 
44 #ifdef USE_SOCKET
45 #include "usesock.h"
46 #endif // USE_SOCKET
47 #include "mbc.h"
48 
49 /* ExtFileHandlerBase - begin */
50 
52 public:
53  enum SendWhen {
57  };
58 
59  enum Negotiate {
63  };
64 
65  enum OutputFlags {
66  OUTPUT_KIN = 0x1000,
67  OUTPUT_DYN = 0x2000,
68  OUTPUT_REF = 0x4000,
69 
72  };
73 
74 protected:
75  std::streamsize Precision;
77  bool bOK;
78 
79 public:
81  virtual ~ExtFileHandlerBase(void);
82 
83  // NOTE: returns true if Prepare() must be called
84  virtual bool Prepare_pre(void) = 0;
85  // NOTE: returns true if peer must request negotiation during Prepare
86  virtual Negotiate NegotiateRequest(void) const;
87  virtual void Prepare_post(bool ok) = 0;
88 
89  virtual void AfterPredict(void) = 0;
90 
91  // NOTE: returns true if Send() must be called
92  virtual bool Send_pre(SendWhen when) = 0;
93  virtual void Send_post(SendWhen when) = 0;
94 
95  // NOTE: returns true if Recv() must be called
96  virtual bool Recv_pre(void) = 0;
97  // NOTE: returns true if converged
98  virtual bool Recv_post(void) = 0;
99 
100  virtual std::ostream *GetOutStream(void);
101  virtual std::istream *GetInStream(void);
102  virtual int GetOutFileDes(void);
103  virtual int GetSendFlags(void) const;
104  virtual int GetInFileDes(void);
105  virtual int GetRecvFlags(void) const;
106 };
107 
108 /* ExtFileHandlerBase - end */
109 
110 /* ExtFileHandler - begin */
111 
113 protected:
114  std::string fin, fout, tmpout;
116 
117  std::ifstream inf;
118  std::ofstream outf;
119 
120 public:
121  ExtFileHandler(std::string& fin,
122  bool bRemoveIn,
123  std::string& fout,
124  bool bNoClobberOut,
126  std::streamsize Precision);
127  virtual ~ExtFileHandler(void);
128 
129  virtual bool Prepare_pre(void);
130  virtual void Prepare_post(bool ok);
131 
132  virtual void AfterPredict(void);
133 
134  virtual bool Send_pre(SendWhen when);
135  virtual void Send_post(SendWhen when);
136 
137  virtual bool Recv_pre(void);
138  virtual bool Recv_post(void);
139 
140  virtual std::ostream *GetOutStream(void);
141  virtual std::istream *GetInStream(void);
142 };
143 
144 /* ExtFileHandler - end */
145 
146 /* ExtSocketHandler - begin */
147 
148 #ifdef USE_SOCKET
149 class ExtSocketHandler : public ExtFileHandlerBase {
150 protected:
151  UseSocket *pUS;
152  unsigned recv_flags;
153  unsigned send_flags;
154  bool bReadForces;
155  bool bLastReadForce;
156 
157  ESCmd u2cmd(unsigned u) const;
158  const char *cmd2str(ESCmd cmd) const;
159 
160 public:
161  ExtSocketHandler(UseSocket *pUS, mbsleep_t SleepTime,
162  int recv_flags, int send_flags);
163  virtual ~ExtSocketHandler(void);
164 
165  virtual bool Prepare_pre(void);
166  virtual Negotiate NegotiateRequest(void) const;
167  virtual void Prepare_post(bool ok);
168 
169  virtual void AfterPredict(void);
170 
171  virtual bool Send_pre(SendWhen when);
172  virtual void Send_post(SendWhen when);
173 
174  virtual bool Recv_pre(void);
175  virtual bool Recv_post(void);
176 
177  virtual int GetOutFileDes(void);
178  virtual int GetSendFlags(void) const;
179  virtual int GetInFileDes(void);
180  virtual int GetRecvFlags(void) const;
181 };
182 #endif // USE_SOCKET
183 
184 /* ExtSocketHandler - end */
185 
186 /* ExtFileHandlerEDGE moved to extedge.h, extedge.cc */
187 
188 /* ExtForce - begin */
189 
190 class ExtForce : virtual public Elem, public Force {
191 protected:
194 
195  // exchange after predict?
197 
198 public:
199  //-2: no coupling, only send kinematics
200  //-1: staggered, send kinematics at end of step, use forces at next step
201  // 0: loose coupling
202  // 1: tight coupling
203  // >1: exchange every iCoupling iterations
204  enum {
209  };
210 
211 protected:
213 
214  // iteration counter
215  mutable int iCouplingCounter;
216 
217  // whether the current residual is the first or not...
218  mutable bool bFirstSend;
219  mutable bool bFirstRecv;
220 
222  void Recv(void);
223 
224  virtual bool Prepare(ExtFileHandlerBase *pEFH) = 0;
225  virtual void Send(ExtFileHandlerBase *pEFH, ExtFileHandlerBase::SendWhen when) = 0;
226  virtual void Recv(ExtFileHandlerBase *pEFH) = 0;
227 
228 public:
229  /* Costruttore */
230  ExtForce(unsigned int uL,
231  DataManager *pDM,
233  bool bSendAfterPredict,
234  int iCoupling,
235  flag fOut);
236 
237  virtual ~ExtForce(void);
238 
239  virtual void SetValue(DataManager *pDM,
241  SimulationEntity::Hints* h = 0);
242  virtual void Update(const VectorHandler& XCurr,
243  const VectorHandler& XPrimeCurr);
244 
245  /*
246  * Elaborazione stato interno dopo la convergenza
247  */
248  virtual void AfterConvergence(const VectorHandler& X,
249  const VectorHandler& XP);
250 
251  virtual void AfterPredict(VectorHandler& X, VectorHandler& XP);
252 
253  virtual void
254  InitialWorkSpaceDim(integer* piNumRows, integer* piNumCols) const;
255 
256  /* Contributo allo jacobiano durante l'assemblaggio iniziale */
257  virtual VariableSubMatrixHandler&
259  const VectorHandler& XCurr);
260 
261  /* Contributo al residuo durante l'assemblaggio iniziale */
262  virtual SubVectorHandler&
264  const VectorHandler& XCurr);
265 };
266 
267 /* ExtForce - end */
268 
269 class DataManager;
270 class MBDynParser;
271 
272 extern void
274  MBDynParser& HP,
275  unsigned int uLabel,
276  ExtFileHandlerBase*& pEFH,
277  bool& bSendAfterPredict,
278  int& iCoupling);
279 
280 extern void
282  MBDynParser& HP,
283  unsigned int uLabel,
284  mbsleep_t& SleepTime,
285  std::streamsize& Precision);
286 
287 #endif // EXTFORCE_H
288 
ESCmd
Legal commands.
Definition: mbc.h:49
virtual bool Send_pre(SendWhen when)
Definition: extforce.cc:153
virtual std::ostream * GetOutStream(void)
Definition: extforce.cc:283
virtual SubVectorHandler & InitialAssRes(SubVectorHandler &WorkVec, const VectorHandler &XCurr)
Definition: extforce.cc:837
virtual std::istream * GetInStream(void)
Definition: extforce.cc:78
long int flag
Definition: mbdyn.h:43
virtual void AfterPredict(void)=0
virtual int GetInFileDes(void)
Definition: extforce.cc:99
virtual int GetOutFileDes(void)
Definition: extforce.cc:85
void Recv(void)
Definition: extforce.cc:798
virtual void Prepare_post(bool ok)
Definition: extforce.cc:141
virtual ~ExtForce(void)
Definition: extforce.cc:696
virtual bool Recv_post(void)
Definition: extforce.cc:257
virtual ~ExtFileHandlerBase(void)
Definition: extforce.cc:57
bool bSendAfterPredict
Definition: extforce.h:196
unsigned long mbsleep_t
Definition: mbsleep.h:51
ExtFileHandlerBase(mbsleep_t SleepTime, std::streamsize Precision)
Definition: extforce.cc:50
void ReadExtForce(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, ExtFileHandlerBase *&pEFH, bool &bSendAfterPredict, int &iCoupling)
Definition: extforce.cc:1141
Definition: force.h:46
std::string fin
Definition: extforce.h:114
Converged c
Definition: extforce.h:192
ExtFileHandler(std::string &fin, bool bRemoveIn, std::string &fout, bool bNoClobberOut, mbsleep_t SleepTime, std::streamsize Precision)
Definition: extforce.cc:116
std::vector< Hint * > Hints
Definition: simentity.h:89
virtual bool Recv_pre(void)=0
int iCouplingCounter
Definition: extforce.h:215
virtual std::istream * GetInStream(void)
Definition: extforce.cc:289
ExtForce(unsigned int uL, DataManager *pDM, ExtFileHandlerBase *pEFH, bool bSendAfterPredict, int iCoupling, flag fOut)
Definition: extforce.cc:677
virtual Negotiate NegotiateRequest(void) const
Definition: extforce.cc:63
virtual ~ExtFileHandler(void)
Definition: extforce.cc:129
virtual bool Prepare_pre(void)
Definition: extforce.cc:135
virtual bool Prepare(ExtFileHandlerBase *pEFH)=0
DataManager * pDM
Definition: mbpar.h:252
virtual bool Send_pre(SendWhen when)=0
void Send(ExtFileHandlerBase::SendWhen when)
Definition: extforce.cc:788
virtual int GetRecvFlags(void) const
Definition: extforce.cc:106
ExtFileHandlerBase * pEFH
Definition: extforce.h:193
virtual void SetValue(DataManager *pDM, VectorHandler &X, VectorHandler &XP, SimulationEntity::Hints *h=0)
Definition: extforce.cc:704
std::ifstream inf
Definition: extforce.h:117
bool bNoClobberOut
Definition: extforce.h:115
std::streamsize Precision
Definition: extforce.h:75
static const char * cmd2str(int cmd)
bool bFirstSend
Definition: extforce.h:218
void ReadExtFileParams(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, mbsleep_t &SleepTime, std::streamsize &Precision)
Definition: extforce.cc:845
std::string tmpout
Definition: extforce.h:114
std::ofstream outf
Definition: extforce.h:118
virtual std::ostream * GetOutStream(void)
Definition: extforce.cc:72
virtual bool Recv_post(void)=0
virtual void Send_post(SendWhen when)
Definition: extforce.cc:219
virtual bool Prepare_pre(void)=0
virtual void InitialWorkSpaceDim(integer *piNumRows, integer *piNumCols) const
Definition: extforce.cc:820
Definition: elem.h:75
bool bFirstRecv
Definition: extforce.h:219
virtual void Send_post(SendWhen when)=0
bool bRemoveIn
Definition: extforce.h:115
virtual bool Recv_pre(void)
Definition: extforce.cc:234
virtual void Prepare_post(bool ok)=0
virtual int GetSendFlags(void) const
Definition: extforce.cc:92
std::string fout
Definition: extforce.h:114
virtual void Update(const VectorHandler &XCurr, const VectorHandler &XPrimeCurr)
Definition: extforce.cc:726
mbsleep_t SleepTime
Definition: extforce.h:76
virtual void AfterPredict(void)
Definition: extforce.cc:147
int iCoupling
Definition: extforce.h:212
long int integer
Definition: colamd.c:51
virtual VariableSubMatrixHandler & InitialAssJac(VariableSubMatrixHandler &WorkMat, const VectorHandler &XCurr)
Definition: extforce.cc:828
virtual void AfterPredict(VectorHandler &X, VectorHandler &XP)
Definition: extforce.cc:751
virtual void AfterConvergence(const VectorHandler &X, const VectorHandler &XP)
Definition: extforce.cc:770