CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes
ConstantStepOdeSolver Class Referenceabstract

#include <ConstantStepOdeSolver.h>

Inheritance diagram for ConstantStepOdeSolver:
EulerOdeSolver RK2 RK4

Public Member Functions

 ConstantStepOdeSolver ()
 
 ConstantStepOdeSolver (const AbsODERHS &rhs)
 
 ConstantStepOdeSolver (const ConstantStepOdeSolver &r)
 
double getCoordinate (const unsigned which, const unsigned idx) const
 
double getIntegrated (unsigned which, unsigned idx) const
 
double getPeakTime (unsigned which) const
 
const AbsODERHSgetRHS () const
 
AbsODERHSgetRHS ()
 
double getTime (const unsigned idx) const
 
double interpolateCoordinate (unsigned which, double t, bool cubic=false) const
 
double interpolateIntegrated (unsigned which, double t, bool cubic=false) const
 
double lastDeltaT () const
 
unsigned lastDim () const
 
double lastMaxT () const
 
unsigned lastRunLength () const
 
virtual const char * methodName () const =0
 
ConstantStepOdeSolveroperator= (const ConstantStepOdeSolver &r)
 
void run (const double *initialConditions, unsigned lenConditions, double dt, unsigned nSteps)
 
void setHistory (double dt, const double *data, unsigned dim, unsigned runLen)
 
void setRHS (const AbsODERHS &rhs)
 
void truncateCoordinate (unsigned which, double minValue, double maxValue)
 
void writeHistory (std::ostream &os, double dt, bool cubic=false) const
 
void writeIntegrated (std::ostream &os, unsigned which, double dt, bool cubic=false) const
 
virtual ~ConstantStepOdeSolver ()
 

Protected Attributes

AbsODERHSrhs_
 

Private Member Functions

void integrateCoordinate (const unsigned which)
 
virtual void step (double t, double dt, const double *x, unsigned lenX, double *coordIncrement) const =0
 

Private Attributes

std::vector< double > chargeBuffer_
 
unsigned dim_
 
double dt_
 
std::vector< double > historyBuffer_
 
unsigned lastIntegrated_
 
unsigned runLen_
 

Detailed Description

Definition at line 14 of file ConstantStepOdeSolver.h.

Constructor & Destructor Documentation

ConstantStepOdeSolver::ConstantStepOdeSolver ( )
inline
ConstantStepOdeSolver::ConstantStepOdeSolver ( const AbsODERHS rhs)
inline

Definition at line 18 of file ConstantStepOdeSolver.h.

References AbsODERHS::clone(), and rhs_.

19  : rhs_(nullptr), dt_(0.0), dim_(0), runLen_(0), lastIntegrated_(0) {
20  rhs_ = rhs.clone();
21  }
virtual AbsODERHS * clone() const =0
ConstantStepOdeSolver::ConstantStepOdeSolver ( const ConstantStepOdeSolver r)

Definition at line 116 of file ConstantStepOdeSolver.cc.

References AbsODERHS::clone(), and rhs_.

117  : rhs_(nullptr),
118  dt_(r.dt_),
119  dim_(r.dim_),
120  runLen_(r.runLen_),
124  if (r.rhs_)
125  rhs_ = r.rhs_->clone();
126 }
virtual AbsODERHS * clone() const =0
std::vector< double > chargeBuffer_
std::vector< double > historyBuffer_
virtual ConstantStepOdeSolver::~ConstantStepOdeSolver ( )
inlinevirtual

Definition at line 27 of file ConstantStepOdeSolver.h.

References rhs_.

27 { delete rhs_; }

Member Function Documentation

double ConstantStepOdeSolver::getCoordinate ( const unsigned  which,
const unsigned  idx 
) const
inline

Definition at line 49 of file ConstantStepOdeSolver.h.

References dim_, Exception, historyBuffer_, runLen_, and eostools::which().

Referenced by writeHistory().

49  {
50  if (which >= dim_ || idx >= runLen_)
51  throw cms::Exception("In ConstantStepOdeSolver::getCoordinate: index out of range");
52  return historyBuffer_[dim_ * idx + which];
53  }
def which
Definition: eostools.py:336
std::vector< double > historyBuffer_
double ConstantStepOdeSolver::getIntegrated ( unsigned  which,
unsigned  idx 
) const

