COPASI API  4.16.103
Classes | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
CLayoutEngine Class Reference

#include <CLayoutEngine.h>

Collaboration diagram for CLayoutEngine:
Collaboration graph
[legend]

Classes

struct  Data
 

Public Member Functions

void calcForces (std::vector< double > &state, std::vector< double > &forces)
 
void calcRHS (std::vector< double > &state, double *rhs)
 
 CLayoutEngine (CAbstractLayoutInterface *l, bool so)
 
CAbstractLayoutInterfacegetLayoutInterface ()
 
double step ()
 

Protected Member Functions

void evalF (const C_FLOAT64 *t, const C_FLOAT64 *y, C_FLOAT64 *ydot)
 
void stepIntegration ()
 

Static Protected Member Functions

static void EvalF (const C_INT *n, const C_FLOAT64 *t, const C_FLOAT64 *y, C_FLOAT64 *ydot)
 

Protected Attributes

Data mData
 
std::vector< C_FLOAT64mDWork
 
std::ostringstream mErrorMsg
 
unsigned int mIndex
 
std::vector< C_INTmIWork
 
CLSODA mLSODA
 
C_INT mLsodaStatus
 
CAbstractLayoutInterfacempLayout
 
std::vector< double > mRhs
 
std::vector< double > mRhsA
 
std::vector< double > mRhsB
 
std::vector< double > mRhsC
 
bool mSecondOrder
 
C_FLOAT64 mTime
 
std::vector< double > mVar2
 
std::vector< double > mVariables
 

Detailed Description

Definition at line 16 of file CLayoutEngine.h.

Constructor & Destructor Documentation

CLayoutEngine::CLayoutEngine ( CAbstractLayoutInterface l,
bool  so 
)

Definition at line 13 of file CLayoutEngine.cpp.

References CLayoutEngine::Data::dim, CAbstractLayoutInterface::getInitialValues(), mData, mDWork, mErrorMsg, mIWork, mLSODA, mLsodaStatus, mpLayout, mRhs, mRhsA, mRhsB, mRhsC, mSecondOrder, mTime, mVar2, mVariables, CLayoutEngine::Data::pMethod, and CInternalSolver::setOstream().

14  : mpLayout(l),
15  mSecondOrder(false)
16 {
17  if (!mpLayout) return;
18 
20 
21  if (mSecondOrder) mVariables.resize(mVariables.size() * 2, 0);
22 
23  mRhs.resize(mVariables.size(), 0);
24 
25  //for RK4
26  mRhsA.resize(mVariables.size(), 0);
27  mRhsB.resize(mVariables.size(), 0);
28  mRhsC.resize(mVariables.size(), 0);
29  mVar2.resize(mVariables.size(), 0);
30 
31  //init LSODA
32  mLsodaStatus = 1;
33  //mState = 1;
34  //mJType = 2;
35  mErrorMsg.str("");
36 
37  mData.pMethod = this;
38  mData.dim = mVariables.size();
39  //mAtol = mpModel->initializeAtolVector(*mpAbsoluteTolerance, *mpReducedModel);
40  //mY = mpState->beginIndependent();
41 
42  //mRtol = *mpRelativeTolerance;
43 
44  mTime = 0;
45 
46  mDWork.resize(22 + mData.dim * std::max<C_INT>(16, mData.dim + 9) + 3 * 0);
47  mDWork[4] = mDWork[5] = mDWork[6] = mDWork[7] = mDWork[8] = mDWork[9] = 0.0;
48  mDWork[7] = 0; //minstep
49  mIWork.resize(20 + mData.dim);
50  mIWork[4] = mIWork[6] = mIWork[9] = 0;
51 
52  mIWork[5] = 100; //*mpMaxInternalSteps;
53  mIWork[7] = 12;
54  mIWork[8] = 5;
56 }
std::vector< double > mRhs
Definition: CLayoutEngine.h:47
CLayoutEngine * pMethod
Definition: CLayoutEngine.h:22
void setOstream(std::ostream &os)
std::vector< double > mRhsA
Definition: CLayoutEngine.h:50
std::vector< double > mVariables
Definition: CLayoutEngine.h:46
virtual const std::vector< double > & getInitialValues() const =0
std::ostringstream mErrorMsg
Definition: CLayoutEngine.h:74
std::vector< C_INT > mIWork
Definition: CLayoutEngine.h:69
C_FLOAT64 mTime
Definition: CLayoutEngine.h:90
CAbstractLayoutInterface * mpLayout
Definition: CLayoutEngine.h:42
std::vector< C_FLOAT64 > mDWork
Definition: CLayoutEngine.h:64
std::vector< double > mRhsB
Definition: CLayoutEngine.h:51
C_INT mLsodaStatus
Definition: CLayoutEngine.h:79
std::vector< double > mVar2
Definition: CLayoutEngine.h:53
std::vector< double > mRhsC
Definition: CLayoutEngine.h:52

