Esempio n. 1
0
/** C.0: Loads, validates and parses an SBML file,
    also converts SBML level 1 to level 2 files,
    return NULL if errors were encountered during libSBML
    validation and consistency check, stores libSBML warngings
*/
SBMLDocument_t *
parseModel(char *file, int printMessage, int validate, char *schemaPath,
	   char *schema11FileName,
	   char *schema12FileName,
	   char *schema21FileName)
{
    unsigned int i, errors ;
    SBMLDocument_t *d;
    SBMLDocument_t *d2;
    SBMLReader_t *sr;

    if ( validate ) {
        if (  printMessage ) {
            fprintf(stderr, "Validating SBML.\n");
            fprintf(stderr, "This can take a while for SBML level 2.\n");
        }
        sr = newSBMLReader(schemaPath,
			   schema11FileName,
			   schema12FileName,
			   schema21FileName);
    }
    else {
        sr = SBMLReader_create();
    }

    d = SBMLReader_readSBML(sr, file);
    SBMLReader_free(sr);
    
    errors = 0;
    if ( validate ) {
      errors = SBMLDocument_getNumFatals(d) + SBMLDocument_getNumErrors(d);
    }
    
    if (SBMLDocument_getNumFatals(d) + SBMLDocument_getNumErrors(d) == 0)
        SBMLDocument_checkConsistency(d);
        /* AMF 9th Dec 2005 added back because inconsistent models can cause the
           solver to crash despite the consistancy check causing memory leaks - talk to Ben B! */

    /* check for warnings and errors */
    for (i =0 ; i != SBMLDocument_getNumWarnings(d); i++)
        storeSBMLError(WARNING_ERROR_TYPE, SBMLDocument_getWarning(d, i)); 

    for (i =0 ; i != SBMLDocument_getNumErrors(d); i++)
        storeSBMLError(ERROR_ERROR_TYPE, SBMLDocument_getError(d, i)); 

    for (i =0 ; i != SBMLDocument_getNumFatals(d); i++)
        storeSBMLError(FATAL_ERROR_TYPE, SBMLDocument_getFatal(d, i)); 

    RETURN_ON_ERRORS_WITH(NULL);
    
    /* convert level 1 models to level 2 */
    if ( SBMLDocument_getLevel(d) == 1 ) {      
        d2 = convertModel(d);
        SBMLDocument_free(d);
        if ( printMessage )
            fprintf(stderr, "SBML converted from level 1 to level 2.\n"); 
        return (d2);
    }
    return (d);
}
Esempio n. 2
0
SBML_ODESOLVER_API odeModel_t *ODEModel_create(Model_t *m)
{
  Model_t *ode;
  odeModel_t *om;
 
  ode = Model_reduceToOdes(m);
  RETURN_ON_ERRORS_WITH(NULL);

  om = ODEModel_fillStructures(ode);
  /* Errors will cause the program to stop, e.g. when some
      mathematical expressions are missing.  */
  RETURN_ON_ERRORS_WITH(NULL);
  
  om->m = m;
  om->d = NULL; /* will be set if created from file */
    
  return om;
}
Esempio n. 3
0
int doit(int argc, char *argv[])
{
  double i, j ;
  cvodeSettings_t *settings = CvodeSettings_create();
  variableIndex_t *speciesVI, *parameterVI, *parameter2VI;
  integratorInstance_t *integratorInstance;
  char *modelStr, *parameterStr, *parameter2Str, *speciesStr;
  double parameter, parameterEnd, parameterStepSize,
    parameter2, parameter2End, parameter2StepSize,
    errorTolerance, relativeErrorTolerance, endtime, initCond, maxDiff, diff;
  int maximumIntegrationSteps;
  odeModel_t *model ;
        
  if (argc < 11)
  {
    fprintf(
            stderr,
            "usage %s sbml-model variable parameter1 parameter1-start parameter1-end parameter1-step parameter2 parameter2-start parameter2-end parameter2-step [endtime] [error-tolerance] [relative-error-tolerance] [maximum integration steps]\n",
            argv[0]);

    exit(0);
  }

  modelStr = argv[1];
  speciesStr = argv[2];

  parameterStr = argv[3];
  parameter = atof(argv[4]);
  parameterEnd = atof(argv[5]);
  parameterStepSize = atof(argv[6]);
  
  parameter2Str = argv[7];
  parameter2 = atof(argv[8]);
  parameter2End = atof(argv[9]);
  parameter2StepSize = atof(argv[10]);

  if (argc > 12)    
    endtime = atof(argv[11]);
  else 
    endtime = 10000;

  if (argc > 13)
    errorTolerance = atof(argv[12]);
  else
    errorTolerance = 1e-6;

  if (argc > 14)
    relativeErrorTolerance = atof(argv[13]);
  else
    relativeErrorTolerance = 1e-4;

  if (argc > 15)
    maximumIntegrationSteps = atoi(argv[14]);
  else
    maximumIntegrationSteps = 1e9;

  model = ODEModel_createFromFile(modelStr);
  RETURN_ON_ERRORS_WITH(1);

  speciesVI = ODEModel_getVariableIndex(model, speciesStr);
  parameterVI = ODEModel_getVariableIndex(model, parameterStr);
  parameter2VI = ODEModel_getVariableIndex(model, parameter2Str);
  RETURN_ON_ERRORS_WITH(1);

  /* integrate until steady state */
  if ( endtime == 0 )
  {
    /* stop integration upon a steady state */
    CvodeSettings_setIndefinitely(settings, 1);
    CvodeSettings_setSteadyStateThreshold(settings, 1e-9);
    CvodeSettings_setHaltOnSteadyState(settings, 1);
    CvodeSettings_setTime(settings, 1, 1);
  }
  else
  {
    CvodeSettings_setHaltOnSteadyState(settings, 0);
    CvodeSettings_setIndefinitely(settings, 0);
    CvodeSettings_setTime(settings, endtime, 1000);
  }

  /* absolute tolerance in Cvode integration */
  CvodeSettings_setError(settings, errorTolerance);
  /* relative tolerance in Cvode integration */
  CvodeSettings_setRError(settings, relativeErrorTolerance);
  /* maximum step number for CVode integration */
  CvodeSettings_setMxstep(settings, maximumIntegrationSteps);
  /* doesn't stop integration upon an event */
  CvodeSettings_setHaltOnEvent(settings, 0);      
  /* don't Store time course history */
  CvodeSettings_setStoreResults(settings, 0);
  /* compile model */ 
  CvodeSettings_setCompileFunctions(settings, 1); 

    
  integratorInstance = IntegratorInstance_create(model, settings);
    
  printf("set xlabel '%s'\n", ODEModel_getVariableName(model, parameterVI));
  printf("set ylabel '%s'\n", ODEModel_getVariableName(model, parameter2VI));
  printf("splot '-' using 1:2:3 title '%s' with points pointtype 1 pointsize 1 palette\n",
	 ODEModel_getVariableName(model, speciesVI) );


  /* remember initial condition of observed variable */
  initCond = IntegratorInstance_getVariableValue(integratorInstance, speciesVI);
  maxDiff = 0.0;

  
  int error = 0 ;
  int run = 0;
  for ( run=0; run<2; run++ )
  {
    for (i = parameter; i <= parameterEnd; i += parameterStepSize)
    {
      for (j = parameter2; j <= parameter2End; j += parameter2StepSize)
      {
	/* add fourth parameter here */

	IntegratorInstance_reset( integratorInstance);
	RETURN_ON_ERRORS_WITH(1);

	/* for the second run reset the initial condition of the
	   observed variable to a multiple of the maximum observed
	   difference of its value wrt to its initial condition */
	if ( run == 1 )
	  IntegratorInstance_setVariableValue(integratorInstance,
					      speciesVI,
					      fabs(5*(initCond-maxDiff)));
	
	IntegratorInstance_setVariableValue(integratorInstance,
					  parameterVI, i);
	IntegratorInstance_setVariableValue(integratorInstance,
					    parameter2VI, j);
        /* printf("ic %g\t", IntegratorInstance_getVariableValue(integratorInstance, speciesVI)); */

	while(!IntegratorInstance_checkSteadyState(integratorInstance) &&
	      !IntegratorInstance_timeCourseCompleted(integratorInstance) )
	{
	  IntegratorInstance_integrateOneStep(integratorInstance);
	  
	  if (SolverError_getNum(ERROR_ERROR_TYPE) ||
	      SolverError_getNum(FATAL_ERROR_TYPE))
	  {
	    fprintf(stderr,
		    "ERROR at parameter 1 = %g, parameter 2 = %g\n", i, j);
	    DumpErrors();
	    error++;	  
	  }
	  
	}
        /* printf("end %g\n", IntegratorInstance_getVariableValue(integratorInstance, speciesVI)); */
	
	/* check whether the final value has largest difference to initial condition */
	diff =  fabs(initCond - IntegratorInstance_getVariableValue(integratorInstance, speciesVI));
	if ( diff > maxDiff ) maxDiff = diff;
	DumpState(integratorInstance, parameterVI, parameter2VI, speciesVI);
      }
    }
  }
  
  printf("end\n");
  /* printf("end\n init %g, MAX DIFF %g, abs %g\n", initCond, maxDiff, fabs(initCond-maxDiff)); */
  

  
  
  if ( error ) printf("\t%d errors occured\n", error);
  IntegratorInstance_free(integratorInstance);
  VariableIndex_free(parameterVI);
  VariableIndex_free(parameter2VI);
  VariableIndex_free(speciesVI);
  ODEModel_free(model);
  CvodeSettings_free(settings);
  
  return 0;
}
int doIt(void)
{
    int i ;
    cvodeSettings_t *settings, *set2;
    variableIndex_t *s1, *s2;
    integratorInstance_t *integratorInstance;

    odeModel_t *model =
      ODEModel_createFromFile("basic-model1-forward-l2.xml");
    
    RETURN_ON_ERRORS_WITH(1)

    s1 = ODEModel_getVariableIndex(model, "S1");
    s2 = ODEModel_getVariableIndex(model, "S2");

    /* Creating settings with default values */
    settings = CvodeSettings_create();
    
    /* Setting end time to .1, number of time steps to 1 and NULL
       instead of an optional predefined time series (double *); due
       to Indefinitely == 1, Printstep 5 will be ignored and Time =
       0.1 will be used as timestep for infinite integration */
    CvodeSettings_setTime(settings, .1, 5);

    /* Setting Cvode Parameters: absolute tolerance, relative
       tolerance and maximal internal step, respectively */
    CvodeSettings_setErrors(settings, 1e-20, 1e-14, 500);
    
    /* Setting Integration Switches */
    CvodeSettings_setMethod(settings, 1, 12);
    CvodeSettings_setJacobian(settings, 1);
    CvodeSettings_setIndefinitely(settings, 1);
    CvodeSettings_setHaltOnEvent(settings, 0);
    CvodeSettings_setSteadyState(settings, 0);
    CvodeSettings_setStoreResults(settings, 0);
    CvodeSettings_setSensitivity(settings, 0);

    /* first integration run */
    printf("\nFIRST INTEGRATION RUN WITH:\n");
    CvodeSettings_dump(settings);
    integratorInstance = IntegratorInstance_create(model, settings);
    RETURN_ON_ERRORS_WITH(1);

    /* print variable (ODE, assignments) and constant names */
    printf("#time  ");
    IntegratorInstance_dumpNames(integratorInstance);
    /* print initial conditions and parameters */
    IntegratorInstance_dumpData(integratorInstance);
    
    for (i=0; i != 12; i++)
    {
        /* setting the next time step, uncomment the following
	   function call to see its effect */
     /* IntegratorInstance_setNextTimeStep(integratorInstance, 0.2*(i+1)); */

        IntegratorInstance_integrateOneStep(integratorInstance);
        RETURN_ON_ERRORS_WITH(1);
	/* print current data */
	IntegratorInstance_dumpData(integratorInstance);
    }

    /* now, let's try again, with different settings */

    set2 = CvodeSettings_create();
    /* as Indefinitely will be set to 0, a finite integration
       to time 0.24 in 6 steps will be run */
    CvodeSettings_setTime(set2, 1.2, 6);
    CvodeSettings_setErrors(set2, 1e-16, 1e-14, 500);
    /* switches can be set all together, same order as above */
    CvodeSettings_setSwitches(set2, 1, 0, 0, 0, 0, 0, 0);
    
    printf("\nNOW, LET'S TRY AGAIN WITH DIFFERENT SETTINGS:\n");
    CvodeSettings_dump(set2);
    
    IntegratorInstance_set(integratorInstance, set2);

    IntegratorInstance_dumpData(integratorInstance);  

    while( !IntegratorInstance_timeCourseCompleted(integratorInstance) ) {

        IntegratorInstance_integrateOneStep(integratorInstance);
        RETURN_ON_ERRORS_WITH(1);

 	IntegratorInstance_dumpData(integratorInstance);
    }
    
    printf("\n\nFINISHED SUCCESSFULLY!\n");
    printf("Please, note the different values e.g. at time 1.2.\n");
    printf("The values for the first run a more exact, due to the much\n");
    printf("lower error tolerances. The error tolerances have to be\n");
    printf("adapted to the ranges of each model!!\n\n");

    IntegratorInstance_free(integratorInstance);
    ODEModel_free(model);
    VariableIndex_free(s1);
    VariableIndex_free(s2);
    CvodeSettings_free(settings);
    CvodeSettings_free(set2);
    
    return 0;
}
int doit(void)
{
    int i ;
    cvodeSettings_t *settings ;
    variableIndex_t *s1, *s2;
    integratorInstance_t *integratorInstanceA;
    integratorInstance_t *integratorInstanceB;

    odeModel_t *model = ODEModel_createFromFile("events-2-events-1-assignment-l2.xml");
    RETURN_ON_ERRORS_WITH(1);

    assert(ODEModel_hasVariable(model, "S1"));
    assert(ODEModel_hasVariable(model, "S1"));
    assert(!ODEModel_hasVariable(model, "foobar"));

    s1 = ODEModel_getVariableIndex(model, "S1");
    s2 = ODEModel_getVariableIndex(model, "S2");
    
    /* Creating settings with default values */
    settings = CvodeSettings_create();

    
    /* Setting end time to .1, number of time steps to 1 and NULL
       instead of an optional predefined time series (double *); due
       to Indefinitely == 1, Printstep 1 will be ignored and Time =
       0.1 will be used as timestep for infinite integration */
    CvodeSettings_setTime(settings, .01, 1);

    /* Setting Cvode Parameters: absolute and relative tolerances and
       maximal internal step */
    CvodeSettings_setErrors(settings, 1e-18, 1e-14, 500);

    
    /* Setting Integration Switches: see documentation or
       example simpleIntegratorInstance.c for details on
       the passed values */
    CvodeSettings_setSwitches(settings, 1, 1, 0, 0, 0, 0);

    integratorInstanceA = IntegratorInstance_create(model, settings);
    RETURN_ON_ERRORS_WITH(1);

    integratorInstanceB = IntegratorInstance_create(model, settings);
    RETURN_ON_ERRORS_WITH(1);

    DumpState(integratorInstanceA, integratorInstanceB, s1, s2);

    for (i=0; i != 500; i++)
    {
        IntegratorInstance_integrateOneStep(integratorInstanceA);
        RETURN_ON_ERRORS_WITH(1);

        IntegratorInstance_integrateOneStep(integratorInstanceB);
        RETURN_ON_ERRORS_WITH(1);

        DumpState(integratorInstanceA, integratorInstanceB, s1, s2);

    }

    IntegratorInstance_free(integratorInstanceA);
    IntegratorInstance_free(integratorInstanceB);
    VariableIndex_free(s1);
    VariableIndex_free(s2);
    ODEModel_free(model);
    CvodeSettings_free(settings);

    return 0;
}
int doit(void)
{
    int i ;
    double value;
    cvodeSettings_t *settings ;
    variableIndex_t *s1, *s2;
    integratorInstance_t *integratorInstanceA;
    integratorInstance_t *integratorInstanceB;

    odeModel_t *model = ODEModel_createFromFile("basic-model1-forward-l2.xml");
    RETURN_ON_ERRORS_WITH(1);

    s1 = ODEModel_getVariableIndex(model, "S1");
    s2 = ODEModel_getVariableIndex(model, "S2");
    RETURN_ON_ERRORS_WITH(1);

    /* Creating settings with default values */
    settings = CvodeSettings_create();

    
    /* Setting end time to .1, number of time steps to 1 and NULL
       instead of an optional predefined time series (double *); due
       to Indefinitely == 1, Printstep 1 will be ignored and Time =
       0.1 will be used as timestep for infinite integration */
    CvodeSettings_setTime(settings, .1, 1);

    /* Setting Cvode Parameters: absolute and relative tolerances and
       maximal internal step */
    CvodeSettings_setErrors(settings, 1e-18, 1e-14, 500);

    
    /* Setting Integration Switches: see documentation or
       example simpleIntegratorInstance.c for details on
       the passed values */
    CvodeSettings_setSwitches(settings, 1, 1, 0, 0, 0, 0, 0);

    /* use compiled model */
    CvodeSettings_setCompileFunctions(settings, 0);

    /* Generate two independent integrator instances from the same
       odeModel and cvodeSettings */
    integratorInstanceA = IntegratorInstance_create(model, settings);
    RETURN_ON_ERRORS_WITH(1);

    integratorInstanceB = IntegratorInstance_create(model, settings);
    RETURN_ON_ERRORS_WITH(1);

    DumpState(integratorInstanceA, integratorInstanceB, s1, s2);

    for (i=0; i != 30; i++)
    {

        /* run integrations A and B */
        IntegratorInstance_integrateOneStep(integratorInstanceA);
        RETURN_ON_ERRORS_WITH(1);

        IntegratorInstance_integrateOneStep(integratorInstanceB);
        RETURN_ON_ERRORS_WITH(1);

	/* While variable s1 from integration A is between 1e-15 and
	   1e-16, set variables s1 and s2 in integration B to some
	   value.  This function also takes care of creating and
	   freeing CVODE solver structures when ODE variables are
	   changed!
	*/
	value = IntegratorInstance_getVariableValue(integratorInstanceA, s1);
        if ( value < 1.e-15 && value >  1.e-16 )
        {
            IntegratorInstance_setVariableValue(integratorInstanceB,
						s1,1.5e-15);
            IntegratorInstance_setVariableValue(integratorInstanceB,
						s2,1.5e-15);
        }

        DumpState(integratorInstanceA, integratorInstanceB, s1, s2);

    }

    IntegratorInstance_free(integratorInstanceA);
    IntegratorInstance_free(integratorInstanceB);
    VariableIndex_free(s1);
    VariableIndex_free(s2);
    ODEModel_free(model);
    CvodeSettings_free(settings);

    return 0;
}