Definition at line 35 of file ConstantStepOdeSolver.cc.

References chargeBuffer_, dim_, Exception, lastIntegrated_, and runLen_.

Referenced by writeIntegrated().

35  {
36  if (which >= dim_ || idx >= runLen_)
37  throw cms::Exception("In ConstantStepOdeSolver::getIntegrated: index out of range");
38  if (lastIntegrated_ != which)
39  (const_cast<ConstantStepOdeSolver*>(this))->integrateCoordinate(which);
40  return chargeBuffer_[idx];
41 }
def which
Definition: eostools.py:336
std::vector< double > chargeBuffer_
double ConstantStepOdeSolver::getPeakTime ( unsigned  which) const

Definition at line 9 of file ConstantStepOdeSolver.cc.

References dim_, dt_, Exception, historyBuffer_, mps_fire::i, cmsLHEtoEOSManager::l, alignCSCRings::r, runLen_, and eostools::which().

Referenced by QIE8Simulator::preampPeakTime().

9  {
10  if (which >= dim_)
11  throw cms::Exception("In ConstantStepOdeSolver::getPeakTime: index out of range");
12  if (runLen_ < 3)
13  throw cms::Exception("In ConstantStepOdeSolver::getPeakTime: not enough data");
14 
15  const double* hbuf = &historyBuffer_[which];
16  double maxval = hbuf[0];
17  unsigned maxind = 0;
18  for (unsigned i = 1; i < runLen_; ++i)
19  if (hbuf[dim_ * i] > maxval) {
20  maxval = hbuf[dim_ * i];
21  maxind = i;
22  }
23  if (maxind == 0U)
24  return 0.0;
25  if (maxind == runLen_ - 1U)
26  return dt_ * maxind;
27  const double l = hbuf[dim_ * (maxind - 1U)];
28  const double r = hbuf[dim_ * (maxind + 1U)];
29  if (l < maxval || r < maxval)
30  return dt_ * (maxind + (l - r) / 2.0 / (l + r - 2.0 * maxval));
31  else
32  return dt_ * maxind;
33 }
def which
Definition: eostools.py:336
std::vector< double > historyBuffer_
const AbsODERHS* ConstantStepOdeSolver::getRHS ( ) const
inline

Definition at line 34 of file ConstantStepOdeSolver.h.

References rhs_.

Referenced by QIE8Simulator::getRHS(), and QIE8Simulator::modifiableRHS().

34 { return rhs_; }
AbsODERHS* ConstantStepOdeSolver::getRHS ( )
inline

Definition at line 35 of file ConstantStepOdeSolver.h.

References rhs_.

35 { return rhs_; }
double ConstantStepOdeSolver::getTime ( const unsigned  idx) const
inline

Definition at line 43 of file ConstantStepOdeSolver.h.

References dt_, Exception, and runLen_.

Referenced by writeHistory(), and writeIntegrated().

43  {
44  if (idx >= runLen_)
45  throw cms::Exception("In ConstantStepOdeSolver::getTime: index out of range");
46  return idx * dt_;
47  }
void ConstantStepOdeSolver::integrateCoordinate ( const unsigned  which)
private

Definition at line 43 of file ConstantStepOdeSolver.cc.

References chargeBuffer_, dim_, dt_, Exception, historyBuffer_, mps_fire::i, lastIntegrated_, runLen_, and eostools::which().

