<commit_before>/*
  Last changed Time-stamp: <2005-08-02 17:08:42 raim>
  $Id: odeSolver.c,v 1.12 2005/08/02 15:47:59 raimc Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <string.h>
#include <time.h>

#include <sbml/SBMLTypes.h>

#include "sbmlsolver/odeSolver.h"
#include "sbmlsolver/sbml.h"
#include "sbmlsolver/solverError.h"


SBMLResults_t *
Model_odeSolver(SBMLDocument_t *d, cvodeSettings_t *set) {
  
  SBMLDocument_t *d2 = NULL;
  cvodeData_t *data;
  SBMLResults_t *results;
  Model_t *m;

 
  /** Convert SBML Document level 1 to level 2, and
      get the contained model
  */
  if ( SBMLDocument_getLevel(d) == 1 ) {
    d2 = convertModel(d);
    m = SBMLDocument_getModel(d2);
  }
  else {
    m = SBMLDocument_getModel(d);
  }
  
  /** At first, the function constructODEs(m)
      will attempt to construct a simplified SBML model,
      that only consists of species and their ODEs, represented
      as Rate Rules, and of Events. All constant species, parameters
      compartments, assignment rules and function definitions
      will be replaced by their values or expressions respectively
      in all remaining formulas (ie. rate and algebraic rules and
      events).
      Then the initial values and ODEs of the remaining species
      will be written to the structure cvodeData_t *data.
  */
  
  data = constructODEs(m, set->UseJacobian);
  /** Errors, found during odeConstruct, will cause the program to exit,
      e.g. when some mathematical expressions are missing.
  */
  SolverError_haltOnErrors();
    
  /** Set integration parameters:
      Now that we have arrived here, we can set the parameters
      for integration, like
      the end time and the number of steps to be printed out,
      absolute and relative error tolerances,
      use of exact or approximated Jacobian matrix,
      printing of messages during integration,
      event handling, steady state detection, and
      runtime printing of results.
      And then ..
  */
  data->tout  = set->Time;
  data->nout  = set->PrintStep;
  data->tmult = set->Time / set->PrintStep;
  data->currenttime = 0.0;
  data->t0 = 0.0;
  data->opt = set;

  /* allow setting of Jacobian,
     only if its construction was succesfull */
   if ( data->opt->UseJacobian == 1 ) { 
     data->opt->UseJacobian = set->UseJacobian && data->model->jacobian; 
   } 
  
  /** .... we can call the integrator function,
      that invokeds CVODE and stores results.
      The function will also handle events and
      check for steady states.
  */    

  integrator(data, 0, 0, stdout);
  SolverError_dump();
  RETURN_ON_FATALS_WITH(NULL);
  SolverError_clear();

  /* Write simulation results into result structure */
  results = Results_fromCvode(data); 
  CvodeData_free(data);
  /* free temporary level 2 version of the document */
  if ( d2 != NULL ) {
    SBMLDocument_free(d2);
  }
  return(results);
}

SBMLResults_t **
Model_odeSolverBatch (SBMLDocument_t *d,
		      cvodeSettings_t *settings, VarySettings vary) {

  int i;
  double value, increment;
  SBMLResults_t **results;
  SBMLDocument_t *d2 = NULL;
  Model_t *m;

  /** Convert SBML Document level 1 to level 2, and
      get the contained model
  */
  if ( SBMLDocument_getLevel(d) == 1 ) {
    d2 = convertModel(d);
    d = d2;
  }
  m = SBMLDocument_getModel(d);
    
  if(!(results = (SBMLResults_t **)calloc(vary.steps+1, sizeof(*results)))){
    fprintf(stderr, "failed!\n");
  }

  value = vary.start;
  increment = (vary.end - vary.start) / vary.steps;
  
  for ( i=0; i<=vary.steps; i++ ) {
    
      
    if ( ! Model_setValue(m, vary.id, vary.rid, value) ) {
      Warn(stderr, "Parameter for variation not found in the model.", vary.id);
      return(NULL);
    }
    results[i] = Model_odeSolver(d, settings);
    value = value + increment;
  }

  /* free temporary level 2 version of the document */
  if ( d2 != NULL ) {
    SBMLDocument_free(d2);
  }

  return(results);

}

