示例#1
0
END_TEST


START_TEST (test_L3_Model_name)
{
  const char *name = "My_Favorite_Factory";


  fail_unless( !Model_isSetName(M) );

  Model_setName(M, name);

  fail_unless( !strcmp(Model_getName(M), name) );
  fail_unless( Model_isSetName(M) );

  if (Model_getName(M) == name)
  {
    fail("Model_setName(...) did not make a copy of string.");
  }

  Model_unsetName(M);
  
  fail_unless( !Model_isSetName(M) );

  if (Model_getName(M) != NULL)
  {
    fail("Model_unsetName(M) did not clear string.");
  }
}
END_TEST


START_TEST (test_comp_modelDefinition_name)
{
  const char *name = "My_Favorite_Factory";


  fail_unless( !Model_isSetName(P) );

  Model_setName(P, name);

  const char* getchar = Model_getName(P);
  fail_unless( !strcmp(getchar, name) );
  fail_unless( Model_isSetName(P) );

  if (getchar == name)
  {
    fail("Model_setName(...) did not make a copy of string.");
  }

  Model_unsetName(P);
  
  fail_unless( !Model_isSetName(P) );

  if (Model_getName(P) != NULL)
  {
    fail("Model_unsetName(P) did not clear string.");
  }
}
示例#3
0
static int drawJacobyTxt(cvodeData_t *data, char *file)
{

  int i, j;
  char filename[WORDSIZE];
  FILE *f;

  sprintf(filename, "%s.dot", file);
  f = fopen(filename, "w");
  fprintf(f ,"digraph jacoby {\n");
  fprintf(f ,"overlap=scale;\n");
  if ( Model_isSetName(data->model->m) )
    fprintf(f ,"label=\"%s at time %g\";\n", Model_getName(data->model->m),
	    data->currenttime);
  else if ( Model_isSetId(data->model->m) )
    fprintf(f ,"label=\"%s at time %g\";\n", Model_getId(data->model->m),
	    data->currenttime);
  else
    fprintf(f ,"label=\"at time %g\";\n", data->currenttime);


  /*
    Set edges from species A to species B if the
    corresponding entry in the jacobian ((d[B]/dt)/d[A])
    is not '0'. Set edge color 'red' and arrowhead 'tee'
    if negative.
  */


  for ( i=0; i<data->model->neq; i++ )
  {
    for ( j=0; j<data->model->neq; j++ )
    {
      if ( evaluateAST(data->model->jacob[i][j], data) != 0 )
      {
	fprintf(f ,"%s->%s [label=\"%g\" ",
		data->model->names[j],
		data->model->names[i],
		evaluateAST(data->model->jacob[i][j],
			    data));
	if ( evaluateAST(data->model->jacob[i][j], data) < 0 )
	  fprintf(f ,"arrowhead=tee color=red];\n");
	else
	  fprintf(f ,"];\n");
      }
    }
  }
  for ( i=0; i<data->model->neq; i++ )
  {
    fprintf(f ,"%s [label=\"%s\"];", data->model->names[i],
	    data->model->names[i]);
  }   
  fprintf(f, "}\n");
  return 1;
}
示例#4
0
END_TEST