43  {
44  if (runLen_ < 4)
45  throw cms::Exception("In ConstantStepOdeSolver::integrateCoordinate: not enough data");
46  if (chargeBuffer_.size() < runLen_)
47  chargeBuffer_.resize(runLen_);
48  double* integ = &chargeBuffer_[0];
49  const double* coord = &historyBuffer_[which];
50 
51  integ[0] = 0.0;
52  integ[1] = coord[dim_ * 0] * (3.0 / 8.0) + coord[dim_ * 1] * (19.0 / 24.0) + coord[dim_ * 2] * (-5.0 / 24.0) +
53  coord[dim_ * 3] * (1.0 / 24.0);
54  long double sum = integ[1];
55  const unsigned rlenm1 = runLen_ - 1U;
56  for (unsigned i = 2; i < rlenm1; ++i) {
57  sum += (coord[dim_ * (i - 2U)] * (-1.0 / 24.0) + coord[dim_ * (i - 1U)] * (13.0 / 24.0) +
58  coord[dim_ * i] * (13.0 / 24.0) + coord[dim_ * (i + 1U)] * (-1.0 / 24.0));
59  integ[i] = sum;
60  }
61  sum += (coord[dim_ * rlenm1] * (3.0 / 8.0) + coord[dim_ * (rlenm1 - 1U)] * (19.0 / 24.0) +
62  coord[dim_ * (rlenm1 - 2U)] * (-5.0 / 24.0) + coord[dim_ * (rlenm1 - 3U)] * (1.0 / 24.0));
63  integ[rlenm1] = sum;
64  if (dt_ != 1.0)
65  for (unsigned i = 1; i < runLen_; ++i)
66  integ[i] *= dt_;
68 }
def which
Definition: eostools.py:336
std::vector< double > chargeBuffer_
std::vector< double > historyBuffer_
double ConstantStepOdeSolver::interpolateCoordinate ( unsigned  which,
double  t,
bool  cubic = false 
) const

Definition at line 159 of file ConstantStepOdeSolver.cc.

References newFWLiteAna::base, dim_, dt_, Exception, historyBuffer_, interpolateLinear(), runLen_, eostools::which(), and x.

Referenced by QIE8Simulator::controlOutput(), QIE8Simulator::getCharge(), QIE8Simulator::preampOutput(), and writeHistory().

159  {
160  if (which >= dim_)
161  throw cms::Exception("In ConstantStepOdeSolver::interpolateCoordinate: index out of range");
162  if (runLen_ < 2U || (cubic && runLen_ < 4U))
163  throw cms::Exception("In ConstantStepOdeSolver::interpolateCoordinate: not enough data");
164  const double maxt = runLen_ ? dt_ * (runLen_ - 1U) : 0.0;
165  if (t < 0.0 || t > maxt)
166  throw cms::Exception("In ConstantStepOdeSolver::interpolateCoordinate: time out of range");
167 
168  const double* arr = &historyBuffer_[0];
169  if (t == 0.0)
170  return arr[which];
171  else if (t == maxt)
172  return arr[which + dim_ * (runLen_ - 1U)];
173 
174  // Translate time into timestep units
175  const double tSteps = t / dt_;
176  unsigned nLow = tSteps;
177  if (nLow >= runLen_ - 1)
178  nLow = runLen_ - 2;
179  double x = tSteps - nLow;
180 
181  if (cubic) {
182  unsigned i0 = 0;
183  if (nLow == runLen_ - 2) {
184  i0 = nLow - 2U;
185  x += 2.0;
186  } else if (nLow) {
187  i0 = nLow - 1U;
188  x += 1.0;
189  }
190  const double* base = arr + (which + dim_ * i0);
191  return interpolateLinear(x * (3.0 - x) / 2.0,
192  interpolateLinear(x / 3.0, base[0], base[dim_ * 3]),
193  interpolateLinear(x - 1.0, base[dim_], base[dim_ * 2]));
194  } else
195  return interpolateLinear(x, arr[which + dim_ * nLow], arr[which + dim_ * (nLow + 1U)]);
196 }
static double interpolateLinear(const double x, const double f0, const double f1)
tuple base
Main Program
Definition: newFWLiteAna.py:92
def which
Definition: eostools.py:336
std::vector< double > historyBuffer_
double ConstantStepOdeSolver::interpolateIntegrated ( unsigned  which,
double  t,
bool  cubic = false 
) const

Definition at line 198 of file ConstantStepOdeSolver.cc.

References newFWLiteAna::base, visDQMUpload::buf, chargeBuffer_, dim_, dt_, Exception, interpolateLinear(), lastIntegrated_, runLen_, and x.

Referenced by QIE8Simulator::getCharge(), and writeIntegrated().

