00001
00012
00013
00014
00015
00016
00017
00018 #include "math.h"
00019 #include "mex.h"
00020
00021 #include "string.h"
00022
00023 #include "hydro_source/hydro_includes.h"
00024
00025 #include "CVODEincludes.h"
00026
00027
00028 #include "f.h"
00029
00030
00031 #ifndef PI_DEF
00032 #define pi 3.141592653589793
00033 #define PI 3.141592653589793
00034 #endif
00035
00036
00037
00038
00039 #define Ith(v,i) NV_Ith_S(v,i-1)
00040 #define IJth(A,i,j) DENSE_ELEM(A,i-1,j-1)
00041
00042
00043
00044 #define NEQ 12//18
00045 #define T0 RCONST(0.0)
00046
00047
00048
00049
00050
00051
00052
00053
00055
00063 static int f(realtype t, N_Vector y, N_Vector ydot, void *f_data)
00064 {
00065 datas *d;
00066 parameters *P;
00067 ODE_data *f_dat;
00068 double *state;
00069
00070 f_dat = (ODE_data*)f_data;
00071
00072 d = f_dat->d;
00073 P = f_dat->P;
00074 state = f_dat->state;
00075
00076
00077
00078 state[0] = Ith(y,1);
00079 state[1] = Ith(y,2);
00080 state[2] = Ith(y,3);
00081 state[3] = Ith(y,4);
00082 state[4] = Ith(y,5);
00083 state[5] = Ith(y,6);
00084 state[6] = Ith(y,7);
00085 state[7] = Ith(y,8);
00086 state[8] = Ith(y,9);
00087 state[9] = Ith(y,10);
00088 state[10] = Ith(y,11);
00089 state[11] = Ith(y,12);
00090
00091
00092
00093
00094
00095
00096
00097 data_update_state(d, state);
00098
00099 compute_state_dot(d,P,state);
00100
00101 Ith(ydot,1) = d->dx;
00102 Ith(ydot,2) = d->dy;
00103 Ith(ydot,3) = d->dz;
00104 Ith(ydot,4) = d->dphi;
00105 Ith(ydot,5) = d->dtheta;
00106 Ith(ydot,6) = d->dpsi;
00107 Ith(ydot,7) = d->ddq[0];
00108 Ith(ydot,8) = d->ddq[1];
00109 Ith(ydot,9) = d->ddq[2];
00110 Ith(ydot,10) = d->ddq[3];
00111 Ith(ydot,11) = d->ddq[4];
00112 Ith(ydot,12) = d->ddq[5];
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 return(0);
00142 }
00143
00144
00145
00146
00147
00149
00154 void solver_init(ODE_data* f_dat)
00155 {
00156 int k;
00157
00158 f_dat->y = NULL;
00159 f_dat->abstol = NULL;
00160 f_dat->cvode_mem = NULL;
00161
00162
00163 f_dat->y = N_VNew_Serial(NEQ);
00164 f_dat->abstol = N_VNew_Serial(NEQ);
00165
00166
00167 for (k=1; k<=NEQ; k++)
00168 {
00169 Ith(f_dat->y,k) = f_dat->state[k-1];
00170
00171 }
00172
00173
00174
00175 f_dat->reltol = f_dat->P->reltol;
00176
00177 for (k=1; k<=NEQ; k++)
00178 {
00179 Ith(f_dat->abstol,k) = f_dat->P->abstol[k-1];
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 f_dat->cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON);
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 f_dat->flag = CVodeMalloc(f_dat->cvode_mem, f, T0, f_dat->y, CV_SV, f_dat->reltol, f_dat->abstol);
00208
00209 f_dat->flag = CVodeSetFdata(f_dat->cvode_mem, (void*)f_dat);
00210
00211
00212
00213
00214
00215 f_dat->flag = CVDense(f_dat->cvode_mem, NEQ);
00216
00217
00218
00219
00220 }
00221
00222
00223
00224
00225
00226
00228
00233 void solve_ODE(ODE_data* f_dat, realtype tout)
00234 {
00235 int k, tempk;
00236 long ti, nst;
00237
00238
00239 f_dat->flag = CVode(f_dat->cvode_mem, tout, f_dat->y, &f_dat->d->t, CV_NORMAL);
00240
00241
00242 if (f_dat->time_param != 0.0) store_state(f_dat->d->t, f_dat->y);
00243
00244
00245 f_dat->flag = CVodeGetNumSteps(f_dat->cvode_mem, &nst);
00246 f_dat->CumulNumOfSteps = (double)nst;
00247 }
00248
00249
00251
00255 void solver_free(ODE_data* f_dat)
00256 {
00257
00258 N_VDestroy_Serial(f_dat->y);
00259
00260
00261 CVodeFree(&f_dat->cvode_mem);
00262 }