Example #1
0
static int CvodeData_allocateSens(cvodeData_t *data, int neq, int nsens)
{
  int i;
  ASSIGN_NEW_MEMORY_BLOCK(data->p, nsens, realtype, 0);
  ASSIGN_NEW_MEMORY_BLOCK(data->p_orig, nsens, realtype, 0);
  ASSIGN_NEW_MEMORY_BLOCK(data->sensitivity, neq, double *, 0);
  for ( i=0; i<neq; i++ )
    ASSIGN_NEW_MEMORY_BLOCK(data->sensitivity[i], nsens, double, 0);

  data->nsens = nsens;
  data->neq = neq;

  return 1;
}
/* creates CVODES forward sensitivity solver structures
   return 1 => success
   return 0 => failure
*/
int
IntegratorInstance_createCVODESSolverStructures(integratorInstance_t *engine)
{
    int i, j, flag, neq, ns;
    realtype *ydata, *abstoldata, *ySdata, *senstoldata;

    odeModel_t *om = engine->om;
    cvodeData_t *data = engine->data;
    cvodeSolver_t *solver = engine->solver;
    cvodeSettings_t *opt = engine->opt;

    /* realtype pbar[data->nsens+1]; */
    /*int *plist; removed by AMF 8/11/05
    realtype *pbar;

    ASSIGN_NEW_MEMORY_BLOCK(plist, data->nsens+1, int, 0)
    ASSIGN_NEW_MEMORY_BLOCK(pbar, data->nsens+1, realtype, 0)*/


    /*****  adding sensitivity specific structures ******/

    /**
     * construct sensitivity related structures
     */
    /* free sensitivity from former runs (changed for non-default cases!) */
    ODEModel_freeSensitivity(om);

    /* if jacobian matrix has been constructed successfully,
       construct sensitivity matrix dx/dp, sets om->sensitivity
       to 1 if successful, 0 otherwise */
    /*!!! this function will require additional input for
       non-default case, via sensitivity input settings! !!!*/

    if ( om->jacobian ) 
      ODEModel_constructSensitivity(om);
    else {
      om->sensitivity = 0;
      om->jacob_sens = NULL;
      om->nsens = om->nconst;

      ASSIGN_NEW_MEMORY_BLOCK(om->index_sens, om->nsens, int, 0);
      /*!!! non-default case:
	these values should be passed for other cases !!!*/
      for ( i=0; i<om->nsens; i++ )
	om->index_sens[i] = om->neq + om->nass + i;
    }
    
    engine->solver->nsens = data->nsens;

    solver->yS = N_VNewVectorArray_Serial(data->nsens, data->neq);      
    if (check_flag((void *)solver->yS, "N_VNewVectorArray_Serial",
		   1, stderr))
      return(0);

    /*
     * (re)initialize ySdata sensitivities
     */
    /* absolute tolerance for sensitivity error control */
    solver->senstol = N_VNew_Serial(data->nsens);
    abstoldata = NV_DATA_S(solver->senstol);
    for ( j=0; j<data->nsens; j++ ) {
      abstoldata[j] = 1e-4;
      ySdata = NV_DATA_S(solver->yS[j]);
      for ( i=0; i<data->neq; i++ ) 
	ySdata[i] = data->sensitivity[i][j];
    }

    /*
     * set method
     */
    if ( opt->SensMethod == 0 ) 
      flag =CVodeSensMalloc(solver->cvode_mem,data->nsens,
			    CV_SIMULTANEOUS, solver->yS);
    else if ( opt->SensMethod == 1 )
      flag = CVodeSensMalloc(solver->cvode_mem, data->nsens,
			     CV_STAGGERED, solver->yS);
    else if ( opt->SensMethod == 2 )
      flag = CVodeSensMalloc(solver->cvode_mem, data->nsens,
			     CV_STAGGERED1, solver->yS);
    if(check_flag(&flag, "CVodeSensMalloc", 1, stderr)) {
      return 0;
      /* ERROR HANDLING CODE if failes */
    }

    /* *** set parameter values or R.H.S function fS *****/
    /* NOTES: */
    /* !!! plist could later be used to specify requested parameters
       for sens.analysis !!! */
    
    /* was construction of Jacobian and
       parametric matrix successfull ? */
    if ( om->sensitivity && om->jacobian ) {
      flag = CVodeSetSensRhs1Fn(solver->cvode_mem, fS);
      if (check_flag(&flag, "CVodeSetSensRhs1Fn", 1, stderr)) {
	return 0;
	/* ERROR HANDLING CODE if failes */
      }
      flag = CVodeSetSensFdata(solver->cvode_mem, data);
      if (check_flag(&flag, "CVodeSetSensFdata", 1, stderr))  {
	return 0;
	/* ERROR HANDLING CODE if  failes */
      }  
      data->p = NULL;
    }
    else {
      ASSIGN_NEW_MEMORY_BLOCK(data->p, data->nsens, realtype, 0);
      for ( i=0; i<data->nsens; i++ ) {
        /* data->p is only required if R.H.S. fS cannot be supplied */
	/* plist[i] = i+1; */
	data->p[i] = data->value[om->index_sens[i]]; 
	/* pbar[i] = abs(data->p[i]);  */ /*??? WHAT IS PBAR ???*/ 
      }
      flag = CVodeSetSensParams(solver->cvode_mem, data->p, NULL, NULL);
      if (check_flag(&flag, "CVodeSetSensParams", 1, stderr))  {
	return 0;
	/* ERROR HANDLING CODE if  failes */
      }
      flag = CVodeSetSensRho(solver->cvode_mem, 0.0); /* what is it? */
      if (check_flag(&flag, "CVodeSetSensRhs1Fn", 1, stderr)) {
	/* ERROR HANDLING CODE if  failes */
	return 0;
      }
    }
/*     CVodeSetSensTolerances(solver->cvode_mem, CV_SS, */
/* 			   solver->reltol, &solver->senstol); */
    
    /* difference FALSE/TRUE ? */
    flag = CVodeSetSensErrCon(solver->cvode_mem, FALSE);
    if (check_flag(&flag, "CVodeSetSensFdata", 1, stderr)) {
      return 0;
      /* ERROR HANDLING CODE if failes */
    } 
    
    return 1; /* OK */
}
Example #3
0
time_series_t *read_data(char *file, int n_var, char **var)
{
    int i;
    char *name;

    int n_data;      /* number of relevant data columns */
    int *col;        /* positions of relevant columns in data file */
    int *index;      /* corresponding indices in variable list */

    int n_time;      /* number of data rows */

    time_series_t *ts;

    /* alloc mem */
    ASSIGN_NEW_MEMORY_BLOCK(ts, 1, time_series_t, NULL);

    /* alloc mem for index lists */
    ts->n_var = n_var;
    ASSIGN_NEW_MEMORY_BLOCK(ts->var,   n_var, char *,   NULL);
    ASSIGN_NEW_MEMORY_BLOCK(ts->data,  n_var, double *, NULL); 
    ASSIGN_NEW_MEMORY_BLOCK(ts->data2, n_var, double *, NULL);    

    /* initialize index lists */
    for ( i=0; i<n_var; i++ )
	{
	    ASSIGN_NEW_MEMORY_BLOCK(name, strlen(var[i])+1, char , NULL);
	    strcpy(name, var[i]);
	    ts->var[i]   = name;
	    ts->data[i]  = NULL;
	    ts->data2[i] = NULL;
	}

    /* alloc temp mem for column info */
    ASSIGN_NEW_MEMORY_BLOCK(col,   n_var, int, NULL);
    ASSIGN_NEW_MEMORY_BLOCK(index, n_var, int, NULL);

    /* read header line */
    n_data = read_header_line(file, n_var, var, col, index);
    ts->n_data = n_data;
	
    /* count number of lines */
    n_time = read_columns(file, 0, NULL, NULL, NULL);
    ts->n_time = n_time;

    /* alloc mem for data */
    for ( i=0; i<n_data; i++ )
	{
	    ASSIGN_NEW_MEMORY_BLOCK(ts->data[index[i]],  n_time, double, NULL);
	    ASSIGN_NEW_MEMORY_BLOCK(ts->data2[index[i]], n_time, double, NULL);
	}
    ASSIGN_NEW_MEMORY_BLOCK(ts->time,  n_time, double, NULL);

    /* read data */
    read_columns(file, n_data, col, index, ts);

    /* free temp mem */
    free(col);
    free(index);

    /* initialize interpolation type */
    ts->type = 3;
    /* calculate second derivatives */
    for ( i=0; i<n_var; i++ )
	if ( ts->data[i] != NULL )
	    {
		if ( spline(ts->n_time, ts->time, ts->data[i], ts->data2[i]) != 1 )
		    return NULL; /* ran out of memory during spline routine */
	    }

    ts->last = 0;
    
    /* alloc mem for warnings */
    ASSIGN_NEW_MEMORY_BLOCK(ts->mess, 2, char *, NULL);
    ASSIGN_NEW_MEMORY_BLOCK(ts->warn, 2, int,    NULL);   

    /* initialize warnings */
    ts->mess[0] = "argument out of range (left) ";
    ts->mess[1] = "argument out of range (right)";
    for ( i=0; i<2; i++ )
	ts->warn[i] = 0;  

    return ts;
    
}