198  {
199  if (which >= dim_)
200  throw cms::Exception("In ConstantStepOdeSolver::interpolateIntegrated: index out of range");
201  if (runLen_ < 2U || (cubic && runLen_ < 4U))
202  throw cms::Exception("In ConstantStepOdeSolver::interpolateIntegrated: not enough data");
203  const double maxt = runLen_ ? dt_ * (runLen_ - 1U) : 0.0;
204  if (t < 0.0 || t > maxt)
205  throw cms::Exception("In ConstantStepOdeSolver::interpolateIntegrated: time out of range");
206  if (lastIntegrated_ != which)
207  (const_cast<ConstantStepOdeSolver*>(this))->integrateCoordinate(which);
208 
209  const double* buf = &chargeBuffer_[0];
210  if (t == 0.0)
211  return buf[0];
212  else if (t == maxt)
213  return buf[runLen_ - 1U];
214 
215  // Translate time into timestep units
216  const double tSteps = t / dt_;
217  unsigned nLow = tSteps;
218  if (nLow >= runLen_ - 1)
219  nLow = runLen_ - 2;
220  double x = tSteps - nLow;
221 
222  if (cubic) {
223  unsigned i0 = 0;
224  if (nLow == runLen_ - 2) {
225  i0 = nLow - 2U;
226  x += 2.0;
227  } else if (nLow) {
228  i0 = nLow - 1U;
229  x += 1.0;
230  }
231  const double* base = buf + i0;
232  return interpolateLinear(x * (3.0 - x) / 2.0,
233  interpolateLinear(x / 3.0, base[0], base[3]),
234  interpolateLinear(x - 1.0, base[1], base[2]));
235  } else
236  return interpolateLinear(x, buf[nLow], buf[nLow + 1U]);
237 }
static double interpolateLinear(const double x, const double f0, const double f1)
tuple base
Main Program
Definition: newFWLiteAna.py:92
def which
Definition: eostools.py:336
std::vector< double > chargeBuffer_
double ConstantStepOdeSolver::lastDeltaT ( ) const
inline

Definition at line 40 of file ConstantStepOdeSolver.h.

References dt_.

Referenced by operator<<().

40 { return dt_; }
unsigned ConstantStepOdeSolver::lastDim ( ) const
inline

Definition at line 38 of file ConstantStepOdeSolver.h.

References dim_.

38 { return dim_; }
double ConstantStepOdeSolver::lastMaxT ( ) const
inline

Definition at line 41 of file ConstantStepOdeSolver.h.

References dt_, and runLen_.

Referenced by QIE8Simulator::lastStopTime(), QIE8Simulator::run(), writeHistory(), and writeIntegrated().

41 { return runLen_ ? dt_ * (runLen_ - 1U) : 0.0; }
unsigned ConstantStepOdeSolver::lastRunLength ( ) const
inline

Definition at line 39 of file ConstantStepOdeSolver.h.

References runLen_.

39 { return runLen_; }
virtual const char* ConstantStepOdeSolver::methodName ( ) const
pure virtual

Implemented in RK4, RK2, and EulerOdeSolver.

Referenced by writeHistory(), and writeIntegrated().

ConstantStepOdeSolver & ConstantStepOdeSolver::operator= ( const ConstantStepOdeSolver r)

Definition at line 128 of file ConstantStepOdeSolver.cc.

References chargeBuffer_, AbsODERHS::clone(), dim_, dt_, historyBuffer_, lastIntegrated_, rhs_, and runLen_.

128  {
129  if (this != &r) {
130  delete rhs_;
131  rhs_ = nullptr;
132  dt_ = r.dt_;
133  dim_ = r.dim_;
134  runLen_ = r.runLen_;
138  if (r.rhs_)
139  rhs_ = r.rhs_->clone();
140  }
141  return *this;
142 }
virtual AbsODERHS * clone() const =0
std::vector< double > chargeBuffer_
std::vector< double > historyBuffer_
void ConstantStepOdeSolver::run ( const double *  initialConditions,
unsigned  lenConditions,
double  dt,
unsigned  nSteps 
)

Definition at line 260 of file ConstantStepOdeSolver.cc.

