Skip to content

Commit

Permalink
Reorganize the file for code generation a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
AleMorales committed Mar 13, 2015
1 parent 08b554b commit 931bcf8
Showing 1 changed file with 90 additions and 39 deletions.
129 changes: 90 additions & 39 deletions src/codegeneration.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
########## Check units ###############
####################################################################################
####################################################################################
########################### CHECK UNITS #################################
####################################################################################
####################################################################################

function calculate_units(exp::Expr, units::Dict{Symbol, Dimension}, c::Int64)
ex = deepcopy(exp)
Expand Down Expand Up @@ -44,9 +48,12 @@ The right hand side expression was $(val.Expr)")
end
end

########## Generate Jacobian ###############
####################################################################################
####################################################################################
######################## GENERATE JACOBIAN ##############################
####################################################################################
####################################################################################

using Calculus

# Calculation Jacobian matrix of the model
function generate_jacobian_matrix(compressed_model::OdeSorted)
Expand All @@ -66,7 +73,11 @@ function generate_jacobian_matrix(compressed_model::OdeSorted)
return Jacobian
end

########## Generate Extended System ###############
####################################################################################
####################################################################################
################### GENERATE EXTENDED SYSTEM ############################
####################################################################################
####################################################################################

# Calculate array of sensitivities
function generate_sensitivity_array(compressed_model::OdeSorted)
Expand All @@ -89,7 +100,7 @@ end

####################################################################################
####################################################################################
########################## JULIA CODE GENERATION #################################
########################## SOURCE CODE GENERATION ################################
####################################################################################
####################################################################################

Expand Down Expand Up @@ -177,6 +188,73 @@ function generate_extended_system_julia(compressed_model::OdeSorted, name)
end


####################################################################################
####################################################################################
########################### FULL MODEL GENERATION ################################
####################################################################################
####################################################################################

"""
`generate_code_Julia!` will generate a Julia source file containing the code necessary
to load the model and simulate (using the SimulationModels packge). This function
will return nothing. As a side effect, it will write a file to the hard-drive.
Arguments
----------
`source`: Compulsory argument that contains a path to the ode file
`unit_analysis`: Keyword boolean argument to indicate whether we want to perform
analysis of physical dimensions and unit conversion
`name`: Keyword string argument with the name of the model we want to generate.
This name will be appended to the default names of the functions being generated
as well as the factory function from which we can generate new instances of the model.
`file`: Keyword string argument with the path to the file where we want to generate
the model. It does not have to coincide with the `name` argument given before. Note that
the extension should not be included.
`jacobian`: Keyword boolean argument to indicate whether the Jacobian of the model should
be generated or not.
`sensitivities`: Keyword boolean argument to indicate whether the sensitivity functions
associated to the model should be generated or not.
"""
function generate_code_Julia!(source::String; unit_analysis = false, name = "autogenerated_model", file = "autogenerated_model", jacobian = false, sensitivities = false)
parsed_model = process_file(source)
reaction_model = convert_master_equation(parsed_model)
ode_model = convert_reaction_model(reaction_model)
generate_code_Julia!(ode_model, unit_analysis = unit_analysis, name = name, file = file, jacobian = jacobian, sensitivities = sensitivities)
end

"""
`generate_code_Julia!` will generate a Julia source file containing the code necessary
to load the model and simulate (using the SimulationModels packge). This function
will return nothing. As a side effect, it will write a file to the hard-drive.
Arguments
----------
`source`: Compulsory argument that contains an OdeSource model
`unit_analysis`: Keyword boolean argument to indicate whether we want to perform
analysis of physical dimensions and unit conversion
`name`: Keyword string argument with the name of the model we want to generate.
This name will be appended to the default names of the functions being generated
as well as the factory function from which we can generate new instances of the model.
`file`: Keyword string argument with the path to the file where we want to generate
the model. It does not have to coincide with the `name` argument given before. Note that
the extension should not be included.
`jacobian`: Keyword boolean argument to indicate whether the Jacobian of the model should
be generated or not.
`sensitivities`: Keyword boolean argument to indicate whether the sensitivity functions
associated to the model should be generated or not.
"""

function generate_code_Julia!(ode_model::OdeSource; unit_analysis = true, name = "autogenerated_model", file = "autogenerated_model", jacobian = false, sensitivities = false)

Expand Down Expand Up @@ -244,6 +322,13 @@ function generate_code_Julia!(ode_model::OdeSource; unit_analysis = true, name =
nothing
end


"""
`write_model_Julia!` will write the source file required to run the models in julia.
It is generated according to the interface provided by SimulationModels which will take care
of wrapping the corresponding solvers, converting units, etc.
"""
function write_model_Julia!(States::OrderedDict{ASCIIString, Float64},
Coef_states::OrderedDict{ASCIIString, Float64},
Parameters::OrderedDict{ASCIIString, Float64},
Expand Down Expand Up @@ -302,37 +387,3 @@ function write_model_Julia!(States::OrderedDict{ASCIIString, Float64},
close(f)
nothing
end

"""
`generate_code_Julia!` will generate a Julia source file containing the code necessary
to load the model and simulate (using the SimulationModels packge). This function
will return nothing. As a side effect, it will write a file to the hard-drive.
Arguments
----------
`source`: Compulsory argument that contains a path to the ode file
`unit_analysis`: Keyword boolean argument to indicate whether we want to perform
analysis of physical dimensions and unit conversion
`name`: Keyword string argument with the name of the model we want to generate.
This name will be appended to the default names of the functions being generated
as well as the factory function from which we can generate new instances of the model.
`file`: Keyword string argument with the path to the file where we want to generate
the model. It does not have to coincide with the `name` argument given before. Note that
the extension should not be included.
`jacobian`: Keyword boolean argument to indicate whether the Jacobian of the model should
be generated or not.
`sensitivities`: Keyword boolean argument to indicate whether the sensitivity functions
associated to the model should be generated or not.
"""
function generate_code_Julia!(source::String; unit_analysis = false, name = "autogenerated_model", file = "autogenerated_model", jacobian = false, sensitivities = false)
parsed_model = process_file(source)
reaction_model = convert_master_equation(parsed_model)
ode_model = convert_reaction_model(reaction_model)
generate_code_Julia!(ode_model, unit_analysis = unit_analysis, name = name, file = file, jacobian = jacobian, sensitivities = sensitivities)
end

0 comments on commit 931bcf8

Please sign in to comment.