Member Function Documentation

void CLayoutEngine::calcForces ( std::vector< double > &  state,
std::vector< double > &  forces 
)

Definition at line 80 of file CLayoutEngine.cpp.

References CAbstractLayoutInterface::getNumVariables(), CAbstractLayoutInterface::getPotential(), mpLayout, and CAbstractLayoutInterface::setState().

Referenced by calcRHS().

81 {
82  if (!mpLayout) return;
83 
84  //do numerical derivations...
85  mpLayout->setState(state);
86 
87  //assymmetric algorithm
88  /*double pot0 = mpLayout->getPotential();
89 
90  double store, newpot;
91  unsigned int i, imax = mpLayout->getNumVariables();
92  for (i=0; i<imax; ++i)
93  {
94  store = mVariables[i];
95  mVariables[i]+=1.0;
96  mpLayout->setState(mVariables);
97  newpot = mpLayout->getPotential();
98  mVariables[i] = store;
99  mForce[i] = pot0-newpot;
100  }*/
101 
102  //symmetric algorithm
103  double pot0;
104  double store, newpot;
105  unsigned int i, imax = mpLayout->getNumVariables();
106 
107  for (i = 0; i < imax; ++i)
108  {
109  store = state[i];
110  //std::cout << "var " << store;
111  state[i] -= 0.5;
112  mpLayout->setState(state);
113  pot0 = mpLayout->getPotential();
114  //std::cout << "pot0 " << pot0 << std::endl;
115  state[i] += 1.0;
116  mpLayout->setState(state);
117  newpot = mpLayout->getPotential();
118  state[i] = store;
119  forces[i] = pot0 - newpot;
120  //std::cout << "force " << i << " = " << forces[i] << std::endl;
121  }
122 }
CAbstractLayoutInterface * mpLayout
Definition: CLayoutEngine.h:42
virtual double getPotential()=0
unsigned int getNumVariables() const
virtual bool setState(const std::vector< double > &vars)=0
void CLayoutEngine::calcRHS ( std::vector< double > &  state,
double *  rhs 
)

Definition at line 58 of file CLayoutEngine.cpp.

References calcForces(), CAbstractLayoutInterface::getMassVector(), CAbstractLayoutInterface::getNumVariables(), mpLayout, and mSecondOrder.

Referenced by evalF(), step(), and stepIntegration().

59 {
60  std::vector<double> forces; forces.resize(mpLayout->getNumVariables());
61  calcForces(state, forces);
62 
63  unsigned int i, imax = mpLayout->getNumVariables();
64 
65  for (i = 0; i < imax; ++i)
66  {
67  if (mSecondOrder)
68  {
69  rhs[i + imax] = (forces[i] * 0.04 - state[i + imax] * 0.05) / mpLayout->getMassVector()[i];
70  rhs[i] = state[i + imax];
71  }
72  else
73  {
74  //first order
75  rhs[i] = forces[i] / mpLayout->getMassVector()[i];
76  }
77  }
78 }
virtual const std::vector< double > & getMassVector() const
CAbstractLayoutInterface * mpLayout
Definition: CLayoutEngine.h:42
unsigned int getNumVariables() const
void calcForces(std::vector< double > &state, std::vector< double > &forces)
void CLayoutEngine::EvalF ( const C_INT n,
const C_FLOAT64 t,
const C_FLOAT64 y,
C_FLOAT64 ydot 
)
staticprotected

Definition at line 254 of file CLayoutEngine.cpp.

References evalF(), and CLayoutEngine::Data::pMethod.

255 {static_cast<Data *>((void *) n)->pMethod->evalF(t, y, ydot);}
void CLayoutEngine::evalF ( const C_FLOAT64 t,
const C_FLOAT64 y,
C_FLOAT64 ydot 
)
protected

This evaluates the derivatives

Definition at line 257 of file CLayoutEngine.cpp.

References calcRHS(), and mVariables.

Referenced by EvalF().