SBMLResults_t ***
Model_odeSolverBatch2 (SBMLDocument_t *d, cvodeSettings_t *settings,
		      VarySettings vary1, VarySettings vary2) {

  int i, j;
  double value1, increment1, value2, increment2;
  SBMLResults_t ***results;
  SBMLDocument_t *d2 = NULL;
  Model_t *m;

  /** Convert SBML Document level 1 to level 2, and
      get the contained model
  */
  if ( SBMLDocument_getLevel(d) == 1 ) {
    d2 = convertModel(d);
    d = d2;
  }
  m = SBMLDocument_getModel(d);
    
  if(!(results = (SBMLResults_t ***)calloc(vary1.steps+1, sizeof(**results)))){
    fprintf(stderr, "failed!\n");
  }
  for ( i=0; i<=vary1.steps; i++ ) {
    if(!(results[i] = (SBMLResults_t **)calloc(vary2.steps+1, sizeof(*results)))){
      fprintf(stderr, "failed!\n");
    }    
  }

  value1 = vary1.start;
  increment1 = (vary1.end - vary1.start) / vary1.steps;
  
  value2 = vary2.start;
  increment2 = (vary2.end - vary2.start) / vary2.steps;
  
  for ( i=0; i<=vary1.steps; i++ ) {
    if ( ! Model_setValue(m, vary1.id, vary1.rid, value1) ) {<commit_msg>Bug in batch integration, reported by <NAME><commit_after>/*
  Last changed Time-stamp: <2005-09-22 10:33:36 raim>
  $Id: odeSolver.c,v 1.13 2005/09/22 08:35:10 raimc Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <string.h>
#include <time.h>

#include <sbml/SBMLTypes.h>

#include "sbmlsolver/odeSolver.h"
#include "sbmlsolver/sbml.h"
#include "sbmlsolver/solverError.h"


SBMLResults_t *
Model_odeSolver(SBMLDocument_t *d, cvodeSettings_t *set) {
  
  SBMLDocument_t *d2 = NULL;
  cvodeData_t *data;
  SBMLResults_t *results;
  Model_t *m;

 
  /** Convert SBML Document level 1 to level 2, and
      get the contained model
  */
  if ( SBMLDocument_getLevel(d) == 1 ) {
    d2 = convertModel(d);
    m = SBMLDocument_getModel(d2);
  }
  else {
    m = SBMLDocument_getModel(d);
  }
  
  /** At first, the function constructODEs(m)
      will attempt to construct a simplified SBML model,
      that only consists of species and their ODEs, represented
      as Rate Rules, and of Events. All constant species, parameters
      compartments, assignment rules and function definitions
      will be replaced by their values or expressions respectively
      in all remaining formulas (ie. rate and algebraic rules and
      events).
      Then the initial values and ODEs of the remaining species
      will be written to the structure cvodeData_t *data.
  */
  
  data = constructODEs(m, set->UseJacobian);
  /** Errors, found during odeConstruct, will cause the program to exit,
      e.g. when some mathematical expressions are missing.
  */
  SolverError_haltOnErrors();
    
  /** Set integration parameters:
      Now that we have arrived here, we can set the parameters
      for integration, like
      the end time and the number of steps to be printed out,
      absolute and relative error tolerances,
      use of exact or approximated Jacobian matrix,
      printing of messages during integration,
      event handling, steady state detection, and
      runtime printing of results.
      And then ..
  */
  data->tout  = set->Time;
  data->nout  = set->PrintStep;
  data->tmult = set->Time / set->PrintStep;
  data->currenttime = 0.0;
  data->t0 = 0.0;
  data->opt = set;

  /* allow setting of Jacobian,
     only if its construction was succesfull */
   if ( data->opt->UseJacobian == 1 ) { 
     data->opt->UseJacobian = set->UseJacobian && data->model->jacobian; 
   } 
  
  /** .... we can call the integrator function,
      that invokeds CVODE and stores results.
      The function will also handle events and
      check for steady states.
  */    

  integrator(data, 0, 0, stdout);
  SolverError_dump();
  RETURN_ON_FATALS_WITH(NULL);
  SolverError_clear();

  /* Write simulation results into result structure */
  results = Results_fromCvode(data); 
  CvodeData_free(data);
  /* free temporary level 2 version of the document */
  if ( d2 != NULL ) {
    SBMLDocument_free(d2);
  }
  return(results);
}

SBMLResults_t **
Model_odeSolverBatch (SBMLDocument_t *d,
		      cvodeSettings_t *settings, VarySettings vary) {

  int i;
  double value, increment;
  SBMLResults_t **results;
  SBMLDocument_t *d2 = NULL;
  Model_t *m;

  /** Convert SBML Document level 1 to level 2, and
      get the contained model
  */
  if ( SBMLDocument_getLevel(d) == 1 ) {
    d2 = convertModel(d);
    m = SBMLDocument_getModel(d2);
  }
  else {
    m = SBMLDocument_getModel(d);
  }
    
  if(!(results = (SBMLResults_t **)calloc(vary.steps+1, sizeof(*results)))){
    fprintf(stderr, "failed!\n");
  }

  value = vary.start;
  increment = (vary.end - vary.start) / vary.steps;
  
  for ( i=0; i<=vary.steps; i++ ) {
    
      
    if ( ! Model_setValue(m, vary.id, vary.rid, value) ) {
      Warn(stderr, "Parameter for variation not found in the model.", vary.id);
      return(NULL);
    }
    results[i] = Model_odeSolver(d, settings);
    value = value + increment;
  }

  /* free temporary level 2 version of the document */
  if ( d2 != NULL ) {
    SBMLDocument_free(d2);
  }

  return(results);

}

SBMLResults_t ***
Model_odeSolverBatch2 (SBMLDocument_t *d, cvodeSettings_t *settings,
		      VarySettings vary1, VarySettings vary2) {

  int i, j;
  double value1, increment1, value2, increment2;
  SBMLResults_t ***results;
  SBMLDocument_t *d2 = NULL;
  Model_t *m;

  /** Convert SBML Document level 1 to level 2, and
      get the contained model
  */
  if ( SBMLDocument_getLevel(d) == 1 ) {
    d2 = convertModel(d);
    m = SBMLDocument_getModel(d2);
  }
  else {
    m = SBMLDocument_getModel(d);
  }
      
  if(!(results = (SBMLResults_t ***)calloc(vary1.steps+1, sizeof(**results)))){
    fprintf(stderr, "failed!\n");
  }
  for ( i=0; i<=vary1.steps; i++ ) {
    if(!(results[i] = (SBMLResults_t **)calloc(vary2.steps+1, sizeof(*results)))){
      fprintf(stderr, "failed!\n");
    }    
  }

  value1 = vary1.start;
  increment1 = (vary1.end - vary1.start) / vary1.steps;
  
  value2 = vary2.start;
  increment2 = (vary2.end - vary2.start) / vary2.steps;
  
  for ( i=0; i<=vary1.steps; i++ ) {
    if ( ! Model_setValue(m, vary1.id, vary1.rid, value1) ) {
{'fraction_non_alphanumeric': 0.11958337427532671, 'fraction_numerical': 0.019062592119485113, 'mean_word_length': 3.056596253487445, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '58854', 'n_tokens_mistral': 3636, 'n_tokens_neox': 3482, 'n_words': 978}