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

#include <varstep.h>

Inheritance diagram for VariableStepFileDrive:
Collaboration diagram for VariableStepFileDrive:

Public Member Functions

 VariableStepFileDrive (unsigned int uL, const DriveHandler *pDH, const char *const sFileName, integer id, bool bl, bool pz, Drive::Bailout bo)
 
virtual ~VariableStepFileDrive (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

integer iNumSteps
 
integer iCurrStep
 
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 varstep.h.

Constructor & Destructor Documentation

VariableStepFileDrive::VariableStepFileDrive ( unsigned int  uL,
const DriveHandler pDH,
const char *const  sFileName,
integer  id,
bool  bl,
bool  pz,
Drive::Bailout  bo 
)

Definition at line 48 of file varstep.cc.

References ASSERT, buf, c, DriveHandler::dGetTime(), iCurrStep, FileDrive::iNumDrives, iNumSteps, MBDYN_EXCEPT_ARGS, pd, pvd, SAFENEWARR, ServePending(), and STRLENOF.

52 : FileDrive(uL, pDH, sFileName, ind, v0),
53 iNumSteps(-1), iCurrStep(-1),
54 bLinear(bl), bPadZeroes(pz), boWhen(bo), pd(0), pvd(0)
55 {
56  ASSERT(iNumDrives > 0);
57  ASSERT(sFileName != NULL);
58 
59  std::ifstream in(sFileName);
60  if (!in) {
61  silent_cerr("can't open file \""
62  << sFileName << "\"" << std::endl);
64  }
65 
66  /*
67  * Mangia gli eventuali commenti iniziali
68  */
69  char c = '\0';
70  while (in.get(c), c == '#') {
71  char buf[1024];
72 
73  do {
74  in.getline(buf, sizeof(buf));
75  } while (strlen(buf) == STRLENOF(buf) && buf[STRLENOF(buf)] != '\n');
76  }
77 
78  if (c != '#') {
79  in.putback(c);
80  }
81 
82  if (iNumSteps == -1) {
83  std::streampos pos = in.tellg();
84 
85  integer ins;
86  for (ins = 0; !in.eof(); ins++) {
87  char buf[1024];
88 
89  do {
90  in.getline(buf, sizeof(buf));
91  } while (strlen(buf) == STRLENOF(buf) && buf[STRLENOF(buf)] != '\n');
92  }
93  iNumSteps = --ins;
94 
95  in.clear();
96  in.seekg(pos);
97 
98  silent_cout("VariableStepFileDrive(" << uL << "): "
99  "counted " << ins << " steps" << std::endl);
100  }
101 
104 
105  for (integer i = iNumDrives + 1; i-- > 0; ) {
106  pvd[i] = pd + i*iNumSteps;
107  }
108 
109  for (integer j = 0; j < iNumSteps; j++) {
110  // 0 -> iNumDrives to account for time
111  for (integer i = 0; i <= iNumDrives; i++) {
112  in >> pvd[i][j];
113  if (in.eof()) {
114  silent_cerr("unexpected end of file '"
115  << sFileName << '\'' << std::endl);
117  }
118 
119  int c;
120  for (c = in.get(); isspace(c); c = in.get()) {
121  if (c == '\n') {
122  if (i == 0) {
123  silent_cerr("unexpected end of line #" << j + 1 << " after time=" << pvd[0][j] << ", column #" << i + 1 << " of file '"
124  << sFileName << '\'' << std::endl);
126  }
127  if (i != iNumDrives) {
128  silent_cerr("unexpected end of line #" << j + 1 << ", time=" << pvd[0][j] << " after channel #" << i << ", column #" << i + 1 << " of file '"
129  << sFileName << '\'' << std::endl);
131  }
132  break;
133  }
134  }
135 
136  if (i == iNumDrives && c != '\n') {
137  silent_cerr("missing end-of-line at line #" << j + 1 << ", time=" << pvd[0][j] << " of file '"
138  << sFileName << '\'' << std::endl);
140  }
141 
142  in.putback(c);
143  }
144 
145  if (j > 0) {
146  if (pvd[0][j] <= pvd[0][j - 1]) {
147  silent_cerr("time[" << j << "]=" << pvd[0][j]
148  << " <= time[" << j - 1 << "]=" << pvd[0][j - 1]
149  << " in file '" << sFileName << "'" << std::endl);
151  }
152  }
153  }
154 
155  // All data is available, so initialize the buffer accordingly
156  // use bisection to initialize iCurrStep
157  doublereal dTime = pDH->dGetTime();
158  iCurrStep = bisec(pvd[0], dTime, 0, iNumSteps - 1);
159  if (iCurrStep < 0) {
160  iCurrStep++;
161  }
162 
163  ServePending(dTime);
164 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
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
virtual void ServePending(const doublereal &t)
Definition: varstep.cc:182
doublereal * pd
Definition: varstep.h:49
static const std::vector< doublereal > v0
Definition: varstep.cc:46
#define ASSERT(expression)
Definition: colamd.c:977
static std::stack< cleanup * > c
Definition: cleanup.cc:59
#define STRLENOF(s)
Definition: mbdyn.h:166
doublereal ** pvd
Definition: varstep.h:50
doublereal dGetTime(void) const
Definition: drive.h:386
#define SAFENEWARR(pnt, item, sz)
Definition: mynewmem.h:701
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51

Here is the call graph for this function:

VariableStepFileDrive::~VariableStepFileDrive ( void  )
virtual

Definition at line 166 of file varstep.cc.

References pd, pvd, and SAFEDELETEARR.

167 {
168  SAFEDELETEARR(pd);
170 }
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
doublereal * pd
Definition: varstep.h:49
doublereal ** pvd
Definition: varstep.h:50

Member Function Documentation

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

Implements FileDrive.

Definition at line 175 of file varstep.cc.

176 {
177  return out << "0. /* VariableStepFileDrive: not implemented yet! */"
178  << std::endl;
179 }
void VariableStepFileDrive::ServePending ( const doublereal t)
virtual

Implements Drive.

Definition at line 182 of file varstep.cc.

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

Referenced by VariableStepFileDrive().

183 {
184  if (t <= pvd[0][0]) {
185  if (t < pvd[0][0] && (boWhen & Drive::BO_LOWER)) {
186  throw Solver::EndOfSimulation(EXIT_SUCCESS,
188  "A variable step file drive lower bound is halting the simulation");
189  }
190 
191  if (bPadZeroes) {
192  for (int i = 1; i <= iNumDrives; i++) {
193  pdVal[i] = 0.;
194  }
195 
196  } else {
197  for (int i = 1; i <= iNumDrives; i++) {
198  pdVal[i] = pvd[i][0];
199  }
200  }
201 
202  } else if (t >= pvd[0][iNumSteps - 1]) {
203  if (t > pvd[0][iNumSteps - 1] && (boWhen & Drive::BO_UPPER)) {
204  throw Solver::EndOfSimulation(EXIT_SUCCESS,
206  "A variable step file drive upper bound is halting the simulation");
207  }
208 
209  if (bPadZeroes) {
210  for (int i = 1; i <= iNumDrives; i++) {
211  pdVal[i] = 0.;
212  }
213 
214  } else {
215  for (int i = 1; i <= iNumDrives; i++) {
216  pdVal[i] = pvd[i][iNumSteps - 1];
217  }
218  }
219 
220  } else {
221  // look for step exactly before
222  // note: use linear search, under the assumption
223  // that we always start from a relatively close guess
224  while (pvd[0][iCurrStep] > t) {
225  iCurrStep--;
226  }
227 
228  // no need to check for under/uverflow
229  ASSERT(iCurrStep >= 0);
230 
231  while (pvd[0][iCurrStep + 1] <= t) {
232  iCurrStep++;
233  }
235 
236  integer j1 = iCurrStep;
237  if (bLinear) {
238  integer j2 = j1 + 1;
239  doublereal dt1 = pvd[0][j1];
240  doublereal dt2 = pvd[0][j2];
241  doublereal dw1 = (dt2 - t)/(dt2 - dt1);
242  doublereal dw2 = (t - dt1)/(dt2 - dt1);
243 
244  for (int i = 1; i <= iNumDrives; i++) {
245  pdVal[i] = pvd[i][j2]*dw2 + pvd[i][j1]*dw1;
246  }
247 
248  } else {
249  for (int i = 1; i <= iNumDrives; i++) {
250  pdVal[i] = pvd[i][j1];
251  }
252  }
253  }
254 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
integer iNumDrives
Definition: filedrv.h:47
doublereal * pdVal
Definition: filedrv.h:48
#define ASSERT(expression)
Definition: colamd.c:977
doublereal ** pvd
Definition: varstep.h:50
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51

Member Data Documentation

bool VariableStepFileDrive::bLinear
protected

Definition at line 45 of file varstep.h.

Referenced by ServePending().

Bailout VariableStepFileDrive::boWhen
protected

Definition at line 47 of file varstep.h.

Referenced by ServePending().

bool VariableStepFileDrive::bPadZeroes
protected

Definition at line 46 of file varstep.h.

Referenced by ServePending().

integer VariableStepFileDrive::iCurrStep
protected

Definition at line 44 of file varstep.h.

Referenced by ServePending(), and VariableStepFileDrive().

integer VariableStepFileDrive::iNumSteps
protected

Definition at line 43 of file varstep.h.

Referenced by ServePending(), and VariableStepFileDrive().

doublereal* VariableStepFileDrive::pd
protected

Definition at line 49 of file varstep.h.

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

doublereal** VariableStepFileDrive::pvd
protected

Definition at line 50 of file varstep.h.

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


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