END_TEST START_TEST (test_L3_Model_id) { const char *id = "mitochondria"; fail_unless( !Model_isSetId(M) ); Model_setId(M, id); fail_unless( !strcmp(Model_getId(M), id) ); fail_unless( Model_isSetId(M) ); if (Model_getId(M) == id) { fail("Model_setId(...) did not make a copy of string."); } Model_unsetId(M); fail_unless( !Model_isSetId(M) ); if (Model_getId(M) != NULL) { fail("Model_unsetId(M) did not clear string."); } }
int main (int argc, char *argv[]) { const char *filename; SBMLDocument_t *d; Model_t *m; unsigned int level, version; if (argc != 2) { printf("Usage: printSBML filename\n"); return 2; } filename = argv[1]; d = readSBML(filename); SBMLDocument_printErrors(d, stdout); m = SBMLDocument_getModel(d); level = SBMLDocument_getLevel (d); version = SBMLDocument_getVersion(d); printf("\n"); printf("File: %s (Level %u, version %u)\n", filename, level, version); if (m == NULL) { printf("No model present."); return 1; } printf(" "); printf(" model id: %s\n", Model_isSetId(m) ? Model_getId(m) : "(empty)"); printf( "functionDefinitions: %d\n", Model_getNumFunctionDefinitions(m) ); printf( " unitDefinitions: %d\n", Model_getNumUnitDefinitions (m) ); printf( " compartmentTypes: %d\n", Model_getNumCompartmentTypes (m) ); printf( " specieTypes: %d\n", Model_getNumSpeciesTypes (m) ); printf( " compartments: %d\n", Model_getNumCompartments (m) ); printf( " species: %d\n", Model_getNumSpecies (m) ); printf( " parameters: %d\n", Model_getNumParameters (m) ); printf( " initialAssignments: %d\n", Model_getNumInitialAssignments (m) ); printf( " rules: %d\n", Model_getNumRules (m) ); printf( " constraints: %d\n", Model_getNumConstraints (m) ); printf( " reactions: %d\n", Model_getNumReactions (m) ); printf( " events: %d\n", Model_getNumEvents (m) ); printf( "\n" ); SBMLDocument_free(d); return 0; }
void printMath (Model_t *m) { unsigned int n; /* a digraph must have a name thus * need to check that Model_getId does not return NULL * and provide a name if it does */ if (Model_getId(m) != NULL) { fprintf(fout, "digraph %s {\n", Model_getId(m)); } else { fprintf(fout, "digraph example {\n"); } fprintf(fout, "compound=true;\n"); for (n = 0; n < Model_getNumFunctionDefinitions(m); ++n) { printFunctionDefinition(n + 1, Model_getFunctionDefinition(m, n)); } for (n = 0; n < Model_getNumRules(m); ++n) { printRuleMath(n + 1, Model_getRule(m, n)); } printf("\n"); for (n = 0; n < Model_getNumReactions(m); ++n) { printReactionMath(n + 1, Model_getReaction(m, n)); } printf("\n"); for (n = 0; n < Model_getNumEvents(m); ++n) { printEventMath(n + 1, Model_getEvent(m, n)); } fprintf(fout, "}\n"); }
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; }
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"); }
myResult* simulate_implicit(Model_t *m, myResult *result, mySpecies *sp[], myParameter *param[], myCompartment *comp[], myReaction *re[], myRule *rule[], myEvent *event[], myInitialAssignment *initAssign[], myAlgebraicEquations *algEq, timeVariantAssignments *timeVarAssign, double sim_time, double dt, int print_interval, double *time, int order, int use_lazy_method, int print_amount, allocated_memory *mem){ unsigned int i, j; int cycle; int error; int end_cycle = get_end_cycle(sim_time, dt); double reverse_time; double *value_time_p = result->values_time; double *value_sp_p = result->values_sp; double *value_param_p = result->values_param; double *value_comp_p = result->values_comp; double **coefficient_matrix = NULL; double *constant_vector = NULL; int *alg_pivot = NULL; double reactants_numerator, products_numerator; double min_value; double *init_val; /* for implicit */ double **jacobian; int is_convergence = 0; double *b; double *pre_b; int *p; /* for pivot selection */ boolean flag; double delta = 1.0e-8; double tolerance = 1.0e-4; /* error tolerance of neuton method */ unsigned int loop; double *delta_value; double k_next; /* speculated k value : k(t+1) */ double *k_t; /* k(t) */ /* num of SBase objects */ unsigned int num_of_species = Model_getNumSpecies(m); unsigned int num_of_parameters = Model_getNumParameters(m); unsigned int num_of_compartments = Model_getNumCompartments(m); unsigned int num_of_reactions = Model_getNumReactions(m); unsigned int num_of_rules = Model_getNumRules(m); unsigned int num_of_events = Model_getNumEvents(m); unsigned int num_of_initialAssignments = Model_getNumInitialAssignments(m); /* num of variables whose quantity is not a constant */ unsigned int num_of_all_var_species = 0; unsigned int num_of_all_var_parameters = 0; unsigned int num_of_all_var_compartments = 0; unsigned int num_of_all_var_species_reference = 0; /* num of variables (which is NOT changed by assignment nor algebraic rule) */ unsigned int num_of_var_species = 0; unsigned int num_of_var_parameters = 0; unsigned int num_of_var_compartments = 0; unsigned int num_of_var_species_reference = 0; unsigned int sum_num_of_vars; /* All variables (whose quantity is not a constant) */ mySpecies **all_var_sp; /* all variable species */ myParameter **all_var_param; /* all variable parameters */ myCompartment **all_var_comp; /* all variable compartments */ mySpeciesReference **all_var_spr; /* all varialbe SpeciesReferences */ /* variables (which is NOT changed by assignment nor algebraic rule) */ mySpecies **var_sp; myParameter **var_param; myCompartment **var_comp; mySpeciesReference **var_spr; set_seed(); check_num(num_of_species, num_of_parameters, num_of_compartments, num_of_reactions, &num_of_all_var_species, &num_of_all_var_parameters, &num_of_all_var_compartments, &num_of_all_var_species_reference, &num_of_var_species, &num_of_var_parameters, &num_of_var_compartments, &num_of_var_species_reference, sp, param, comp, re); /* create objects */ all_var_sp = (mySpecies **)malloc(sizeof(mySpecies *) * num_of_all_var_species); all_var_param = (myParameter **)malloc(sizeof(myParameter *) * num_of_all_var_parameters); all_var_comp = (myCompartment **)malloc(sizeof(myCompartment *) * num_of_all_var_compartments); all_var_spr = (mySpeciesReference **)malloc(sizeof(mySpeciesReference *) * num_of_all_var_species_reference); var_sp = (mySpecies **)malloc(sizeof(mySpecies *) * num_of_var_species); var_param = (myParameter **)malloc(sizeof(myParameter *) * num_of_var_parameters); var_comp = (myCompartment **)malloc(sizeof(myCompartment *) * num_of_var_compartments); var_spr = (mySpeciesReference **)malloc(sizeof(mySpeciesReference *) * num_of_var_species_reference); /* mySpecies *all_var_sp[num_of_all_var_species]; */ /* myParameter *all_var_param[num_of_all_var_parameters]; */ /* myCompartment *all_var_comp[num_of_all_var_compartments]; */ /* mySpeciesReference *all_var_spr[num_of_all_var_species_reference]; */ /* mySpecies *var_sp[num_of_var_species]; */ /* myParameter *var_param[num_of_var_parameters]; */ /* myCompartment *var_comp[num_of_var_compartments]; */ /* mySpeciesReference *var_spr[num_of_var_species_reference]; */ create_calc_object_list(num_of_species, num_of_parameters, num_of_compartments, num_of_reactions, all_var_sp, all_var_param, all_var_comp, all_var_spr, var_sp, var_param, var_comp, var_spr, sp, param, comp, re); sum_num_of_vars = num_of_var_species + num_of_var_parameters + num_of_var_compartments + num_of_var_species_reference; jacobian = (double**)malloc(sizeof(double*)*(sum_num_of_vars)); for(i=0; i<sum_num_of_vars; i++){ jacobian[i] = (double*)malloc(sizeof(double)*(sum_num_of_vars)); } b = (double *)malloc(sizeof(double) * (sum_num_of_vars)); pre_b = (double *)malloc(sizeof(double) * (sum_num_of_vars)); p = (int *)malloc(sizeof(int) * (sum_num_of_vars)); delta_value = (double *)malloc(sizeof(double) * (sum_num_of_vars)); k_t = (double *)malloc(sizeof(double) * (sum_num_of_vars)); /* double b[sum_num_of_vars]; double pre_b[sum_num_of_vars]; int p[sum_num_of_vars]; double delta_value[sum_num_of_vars]; double k_t[sum_num_of_vars]; */ if(algEq != NULL){ coefficient_matrix = (double**)malloc(sizeof(double*)*(algEq->num_of_algebraic_variables)); for(i=0; i<algEq->num_of_algebraic_variables; i++){ coefficient_matrix[i] = (double*)malloc(sizeof(double)*(algEq->num_of_algebraic_variables)); } constant_vector = (double*)malloc(sizeof(double)*(algEq->num_of_algebraic_variables)); alg_pivot = (int*)malloc(sizeof(int)*(algEq->num_of_algebraic_variables)); } PRG_TRACE(("Simulation for [%s] Starts!\n", Model_getId(m))); cycle = 0; /* initialize delay_val */ initialize_delay_val(sp, num_of_species, param, num_of_parameters, comp, num_of_compartments, re, num_of_reactions, sim_time, dt, 0); /* calc temp value by assignment */ for(i=0; i<num_of_all_var_species; i++){ if(all_var_sp[i]->depending_rule != NULL && all_var_sp[i]->depending_rule->is_assignment){ all_var_sp[i]->temp_value = calc(all_var_sp[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_parameters; i++){ if(all_var_param[i]->depending_rule != NULL && all_var_param[i]->depending_rule->is_assignment){ all_var_param[i]->temp_value = calc(all_var_param[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_compartments; i++){ if(all_var_comp[i]->depending_rule != NULL && all_var_comp[i]->depending_rule->is_assignment){ all_var_comp[i]->temp_value = calc(all_var_comp[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_species_reference; i++){ if(all_var_spr[i]->depending_rule != NULL && all_var_spr[i]->depending_rule->is_assignment){ all_var_spr[i]->temp_value = calc(all_var_spr[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } /* forwarding value */ forwarding_value(all_var_sp, num_of_all_var_species, all_var_param, num_of_all_var_parameters, all_var_comp, num_of_all_var_compartments, all_var_spr, num_of_all_var_species_reference); /* initialize delay_val */ initialize_delay_val(sp, num_of_species, param, num_of_parameters, comp, num_of_compartments, re, num_of_reactions, sim_time, dt, 0); /* calc InitialAssignment */ calc_initial_assignment(initAssign, num_of_initialAssignments, dt, cycle, &reverse_time); /* initialize delay_val */ initialize_delay_val(sp, num_of_species, param, num_of_parameters, comp, num_of_compartments, re, num_of_reactions, sim_time, dt, 0); /* rewriting for explicit delay */ for(i=0; i<num_of_initialAssignments; i++){ for(j=0; j<initAssign[i]->eq->math_length; j++){ if(initAssign[i]->eq->number[j] == time){ TRACE(("time is replaced with reverse time\n")); initAssign[i]->eq->number[j] = &reverse_time; }else if(initAssign[i]->eq->number[j] != NULL){ init_val = (double*)malloc(sizeof(double)); *init_val = *initAssign[i]->eq->number[j]; mem->memory[mem->num_of_allocated_memory++] = init_val; initAssign[i]->eq->number[j] = init_val; } } } for(i=0; i<timeVarAssign->num_of_time_variant_assignments; i++){ for(j=0; j<timeVarAssign->eq[i]->math_length; j++){ if(timeVarAssign->eq[i]->number[j] == time){ TRACE(("time is replaced with reverse time\n")); timeVarAssign->eq[i]->number[j] = &reverse_time; }else if(timeVarAssign->eq[i]->number[j] != NULL){ init_val = (double*)malloc(sizeof(double)); *init_val = *timeVarAssign->eq[i]->number[j]; mem->memory[mem->num_of_allocated_memory++] = init_val; timeVarAssign->eq[i]->number[j] = init_val; } } } /* initialize delay_val */ initialize_delay_val(sp, num_of_species, param, num_of_parameters, comp, num_of_compartments, re, num_of_reactions, sim_time, dt, 0); /* calc temp value by assignment */ for(i=0; i<num_of_all_var_species; i++){ if(all_var_sp[i]->depending_rule != NULL && all_var_sp[i]->depending_rule->is_assignment){ all_var_sp[i]->temp_value = calc(all_var_sp[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_parameters; i++){ if(all_var_param[i]->depending_rule != NULL && all_var_param[i]->depending_rule->is_assignment){ all_var_param[i]->temp_value = calc(all_var_param[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_compartments; i++){ if(all_var_comp[i]->depending_rule != NULL && all_var_comp[i]->depending_rule->is_assignment){ all_var_comp[i]->temp_value = calc(all_var_comp[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_species_reference; i++){ if(all_var_spr[i]->depending_rule != NULL && all_var_spr[i]->depending_rule->is_assignment){ all_var_spr[i]->temp_value = calc(all_var_spr[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } /* forwarding value */ forwarding_value(all_var_sp, num_of_all_var_species, all_var_param, num_of_all_var_parameters, all_var_comp, num_of_all_var_compartments, all_var_spr, num_of_all_var_species_reference); /* initialize delay_val */ initialize_delay_val(sp, num_of_species, param, num_of_parameters, comp, num_of_compartments, re, num_of_reactions, sim_time, dt, 0); /* calc temp value algebraic by algebraic */ if(algEq != NULL){ if(algEq->num_of_algebraic_variables > 1){ /* initialize pivot */ for(i=0; i<algEq->num_of_algebraic_variables; i++){ alg_pivot[i] = i; } for(i=0; i<algEq->num_of_algebraic_variables; i++){ for(j=0; j<algEq->num_of_algebraic_variables; j++){ coefficient_matrix[i][j] = calc(algEq->coefficient_matrix[i][j], dt, cycle, &reverse_time, 0); /* TRACE(("coefficient matrix[%d][%d] = %lf\n", i, j, coefficient_matrix[i][j])); */ } } for(i=0; i<algEq->num_of_algebraic_variables; i++){ constant_vector[i] = -calc(algEq->constant_vector[i], dt, cycle, &reverse_time, 0); /* TRACE(("constant vector[%d] = %lf\n", i, constant_vector[i])); */ } /* LU decompostion */ error = lu_decomposition(coefficient_matrix, alg_pivot, algEq->num_of_algebraic_variables); if(error == 0){/* failure in LU decomposition */ return NULL; } /* forward substitution & backward substitution */ lu_solve(coefficient_matrix, alg_pivot, algEq->num_of_algebraic_variables, constant_vector); /* for(i=0; i<algEq->num_of_algebraic_variables; i++){ */ /* TRACE(("ans[%d] = %lf\n", i, constant_vector[i])); */ /* } */ for(i=0; i<algEq->num_of_alg_target_sp; i++){ algEq->alg_target_species[i]->target_species->temp_value = constant_vector[algEq->alg_target_species[i]->order]; } for(i=0; i<algEq->num_of_alg_target_param; i++){ algEq->alg_target_parameter[i]->target_parameter->temp_value = constant_vector[algEq->alg_target_parameter[i]->order]; } for(i=0; i<algEq->num_of_alg_target_comp; i++){ /* new code */ for(j=0; j<algEq->alg_target_compartment[i]->target_compartment->num_of_including_species; j++){ if(algEq->alg_target_compartment[i]->target_compartment->including_species[j]->is_concentration){ algEq->alg_target_compartment[i]->target_compartment->including_species[j]->temp_value = algEq->alg_target_compartment[i]->target_compartment->including_species[j]->temp_value*algEq->alg_target_compartment[i]->target_compartment->temp_value/constant_vector[algEq->alg_target_compartment[i]->order]; } } /* new code end */ algEq->alg_target_compartment[i]->target_compartment->temp_value = constant_vector[algEq->alg_target_compartment[i]->order]; } }else{ if(algEq->target_species != NULL){ algEq->target_species->temp_value = -calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0); } if(algEq->target_parameter != NULL){ algEq->target_parameter->temp_value = -calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0); } if(algEq->target_compartment != NULL){ /* new code */ for(i=0; i<algEq->target_compartment->num_of_including_species; i++){ if(algEq->target_compartment->including_species[i]->is_concentration){ algEq->target_compartment->including_species[i]->temp_value = algEq->target_compartment->including_species[i]->temp_value*algEq->target_compartment->temp_value/(-calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0)); } } /* new code end */ algEq->target_compartment->temp_value = -calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0); } } /* forwarding value */ forwarding_value(all_var_sp, num_of_all_var_species, all_var_param, num_of_all_var_parameters, all_var_comp, num_of_all_var_compartments, all_var_spr, num_of_all_var_species_reference); } /* initialize delay_val */ initialize_delay_val(sp, num_of_species, param, num_of_parameters, comp, num_of_compartments, re, num_of_reactions, sim_time, dt, 1); /* cycle start */ for(cycle=0; cycle<=end_cycle; cycle++){ /* calculate unreversible fast reaction */ for(i=0; i<num_of_reactions; i++){ if(re[i]->is_fast && !re[i]->is_reversible){ if(calc(re[i]->eq, dt, cycle, &reverse_time, 0) > 0){ min_value = DBL_MAX; for(j=0; j<re[i]->num_of_reactants; j++){ if(min_value > re[i]->reactants[j]->mySp->value/calc(re[i]->reactants[j]->eq, dt, cycle, &reverse_time, 0)){ min_value = re[i]->reactants[j]->mySp->value/calc(re[i]->reactants[j]->eq, dt, cycle, &reverse_time, 0); } } for(j=0; j<re[i]->num_of_products; j++){ if(!Species_getBoundaryCondition(re[i]->products[j]->mySp->origin)){ re[i]->products[j]->mySp->value += calc(re[i]->products[j]->eq, dt, cycle, &reverse_time, 0)*min_value; re[i]->products[j]->mySp->temp_value = re[i]->products[j]->mySp->value; } } for(j=0; j<re[i]->num_of_reactants; j++){ if(!Species_getBoundaryCondition(re[i]->reactants[j]->mySp->origin)){ re[i]->reactants[j]->mySp->value -= calc(re[i]->reactants[j]->eq, dt, cycle, &reverse_time, 0)*min_value; re[i]->reactants[j]->mySp->temp_value = re[i]->reactants[j]->mySp->value; } } } } } /* calculate reversible fast reactioin */ for(i=0; i<num_of_reactions; i++){ if(re[i]->is_fast && re[i]->is_reversible){ if(!(Species_getBoundaryCondition(re[i]->products[0]->mySp->origin) && Species_getBoundaryCondition(re[i]->reactants[0]->mySp->origin))){ products_numerator = calc(re[i]->products_equili_numerator, dt, cycle, &reverse_time, 0); reactants_numerator = calc(re[i]->reactants_equili_numerator, dt, cycle, &reverse_time, 0); if(products_numerator > 0 || reactants_numerator > 0){ if(Species_getBoundaryCondition(re[i]->products[0]->mySp->origin)){ re[i]->reactants[0]->mySp->value = (reactants_numerator/products_numerator)*re[i]->products[0]->mySp->value; re[i]->reactants[0]->mySp->temp_value = re[i]->reactants[0]->mySp->value; }else if(Species_getBoundaryCondition(re[i]->reactants[0]->mySp->origin)){ re[i]->products[0]->mySp->value = (products_numerator/reactants_numerator)*re[i]->reactants[0]->mySp->value; re[i]->products[0]->mySp->temp_value = re[i]->products[0]->mySp->value; }else{ re[i]->products[0]->mySp->value = (products_numerator/(products_numerator+reactants_numerator))*(re[i]->products[0]->mySp->temp_value+re[i]->reactants[0]->mySp->temp_value); re[i]->reactants[0]->mySp->value = (reactants_numerator/(products_numerator+reactants_numerator))*(re[i]->products[0]->mySp->temp_value+re[i]->reactants[0]->mySp->temp_value); re[i]->products[0]->mySp->temp_value = re[i]->products[0]->mySp->value; re[i]->reactants[0]->mySp->temp_value = re[i]->reactants[0]->mySp->value; } } } } } /* event */ calc_event(event, num_of_events, dt, *time, cycle, &reverse_time); /* substitute delay val */ substitute_delay_val(sp, num_of_species, param, num_of_parameters, comp, num_of_compartments, re, num_of_reactions, cycle); /* progress */ if(cycle%(int)(end_cycle/10) == 0){ PRG_TRACE(("%3d %%\n", (int)(100*((double)cycle/(double)end_cycle)))); PRG_TRACE(("\x1b[1A")); PRG_TRACE(("\x1b[5D")); } /* print result */ if(cycle%print_interval == 0){ /* Time */ *value_time_p = *time; value_time_p++; /* Species */ for(i=0; i<num_of_species; i++){ /* if(!(Species_getConstant(sp[i]->origin) && Species_getBoundaryCondition(sp[i]->origin))){ // XXX must remove this */ if(print_amount){ if(sp[i]->is_concentration){ *value_sp_p = sp[i]->value*sp[i]->locating_compartment->value; }else{ *value_sp_p = sp[i]->value; } }else{ if(sp[i]->is_amount){ *value_sp_p = sp[i]->value/sp[i]->locating_compartment->value; }else{ *value_sp_p = sp[i]->value; } } value_sp_p++; /* } */ } /* Parameter */ for(i=0; i<num_of_parameters; i++){ /* if(!Parameter_getConstant(param[i]->origin)){ // XXX must remove this */ *value_param_p = param[i]->value; /* } */ value_param_p++; } /* Compartment */ for(i=0; i<num_of_compartments; i++){ /* if(!Compartment_getConstant(comp[i]->origin)){ // XXX must remove this */ *value_comp_p = comp[i]->value; /* } */ value_comp_p++; } } /* time increase */ *time = (cycle+1)*dt; /* implicit method */ /* define init value by Euler start */ calc_k(all_var_sp, num_of_all_var_species, all_var_param, num_of_all_var_parameters, all_var_comp, num_of_all_var_compartments, all_var_spr, num_of_all_var_species_reference, re, num_of_reactions, rule, num_of_rules, cycle, dt, &reverse_time, 0, 1); /* preserve k(t) value */ for(i=0; i<sum_num_of_vars; i++){ if(i < num_of_var_species){ k_t[i] = var_sp[i]->k[0]; }else if(i < num_of_var_species+num_of_var_parameters){ k_t[i] = var_param[i-num_of_var_species]->k[0]; }else if(i < num_of_var_species+num_of_var_parameters+num_of_var_compartments){ k_t[i] = var_comp[i-num_of_var_species-num_of_var_parameters]->k[0]; }else{ k_t[i] = var_spr[i-num_of_var_species-num_of_var_parameters-num_of_var_compartments]->k[0]; } } calc_temp_value(all_var_sp, num_of_all_var_species, all_var_param, num_of_all_var_parameters, all_var_comp, num_of_all_var_compartments, all_var_spr, num_of_all_var_species_reference, dt, 0); /* define init value by Euler end */ /* newton method */ if(use_lazy_method){ is_convergence = 0; for(i=0; i<sum_num_of_vars; i++){ pre_b[i] = 0; } } flag = 1; while(flag){ /* calc b */ calc_k(var_sp, num_of_var_species, var_param, num_of_var_parameters, var_comp, num_of_var_compartments, var_spr, num_of_var_species_reference, re, num_of_reactions, rule, num_of_rules, cycle, dt, &reverse_time, 0, 0); for(i=0; i<num_of_var_species; i++){ k_next = var_sp[i]->k[0]; b[i] = calc_implicit_formula(order, var_sp[i]->temp_value, var_sp[i]->value, var_sp[i]->prev_val[0], var_sp[i]->prev_val[1], var_sp[i]->prev_val[2], k_next, k_t[i], var_sp[i]->prev_k[0], var_sp[i]->prev_k[1], dt); } for(i=0; i<num_of_var_parameters; i++){ b[num_of_var_species+i] = calc_implicit_formula(order, var_param[i]->temp_value, var_param[i]->value, var_param[i]->prev_val[0], var_param[i]->prev_val[1], var_param[i]->prev_val[2], var_param[i]->k[0], k_t[num_of_var_species+i], var_param[i]->prev_k[0], var_param[i]->prev_k[1], dt); } for(i=0; i<num_of_var_compartments; i++){ b[num_of_var_species+num_of_var_parameters+i] = calc_implicit_formula(order, var_comp[i]->temp_value, var_comp[i]->value, var_comp[i]->prev_val[0], var_comp[i]->prev_val[1], var_comp[i]->prev_val[2], var_comp[i]->k[0], k_t[num_of_var_species+num_of_var_parameters+i], var_comp[i]->prev_k[0], var_comp[i]->prev_k[1], dt); } for(i=0; i<num_of_var_species_reference; i++){ b[num_of_var_species+num_of_var_parameters+num_of_var_compartments+i] = calc_implicit_formula(order, var_spr[i]->temp_value, var_spr[i]->value, var_spr[i]->prev_val[0], var_spr[i]->prev_val[1], var_spr[i]->prev_val[2], var_spr[i]->k[0], k_t[num_of_var_species+num_of_var_parameters+num_of_var_compartments+i], var_spr[i]->prev_k[0], var_spr[i]->prev_k[1], dt); } if(!use_lazy_method || !is_convergence){ /* calc jacobian by numerical differentiation */ for(loop=0; loop<sum_num_of_vars; loop++){ if(loop < num_of_var_species){ var_sp[loop]->temp_value += delta; }else if(loop < num_of_var_species+num_of_var_parameters){ var_param[loop-num_of_var_species]->temp_value += delta; }else if(loop < num_of_var_species+num_of_var_parameters+num_of_var_compartments){ var_comp[loop-num_of_var_species-num_of_var_parameters]->temp_value += delta; }else{ var_spr[loop-num_of_var_species-num_of_var_parameters-num_of_var_compartments]->temp_value += delta; } calc_k(var_sp, num_of_var_species, var_param, num_of_var_parameters, var_comp, num_of_var_compartments, var_spr, num_of_var_species_reference, re, num_of_reactions, rule, num_of_rules, cycle, dt, &reverse_time, 0, 0); for(i=0; i<num_of_var_species; i++){ k_next = var_sp[i]->k[0]; delta_value[i] = calc_implicit_formula(order, var_sp[i]->temp_value, var_sp[i]->value, var_sp[i]->prev_val[0], var_sp[i]->prev_val[1], var_sp[i]->prev_val[2], k_next, k_t[i], var_sp[i]->prev_k[0], var_sp[i]->prev_k[1], dt); /* numerical differentiation */ jacobian[i][loop] = (delta_value[i]-b[i])/delta; } for(i=0; i<num_of_var_parameters; i++){ delta_value[num_of_var_species+i] = calc_implicit_formula(order, var_param[i]->temp_value, var_param[i]->value, var_param[i]->prev_val[0], var_param[i]->prev_val[1], var_param[i]->prev_val[2], var_param[i]->k[0], k_t[num_of_var_species+i], var_param[i]->prev_k[0], var_param[i]->prev_k[1], dt); /* numerical differentiation */ jacobian[num_of_var_species+i][loop] = (delta_value[num_of_var_species+i]-b[num_of_var_species+i])/delta; } for(i=0; i<num_of_var_compartments; i++){ delta_value[num_of_var_species+num_of_var_parameters+i] = calc_implicit_formula(order, var_comp[i]->temp_value, var_comp[i]->value, var_comp[i]->prev_val[0], var_comp[i]->prev_val[1], var_comp[i]->prev_val[2], var_comp[i]->k[0], k_t[num_of_var_species+num_of_var_parameters+i], var_comp[i]->prev_k[0], var_comp[i]->prev_k[1], dt); /* numerical differentiation */ jacobian[num_of_var_species+num_of_var_parameters+i][loop] = (delta_value[num_of_var_species+num_of_var_parameters+i]-b[num_of_var_species+num_of_var_parameters+i])/delta; } for(i=0; i<num_of_var_species_reference; i++){ delta_value[num_of_var_species+num_of_var_parameters+num_of_var_compartments+i] = calc_implicit_formula(order, var_spr[i]->temp_value, var_spr[i]->value, var_spr[i]->prev_val[0], var_spr[i]->prev_val[1], var_spr[i]->prev_val[2], var_spr[i]->k[0], k_t[num_of_var_species+num_of_var_parameters+num_of_var_compartments+i], var_spr[i]->prev_k[0], var_spr[i]->prev_k[1], dt); /* numerical differentiation */ jacobian[num_of_var_species+num_of_var_parameters+num_of_var_compartments+i][loop] = (delta_value[num_of_var_species+num_of_var_parameters+num_of_var_compartments+i]-b[num_of_var_species+num_of_var_parameters+num_of_var_compartments+i])/delta; } if(loop < num_of_var_species){ var_sp[loop]->temp_value -= delta; }else if(loop < num_of_var_species+num_of_var_parameters){ var_param[loop-num_of_var_species]->temp_value -= delta; }else if(loop < num_of_var_species+num_of_var_parameters+num_of_var_compartments){ var_comp[loop-num_of_var_species-num_of_var_parameters]->temp_value -= delta; }else{ var_spr[loop-num_of_var_species-num_of_var_parameters-num_of_var_compartments]->temp_value -= delta; } } } /* initialize p */ for(i=0; i<sum_num_of_vars; i++){ p[i] = i; } /* LU decomposition */ error = lu_decomposition(jacobian, p, sum_num_of_vars); if(error == 0){/* failure in LU decomposition */ return NULL; } /* forward substitution & backward substitution */ lu_solve(jacobian, p, sum_num_of_vars, b); /* calculate next temp value */ for(i=0; i<sum_num_of_vars; i++){ if(i < num_of_var_species){ var_sp[i]->temp_value -= b[i]; }else if(i < num_of_var_species+num_of_var_parameters){ var_param[i-num_of_var_species]->temp_value -= b[i]; }else if(i < num_of_var_species+num_of_var_parameters+num_of_var_compartments){ var_comp[i-num_of_var_species-num_of_var_parameters]->temp_value -= b[i]; }else{ var_spr[i-num_of_var_species-num_of_var_parameters-num_of_var_compartments]->temp_value -= b[i]; } } /* convergence judgement */ if(use_lazy_method){ is_convergence = 1; for(i=0; i<sum_num_of_vars; i++){ if(fabs(b[i]) > fabs(pre_b[i])){ is_convergence = 0; } } for(i=0; i<sum_num_of_vars; i++){ pre_b[i] = b[i]; } } /* error judgement */ flag = 0; for(i=0; i<sum_num_of_vars; i++){ if(fabs(b[i]) > tolerance){ flag = 1; } } } /* calc temp value by assignment */ for(i=0; i<num_of_all_var_species; i++){ if(all_var_sp[i]->depending_rule != NULL && all_var_sp[i]->depending_rule->is_assignment){ all_var_sp[i]->temp_value = calc(all_var_sp[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_parameters; i++){ if(all_var_param[i]->depending_rule != NULL && all_var_param[i]->depending_rule->is_assignment){ all_var_param[i]->temp_value = calc(all_var_param[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_compartments; i++){ if(all_var_comp[i]->depending_rule != NULL && all_var_comp[i]->depending_rule->is_assignment){ all_var_comp[i]->temp_value = calc(all_var_comp[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } for(i=0; i<num_of_all_var_species_reference; i++){ if(all_var_spr[i]->depending_rule != NULL && all_var_spr[i]->depending_rule->is_assignment){ all_var_spr[i]->temp_value = calc(all_var_spr[i]->depending_rule->eq, dt, cycle, &reverse_time, 0); } } /* calc temp value algebraic by algebraic */ if(algEq != NULL){ if(algEq->num_of_algebraic_variables > 1){ /* initialize pivot */ for(i=0; i<algEq->num_of_algebraic_variables; i++){ alg_pivot[i] = i; } for(i=0; i<algEq->num_of_algebraic_variables; i++){ for(j=0; j<algEq->num_of_algebraic_variables; j++){ coefficient_matrix[i][j] = calc(algEq->coefficient_matrix[i][j], dt, cycle, &reverse_time, 0); } } for(i=0; i<algEq->num_of_algebraic_variables; i++){ constant_vector[i] = -calc(algEq->constant_vector[i], dt, cycle, &reverse_time, 0); } /* LU decompostion */ error = lu_decomposition(coefficient_matrix, alg_pivot, algEq->num_of_algebraic_variables); if(error == 0){/* failure in LU decomposition */ return NULL; } /* forward substitution & backward substitution */ lu_solve(coefficient_matrix, alg_pivot, algEq->num_of_algebraic_variables, constant_vector); for(i=0; i<algEq->num_of_alg_target_sp; i++){ algEq->alg_target_species[i]->target_species->temp_value = constant_vector[algEq->alg_target_species[i]->order]; } for(i=0; i<algEq->num_of_alg_target_param; i++){ algEq->alg_target_parameter[i]->target_parameter->temp_value = constant_vector[algEq->alg_target_parameter[i]->order]; } for(i=0; i<algEq->num_of_alg_target_comp; i++){ /* new code */ for(j=0; j<algEq->alg_target_compartment[i]->target_compartment->num_of_including_species; j++){ if(algEq->alg_target_compartment[i]->target_compartment->including_species[j]->is_concentration){ algEq->alg_target_compartment[i]->target_compartment->including_species[j]->temp_value = algEq->alg_target_compartment[i]->target_compartment->including_species[j]->temp_value*algEq->alg_target_compartment[i]->target_compartment->temp_value/constant_vector[algEq->alg_target_compartment[i]->order]; } } /* new code end */ algEq->alg_target_compartment[i]->target_compartment->temp_value = constant_vector[algEq->alg_target_compartment[i]->order]; } }else{ if(algEq->target_species != NULL){ algEq->target_species->temp_value = -calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0); } if(algEq->target_parameter != NULL){ algEq->target_parameter->temp_value = -calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0); } if(algEq->target_compartment != NULL){ /* new code */ for(i=0; i<algEq->target_compartment->num_of_including_species; i++){ if(algEq->target_compartment->including_species[i]->is_concentration){ algEq->target_compartment->including_species[i]->temp_value = algEq->target_compartment->including_species[i]->temp_value*algEq->target_compartment->temp_value/(-calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0)); } } /* new code end */ algEq->target_compartment->temp_value = -calc(algEq->constant, dt, cycle, &reverse_time, 0)/calc(algEq->coefficient, dt, cycle, &reverse_time, 0); } } } /* preserve prev_value and prev_k for multistep solution */ for(i=0; i<num_of_var_species; i++){ var_sp[i]->prev_val[2] = var_sp[i]->prev_val[1]; var_sp[i]->prev_val[1] = var_sp[i]->prev_val[0]; var_sp[i]->prev_val[0] = var_sp[i]->value; var_sp[i]->prev_k[2] = var_sp[i]->prev_k[1]; var_sp[i]->prev_k[1] = var_sp[i]->prev_k[0]; var_sp[i]->prev_k[0] = k_t[i]; } for(i=0; i<num_of_var_parameters; i++){ var_param[i]->prev_val[2] = var_param[i]->prev_val[1]; var_param[i]->prev_val[1] = var_param[i]->prev_val[0]; var_param[i]->prev_val[0] = var_param[i]->value; var_param[i]->prev_k[2] = var_param[i]->prev_k[1]; var_param[i]->prev_k[1] = var_param[i]->prev_k[0]; var_param[i]->prev_k[0] = k_t[num_of_var_species+i]; } for(i=0; i<num_of_var_compartments; i++){ var_comp[i]->prev_val[2] = var_comp[i]->prev_val[1]; var_comp[i]->prev_val[1] = var_comp[i]->prev_val[0]; var_comp[i]->prev_val[0] = var_comp[i]->value; var_comp[i]->prev_k[2] = var_comp[i]->prev_k[1]; var_comp[i]->prev_k[1] = var_comp[i]->prev_k[0]; var_comp[i]->prev_k[0] = k_t[num_of_var_species+num_of_var_parameters+i]; } for(i=0; i<num_of_var_species_reference; i++){ var_spr[i]->prev_val[2] = var_spr[i]->prev_val[1]; var_spr[i]->prev_val[1] = var_spr[i]->prev_val[0]; var_spr[i]->prev_val[0] = var_spr[i]->value; var_spr[i]->prev_k[2] = var_spr[i]->prev_k[1]; var_spr[i]->prev_k[1] = var_spr[i]->prev_k[0]; var_spr[i]->prev_k[0] = k_t[num_of_var_species+num_of_var_parameters+i]; } /* forwarding value */ forwarding_value(all_var_sp, num_of_all_var_species, all_var_param, num_of_all_var_parameters, all_var_comp, num_of_all_var_compartments, all_var_spr, num_of_all_var_species_reference); } PRG_TRACE(("Simulation for [%s] Ends!\n", Model_getId(m))); if(algEq != NULL){ for(i=0; i<algEq->num_of_algebraic_variables; i++){ free(coefficient_matrix[i]); } free(coefficient_matrix); free(constant_vector); free(alg_pivot); } for(i=0; i<sum_num_of_vars; i++){ free(jacobian[i]); } free(all_var_sp); free(all_var_param); free(all_var_comp); free(all_var_spr); free(var_sp); free(var_param); free(var_comp); free(var_spr); /* for implicit */ free(jacobian); return result; }
int main (int argc, char* argv[]) { unsigned int i,j,errors; const char* filename; SBMLDocument_t* document; Model_t* m; if (argc != 2) { printf("\nUsage: printNotes filename\n\n"); return 1; } filename = argv[1]; document = readSBML(filename); errors = SBMLDocument_getNumErrors( document); printf("\n%s\n\n", filename); if(errors > 0) { SBMLDocument_printErrors(document, stderr); SBMLDocument_free(document); return errors; } /* Model */ m = SBMLDocument_getModel(document); printNotes((SBase_t*)m, Model_getId(m)); for(i=0; i < Model_getNumReactions(m); i++) { Reaction_t* re = Model_getReaction( m, i); printNotes((SBase_t*)re, Reaction_getId(re)); /* SpeciesReference (Reactant) */ for(j=0; j < Reaction_getNumReactants( re); j++) { SpeciesReference_t* rt = Reaction_getReactant(re, j); if (SBase_isSetNotes((SBase_t*) rt)) printf(" "); printNotes((SBase_t*)rt, SpeciesReference_getSpecies( rt ) ); } /* SpeciesReference (Product) */ for(j=0; j < Reaction_getNumProducts( re ); j++) { SpeciesReference_t* rt = Reaction_getProduct( re, j); if (SBase_isSetNotes((SBase_t*) rt)) printf(" "); printNotes((SBase_t*)rt, SpeciesReference_getSpecies( rt ) ); } /* ModifierSpeciesReference (Modifiers) */ for(j=0; j < Reaction_getNumModifiers( re ); j++) { SpeciesReference_t* md = Reaction_getModifier(re, j); if (SBase_isSetNotes((SBase_t*) md)) printf(" "); printNotes((SBase_t*)md, SpeciesReference_getSpecies( md ) ); } /* KineticLaw */ if(Reaction_isSetKineticLaw( re )) { KineticLaw_t* kl = Reaction_getKineticLaw( re ); if (SBase_isSetNotes((SBase_t*) kl)) printf(" "); printNotes((SBase_t*)kl, ""); /* Parameter */ for(j=0; j < KineticLaw_getNumParameters( kl ); j++) { Parameter_t* pa = KineticLaw_getParameter( kl, j); if (SBase_isSetNotes((SBase_t*) pa)) printf(" "); printNotes((SBase_t*)pa, Parameter_getId(pa)); } } } /* Species */ for(i=0; i < Model_getNumSpecies(m); i++) { Species_t* sp = Model_getSpecies(m, i); printNotes((SBase_t*)sp, Species_getId(sp)); } /* Compartments */ for(i=0; i < Model_getNumCompartments( m ); i++) { Compartment_t* sp = Model_getCompartment(m, i); printNotes((SBase_t*)sp, Compartment_getId(sp)); } /* FunctionDefinition */ for(i=0; i < Model_getNumFunctionDefinitions(m); i++) { FunctionDefinition_t* sp = Model_getFunctionDefinition(m, i); printNotes((SBase_t*)sp, FunctionDefinition_getId(sp)); } /* UnitDefinition */ for(i=0; i < Model_getNumUnitDefinitions(m); i++) { UnitDefinition_t* sp = Model_getUnitDefinition( m, i); printNotes((SBase_t*)sp, UnitDefinition_getId(sp)); } /* Parameter */ for(i=0; i < Model_getNumParameters( m ); i++) { Parameter_t* sp = Model_getParameter( m, i); printNotes((SBase_t*)sp, Parameter_getId(sp)); } /* Rule */ for(i=0; i < Model_getNumReactions( m ); i++) { Rule_t* sp = Model_getRule(m, i); printNotes((SBase_t*)sp, ""); } /* InitialAssignment */ for(i=0; i < Model_getNumInitialAssignments(m); i++) { InitialAssignment_t* sp = Model_getInitialAssignment(m, i); printNotes((SBase_t*)sp, ""); } /* Event */ for(i=0; i < Model_getNumEvents(m); i++) { Event_t* sp = Model_getEvent(m, i); printNotes((SBase_t*)sp, Event_getId(sp)); /* Trigger */ if(Event_isSetTrigger( sp )) { Trigger_t* tg = Event_getTrigger(sp); if (SBase_isSetNotes( (SBase_t*) tg)) printf( " " ); printNotes((SBase_t*)tg, ""); } /* Delay */ if(Event_isSetDelay(sp)) { Delay_t* dl = Event_getDelay(sp); if (SBase_isSetNotes( (SBase_t*) dl)) printf( " " ); printNotes((SBase_t*) dl, ""); } /* EventAssignment */ for(j=0; j < Event_getNumEventAssignments(sp); j++) { EventAssignment_t* ea = Event_getEventAssignment(sp, j); if (SBase_isSetNotes( (SBase_t*) ea)) printf( " " ); printNotes((SBase_t*)ea, ""); } } /* SpeciesType */ for(i=0; i < Model_getNumSpeciesTypes(m); i++) { SpeciesType_t* sp = Model_getSpeciesType(m, i); printNotes((SBase_t*)sp, SpeciesType_getId(sp)); } /* Constraints */ for(i=0; i < Model_getNumConstraints(m); i++) { Constraint_t* sp = Model_getConstraint(m, i); printNotes((SBase_t*)sp, ""); } SBMLDocument_free( document ); return errors; }
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; }