diff --git a/biosimulators_tellurium/_version.py b/biosimulators_tellurium/_version.py index fbd991f..8e7b6eb 100644 --- a/biosimulators_tellurium/_version.py +++ b/biosimulators_tellurium/_version.py @@ -1 +1 @@ -__version__ = '0.1.41' +__version__ = '0.1.42' diff --git a/biosimulators_tellurium/core.py b/biosimulators_tellurium/core.py index 3236786..acc5fec 100644 --- a/biosimulators_tellurium/core.py +++ b/biosimulators_tellurium/core.py @@ -273,12 +273,41 @@ def exec_sed_task(task, variables, preprocessed_task=None, log=None, config=None results = numpy.array(road_runner.simulate(sim.output_start_time, sim.output_end_time, sim.number_of_steps+1).tolist()).transpose() else: - road_runner.steadyState() - results = road_runner.getSteadyStateValues() + results = None + simdists = [0, 0.1, 1, 10, 100, 1000] + sd = 0 + lasterr = "" + while sd < len(simdists) and results is None: + try: + if simdists[sd] > 0: + road_runner.resetAll() + if model.changes: + for change in model.changes: + component_id = preprocessed_task.model_change_target_tellurium_id_maps[task.id][(change.model, + change.target, + change.symbol)] + new_value = float(change.new_value) + road_runner[component_id] = new_value + road_runner.simulate(end=simdists[sd]) + road_runner.steadyState() + results = road_runner.getSteadyStateValues() + except Exception as e: + lasterr = str(e) + sd += 1 + if results is None: + msg = 'Steady state analysis failed with algorithm `{}` ({}):'.format( + preprocessed_task.algorithm_kisao_ids[task.id], + KISAO_ALGORITHM_MAP[preprocessed_task.algorithm_kisao_ids[task.id]]['id']) + msg += "\n '" + lasterr + "'" + for i_param in range(preprocessed_task.solvers[task.id].getNumParams()): + param_name = preprocessed_task.solvers[task.id].getParamName(i_param) + msg += '\n - {}: {}'.format(param_name, getattr(preprocessed_task.solvers[task.id], param_name)) + raise ValueError(msg) # check simulation succeeded if config.VALIDATE_RESULTS and numpy.any(numpy.isnan(results)): - msg = 'Simulation failed with algorithm `{}` ({})'.format( + msg = 'Simulation failed: ' + str(numpy.count_nonzero(numpy.isnan(results))) +\ + ' nan value(s) found in results with algorithm `{}` ({})'.format( preprocessed_task.algorithm_kisao_ids[task.id], KISAO_ALGORITHM_MAP[preprocessed_task.algorithm_kisao_ids[task.id]]['id']) for i_param in range(preprocessed_task.solvers[task.id].getNumParams()): @@ -425,51 +454,52 @@ def preprocess_sed_task(task, variables, config=None, simulator_config=None): error_summary='{} `{}` is not supported.'.format(sim.__class__.__name__, sim.id)) # set the parameters of the solver - if exec_alg_kisao_id == sim.algorithm.kisao_id: - for change in sim.algorithm.changes: - param_props = alg_props['parameters'].get(change.kisao_id, None) - if not config.VALIDATE_SEDML or param_props: - if not config.VALIDATE_SEDML or validate_str_value(change.new_value, param_props['type']): - new_value = parse_value(change.new_value, param_props['type']) - att = param_props['id'] - if "roadrunner_attribute" in param_props: - att = param_props['roadrunner_attribute'] - setattr(solver, att, new_value) - - else: - if ( - ALGORITHM_SUBSTITUTION_POLICY_LEVELS[algorithm_substitution_policy] - <= ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.NONE] - ): - msg = "'{}' is not a valid {} value for parameter {}".format( - change.new_value, param_props['type'].name, change.kisao_id) - raise ValueError(msg) - else: - msg = "'{}' was ignored because it is not a valid {} value for parameter {}".format( - change.new_value, param_props['type'].name, change.kisao_id) - warn(msg, BioSimulatorsWarning) + for change in sim.algorithm.changes: + param_props = alg_props['parameters'].get(change.kisao_id, None) + if not config.VALIDATE_SEDML or param_props: + if not config.VALIDATE_SEDML or validate_str_value(change.new_value, param_props['type']): + new_value = parse_value(change.new_value, param_props['type']) + att = param_props['id'] + if "roadrunner_attribute" in param_props: + att = param_props['roadrunner_attribute'] + setattr(solver, att, new_value) else: if ( + exec_alg_kisao_id == sim.algorithm.kisao_id and ALGORITHM_SUBSTITUTION_POLICY_LEVELS[algorithm_substitution_policy] <= ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.NONE] ): - msg = "".join([ - "Algorithm parameter with KiSAO id '{}' is not supported. ".format(change.kisao_id), - "Parameter must have one of the following KiSAO ids:\n - {}".format('\n - '.join( - '{}: {} ({})'.format(kisao_id, param_props['id'], param_props['name']) - for kisao_id, param_props in alg_props['parameters'].items())), - ]) - raise NotImplementedError(msg) + msg = "'{}' is not a valid {} value for parameter {}".format( + change.new_value, param_props['type'].name, change.kisao_id) + raise ValueError(msg) else: - msg = "".join([ - "Algorithm parameter with KiSAO id '{}' was ignored because it is not supported. ".format(change.kisao_id), - "Parameter must have one of the following KiSAO ids:\n - {}".format('\n - '.join( - '{}: {} ({})'.format(kisao_id, param_props['id'], param_props['name']) - for kisao_id, param_props in alg_props['parameters'].items())), - ]) + msg = "'{}' was ignored because it is not a valid {} value for parameter {}".format( + change.new_value, param_props['type'].name, change.kisao_id) warn(msg, BioSimulatorsWarning) + else: + if ( + exec_alg_kisao_id == sim.algorithm.kisao_id and + ALGORITHM_SUBSTITUTION_POLICY_LEVELS[algorithm_substitution_policy] + <= ALGORITHM_SUBSTITUTION_POLICY_LEVELS[AlgorithmSubstitutionPolicy.NONE] + ): + msg = "".join([ + "Algorithm parameter with KiSAO id '{}' is not supported. ".format(change.kisao_id), + "Parameter must have one of the following KiSAO ids:\n - {}".format('\n - '.join( + '{}: {} ({})'.format(kisao_id, param_props['id'], param_props['name']) + for kisao_id, param_props in alg_props['parameters'].items())), + ]) + raise NotImplementedError(msg) + else: + msg = "".join([ + "Algorithm parameter with KiSAO id '{}' was ignored because it is not supported. ".format(change.kisao_id), + "Parameter must have one of the following KiSAO ids:\n - {}".format('\n - '.join( + '{}: {} ({})'.format(kisao_id, param_props['id'], param_props['name']) + for kisao_id, param_props in alg_props['parameters'].items())), + ]) + warn(msg, BioSimulatorsWarning) + # validate model changes and build map if isinstance(subtask, RepeatedTask): allchanges = allchanges + subtask.changes