-
Notifications
You must be signed in to change notification settings - Fork 1
/
IsotonicContractionProcess.cpp
280 lines (213 loc) · 6.78 KB
/
IsotonicContractionProcess.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_roots.h>
#include <vector>
#include <libecs/libecs.hpp>
#include <libecs/Process.hpp>
#include <libecs/Stepper.hpp>
#include <libecs/FullID.hpp>
USE_LIBECS;
LIBECS_DM_CLASS( IsotonicContractionProcess, Process )
{
public:
LIBECS_DM_OBJECT( IsotonicContractionProcess, Process )
{
INHERIT_PROPERTIES( Process );
PROPERTYSLOT_SET_GET( Real, A );
PROPERTYSLOT_SET_GET( Real, L0 );
PROPERTYSLOT_SET_GET( Real, K );
PROPERTYSLOT_SET_GET( Real, Kl );
PROPERTYSLOT_SET_GET( Real, epsabs );
PROPERTYSLOT_SET_GET( Real, epsrel );
}
IsotonicContractionProcess()
:
A( 3.06e6 ),
L0( 0.97 ),
K( 140000.0 ),
Kl( 200.0 ),
epsabs( 0.0 ),
epsrel( 1.0e-6 )
{
// do nothing
}
SIMPLE_SET_GET_METHOD( Real, A );
SIMPLE_SET_GET_METHOD( Real, L0 );
SIMPLE_SET_GET_METHOD( Real, K );
SIMPLE_SET_GET_METHOD( Real, Kl );
SIMPLE_SET_GET_METHOD( Real, epsabs );
SIMPLE_SET_GET_METHOD( Real, epsrel );
virtual void initialize()
{
Process::initialize();
L = getVariableReference( "L" ).getVariable();
forceExt = getVariableReference( "forceExt" ).getVariable();
X = getVariableReference( "X" ).getVariable();
TCaCB = getVariableReference( "TCaCB" ).getVariable();
TCB = getVariableReference( "TCB" ).getVariable();
max_iter = 100;
// パラメータ内容を代入
params.push_back( TCaCB->getMolarConc() * 1000.0 );
params.push_back( TCB->getMolarConc() * 1000.0 );
params.push_back( X->getValue() );
params.push_back( forceExt->getValue() );
params.push_back( K );
params.push_back( Kl );
params.push_back( L0 );
params.push_back( A );
// FDFに関数、導関数、パラメータを指定する
FDF.f = &IsotonicContractionProcess::the_f;
FDF.df = &IsotonicContractionProcess::the_df;
FDF.fdf = &IsotonicContractionProcess::the_fdf;
FDF.params = ¶ms;
// 求根法のインスタンスsを生成する。simBioではsecant method
//T = gsl_root_fdfsolver_secant;
T = gsl_root_fdfsolver_steffenson;
//T = gsl_root_fdfsolver_newton;
s = gsl_root_fdfsolver_alloc( T );
}
virtual void fire();
protected:
Real A;
Real L0;
Real K;
Real Kl;
Real epsabs;
Real epsrel;
Variable* L;
Variable* forceExt;
Variable* X;
Variable* TCaCB;
Variable* TCB;
private:
int status;
int iter;
int max_iter;
Real x0;
Real x;
// Pointer of a derivative-based solver instance of type T
const gsl_root_fdfsolver_type *T;
gsl_root_fdfsolver *s;
// A general function with parameters and its first derivative.
gsl_function_fdf FDF;
// Defintion of parameters of the function.
std::vector< Real > params;
// Prototypes of the function to solve (f), its derivative (df), and set of f & df (fdf)
// They are set as static because their POINTERs will be called by a gsl_function_fdf instance.
static Real the_f (Real x, void *params);
static Real the_df (Real x, void *params);
static void the_fdf (Real x, void *params, Real *y, Real *dy);
};
LIBECS_DM_INIT( IsotonicContractionProcess, Process );
Real IsotonicContractionProcess::the_f (Real x, void *params)
{
std::vector< Real > *p = ( std::vector< Real > * ) params;
Real cTCaCB = p->at( 0 );
Real cTCB = p->at( 1 );
Real ex = p->at( 2 );
Real forceExt = p->at( 3 );
Real K = p->at( 4 );
Real Kl = p->at( 5 );
Real L0 = p->at( 6 );
Real A = p->at( 7 );
double strain = x - L0;
double dforceNonLinear = K * pow( strain, 4.0 );
double dforceB = A * (cTCaCB + cTCB);
return forceExt - dforceNonLinear * strain - Kl * strain - dforceB * (x - ex);
/*
double f = forceExt - dforceNonLinear * strain - Kl * strain - dforceB * (x - ex);
printf( "\nf = %1.12e", f );
return f;
*/
}
Real IsotonicContractionProcess::the_df (Real x, void *params)
{
std::vector< Real > *p = ( std::vector< Real > * ) params;
Real cTCaCB = p->at( 0 );
Real cTCB = p->at( 1 );
Real ex = p->at( 2 );
Real forceExt = p->at( 3 );
Real K = p->at( 4 );
Real Kl = p->at( 5 );
Real L0 = p->at( 6 );
Real A = p->at( 7 );
return - 5.0 * K * pow( x - L0, 4.0 ) - Kl - A * (cTCaCB + cTCB);
/*
double df = - 5.0 * K * pow( x - L0, 4.0 ) - Kl - A * (cTCaCB + cTCB);
printf( " df = %1.12e", df );
return df;
*/
}
void IsotonicContractionProcess::the_fdf (Real x, void *params, Real *y, Real *dy)
{
/*
*y = the_f( x, params);
*dy = the_df( x, params);
*/
std::vector< Real > *p = ( std::vector< Real > * ) params;
Real cTCaCB = p->at( 0 );
Real cTCB = p->at( 1 );
Real ex = p->at( 2 );
Real forceExt = p->at( 3 );
Real K = p->at( 4 );
Real Kl = p->at( 5 );
Real L0 = p->at( 6 );
Real A = p->at( 7 );
double strain = x - L0;
double dforceNonLinear = 5.0 * K * pow( strain, 4.0 );
double dforceB = A * (cTCaCB + cTCB);
*y = forceExt - dforceNonLinear / 5.0 * strain - Kl * strain - dforceB * (x - ex);
*dy = - dforceNonLinear - Kl - dforceB;
}
void IsotonicContractionProcess::fire()
{
iter = 0;
// パラメータ内容を代入
//params = t->getValue();
params[ 0 ] = TCaCB->getMolarConc() * 1000.0 ;
params[ 1 ] = TCB->getMolarConc() * 1000.0;
params[ 2 ] = X->getValue();
params[ 3 ] = forceExt->getValue();
x = L->getValue();
// x1に相当する値を計算してx0に格納
x0 = x - ( the_f( x, ¶ms ) / the_df( x, ¶ms ) );
// Secant法のインスタンスsを関数FDFに適用するために初期化し、探索点の初期値をxに設定する。
gsl_root_fdfsolver_set( s, &FDF, x );
/*
//すでに十分な解に達していた場合、求根を行わずに脱出
if ( gsl_root_test_delta ( x, x0, epsabs, epsrel ) == GSL_CONTINUE ) {
do
{
iter++;
// 求根法の繰り返し計算を1回実行。
printf( "x0 = %12.10f\n", x0 );
printf( "x = %12.10f\n", x );
printf( "dx = %e\n", x - x0 );
status = gsl_root_fdfsolver_iterate( s );
printf( "status: %d\n\n", status );
x0 = x;
x = gsl_root_fdfsolver_root( s );
status = gsl_root_test_delta ( x, x0, epsabs, epsrel );
}
while ( status == GSL_CONTINUE && iter < max_iter );
}
*/
for ( iter = 0; iter < max_iter; iter++ )
{
if ( gsl_root_test_delta ( x, x0, epsabs, epsrel ) == GSL_SUCCESS ) break;
// 求根法の繰り返し計算を1回実行。
//printf( "\nx0 = %12.10f", x0 );
//printf( "\nx = %12.10f", x );
//printf( "\ndx = %e", x - x0 );
status = gsl_root_fdfsolver_iterate( s );
if ( status ) {
if ( status == GSL_EBADFUNC ) printf( "gsl_root_fdfsolver_iterate: GSL_EBADFUNC" );
if ( status == GSL_EZERODIV ) printf( "gsl_root_fdfsolver_iterate: GSL_EZERODIV" );
}
//printf( "\nstatus: %d\n", status );
x0 = x;
x = gsl_root_fdfsolver_root( s );
}
L->setValue( x );
setActivity( x );
}