1 |
#include "integrator.h" |
2 |
#include "integratorreporter.h" |
3 |
|
4 |
extern "C"{ |
5 |
#include <ascend/utilities/error.h> |
6 |
#include <ascend/integrator/integrator.h> |
7 |
} |
8 |
|
9 |
#include <vector> |
10 |
#include <stdexcept> |
11 |
#include <iostream> |
12 |
#include <iomanip> |
13 |
#include <iterator> |
14 |
#include <sstream> |
15 |
using namespace std; |
16 |
|
17 |
//--------------------------------------------- |
18 |
// NULL INTEGRATOR REPORTER (makes no output at all) |
19 |
|
20 |
IntegratorReporterNull::IntegratorReporterNull(Integrator *integrator) : IntegratorReporterCxx(integrator){ |
21 |
// nothing else |
22 |
} |
23 |
|
24 |
IntegratorReporterNull::~IntegratorReporterNull(){ |
25 |
// nothing else |
26 |
} |
27 |
|
28 |
int |
29 |
IntegratorReporterNull::initOutput(){ |
30 |
return 1; |
31 |
} |
32 |
|
33 |
int IntegratorReporterNull::closeOutput(){ |
34 |
return 1; |
35 |
} |
36 |
|
37 |
int IntegratorReporterNull::updateStatus(){ |
38 |
return 1; |
39 |
} |
40 |
|
41 |
int IntegratorReporterNull::recordObservedValues(){ |
42 |
return 1; |
43 |
} |
44 |
|
45 |
//------------------------------------------------------------------------------ |
46 |
// SIMPLE CONSOLE INTEGRATOR REPORTER |
47 |
|
48 |
IntegratorReporterConsole::IntegratorReporterConsole(Integrator *integrator) |
49 |
: IntegratorReporterCxx(integrator), f(cout){ |
50 |
// nothing else |
51 |
} |
52 |
|
53 |
IntegratorReporterConsole::~IntegratorReporterConsole(){ |
54 |
// nothing else |
55 |
} |
56 |
|
57 |
int |
58 |
IntegratorReporterConsole::initOutput(){ |
59 |
long nobs = integrator->getNumObservedVars(); |
60 |
stringstream ss; |
61 |
f << setw(11) << right << "t"; |
62 |
ss << setw(11)<< right << "-------"; |
63 |
for(long i=0; i<nobs; ++i){ |
64 |
Variable v = integrator->getObservedVariable(i); |
65 |
f << " " << setw(11) << right << v.getName(); |
66 |
ss<< " " << setw(11) << right << "-----------"; |
67 |
} |
68 |
f << endl; |
69 |
f << ss.str() << endl; |
70 |
return 1; |
71 |
} |
72 |
|
73 |
int IntegratorReporterConsole::closeOutput(){ |
74 |
return 1; |
75 |
} |
76 |
|
77 |
int IntegratorReporterConsole::updateStatus(){ |
78 |
return 1; |
79 |
} |
80 |
|
81 |
int IntegratorReporterConsole::recordObservedValues(){ |
82 |
IntegratorSystem *sys = integrator->getInternalType(); |
83 |
f << setw(11) << integrator_get_t(sys); |
84 |
vector<double> data(integrator->getNumObservedVars()); |
85 |
integrator_get_observations(sys,&data[0]); |
86 |
//copy(data.begin(),data.end(),ostream_iterator<double>(f,"\t")); |
87 |
for(vector<double>::iterator i=data.begin();i<data.end();++i){ |
88 |
f << " " << setw(11) << *i; |
89 |
} |
90 |
f << endl; |
91 |
return 1; |
92 |
} |
93 |
|
94 |
//---------------------------------------------------- |
95 |
// DEFAULT INTEGRATOR REPORTER (reporter start and end, outputs time at each step) |
96 |
|
97 |
IntegratorReporterCxx::IntegratorReporterCxx(Integrator *integrator){ |
98 |
// Initialise the C-API structure with flat function pointers |
99 |
reporter.init = &ascxx_integratorreporter_init; |
100 |
reporter.write = &ascxx_integratorreporter_write; |
101 |
reporter.write_obs = &ascxx_integratorreporter_write_obs; |
102 |
reporter.close = &ascxx_integratorreporter_close; |
103 |
this->integrator=integrator; |
104 |
} |
105 |
|
106 |
IntegratorReporterCxx::~IntegratorReporterCxx(){ |
107 |
// nothing, just virtual destructor |
108 |
CONSOLE_DEBUG("DESTROYING INTEGRATOR REPORTER CXX"); |
109 |
} |
110 |
|
111 |
IntegratorReporter * |
112 |
IntegratorReporterCxx::getInternalType(){ |
113 |
return &reporter; |
114 |
} |
115 |
|
116 |
int |
117 |
IntegratorReporterCxx::initOutput(){ |
118 |
return ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"Starting integration reporting..."); |
119 |
} |
120 |
|
121 |
int |
122 |
IntegratorReporterCxx::closeOutput(){ |
123 |
return ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"Closing integration reporting..."); |
124 |
} |
125 |
|
126 |
int |
127 |
IntegratorReporterCxx::updateStatus(){ |
128 |
double t = integrator->getCurrentTime(); |
129 |
return ERROR_REPORTER_NOLINE(ASC_USER_NOTE,"t = %f",t); |
130 |
} |
131 |
|
132 |
int |
133 |
IntegratorReporterCxx::recordObservedValues(){ |
134 |
// CONSOLE_DEBUG("..."); |
135 |
double *data = ASC_NEW_ARRAY(double,integrator->getNumObservedVars()); |
136 |
integrator_get_observations(integrator->getInternalType(),data); |
137 |
return 0; |
138 |
} |
139 |
|
140 |
Integrator * |
141 |
IntegratorReporterCxx::getIntegrator(){ |
142 |
return integrator; |
143 |
} |
144 |
|
145 |
int ascxx_integratorreporter_init(IntegratorSystem *blsys){ |
146 |
IntegratorReporterCxx *r = (IntegratorReporterCxx *)blsys->clientdata; |
147 |
if(r==NULL){ |
148 |
throw runtime_error("blsys->clientdata was null"); |
149 |
} |
150 |
return r->initOutput(); |
151 |
} |
152 |
|
153 |
int ascxx_integratorreporter_write(IntegratorSystem *blsys){ |
154 |
IntegratorReporterCxx *r = (IntegratorReporterCxx *)blsys->clientdata; |
155 |
return r->updateStatus(); |
156 |
} |
157 |
|
158 |
int ascxx_integratorreporter_write_obs(IntegratorSystem *blsys){ |
159 |
IntegratorReporterCxx *r = (IntegratorReporterCxx *)blsys->clientdata; |
160 |
return r->recordObservedValues(); |
161 |
} |
162 |
|
163 |
int ascxx_integratorreporter_close(IntegratorSystem *blsys){ |
164 |
IntegratorReporterCxx *r = (IntegratorReporterCxx *)blsys->clientdata; |
165 |
return r->closeOutput(); |
166 |
} |