MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
FixedStepFileDrive Class Reference

#include <fixedstep.h>

Inheritance diagram for FixedStepFileDrive:
Collaboration diagram for FixedStepFileDrive:

Public Member Functions

 FixedStepFileDrive (unsigned int uL, const DriveHandler *pDH, const char *const sFileName, integer is, integer id, doublereal t0, doublereal dt, bool bl, bool pz, Drive::Bailout bo)
 
virtual ~FixedStepFileDrive (void)
 
virtual std::ostream & Restart (std::ostream &out) const
 
virtual void ServePending (const doublereal &t)
 
- Public Member Functions inherited from FileDrive
 FileDrive (unsigned int uL, const DriveHandler *pDH, const std::string &s, integer nd, const std::vector< doublereal > &v0)
 
virtual ~FileDrive (void)
 
virtual Drive::Type GetDriveType (void) const
 
virtual integer iGetNumDrives (void) const
 
virtual doublereal dGet (const doublereal &t, int i=1) const
 
- Public Member Functions inherited from Drive
 Drive (unsigned int uL, const DriveHandler *pDH)
 
virtual ~Drive (void)
 
- Public Member Functions inherited from WithLabel
 WithLabel (unsigned int uL=0, const std::string &sN="")
 
virtual ~WithLabel (void)
 
void PutLabel (unsigned int uL)
 
void PutName (const std::string &sN)
 
unsigned int GetLabel (void) const
 
const std::string & GetName (void) const
 

Protected Attributes

doublereal dT0
 
doublereal dDT
 
integer iNumSteps
 
bool bLinear
 
bool bPadZeroes
 
Bailout boWhen
 
doublerealpd
 
doublereal ** pvd
 
- Protected Attributes inherited from FileDrive
std::string sFileName
 
integer iNumDrives
 
doublerealpdVal
 
- Protected Attributes inherited from Drive
const DriveHandlerpDrvHdl
 
- Protected Attributes inherited from WithLabel
unsigned int uLabel
 
std::string sName
 

Additional Inherited Members

- Public Types inherited from Drive
enum  Type { UNKNOWN = -1, FILEDRIVE = 0, LASTDRIVETYPE }
 
enum  Bailout { BO_NONE = 0x0, BO_UPPER = 0x1, BO_LOWER = 0x2, BO_ANY = (BO_UPPER | BO_LOWER) }
 
- Static Protected Attributes inherited from Drive
static doublereal dReturnValue = 0.
 

Detailed Description

Definition at line 41 of file fixedstep.h.

Constructor & Destructor Documentation

FixedStepFileDrive::FixedStepFileDrive ( unsigned int  uL,
const DriveHandler pDH,
const char *const  sFileName,
integer  is,
integer  id,
doublereal  t0,
doublereal  dt,
bool  bl,
bool  pz,
Drive::Bailout  bo 
)

Definition at line 48 of file fixedstep.cc.

References ASSERT, c, dDT, dFromFile, DriveHandler::dGetTime(), dT0, FIXED_BUFSIZE, FileDrive::iNumDrives, iNumSteps, MBDYN_EXCEPT_ARGS, pd, pvd, SAFENEWARR, ServePending(), and STRLENOF.