References cms::cuda::assert(), dim_, dt, dt_, Exception, historyBuffer_, mps_fire::i, lastIntegrated_, GetRecoTauVFromDQM_MC_cff::next, rhs_, runLen_, step(), and submitPVValidationJobs::t.

Referenced by DTWorkflow.DTWorkflow::all(), Types.EventID::cppID(), Types.LuminosityBlockID::cppID(), o2olib.O2OTool::execute(), and QIE8Simulator::run().

263  {
264  if (!nSteps || !lenInitialConditions)
265  return;
266  if (!rhs_)
267  throw cms::Exception("In ConstantStepOdeSolver::run: ODE right hand side has not been set");
268  if (dt <= 0.0)
269  throw cms::Exception("In ConstantStepOdeSolver::run: can not run backwards in time");
270  assert(initialConditions);
271  dt_ = dt;
272  dim_ = lenInitialConditions;
274  runLen_ = nSteps + 1U;
275  const unsigned sz = (runLen_ + 1U) * dim_;
276  if (historyBuffer_.size() < sz)
277  historyBuffer_.resize(sz);
278  double* arr = &historyBuffer_[0];
279  for (unsigned i = 0; i < lenInitialConditions; ++i)
280  arr[i] = initialConditions[i];
281  double* stepBuffer = arr + runLen_ * dim_;
282 
283  for (unsigned i = 0; i < nSteps; ++i, arr += lenInitialConditions) {
284  const double t = i * dt;
285  this->step(t, dt, arr, lenInitialConditions, stepBuffer);
286  double* next = arr + lenInitialConditions;
287  for (unsigned i = 0; i < lenInitialConditions; ++i)
288  next[i] = arr[i] + stepBuffer[i];
289  }
290 }
float dt
Definition: AMPTWrapper.h:136
assert(be >=bs)
virtual void step(double t, double dt, const double *x, unsigned lenX, double *coordIncrement) const =0
std::vector< double > historyBuffer_
void ConstantStepOdeSolver::setHistory ( double  dt,
const double *  data,
unsigned  dim,
unsigned  runLen 
)

Definition at line 239 of file ConstantStepOdeSolver.cc.

References cms::cuda::assert(), dim_, dt, dt_, Exception, historyBuffer_, mps_fire::i, lastIntegrated_, and runLen_.

Referenced by QIE8Simulator::run().

239  {
240  const unsigned len = dim * runLen;
241  if (!len)
242  return;
243  if (dt <= 0.0)
244  throw cms::Exception("In ConstantStepOdeSolver::setHistory: can not run backwards in time");
245  assert(data);
246  const unsigned sz = dim * (runLen + 1U);
247  if (historyBuffer_.size() < sz)
248  historyBuffer_.resize(sz);
249  dt_ = dt;
250  dim_ = dim;
251  runLen_ = runLen;
253  double* arr = &historyBuffer_[0];
254  for (unsigned i = 0; i < len; ++i)
255  *arr++ = *data++;
256  for (unsigned i = 0; i < dim; ++i)
257  *arr++ = 0.0;
258 }
float dt
Definition: AMPTWrapper.h:136
assert(be >=bs)
std::vector< double > historyBuffer_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
void ConstantStepOdeSolver::setRHS ( const AbsODERHS rhs)
inline

Definition at line 30 of file ConstantStepOdeSolver.h.

References AbsODERHS::clone(), and rhs_.

Referenced by QIE8Simulator::setRHS().

30  {
31  delete rhs_;
32  rhs_ = rhs.clone();
33  }
virtual AbsODERHS * clone() const =0
virtual void ConstantStepOdeSolver::step ( double  t,
double  dt,
const double *  x,
unsigned  lenX,
double *  coordIncrement 
) const
privatepure virtual

Implemented in RK4, RK2, and EulerOdeSolver.

Referenced by run().

void ConstantStepOdeSolver::truncateCoordinate ( unsigned  which,
double  minValue,
double  maxValue 
)

Definition at line 144 of file ConstantStepOdeSolver.cc.

References visDQMUpload::buf, dim_, Exception, historyBuffer_, mps_fire::i, runLen_, and eostools::which().

