Skip to content

Commit

Permalink
Merge pull request #557 from StochSS/hybrid-c
Browse files Browse the repository at this point in the history
C++ Implementation of the Tau Hybrid solver
  • Loading branch information
ethangreen-dev authored Jul 20, 2021
2 parents 7e8faea + 49710b7 commit 97dbd57
Show file tree
Hide file tree
Showing 27 changed files with 1,894 additions and 265 deletions.
3 changes: 2 additions & 1 deletion gillespy2/solvers/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from gillespy2.solvers.cpp.ode_c_solver import ODECSolver
from gillespy2.solvers.cpp.tau_leaping_c_solver import TauLeapingCSolver
from gillespy2.solvers.cpp.variable_ssa_c_solver import VariableSSACSolver
from gillespy2.solvers.cpp.tau_hybrid_c_solver import TauHybridCSolver

# Check to see if we're missing any dependencies.
from .build.build_engine import BuildEngine
Expand All @@ -34,4 +35,4 @@
)

__all__ = ['SSACSolver', 'VariableSSACSolver']
__all__ = ['SSACSolver', 'ODECSolver', 'TauLeapingCSolver', 'VariableSSACSolver']
__all__ = ['SSACSolver', 'ODECSolver', 'TauLeapingCSolver', 'VariableSSACSolver', 'TauHybridCSolver']
7 changes: 6 additions & 1 deletion gillespy2/solvers/cpp/build/build_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

class BuildEngine():
template_definitions_name = "template_definitions.h"
template_options_name = "template_opts.h"

def __init__(self, debug: bool = False, output_dir: str = None):
self.self_dir = Path(__file__).parent
Expand Down Expand Up @@ -68,7 +69,7 @@ def get_missing_dependencies(cls) -> "list[str]":

return missing