START_TEST (test_L3_Model_createWithNS )
{
  XMLNamespaces_t *xmlns = XMLNamespaces_create();
  XMLNamespaces_add(xmlns, "http://www.sbml.org", "testsbml");
  SBMLNamespaces_t *sbmlns = SBMLNamespaces_create(3,1);
  SBMLNamespaces_addNamespaces(sbmlns,xmlns);

  Model_t *m = 
    Model_createWithNS (sbmlns);


  fail_unless( SBase_getTypeCode  ((SBase_t *) m) == SBML_MODEL );
  fail_unless( SBase_getMetaId    ((SBase_t *) m) == NULL );
  fail_unless( SBase_getNotes     ((SBase_t *) m) == NULL );
  fail_unless( SBase_getAnnotation((SBase_t *) m) == NULL );

  fail_unless( SBase_getLevel       ((SBase_t *) m) == 3 );
  fail_unless( SBase_getVersion     ((SBase_t *) m) == 1 );

  fail_unless( Model_getNamespaces     (m) != NULL );
  fail_unless( XMLNamespaces_getLength(Model_getNamespaces(m)) == 2 );


  fail_unless( Model_getId     (m) == NULL );
  fail_unless( Model_getName   (m) == NULL );
  fail_unless( Model_getSubstanceUnits(m) == NULL );
  fail_unless( Model_getTimeUnits(m) == NULL );
  fail_unless( Model_getVolumeUnits(m) == NULL );
  fail_unless( Model_getAreaUnits(m) == NULL );
  fail_unless( Model_getLengthUnits(m) == NULL );
  fail_unless( Model_getConversionFactor(m) == NULL );

  fail_unless( !Model_isSetId     (m) );
  fail_unless( !Model_isSetName   (m) );
  fail_unless( !Model_isSetSubstanceUnits(m) );
  fail_unless( !Model_isSetTimeUnits(m) );
  fail_unless( !Model_isSetVolumeUnits(m) );
  fail_unless( !Model_isSetAreaUnits(m) );
  fail_unless( !Model_isSetLengthUnits(m) );
  fail_unless( !Model_isSetConversionFactor(m) );

  Model_free(m);
  XMLNamespaces_free(xmlns);
  SBMLNamespaces_free(sbmlns);
}
static int openXMGrace(cvodeData_t *data)
{
  double maxY;

  /* Open XMGrace */
  if ( !GraceIsOpen() ) {     
    GraceRegisterErrorFunction(grace_error); 
    /* Start Grace with a buffer size of 2048 and open the pipe */
    if ( GraceOpen(2048) == -1 ) {
      fprintf(stderr, "Can't run Grace. \n");
      return 1;
    }
    else if ( GraceIsOpen() ) {
      maxY = 1.0;
      /*
	"with g%d" might become useful, when printing multiple
	graphs into one XMGrace subprocess.
      */
      /* GracePrintf("with g%d", data->results->xmgrace); */
      GracePrintf("world xmax %g", data->currenttime);
      GracePrintf("world ymax %g", 1.25*maxY);
      GracePrintf("xaxis tick major %g", data->currenttime/10);
      /* GracePrintf("xaxis tick minor %d", (int) data->currenttime/100); */
      GracePrintf("yaxis tick major %g", (1.25*maxY)/12.5 );
      GracePrintf("xaxis label font 4");
      GracePrintf("xaxis label \"time\"");
      GracePrintf("xaxis ticklabel font 4");
      GracePrintf("xaxis ticklabel char size 0.7");
      GracePrintf("yaxis label font 4");
      GracePrintf("yaxis label \"concentration\"");    
      GracePrintf("yaxis ticklabel font 4");
      GracePrintf("yaxis ticklabel char size 0.7");
      if ( Model_isSetName(data->model->simple) )
	GracePrintf("subtitle \"%s\"", Model_getName(data->model->simple));
      else if  ( Model_isSetId(data->model->simple) )
	GracePrintf("subtitle \"%s\"", Model_getId(data->model->simple));
      else 
	GracePrintf("subtitle \"model has no name/id\"");
      GracePrintf("subtitle font 8");   
    }
  }
  else {
    fprintf(stderr, "Please close XMGrace first.\n");
    return 1;
  }

  return 0;
}
void printModel(Model_t *m, FILE *f)
{
  fprintf(f, "\n");
  fprintf(f, "Model Statistics:\n");
  fprintf(f, " Model id:     %s\n",
	 Model_isSetId(m) ? Model_getId(m) : "(not set)");
  fprintf(f, " Model name:   %s\n",
	 Model_isSetName(m) ? Model_getName(m) : "(not set)"); 
  fprintf(f, "\n");
  fprintf(f, " Compartments: %d\n",  Model_getNumCompartments(m)); 
  fprintf(f, " Species:      %d\n",  Model_getNumSpecies(m));
  fprintf(f, " Reactions:    %d\n",  Model_getNumReactions(m));
  fprintf(f, " Rules:        %d\n",  Model_getNumRules(m));
  fprintf(f, " Events:       %d\n",  Model_getNumEvents(m));
  fprintf(f, " Functions:    %d\n",  Model_getNumFunctionDefinitions(m) ); 
  fprintf(f, "\n");
}
static int
drawModelTxt(Model_t *m, char *file) {

  Species_t *s;
  Reaction_t *re;
  const ASTNode_t *math;
  SpeciesReference_t *sref;
  ModifierSpeciesReference_t *mref;
  int i,j;
  int reversible;
  char filename[WORDSIZE];
  FILE *f;
  
  sprintf(filename, "%s.dot", file);
  f = fopen(filename, "w");

  fprintf(f ,"digraph reactionnetwork {\n");
  fprintf(f ,"label=\"%s\";\n",
	  Model_isSetName(m) ?
	  Model_getName(m) : (Model_isSetId(m) ? Model_getId(m) : "noId") );
  fprintf(f ,"overlap=scale;\n");
 
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    
    re = Model_getReaction(m,i);
    reversible = Reaction_getReversible(re);
    
    for ( j=0; j<Reaction_getNumModifiers(re); j++ ) {
      mref = Reaction_getModifier(re,j);
      fprintf(f ,"%s->%s [style=dashed arrowhead=odot];\n",
	      ModifierSpeciesReference_getSpecies(mref), Reaction_getId(re));
    }
    for ( j=0; j<Reaction_getNumReactants(re); j++ ) {
      sref = Reaction_getReactant(re,j);
      fprintf(f ,"%s->%s [label=\"",
	      SpeciesReference_getSpecies(sref), Reaction_getId(re));
      
      if ( (SpeciesReference_isSetStoichiometryMath(sref)) ) {
	math = SpeciesReference_getStoichiometryMath(sref);
	if ( (strcmp(SBML_formulaToString(math),"1") !=
	      0) ) {
	  fprintf(f ,"%s", SBML_formulaToString(math));
	}	
      }
      else {
	if ( SpeciesReference_getStoichiometry(sref) != 1) {
	  fprintf(f ,"%g",SpeciesReference_getStoichiometry(sref));
	}
      }
      if ( reversible == 1 ) {
	fprintf(f ,"\" arrowtail=onormal];\n");
      }
      else {
	fprintf(f ,"\" ];\n");
      }
    }
    for ( j=0; j<Reaction_getNumProducts(re); j++ ) {
      sref = Reaction_getProduct(re,j);
      fprintf(f ,"%s->%s [label=\"",
	      Reaction_getId(re), SpeciesReference_getSpecies(sref));
      if ( (SpeciesReference_isSetStoichiometryMath(sref)) ) {
	math = SpeciesReference_getStoichiometryMath(sref);
	if ( (strcmp(SBML_formulaToString(math),"1") !=
	      0) ) {
	  fprintf(f ,"%s ", SBML_formulaToString(math));
	}
      }
      else {
	if ( SpeciesReference_getStoichiometry(sref) != 1) {
	  fprintf(f ,"%g ",SpeciesReference_getStoichiometry(sref));
	}
      }
      if ( reversible == 1 ) {
	fprintf(f ,"\" arrowtail=onormal];\n");
      }
      else {
	fprintf(f ,"\" ];\n");
      }    

    }
    
  }
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    re = Model_getReaction(m,i);
    fprintf(f ,"%s [label=\"%s\" shape=box];\n",
	    Reaction_getId(re),
	    Reaction_isSetName(re) ?
	    Reaction_getName(re) : Reaction_getId(re));
  }

  for ( i=0; i<Model_getNumSpecies(m); i++) {
    s = Model_getSpecies(m, i);
    fprintf(f ,"%s [label=\"%s\"];",
	    Species_getId(s),
	    Species_isSetName(s) ? Species_getName(s) : Species_getId(s));
  }  
  fprintf(f ,"}\n");
  return 1;
}
SBML_ODESOLVER_API int drawJacoby(cvodeData_t *data, char *file, char *format) {

#if !USE_GRAPHVIZ

  SolverError_error(
		    WARNING_ERROR_TYPE,
		    SOLVER_ERROR_NO_GRAPHVIZ,
		    "odeSolver has been compiled without GRAPHIZ functionality. ",
		    "Graphs are printed to stdout in the graphviz' .dot format.");

  drawJacobyTxt(data, file);

#else

  int i, j;
  GVC_t *gvc;
  Agraph_t *g;
  Agnode_t *r;
  Agnode_t *s;  
  Agedge_t *e;
  Agsym_t *a;
  char name[WORDSIZE];
  char label[WORDSIZE];  
  char *output[3];
  char *command = "dot";
  char *formatopt;
  char *outfile;
  

  /* setting name of outfile */
  ASSIGN_NEW_MEMORY_BLOCK(outfile, strlen(file)+ strlen(format)+7, char, 0);
  sprintf(outfile, "-o%s_jm.%s", file, format);
  
  /* setting output format */
  ASSIGN_NEW_MEMORY_BLOCK(formatopt, strlen(format)+3, char, 0);
  sprintf(formatopt, "-T%s", format); 

  /* construct command-line */
  output[0] = command;
  output[1] = formatopt;
  output[2] = outfile;
  output[3] = NULL;
    
  /* set up renderer context */
  gvc = (GVC_t *) gvContext();
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION < 4
  dotneato_initialize(gvc, 3, output);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  parse_args(gvc, 3, output);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvParseArgs(gvc, 3, output);
#endif

  g = agopen("G", AGDIGRAPH);

  /* avoid overlapping nodes, for graph embedding by neato */
  a = agraphattr(g, "overlap", "");
  agxset(g, a->index, "scale");

  /* set graph label */
  if ( Model_isSetName(data->model->m) )
    sprintf(label, "%s at time %g",  Model_getName(data->model->m),
	    data->currenttime);
  else if ( Model_isSetId(data->model->m) )
    sprintf(label, "%s at time %g",  Model_getId(data->model->m),
	    data->currenttime);
  else
    sprintf(label, "label=\"at time %g\";\n", data->currenttime);

  a = agraphattr(g, "label", "");
  agxset(g, a->index, label);
  
  /*
    Set edges from species A to species B if the
    corresponding entry in the jacobian ((d[B]/dt)/d[A])
    is not '0'. Set edge color 'red' and arrowhead 'tee'
    if negative.
  */

  for ( i=0; i<data->model->neq; i++ ) {
    for ( j=0; j<data->model->neq; j++ ) {
      if ( evaluateAST(data->model->jacob[i][j], data) != 0 ) {
	
	sprintf(name, "%s", data->model->names[j]);
	r = agnode(g,name);
	agset(r, "label", data->model->names[j]);

	sprintf(label, "%s.htm", data->model->names[j]);
	a = agnodeattr(g, "URL", "");
	agxset(r, a->index, label);
	
	sprintf(name,"%s", data->model->names[i]);
	s = agnode(g,name);
	agset(s, "label", data->model->names[i]);

	sprintf(label, "%s.htm", data->model->names[i]);	
	a = agnodeattr(g, "URL", "");
	agxset(s, a->index, label);
	
	e = agedge(g,r,s);

	a = agedgeattr(g, "label", "");
	sprintf(name, "%g",  evaluateAST(data->model->jacob[i][j], data)); 
	agxset (e, a->index, name);


	
	if ( evaluateAST(data->model->jacob[i][j], data) < 0 ) {
	  a = agedgeattr(g, "arrowhead", "");
	  agxset(e, a->index, "tee");
	  a = agedgeattr(g, "color", "");
	  agxset(e, a->index, "red"); 	    
	}	
      }
    }
  }
  
  /* Compute a layout */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  gvBindContext(gvc, g);
  dot_layout(g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  gvlayout_layout(gvc, g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvLayoutJobs(gvc, g);
#endif
  
  /* Write the graph according to -T and -o options */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  dotneato_write(gvc);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  emit_jobs(gvc, g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvRenderJobs(gvc, g);
#endif
  
  /* Clean out layout data */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  dot_cleanup(g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  gvlayout_cleanup(gvc, g);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6 || GRAPHVIZ_MAJOR_VERSION >= 3
  gvFreeLayout(gvc, g);
#endif
  
  /* Free graph structures */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  dot_cleanup(g);
#endif
  agclose(g);

  /* Clean up output file and errors */
#if GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION <= 2
  gvFREEcontext(gvc);
  dotneato_eof(gvc);
#elif GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION == 4
  dotneato_terminate(gvc);
#elif (GRAPHVIZ_MAJOR_VERSION == 2 && GRAPHVIZ_MINOR_VERSION >= 6) || GRAPHVIZ_MAJOR_VERSION >= 3
  gvFreeContext(gvc);
#endif

  xfree(formatopt);
  xfree(outfile);

#endif

  return 1;
}
static int
drawSensitivityTxt(cvodeData_t *data, char *file, double threshold) {

  int i, j;
  char filename[WORDSIZE];
  FILE *f;
  odeModel_t *om;
  double *highest;
  double *lowest;
  
  om = data->model;

  sprintf(filename, "%s.dot", file);
  f = fopen(filename, "w");
  fprintf(f ,"digraph jacoby {\n");
  fprintf(f ,"overlap=scale;\n");
  if ( Model_isSetName(om->m) )
    fprintf(f ,"label=\"%s at time %g\";\n", Model_getName(om->m),
	    data->currenttime);
  else if ( Model_isSetId(om->m) )
    fprintf(f ,"label=\"%s at time %g\";\n", Model_getId(om->m),
	    data->currenttime);
  else
    fprintf(f ,"label=\"at time %g\";\n", data->currenttime);


  /*
    Set edges from species A to species B if the
    corresponding entry in the jacobian ((d[B]/dt)/d[A])
    is not '0'. Set edge color 'red' and arrowhead 'tee'
    if negative.
  */

  ASSIGN_NEW_MEMORY_BLOCK(highest, data->nsens, double, 0);  
  ASSIGN_NEW_MEMORY_BLOCK(lowest, data->nsens, double, 0);  
  for ( j=0; j<data->nsens; j++ ) {
    highest[j] = 0;
    lowest[j] = 0;
    for ( i=0; i<om->neq; i++ ) {
      if ( data->sensitivity[i][j] > highest[j] )
	highest[j] = data->sensitivity[i][j];
      if ( data->sensitivity[i][j] < lowest[j] )
	lowest[j] = data->sensitivity[i][j];
    }
  }
  
  for ( i=0; i<om->neq; i++ ) {
    for ( j=0; j<data->nsens; j++ ) {
      if ( (data->sensitivity[i][j] > threshold*highest[j]) ||
	   (data->sensitivity[i][j] < threshold*lowest[j]) ) {
	
	fprintf(f ,"%s->%s [label=\"%g\" ",
		om->names[om->index_sens[j]],
		om->names[i],
		data->sensitivity[i][j]);
	
	if ( data->sensitivity[i][j] < 0 ) {
	  fprintf(f ,"arrowhead=tee color=red];\n");
	}
	else {
	  fprintf(f ,"];\n");
	}
      }
    }
  }
  for ( i=0; i<om->neq; i++ ) {
    fprintf(f ,"%s [label=\"%s\"];", om->names[i],
	    om->names[i]);
  }
  for ( i=0; i<data->nsens; i++ ) {
    fprintf(f ,"%s [label=\"%s\"];", om->names[om->index_sens[i]],
	    om->names[om->index_sens[i]]);
  }  
  fprintf(f, "}\n");
  return 1;
}
static int printXMGReactionTimeCourse ( cvodeData_t *data )
{

  int i, j, k, n;
  double maxY, minY, result;
  
  Model_t *m;
  Reaction_t *r;
  KineticLaw_t *kl;
  ASTNode_t **kls;
  
  odeModel_t *om = data->model;
  cvodeResults_t *results = data->results;

  maxY = 0.0;
  minY = 0.0;

  fprintf(stderr,
	  "Printing time development of reaction fluxes to XMGrace!\n");


  if ( om->m == NULL ) {
    fprintf(stderr, "Error: No reaction model availabe\n");
    return 1;
  }
  else m = om->m;

  if ( openXMGrace(data) > 0 ) {
    fprintf(stderr,  "Error: Couldn't open XMGrace\n");
    return 1;
  }
  
  GracePrintf("yaxis label \"%s\"", "flux [substance/time]");
  if ( Model_isSetName(m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getName(m),
		"reaction flux time courses");
  else if  ( Model_isSetId(m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getId(m),
		"reaction flux time courses");
  else 
    GracePrintf("subtitle \"model has no name, %s/id\"",
		"reaction flux time courses");


  /* print legend */  
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    r = Model_getReaction(m, i);
    if ( Reaction_isSetName(r) )
      GracePrintf("g0.s%d legend  \"%s: %s \"\n", i+1,
		  Reaction_getId(r), Reaction_getName(r));
    else
      GracePrintf("g0.s%d legend  \"%s \"\n", i+1, Reaction_getId(r));      
  }  
  GracePrintf("legend 1.155, 0.85");
  GracePrintf("legend font 8");
  GracePrintf("legend char size 0.6");

  if(!(kls = (ASTNode_t **)calloc(Model_getNumReactions(m),
				  sizeof(ASTNode_t *)))) 
    fprintf(stderr, "failed!\n");
  
  for ( i=0; i<Model_getNumReactions(m); i++ ) {
    r = Model_getReaction(m, i);
    kl = Reaction_getKineticLaw(r);
    kls[i] = copyAST(KineticLaw_getMath(kl));
    AST_replaceNameByParameters(kls[i], KineticLaw_getListOfParameters(kl));
    AST_replaceConstants(m, kls[i]);
  }
  
  /* evaluate flux for each time point and print to XMGrace */
  
  for ( i=0; i<=results->nout; i++ ) {  
    n = 1;
    /* set time and variable values to values at time[k] */
    data->currenttime = results->time[i];
    for ( j=0; j<data->model->neq; j++ )
      data->value[j] = results->value[j][i];

    /* evaluate kinetic law expressions */
    for ( j=0; j<Model_getNumReactions(m); j++ ) {
      result = evaluateAST(kls[j], data);
      if ( result > maxY ) {
	maxY = result;
	GracePrintf("world ymax %g", 1.25*maxY);
      }
      if ( result < minY ) {
	minY = result;
	GracePrintf("world ymin %g", 1.25*minY);
      }
      GracePrintf("g0.s%d point %g, %g",
		  n, results->time[i], result);
      n++;
    }
  }

  GracePrintf("yaxis tick major %g", 1.25*(fabs(maxY)+fabs(minY))/10);
  GracePrintf("redraw");
  closeXMGrace(data, "flux");

  /* free temporary ASTNodes */
  for ( i=0; i<Model_getNumReactions(m); i++ ) 
    ASTNode_free(kls[i]);
  free(kls);

  return 0;
  
}
void printPhase(cvodeData_t *data)
{

#if !USE_GRACE

  fprintf(stderr,
	  "odeSolver has been compiled without XMGRACE functionality.\n");
  fprintf(stderr,
	  "Phase diagrams can only be printed to XMGrace at the moment.\n");

#else
  
  int i,j;
  double maxY;
  double minY;
  double maxX;
  char *x;
  double xvalue;
  char *y;
  double yvalue;

  cvodeResults_t *results;

  maxY = 1.0;
  maxX = 1.0;
  minY = 0.0;
  
  if ( data==NULL || data->results==NULL ) {
    Warn(stderr,
	 "No data available to print! Please integrate model first!\n");
    return;
  }

  results = data->results;

  if ( openXMGrace(data) > 0 ) {
    fprintf(stderr,
	    "Error: Couldn't open XMGrace\n");
    return;
  }

  GracePrintf("world xmax %g", 1.25*maxX);
  GracePrintf("world ymax %g", 1.25*maxY);

  GracePrintf("xaxis tick major %g", (1.25*maxX)/12.5);
  /*     GracePrintf("xaxis tick minor %d", (int) data->currenttime/100); */
  GracePrintf("yaxis tick major %g", (1.25*maxY)/12.5 );

  if ( Model_isSetName(data->model->m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getName(data->model->m),
		"phase diagram");
  else if  ( Model_isSetId(data->model->m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getId(data->model->m),
		"phase diagram");
  else
    GracePrintf("subtitle \"model has no name, %s/id\"", "phase diagram");
      
  GracePrintf("xaxis label \"species 1\"");
  GracePrintf("yaxis label \"species 2\"");

  printf("Please enter the IDs and NOT the NAMES of species!\n");
  printf("In interactive mode press 'c' to see ID/NAME pairs.\n\n");
  printf("Please enter the ID of the species for the x axis: ");
  x = get_line(stdin);
  x = util_trim(x);
  GracePrintf("xaxis label \"%s\"", x);
  GracePrintf("redraw");
  
  printf("Please enter the ID of the species for the y axis: ");
  y = get_line(stdin);
  y = util_trim(y);
  GracePrintf("yaxis label \"%s\"", y);
  GracePrintf("redraw");
  
  /* check if species exist */
  xvalue = 1;
  yvalue = 1;
  for ( j=0; j<data->model->neq; j++ ) {
    if ( !strcmp(x, data->model->names[j]) ) {
      xvalue = 0;
      GracePrintf("xaxis label \"%s\"", data->model->names[j]);
    }
    if ( !strcmp(y, data->model->names[j]) ) {
      yvalue = 0;
      GracePrintf("yaxis label \"%s\"", data->model->names[j]);
    }
  }
  if ( xvalue || yvalue ) {
    fprintf(stderr, "One of the entered species does not exist.\n");
    GraceClose();
    fprintf(stderr, "XMGrace subprocess closed. Please try again");
    free(x);
    free(y);
    return;
  }

  fprintf(stderr, "Printing phase diagram to XMGrace!\n");

  for ( i=0; i<=results->nout; ++i ) {     
    for ( j=0; j<data->model->neq; j++ ){
      if ( !strcmp(x, data->model->names[j]) ) {
	xvalue = results->value[j][i];
      }
      if ( !strcmp(y, data->model->names[j]) ) {
	yvalue = results->value[j][i];
      }
    }
    GracePrintf("g0.s1 point %g, %g", xvalue, yvalue);

    if ( yvalue > maxY ) {
      maxY = 1.25*yvalue;
      GracePrintf("world ymax %g", maxY);
      GracePrintf("yaxis tick major %g", maxY/10);      
    }
    if ( xvalue > maxX ) {
      maxX = 1.25*xvalue;
      GracePrintf("world xmax %g", maxX);
      GracePrintf("xaxis tick major %g", maxX/10);
    }

    /*
      redrawing on each 10th step gives an impression,
      how fast the two values change within the phase
      diagram.
    */
    if ( i%10 == 0 ) {
      GracePrintf("redraw");
    }
  }

  GracePrintf("redraw");
  closeXMGrace(data, "phase");
  free(x);  
  free(y);

#endif

  return;
}
static int printXMGOdeTimeCourse(cvodeData_t *data)
{  
  int i, j, n;
  double maxY;
  double minY; 
  double result;

  cvodeResults_t *results;

  maxY = 0.01;
  minY = 0.0;
  

  fprintf(stderr,
	  "Printing time development of the ODEs (rates) to XMGrace!\n");

  results = data->results;

  if ( openXMGrace(data) > 0 ) {
    fprintf(stderr,
	    "Error: Couldn't open XMGrace\n");
    return 1;     
  }

  GracePrintf("yaxis label \"%s\"", "ODE values");
  if ( Model_isSetName(data->model->m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getName(data->model->m),
		  "ODEs time course");
  else if  ( Model_isSetId(data->model->m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getId(data->model->m),
		  "ODEs time course");
  else 
    GracePrintf("subtitle \"model has no name/id, %s\"",
		  "ODEs time course");

  /* print legend */  
  if ( printXMGLegend(data, data->model->neq) > 0 ){
    fprintf(stderr,
	    "Warning: Couldn't print legend\n");
    return 1;
  }


  /* evaluate ODE at each time point and print to XMGrace */

  for ( i=0; i<=results->nout; ++i ) {
    n = 1;
    data->currenttime = results->time[i];
    for ( j=0; j<data->model->neq; j++ ) {
      data->value[j] = results->value[j][i];
    }
    for ( j=0; j<data->model->neq; j++ ) {
      result = evaluateAST(data->model->ode[j],data);
      if ( result > maxY ) {
	maxY = result;
	GracePrintf("world ymax %g", 1.25*maxY);
      }
      if ( result < minY ) {
	minY = result;
	GracePrintf("world ymin %g", 1.25*minY);
      }

      GracePrintf("g0.s%d point %g, %g",
		  n, results->time[i], result);
      n++;     
    }

    /*
    if ( i%10 == 0 ) {
      GracePrintf("yaxis tick major %g", 1.25*(fabs(maxY)+fabs(minY))/10);
      GracePrintf("redraw");
    }
    */
  }
  GracePrintf("yaxis tick major %g", 1.25*(fabs(maxY)+fabs(minY))/10);
  GracePrintf("redraw");
  closeXMGrace(data, "rates");

  return 0;
}
static int printXMGJacobianTimeCourse ( cvodeData_t *data )
{
  int i, j, k, n;
  double maxY;
  double minY; 
  double result;

  cvodeResults_t *results;

  maxY = 0.0;
  minY = 0.0;
  

  fprintf(stderr,
	  "Printing time development of the jacobian matrix to XMGrace!\n");

  results = data->results;

  if ( openXMGrace(data) > 0 ) {
    fprintf(stderr,
	    "Error: Couldn't open XMGrace\n");
    return 1;
  }
  
  GracePrintf("yaxis label \"%s\"", "jacobian matrix value");
  if ( Model_isSetName(data->model->m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getName(data->model->m),
		"jacobian matrix time course");
  else if  ( Model_isSetId(data->model->m) )
    GracePrintf("subtitle \"%s, %s\"", Model_getId(data->model->m),
		"jacobian matrix time course");
  else 
    GracePrintf("subtitle \"model has no name, %s/id\"",
		"jacobian matrix time course");


  /* print legend */  
  n = 1;  
  for ( i=0; i<data->model->neq; i++ ) {
    for ( j=0; j<data->model->neq; j++ ) {
      GracePrintf("g0.s%d legend  \"%s / %s\"\n",
		  n, data->model->names[i], data->model->names[j]);
      n++;
    }
  }  
  GracePrintf("legend 1.155, 0.85");
  GracePrintf("legend font 8");
  GracePrintf("legend char size 0.6");

  /* evaluate jacobian matrix for each time point and print to XMGrace */
  
  for ( k = 0; k<=results->nout; k++ ) {  
    n = 1;
    data->currenttime = results->time[i];
    for ( i=0; i<data->model->neq; i++ ) {
      
      /* set specie values to values at time[k] */
      data->value[i] = results->value[i][k];
      
      for ( j=0; j<data->model->neq; j++ ) {
	
	result =  evaluateAST(data->model->jacob[i][j], data);
	
	if ( result > maxY ) {	  
	  maxY = result;
	  GracePrintf("world ymax %g", 1.25*maxY);
	}
	if ( result < minY ) {
	  minY = result;
	  GracePrintf("world ymin %g", 1.25*minY);
	}
	
	GracePrintf("g0.s%d point %g, %g",
		    n, results->time[k], result);
	n++;

      }
    }
    /*
    if ( k%10 == 0 ) {
      GracePrintf("yaxis tick major %g", 1.25*(fabs(maxY)+fabs(minY))/10);
      GracePrintf("redraw");
    }
    */
  }
  GracePrintf("yaxis tick major %g", 1.25*(fabs(maxY)+fabs(minY))/10);
  GracePrintf("redraw");
  closeXMGrace(data, "jac");


  return 0;
}