Referenced by QIE8Simulator::run().

144  {
145  if (which >= dim_)
146  throw cms::Exception("In ConstantStepOdeSolver::truncateCoordinate: index out of range");
147  if (minValue > maxValue)
148  throw cms::Exception("In ConstantStepOdeSolver::truncateCoordinate: invalid truncation range");
149 
150  double* buf = &historyBuffer_[which];
151  for (unsigned i = 0; i < runLen_; ++i) {
152  if (buf[dim_ * i] < minValue)
153  buf[dim_ * i] = minValue;
154  else if (buf[dim_ * i] > maxValue)
155  buf[dim_ * i] = maxValue;
156  }
157 }
def which
Definition: eostools.py:336
std::vector< double > historyBuffer_
void ConstantStepOdeSolver::writeHistory ( std::ostream &  os,
double  dt,
bool  cubic = false 
) const

Definition at line 70 of file ConstantStepOdeSolver.cc.

References dim_, dt, dt_, getCoordinate(), getTime(), mps_fire::i, interpolateCoordinate(), lastMaxT(), methodName(), runLen_, submitPVValidationJobs::t, tmax, and eostools::which().

Referenced by operator<<().

70  {
71  os << "# " << this->methodName() << '\n';
72  if (dim_ && runLen_) {
73  if (dt == dt_) {
74  for (unsigned ipt = 0; ipt < runLen_; ++ipt) {
75  os << getTime(ipt);
76  for (unsigned which = 0; which < dim_; ++which)
77  os << ' ' << getCoordinate(which, ipt);
78  os << '\n';
79  }
80  } else {
81  const double tmax = lastMaxT();
82  for (unsigned i = 0;; ++i) {
83  const double t = i * dt;
84  if (t > tmax)
85  break;
86  os << t;
87  for (unsigned which = 0; which < dim_; ++which)
88  os << ' ' << interpolateCoordinate(which, t, cubic);
89  os << '\n';
90  }
91  }
92  }
93 }
double getCoordinate(const unsigned which, const unsigned idx) const
float dt
Definition: AMPTWrapper.h:136
def which
Definition: eostools.py:336
double getTime(const unsigned idx) const
virtual const char * methodName() const =0
static const double tmax[3]
double interpolateCoordinate(unsigned which, double t, bool cubic=false) const
void ConstantStepOdeSolver::writeIntegrated ( std::ostream &  os,
unsigned  which,
double  dt,
bool  cubic = false 
) const

Definition at line 95 of file ConstantStepOdeSolver.cc.

References dim_, dt, dt_, getIntegrated(), getTime(), mps_fire::i, interpolateIntegrated(), lastMaxT(), methodName(), runLen_, submitPVValidationJobs::t, and tmax.

98  {
99  os << "# " << this->methodName() << '\n';
100  if (dim_ && runLen_) {
101  if (dt == dt_) {
102  for (unsigned ipt = 0; ipt < runLen_; ++ipt)
103  os << getTime(ipt) << ' ' << getIntegrated(which, ipt) << '\n';
104  } else {
105  const double tmax = lastMaxT();
106  for (unsigned i = 0;; ++i) {
107  const double t = i * dt;
108  if (t > tmax)
109  break;
110  os << t << ' ' << interpolateIntegrated(which, t, cubic) << '\n';
111  }
112  }
113  }
114 }
float dt
Definition: AMPTWrapper.h:136
def which
Definition: eostools.py:336
double interpolateIntegrated(unsigned which, double t, bool cubic=false) const
double getTime(const unsigned idx) const
double getIntegrated(unsigned which, unsigned idx) const
virtual const char * methodName() const =0
static const double tmax[3]

Member Data Documentation

std::vector<double> ConstantStepOdeSolver::chargeBuffer_
private
unsigned ConstantStepOdeSolver::dim_
private
double ConstantStepOdeSolver::dt_
private
std::vector<double> ConstantStepOdeSolver::historyBuffer_
private
unsigned ConstantStepOdeSolver::lastIntegrated_
private
AbsODERHS* ConstantStepOdeSolver::rhs_
protected
unsigned ConstantStepOdeSolver::runLen_
private