258 {
259  //std::cout << *t << " " << y << " " << &mVariables[0] << std::endl;
260 
261 // assert(y == &mVariables[0]);
262 
263  calcRHS(mVariables, ydot);
264 
265  return;
266 }
std::vector< double > mVariables
Definition: CLayoutEngine.h:46
void calcRHS(std::vector< double > &state, double *rhs)
CAbstractLayoutInterface* CLayoutEngine::getLayoutInterface ( )
inline

Definition at line 32 of file CLayoutEngine.h.

References mpLayout.

33  {return mpLayout;};
CAbstractLayoutInterface * mpLayout
Definition: CLayoutEngine.h:42
double CLayoutEngine::step ( )

Definition at line 124 of file CLayoutEngine.cpp.

References calcRHS(), CAbstractLayoutInterface::getPotential(), mpLayout, mRhs, mVariables, and CAbstractLayoutInterface::setState().

Referenced by CQLayoutThread::run().

125 {
126  if (!mpLayout) return -1.0;
127 
128  unsigned int i, imax = mVariables.size();
129 
130  //Euler
131 
133  double pot = mpLayout->getPotential();
134 
135  calcRHS(mVariables, &mRhs[0]);
136 
137  double dt = 3;
138 
139  for (i = 0; i < imax; ++i)
140  {
141  mVariables[i] += mRhs[i] * dt;
142  }
143 
144  double newpot;
145 
146  for (;;)
147  {
149  newpot = mpLayout->getPotential();
150 
151  if (newpot < pot) break;
152 
153  if (dt < 1e-3) break;
154 
155  //step back
156  dt *= 0.5;
157 
158  for (i = 0; i < imax; ++i)
159  {
160  mVariables[i] -= mRhs[i] * dt;
161  }
162  }
163 
164  //std::cout << dt << " " << newpot << std::endl;
165  return newpot;
166 }
std::vector< double > mRhs
Definition: CLayoutEngine.h:47
std::vector< double > mVariables
Definition: CLayoutEngine.h:46
CAbstractLayoutInterface * mpLayout
Definition: CLayoutEngine.h:42
void calcRHS(std::vector< double > &state, double *rhs)
virtual double getPotential()=0
virtual bool setState(const std::vector< double > &vars)=0
void CLayoutEngine::stepIntegration ( )
protected

Definition at line 168 of file CLayoutEngine.cpp.

References calcRHS(), mpLayout, mRhs, mRhsA, mRhsB, mRhsC, mVar2, mVariables, and CAbstractLayoutInterface::setState().

169 {
170  if (!mpLayout) return;
171 
172  const double dt = 0.2;
173 
174  unsigned int i, imax = mVariables.size();
175 
176  //Euler
177  /*calcRHS(mVariables, &mRhs[0]);
178 
179  for (i = 0; i < imax; ++i)
180  {
181  mVariables[i] += mRhs[i] * 0.05;
182  }*/
183 
184  //RK4
185  calcRHS(mVariables, &mRhs[0]);
186 
187  for (i = 0; i < imax; ++i)
188  {
189  mVar2[i] = mVariables[i] + mRhs[i] * 0.5 * dt;
190  }
191 
192  calcRHS(mVar2, &mRhsA[0]);
193 
194  for (i = 0; i < imax; ++i)
195  {
196  mVar2[i] = mVariables[i] + mRhsA[i] * 0.5 * dt;
197  }
198 
199  calcRHS(mVar2, &mRhsB[0]);
200 
201  for (i = 0; i < imax; ++i)
202  {
203  mVar2[i] = mVariables[i] + mRhsB[i] * dt;
204  }
205 
206  calcRHS(mVar2, &mRhsC[0]);
207 
208  for (i = 0; i < imax; ++i)
209  {
210  mVariables[i] += dt / 6 * (mRhs[i] + 2 * mRhsA[i] + 2 * mRhsB[i] + mRhsC[i]);
211  }
212 
213  //LSODA
214  /*
215 
216  C_FLOAT64 EndTime = mTime + dt;
217  C_INT ITOL = 1; // mRtol scalar, mAtol scalar
218  C_INT one = 1;
219  long int two = 2;
220  long int three = 3;
221  double reltol = 0.1;
222  double abstol = 0.5;
223 
224  C_INT DSize = mDWork.size();
225  C_INT ISize = mIWork.size();
226 
227  //std::cout << "jhhjk" << &mVariables[0] << std::endl;
228 
229  mLSODA(&EvalF, // 1. evaluate F
230  &mData.dim, // 2. number of variables
231  &mVariables[0], // 3. the array of current concentrations
232  &mTime, // 4. the current time
233  &EndTime, // 5. the final time
234  &ITOL, // 6. error control
235  &reltol, // 7. relative tolerance array
236  &abstol, // 8. absolute tolerance array
237  &two, // 9. output by overshoot & interpolation
238  &mLsodaStatus, // 10. the state control variable
239  &one, // 11. further options (one)
240  &mDWork[0], // 12. the double work array
241  &DSize, // 13. the double work array size
242  &mIWork[0], // 14. the int work array
243  &ISize, // 15. the int work array size
244  NULL, // 16. evaluate J (not given)
245  &two); // 17. the type of jacobian calculate (2)
246 
247  if (mLsodaStatus != 2)
248  std::cout << mLsodaStatus << std::endl;
249  */
250 
252 }
std::vector< double > mRhs
Definition: CLayoutEngine.h:47
std::vector< double > mRhsA
Definition: CLayoutEngine.h:50
std::vector< double > mVariables
Definition: CLayoutEngine.h:46
CAbstractLayoutInterface * mpLayout
Definition: CLayoutEngine.h:42
void calcRHS(std::vector< double > &state, double *rhs)
std::vector< double > mRhsB
Definition: CLayoutEngine.h:51
virtual bool setState(const std::vector< double > &vars)=0
std::vector< double > mVar2
Definition: CLayoutEngine.h:53
std::vector< double > mRhsC
Definition: CLayoutEngine.h:52

