1313
1414static CuqdynConf * config = NULL ;
1515
16- CuqdynConf * create_cuqdyn_conf (Tolerances tolerances , OdeExpr ode_expr )
16+ CuqdynConf * create_cuqdyn_conf (Tolerances tolerances , OdeExpr ode_expr , StatesTransformer states_transformer )
1717{
1818 CuqdynConf * cuqdyn_conf = malloc (sizeof (CuqdynConf ));
1919 cuqdyn_conf -> tolerances = tolerances ;
2020 cuqdyn_conf -> ode_expr = ode_expr ;
21+ cuqdyn_conf -> states_transformer = states_transformer ;
2122 return cuqdyn_conf ;
2223}
2324
@@ -30,6 +31,7 @@ void destroy_cuqdyn_conf()
3031
3132 destroy_tolerances (config -> tolerances );
3233 destroy_ode_expr (config -> ode_expr );
34+ destroy_states_transformer (config -> states_transformer );
3335 free (config );
3436 config = NULL ;
3537}
@@ -39,8 +41,7 @@ CuqdynConf *init_cuqdyn_conf_from_file(const char *filename)
3941 CuqdynConf * tmp_config = malloc (sizeof (CuqdynConf ));
4042 if (tmp_config == NULL )
4143 {
42- fprintf (stderr , "ERROR: Memory allocation failed in function "
43- "init_cuqdyn_conf_from_file()\n" );
44+ fprintf (stderr , "ERROR: Memory allocation failed in function init_cuqdyn_conf_from_file()\n" );
4445 exit (1 );
4546 }
4647
@@ -53,7 +54,7 @@ CuqdynConf *init_cuqdyn_conf_from_file(const char *filename)
5354 }
5455
5556 config = tmp_config ;
56- mexpreval_init_wrapper (config -> ode_expr );
57+ mexpreval_init_wrapper (* config );
5758 return config ;
5859}
5960
@@ -71,10 +72,14 @@ int parse_cuqdyn_conf(const char *filename, CuqdynConf *config)
7172
7273 sunrealtype rtol = 1e-6 ;
7374 N_Vector atol = NULL ;
75+
7476 int y_count = 0 ;
7577 char * * odes = NULL ;
7678 int p_count = 0 ;
7779
80+ int obs_count = 0 ;
81+ char * * obs_tranformations = NULL ;
82+
7883 for (; cur ; cur = cur -> next )
7984 {
8085 if (cur -> type != XML_ELEMENT_NODE )
@@ -185,19 +190,61 @@ int parse_cuqdyn_conf(const char *filename, CuqdynConf *config)
185190 token = strtok (NULL , "\n" );
186191 }
187192 }
193+ else if (!xmlStrcmp (cur -> name , "states_transformer" ))
194+ {
195+ xmlChar * count_attr = xmlGetProp (cur , "count" );
196+ if (count_attr == NULL )
197+ {
198+ fprintf (stderr , "Error: <states_transformer> node does not have a 'count' attribute\n" );
199+ xmlFreeDoc (doc );
200+ return 1 ;
201+ }
202+
203+ obs_count = atoi ((char * ) count_attr );
204+ xmlFree (count_attr );
205+
206+ xmlChar * content = xmlNodeGetContent (cur );
207+ if (content == NULL )
208+ {
209+ fprintf (stderr , "Error: no content in <states_transformer> node\n" );
210+ xmlFreeDoc (doc );
211+ return 1 ;
212+ }
213+
214+ char * str = (char * ) content ;
215+
216+ char * token = strtok (str , "\n" );
217+
218+ obs_tranformations = calloc (y_count , sizeof (char * ));
219+
220+ int index = 0 ;
221+ while (token != NULL )
222+ {
223+ while (* token == ' ' )
224+ token ++ ;
225+
226+ if (* token != '\0' )
227+ {
228+ obs_tranformations [index ] = token ;
229+ index ++ ;
230+ }
231+ token = strtok (NULL , "\n" );
232+ }
233+ }
188234 }
189235
190236 config -> tolerances = create_tolerances (rtol , atol );
191237 config -> ode_expr = create_ode_expr (y_count , p_count , odes );
238+ config -> states_transformer = create_states_transformer (obs_count , obs_tranformations );
192239 xmlFreeDoc (doc );
193240 return 0 ;
194241}
195242
196- CuqdynConf * init_cuqdyn_conf (Tolerances tolerances , OdeExpr ode_expr )
243+ CuqdynConf * init_cuqdyn_conf (Tolerances tolerances , OdeExpr ode_expr , StatesTransformer states_transformer )
197244{
198245 destroy_cuqdyn_conf ();
199246
200- config = create_cuqdyn_conf (tolerances , ode_expr );
247+ config = create_cuqdyn_conf (tolerances , ode_expr , states_transformer );
201248 return config ;
202249}
203250
@@ -217,11 +264,13 @@ Tolerances create_tolerances(sunrealtype scalar_rtol, N_Vector atol)
217264{
218265 Tolerances tolerances ;
219266 tolerances .rtol = scalar_rtol ;
220- tolerances .atol = atol ;
267+ tolerances .atol_len = NV_LENGTH_S (atol );
268+ tolerances .atol = malloc (tolerances .atol_len * sizeof (double ));
269+ memcpy (tolerances .atol , NV_DATA_S (atol ), tolerances .atol_len * sizeof (double ));
221270 return tolerances ;
222271}
223272
224- void destroy_tolerances (Tolerances tolerances ) { N_VDestroy (tolerances .atol ); }
273+ void destroy_tolerances (Tolerances tolerances ) { free (tolerances .atol ); }
225274
226275OdeExpr create_ode_expr (int y_count , int p_count , char * * exprs )
227276{
@@ -233,3 +282,16 @@ OdeExpr create_ode_expr(int y_count, int p_count, char **exprs)
233282}
234283
235284void destroy_ode_expr (OdeExpr ode_expr ) {}
285+
286+ StatesTransformer create_states_transformer (int count , char * * exprs )
287+ {
288+ StatesTransformer states_transformer ;
289+ states_transformer .count = count ;
290+ states_transformer .exprs = exprs ;
291+ return states_transformer ;
292+ }
293+
294+ void destroy_states_transformer (StatesTransformer states_transformer )
295+ {
296+
297+ }
0 commit comments