def solve_model(self, configuration): """Solve the parameterised version of the model. This assumes that parameterise_model has already been called and that hence a parameterised version of the model exists in a file named self.paramed_file""" start_time = configuration.start_time stop_time = configuration.stop_time data_points = int((stop_time - start_time) / configuration.out_interval) biopepa_command = [ "java", "-jar", "biopepa.jar", "timeseries", self.paramed_file, "--no-warnings", "--solver", "dopr-adaptive", "--timeStep", str(configuration.interval), "--startTime", str(start_time), "--stopTime", str(stop_time), "--dataPoints", str(data_points), # "--output-file", # csvfile ] biopepa_returncode = run_command(biopepa_command) biopepa_process = Popen(biopepa_command, stdout=PIPE, stderr=PIPE) output, errorout = biopepa_process.communicate() if errorout: logging.error ("The biopepa process produced output on stderr") logging.error (errorout) logging.error ("The biopepa command was: ") logging.error (" ".join(biopepa_command)) if biopepa_returncode != 0: logging.error ("biopepa process failed to return") sys.exit(1) # A bug in pylint causes it to complain about this, it incorrectly # thinks that 'output' is of type list, but it is of type string. output_lines = output.split("\n") timecourse = timeseries.parse_csv(output_lines.__iter__(), ", ") return timecourse
def solve_model(self, configuration): """Solve the given model and return a timeseries, or None if solving the model has failed for some reason""" # We need a relatively portable way to check that this command # succeeds. Because we are constrained for the moment to using # os.system, I'm a little unsure about simply checking the return # code. So for now I'm removing the results file and subsequently # I will check if it exists. # We should be able to update this to use subprocess, but for now # this is better than the alternative of reading in the previous # time series. # We have updated this with the user of subprocess, but I'm leaving # it in for now since it seems quite robust, but it would certainly # be a slight optimistation to avoid doing this. model_dir = os.path.dirname(self.model_exec) results_file_prefix = os.path.join(model_dir, "model") results_file = results_file_prefix + "_RHS.dat" if os.path.exists(results_file): # I'm not sure about this try-except block, I'm trying to # overcome what appears to be a bug in sbsi-numerics which creates # these weird files with nothing in them and setuid's with ???? try: os.remove(results_file) except OSError: pass # This could also be run without mpi. mpirun_command = [ "mpirun", self.model_exec, "model_name", # Could get model name from xml file str(configuration.stop_time), str(configuration.start_time), str(configuration.max_times), str(configuration.interval), str(configuration.out_interval), str(configuration.atol), str(configuration.reltol), results_file_prefix, ] if self.param_filename: mpirun_command.append(self.param_filename) mpi_returncode = run_command(mpirun_command) # So we check if the results file actually exists and if # not we assume it failed. Also now I can actually check # the return code if not os.path.exists(results_file) or mpi_returncode != 0: message = "Model solving failed" logging.warning(message) raise SolverError(message) logging.debug("stop-time == " + str(configuration.stop_time)) logging.debug("start-time == " + str(configuration.start_time)) logging.debug("max-times == " + str(configuration.max_times)) logging.debug("interval == " + str(configuration.interval)) logging.debug("out-interval == " + str(configuration.out_interval)) logging.debug("atol == " + str(configuration.atol)) logging.debug("reltol == " + str(configuration.reltol)) # We should remove the parameter overrides file here in case # we wish to simply solve the model on its own later. # This is actually solved because we have updated the solver file # such that it doesn't simply look to see if UserModel/param_overrides # file is there, but instead demands that it is specified on the # command-line if one is to be used. # if os.path.exists(self.param_filename): # os.remove(self.param_filename) csv_file = open(results_file, "r") timecourse = timeseries.parse_csv(csv_file, "\t") csv_file.close() return timecourse