Member Data Documentation

Data CLayoutEngine::mData
protected

mData.dim is the dimension of the ODE system. mData.pMethod contains CLsodaMethod * this to be used in the static method EvalF

Definition at line 85 of file CLayoutEngine.h.

Referenced by CLayoutEngine().

std::vector< C_FLOAT64 > CLayoutEngine::mDWork
protected

LSODA C_FLOAT64 work area

Definition at line 64 of file CLayoutEngine.h.

Referenced by CLayoutEngine().

std::ostringstream CLayoutEngine::mErrorMsg
protected

Stream to capture LSODA error messages

Definition at line 74 of file CLayoutEngine.h.

Referenced by CLayoutEngine().

unsigned int CLayoutEngine::mIndex
protected

Definition at line 44 of file CLayoutEngine.h.

std::vector< C_INT > CLayoutEngine::mIWork
protected

LSODA C_INT work area

Definition at line 69 of file CLayoutEngine.h.

Referenced by CLayoutEngine().

CLSODA CLayoutEngine::mLSODA
protected

Definition at line 59 of file CLayoutEngine.h.

Referenced by CLayoutEngine().

C_INT CLayoutEngine::mLsodaStatus
protected

LSODA state.

Definition at line 79 of file CLayoutEngine.h.

Referenced by CLayoutEngine().

CAbstractLayoutInterface* CLayoutEngine::mpLayout
protected
std::vector<double> CLayoutEngine::mRhs
protected

Definition at line 47 of file CLayoutEngine.h.

Referenced by CLayoutEngine(), step(), and stepIntegration().

std::vector<double> CLayoutEngine::mRhsA
protected

Definition at line 50 of file CLayoutEngine.h.

Referenced by CLayoutEngine(), and stepIntegration().

std::vector<double> CLayoutEngine::mRhsB
protected

Definition at line 51 of file CLayoutEngine.h.

Referenced by CLayoutEngine(), and stepIntegration().

std::vector<double> CLayoutEngine::mRhsC
protected

Definition at line 52 of file CLayoutEngine.h.

Referenced by CLayoutEngine(), and stepIntegration().

bool CLayoutEngine::mSecondOrder
protected

Definition at line 55 of file CLayoutEngine.h.

Referenced by calcRHS(), and CLayoutEngine().

C_FLOAT64 CLayoutEngine::mTime
protected

Current time.

Definition at line 90 of file CLayoutEngine.h.

Referenced by CLayoutEngine().

std::vector<double> CLayoutEngine::mVar2
protected

Definition at line 53 of file CLayoutEngine.h.

Referenced by CLayoutEngine(), and stepIntegration().

std::vector<double> CLayoutEngine::mVariables
protected

Definition at line 46 of file CLayoutEngine.h.

Referenced by CLayoutEngine(), evalF(), step(), and stepIntegration().


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