def prepare(self, model: Model, variable=False) -> str:
def prepare(self, model: Model, variable=False, custom_definitions: "dict[str, str]" = None) -> str:
"""
Prepare the template directory for compilation.
The following operations will be performed:
Expand Down Expand Up @@ -109,6 +110,10 @@ def prepare(self, model: Model, variable=False) -> str:
template_file = self.template_dir.joinpath(self.template_definitions_name)
template_file.unlink()
template_gen.write_template(str(template_file), model, variable)
if custom_definitions is not None:
options_file = self.template_dir.joinpath(self.template_options_name)
options_file.unlink()
template_gen.write_definitions(str(options_file), custom_definitions)

# With all required information gathered, create a Make instance.
self.make = Make(str(self.makefile), str(self.output_dir), str(self.obj_dir))
Expand Down
7 changes: 7 additions & 0 deletions gillespy2/solvers/cpp/build/template_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ def write_template(path: str, model: Model, variable=False):
# Get a dictionary of model defines and transform into a list of strings in
# `#define KEY VALUE` format.
defines = get_model_defines(model, variable)
write_definitions(path, defines)

def write_definitions(path: str, defines: "dict[str, str]"):
"""
"""
# Definition dict is transformed into a list of C++ macro definitions, with:
# `#define KEY VALUE` format.
template_lines = [(f"#define {key} {value}\n") for key, value in defines.items()]

# Write generated lines to the template file.
Expand Down
28 changes: 24 additions & 4 deletions gillespy2/solvers/cpp/c_base/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ CXXFLAGS := -std=c++14 -Wall -O3
GPY_SOLVER ?= ssa
GPY_EXE_ARGS ?= --trajectories 1 --timesteps 21 --end 20
#######################################
.PHONY: run clean build profile ode ssa tau_leap
.PHONY: run clean build profile ode ssa tau_leap hybrid

### Input directories ###
CBASE_DIR ?= .
TEMPLATE_DIR ?= $(CBASE_DIR)/template
TEMPLATE_CPP := $(TEMPLATE_DIR)/template.cpp
SUNDIALS_SRC := $(CBASE_DIR)/Sundials/src
SUNDIALS_INC := $(CBASE_DIR)/Sundials/include
TAU_DIR := $(CBASE_DIR)/Tau
ODE_SOLVER_PATH := $(CBASE_DIR)/ode_cpp_solver
SSA_SOLVER_PATH := $(CBASE_DIR)/ssa_cpp_solver
TAU_LEAPING_SOLVER_PATH := $(CBASE_DIR)/tau_leaping_cpp_solver
TAU_HYBRID_SOLVER_PATH := $(CBASE_DIR)/tau_hybrid_cpp_solver
#########################

### Output directories ###
OBJ_DIR ?= $(CBASE_DIR)
INCLUDES := -I$(CBASE_DIR) -I$(SUNDIALS_INC) -I$(TEMPLATE_DIR)
INCLUDES := -I$(CBASE_DIR) -I$(SUNDIALS_INC) -I$(TEMPLATE_DIR) -I$(TAU_DIR)
SUNDIALS_OBJ ?= $(OBJ_DIR)
OUTPUT_DIR ?= $(CBASE_DIR)
ifeq ($(OS),Windows_NT)
Expand All @@ -42,10 +44,19 @@ SUNOBJ_PATHS := $(SUNOBJ:%.o=$(SUNDIALS_OBJ)/%.o)
GPY_SRC = model.cpp arg_parser.cpp
GPY_OBJ := $(GPY_SRC:%.cpp=$(OBJ_DIR)/%.o)

### DEPENDENCIES FOR TAU-LEAPING BASED SOLVERS ###
GPY_SRC_TAUBASE = tau.cpp
GPY_OBJ_TAUBASE := $(GPY_SRC_TAUBASE:%.cpp=$(OBJ_DIR)/%.o)

### DEPENDENCIES FOR ODE SOLVER ###
GPY_SRC_ODE = ODESimulation.cpp ODESolver.cpp
GPY_OBJ_ODE := $(GPY_SRC_ODE:%.cpp=$(OBJ_DIR)/%.o)

### DEPENDENCIES FOR HYBRID SOLVER ###
GPY_SRC_HYBRID = TauHybridSimulation.cpp TauHybridSolver.cpp HybridModel.cpp \
hybrid_template.cpp integrator.cpp
GPY_OBJ_HYBRID := $(GPY_SRC_HYBRID:%.cpp=$(OBJ_DIR)/%.o)

### DEPENDENCIES FOR SSA SOLVER ###
GPY_SRC_SSA = SSASimulation.cpp SSASolver.cpp
GPY_OBJ_SSA := $(GPY_SRC_SSA:%.cpp=$(OBJ_DIR)/%.o)
Expand All @@ -57,6 +68,9 @@ GPY_OBJ_TAU := $(GPY_SRC_TAU:%.cpp=$(OBJ_DIR)/%.o)
$(GPY_OBJ): $(OBJ_DIR)/%.o: $(CBASE_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^ $(INCLUDES)

$(GPY_OBJ_HYBRID): $(OBJ_DIR)/%.o: $(TAU_HYBRID_SOLVER_PATH)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^ $(INCLUDES)

$(GPY_OBJ_ODE): $(OBJ_DIR)/%.o: $(ODE_SOLVER_PATH)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^ $(INCLUDES)

Expand All @@ -66,6 +80,9 @@ $(GPY_OBJ_SSA): $(OBJ_DIR)/%.o: $(SSA_SOLVER_PATH)/%.cpp
$(GPY_OBJ_TAU): $(OBJ_DIR)/%.o: $(TAU_LEAPING_SOLVER_PATH)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^ $(INCLUDES)

$(GPY_OBJ_TAUBASE): $(OBJ_DIR)/%.o: $(TAU_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^ $(INCLUDES)

$(SUNOBJ_PATHS): $(SUNDIALS_OBJ)/%.o: $(SUNDIALS_SRC)/%.c
$(CXX) -c -o $@ $< $(CFLAGS) -I$(SUNDIALS_INC)
sundials: $(SUNOBJ_PATHS) ;
Expand All @@ -80,10 +97,13 @@ ode: $(GPY_ALL_DEPS) $(GPY_OBJ_ODE) $(SUNOBJ_PATHS)
ssa: $(GPY_ALL_DEPS) $(GPY_OBJ_SSA)
$(CXX) $(CXXFLAGS) -o $(OUTPUT_FILE) $^ $(INCLUDES)

tau_leap: $(GPY_ALL_DEPS) $(GPY_OBJ_TAU) $(TEMPLATE_CPP)
tau_leap: $(GPY_ALL_DEPS) $(GPY_OBJ_TAU) $(GPY_OBJ_TAUBASE)
$(CXX) $(CXXFLAGS) -o $(OUTPUT_FILE) $^ $(INCLUDES)

hybrid: $(GPY_ALL_DEPS) $(GPY_OBJ_HYBRID) $(GPY_OBJ_TAUBASE) $(SUNOBJ_PATHS)
$(CXX) $(CXXFLAGS) -o $(OUTPUT_FILE) $^ $(INCLUDES)

build: $(GPY_ALL_DEPS) $(GPY_OBJ_SSA) $(GPY_OBJ_ODE) $(GPY_OBJ_TAU) ;
build: $(GPY_ALL_DEPS) $(GPY_OBJ_TAUBASE) $(GPY_OBJ_SSA) $(GPY_OBJ_ODE) $(GPY_OBJ_TAU) $(GPY_OBJ_HYBRID)) ;

run: $(GPY_SOLVER)
$(OUTPUT_FILE) $(GPY_EXE_ARGS)
Expand Down
Loading

0 comments on commit 97dbd57

Please sign in to comment.