54 : FileDrive(uL, pDH, sFileName, ind, v0),
55 dT0(t0), dDT(dt), iNumSteps(ins),
56 bLinear(bl), bPadZeroes(pz), boWhen(bo), pd(0), pvd(0)
57 {
58  ASSERT(iNumDrives > 0);
59  ASSERT(sFileName != NULL);
60  ASSERT(dDT > 0.);
61 
62  std::ifstream in(sFileName);
63  if (!in) {
64  silent_cerr("FixedStepFileDrive(" << uL << "): "
65  "can't open file \"" << sFileName << "\""
66  << std::endl);
68  }
69 
70  /*
71  * Mangia gli eventuali commenti iniziali
72  */
73  unsigned uLineNo = 0;
74  char cTmp = '\0';
75  while (in.get(cTmp), cTmp == '#') {
76  // increase this to enlarge the size of the buffer
77 #define FIXED_BUFSIZE (8192)
78  char tmpbuf[FIXED_BUFSIZE];
79 
80  do {
81  in.getline(tmpbuf, sizeof(tmpbuf));
82  ++uLineNo;
83  if (in.fail()) {
84  silent_cerr("FixedStepFileDrive(" << uL << "): "
85  "can't get line " << uLineNo << " "
86  "from file \"" << sFileName << "\"" << std::endl);
88  }
89  int idx = 0;
90  while (isspace(tmpbuf[idx])) {
91  idx++;
92  }
93 
94  if (dT0 == dFromFile && strncasecmp(&tmpbuf[idx], "initial time:", STRLENOF("initial time:")) == 0) {
95  double d;
96  if (sscanf(&tmpbuf[idx + STRLENOF("initial time:")], "%le", &d) != 1) {
97  silent_cerr("FixedStepFileDrive(" << uL << "): "
98  "can't parse \"initial time\" line \"" << &tmpbuf[idx] << "\" "
99  "from file \"" << sFileName << "\"" << std::endl);
101  }
102  dT0 = d;
103 
104  } else if (dDT == dFromFile && strncasecmp(&tmpbuf[idx], "time step:", STRLENOF("time step:")) == 0) {
105  double d;
106  if (sscanf(&tmpbuf[idx + STRLENOF("time step:")], "%le", &d) != 1) {
107  silent_cerr("FixedStepFileDrive(" << uL << "): "
108  "can't parse \"time step\" line \"" << &tmpbuf[idx] << "\" "
109  "from file \"" << sFileName << "\"" << std::endl);
111  }
112  if (d <= 0.) {
113  silent_cerr("FixedStepFileDrive(" << uL << "): "
114  "invalid time step value " << d << " "
115  "in file \"" << sFileName << "\"" << std::endl);
117  }
118 
119  dDT = d;
120  }
121  } while (strlen(tmpbuf) == STRLENOF(tmpbuf) && tmpbuf[STRLENOF(tmpbuf)] != '\n');
122  }
123 
124  if (dT0 == dFromFile) {
125  silent_cerr("FixedStepFileDrive(" << uL << "): "
126  "expecting \"initial time\" line in file \"" << sFileName << "\""
127  << std::endl);
129  }
130 
131  if (dDT == dFromFile) {
132  silent_cerr("FixedStepFileDrive(" << uL << "): "
133  "expecting \"time step\" line in file \"" << sFileName << "\""
134  << std::endl);
136  }
137 
138  if (cTmp != '#') {
139  in.putback(cTmp);
140  }
141 
142  if (ins == -1) {
143  std::streampos pos = in.tellg();
144 
145  for (ins = 0; !in.eof(); ins++) {
146  char tmpbuf[FIXED_BUFSIZE];
147 
148  do {
149  in.getline(tmpbuf, sizeof(tmpbuf));
150  ++uLineNo;
151  if (in.eof()) {
152  break;
153  }
154 
155  if (in.fail()) {
156  silent_cerr("FixedStepFileDrive(" << uL << "): "
157  "can't get line " << uLineNo << " "
158  "from file \"" << sFileName << "\"" << std::endl);
160  }
161  } while (strlen(tmpbuf) == STRLENOF(tmpbuf) && tmpbuf[STRLENOF(tmpbuf)] != '\n');
162  }
163  iNumSteps = --ins;
164 
165  in.clear();
166  in.seekg(pos);
167 
168  silent_cout("FixedStepFileDrive(" << uL << "): "
169  "counted " << ins << " steps" << std::endl);
170  }
171 
174 
175  /* Attenzione: il primo puntatore e' vuoto
176  * (ne e' stato allocato uno in piu'),
177  * cosi' i drives possono essere numerati da 1 a n */
178  for (integer i = iNumDrives; i-- > 0; ) {
179  pvd[i + 1] = pd + i*iNumSteps;
180  }
181 
182  for (integer j = 0; j < iNumSteps; j++) {
183  for (integer i = 1; i <= iNumDrives; i++) {
184  in >> pvd[i][j];
185  if (in.eof()) {
186  silent_cerr("unexpected end of file '"
187  << sFileName << '\'' << std::endl);
189  }
190 
191  int c;
192  for (c = in.get(); isspace(c); c = in.get()) {
193  if (c == '\n') {
194  if (i != iNumDrives) {
195  silent_cerr("unexpected end of line #" << j + 1 << " (" << uLineNo << ") after channel #" << i << ", column #" << i << " of file '"
196  << sFileName << '\'' << std::endl);
198  }
199 
200  ++uLineNo;
201  break;
202  }
203  }
204 
205  if (i == iNumDrives && c != '\n') {
206  silent_cerr("missing end-of-line at line #" << j + 1 << " (" << uLineNo << ") of file '"
207  << sFileName << '\'' << std::endl);
209  }
210 
211  ++uLineNo;
212  in.putback(c);
213  }
214  }
215 
216  /* All data is available, so initialize the buffer accordingly */
217  ServePending(pDH->dGetTime());
218 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual void ServePending(const doublereal &t)
Definition: fixedstep.cc:236
std::string sFileName
Definition: filedrv.h:46
FileDrive(unsigned int uL, const DriveHandler *pDH, const std::string &s, integer nd, const std::vector< doublereal > &v0)
Definition: filedrv.cc:52
integer iNumDrives
Definition: filedrv.h:47
doublereal * pd
Definition: fixedstep.h:50
#define FIXED_BUFSIZE
static const doublereal dFromFile
Definition: fixedstep.cc:46
integer iNumSteps
Definition: fixedstep.h:45
#define ASSERT(expression)
Definition: colamd.c:977
doublereal dDT
Definition: fixedstep.h:44
doublereal dT0
Definition: fixedstep.h:43
static std::stack< cleanup * > c
Definition: cleanup.cc:59
#define STRLENOF(s)
Definition: mbdyn.h:166
doublereal dGetTime(void) const
Definition: drive.h:386
#define SAFENEWARR(pnt, item, sz)
Definition: mynewmem.h:701
doublereal ** pvd
Definition: fixedstep.h:51
static const std::vector< doublereal > v0
Definition: fixedstep.cc:45
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51

Here is the call graph for this function:

FixedStepFileDrive::~FixedStepFileDrive ( void  )
virtual

Definition at line 220 of file fixedstep.cc.

References pd, pvd, and SAFEDELETEARR.

221 {
222  SAFEDELETEARR(pd);
224 }
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
doublereal * pd
Definition: fixedstep.h:50
doublereal ** pvd
Definition: fixedstep.h:51

Member Function Documentation

std::ostream & FixedStepFileDrive::Restart ( std::ostream &  out) const
virtual

Implements FileDrive.

Definition at line 229 of file fixedstep.cc.

230 {
231  return out << "0. /* FixedStepFileDrive: not implemented yet! */"
232  << std::endl;
233 }
void FixedStepFileDrive::ServePending ( const doublereal t)
virtual

Implements Drive.

Definition at line 236 of file fixedstep.cc.

References bLinear, Drive::BO_LOWER, Drive::BO_UPPER, boWhen, bPadZeroes, dDT, dT0, FileDrive::iNumDrives, iNumSteps, MBDYN_EXCEPT_ARGS, FileDrive::pdVal, and pvd.

Referenced by FixedStepFileDrive().

237 {
238  doublereal tt = t - dT0;
239 
240  if (tt < 0) {
241  if (boWhen & Drive::BO_LOWER) {
242  throw Solver::EndOfSimulation(EXIT_SUCCESS,
244  "A fixed step file drive lower bound is halting the simulation");
245  }
246 
247  if (bPadZeroes) {
248  for (int i = 1; i <= iNumDrives; i++) {
249  pdVal[i] = 0.;
250  }
251 
252  } else {
253  for (int i = 1; i <= iNumDrives; i++) {
254  pdVal[i] = pvd[i][0];
255  }
256  }
257 
258  } else if (tt > dDT*(iNumSteps - 1)) {
259  if (boWhen & Drive::BO_UPPER) {
260  throw Solver::EndOfSimulation(EXIT_SUCCESS,
262  "A fixed step file drive upper bound is halting the simulation");
263  }
264 
265  if (bPadZeroes) {
266  for (int i = 1; i <= iNumDrives; i++) {
267  pdVal[i] = 0.;
268  }
269 
270  } else {
271  for (int i = 1; i <= iNumDrives; i++) {
272  pdVal[i] = pvd[i][iNumSteps - 1];
273  }
274  }
275 
276  } else {
277  integer j1 = integer(floor(tt/dDT));
278  if (bLinear) {
279  if (j1 == iNumSteps - 1) {
280  for (int i = 1; i <= iNumDrives; i++) {
281  pdVal[i] = pvd[i][j1];
282  }
283 
284  } else {
285  integer j2 = j1 + 1;
286  doublereal dt1 = dT0 + j1*dDT;
287  doublereal dt2 = dt1 + dDT;
288  doublereal dw1 = (dt2 - t)/dDT;
289  doublereal dw2 = (t - dt1)/dDT;
290 
291  for (int i = 1; i <= iNumDrives; i++) {
292  pdVal[i] = pvd[i][j2]*dw2 + pvd[i][j1]*dw1;
293  }
294  }
295 
296  } else {
297  for (int i = 1; i <= iNumDrives; i++) {
298  pdVal[i] = pvd[i][j1];
299  }
300  }
301  }
302 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
integer iNumDrives
Definition: filedrv.h:47
doublereal * pdVal
Definition: filedrv.h:48
integer iNumSteps
Definition: fixedstep.h:45
doublereal dDT
Definition: fixedstep.h:44
doublereal dT0
Definition: fixedstep.h:43
doublereal ** pvd
Definition: fixedstep.h:51
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51

Member Data Documentation

bool FixedStepFileDrive::bLinear
protected

Definition at line 46 of file fixedstep.h.

Referenced by ServePending().

Bailout FixedStepFileDrive::boWhen
protected

Definition at line 48 of file fixedstep.h.

Referenced by ServePending().

bool FixedStepFileDrive::bPadZeroes
protected

Definition at line 47 of file fixedstep.h.

Referenced by ServePending().

doublereal FixedStepFileDrive::dDT
protected

Definition at line 44 of file fixedstep.h.

Referenced by FixedStepFileDrive(), and ServePending().

doublereal FixedStepFileDrive::dT0
protected

Definition at line 43 of file fixedstep.h.

Referenced by FixedStepFileDrive(), and ServePending().

integer FixedStepFileDrive::iNumSteps
protected

Definition at line 45 of file fixedstep.h.

Referenced by FixedStepFileDrive(), and ServePending().

doublereal* FixedStepFileDrive::pd
protected

Definition at line 50 of file fixedstep.h.

Referenced by FixedStepFileDrive(), and ~FixedStepFileDrive().

doublereal** FixedStepFileDrive::pvd
protected

Definition at line 51 of file fixedstep.h.

Referenced by FixedStepFileDrive(), ServePending(), and ~FixedStepFileDrive().


The documentation for this class was generated from the following files: