Skip to content

Commit

Permalink
Create new versions of all files except julia and r translator. When …
Browse files Browse the repository at this point in the history
…the package is thoroughly tested, the old files will be removed.
  • Loading branch information
AleMorales committed Mar 17, 2015
1 parent ceb2dad commit 119c97c
Show file tree
Hide file tree
Showing 10 changed files with 2,174 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/ODEDSL.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module ODEDSL

using Base.Test
using DataStructures
using Docile
using Lexicon
Expand All @@ -14,12 +15,14 @@ import Calculus.differentiate, Base.min, Base.max

# Include the source files
include("utils.jl")
include("datatypes.jl")
using .DataTypes
include("parser.jl")
include("datatypes2.jl")
import .DataTypes
dt = DataTypes
include("parser2.jl")
include("conversion_reaction.jl")
include("conversion_ode.jl")
include("symbolic.jl")
include("conversion.jl")
include("codegeneration.jl")
include("translate_Rcpp.jl")
include("checks2.jl")
include("translate_RCpp2.jl")

end
143 changes: 143 additions & 0 deletions src/checks2.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
####################################################################################
####################################################################################
############################ CHECK RHS ##################################
####################################################################################
####################################################################################

function check_lhs_rhs(model)
new_model = deepcopy(model)
lhs_states, lhs_parameters, lhs_forcings, lhs_constants, lhs_equations = get_lhs(new_model)
all_rhs, rhs_to_lhs, lhs_to_rhs = get_rhs(new_model.Equations)
for i in all_rhs
i [lhs_states, lhs_parameters, lhs_forcings, lhs_constants, lhs_equations] &&
error("The input $i is missing from the model")
end
for i in lhs_parameters
if i all_rhs
warn("The output is not dependent on the parameter $i. It has been removed from the model.")
delete!(new_model.Parameters, i)
end
end
for i in lhs_constants
if i all_rhs
warn("The output is not dependent on the constant $i. It has been removed from the model.")
delete!(new_model.Constants, i)
end
end
for i in lhs_forcings
if i all_rhs
warn("The output is not dependent on the forcing $i. It has been removed from the model.")
delete!(new_model.Forcings, i)
end
end
for i in lhs_states
if i all_rhs
warn("The output is not dependent on the state $i, but it has NOT been removed.")
end
end
for i in lhs_equations
if (i all_rhs) && (i [model.NamesObserved, model.NamesDerivatives])
#warn("The output is not dependent on the intermediate variable $i. It has been removed from the model.")
delete!(new_model.Equations, i)
end
end
return new_model, rhs_to_lhs, lhs_to_rhs
end


####################################################################################
####################################################################################
########################### CHECK UNITS #################################
####################################################################################
####################################################################################

function calculate_units(exp::Expr, units::Dict{Symbol, dt.Dimension}, c::Int64)
ex = deepcopy(exp)
for i in 1:length(ex.args)
if isa(ex.args[i], Expr)
ex.args[i], c = calculate_units(ex.args[i], units, c)
elseif isa(ex.args[i], Symbol) && haskey(units, symbol(ex.args[i]))
c += 1
name = parse("__unit__$(c)")
@eval global $name = ($(units[ex.args[i]]))
ex.args[i] = name
end
end
ex, c
end

function check_units(sorted_model::dt.OdeSorted)
given_units = Dict{Symbol, dt.Dimension}()
for (key,val) in sorted_model.Constants given_units[symbol(key)] = val.Units.d end
for (key,val) in sorted_model.Parameters given_units[symbol(key)] = val.Units.d end
for (key,val) in sorted_model.States given_units[symbol(key)] = val.Units.d end
for (key,val) in sorted_model.Forcings given_units[symbol(key)] = val.Units.d end
for level in sorted_model.SortedEquations
for (key,val) in level given_units[symbol(key)] = val.Units.d end
end
# Add units of time (as input that is always available)
given_units[symbol("time")] = dt.s.d
for level in sorted_model.SortedEquations[2:end]
for (key,val) in level
expected = given_units[symbol(key)]
if isa(val.Expr, Symbol)
infered = eval(given_units[val.Expr])
else
infered_expr, c = calculate_units(val.Expr, given_units, 0)
infered = try eval(infered_expr) catch
error("Error when calculating units: The rhs of equation $key is not dimensionally homogeneous.
The right hand side expression was $(val.Expr)") end
end
expected != infered && error("Error when calculating units: Expected and given units for $key did not coincide.
I infered the units $infered but you assign it $expected.
The right hand side expression was $(val.Expr)")
end
end
end

####################################################################################
####################################################################################
######################## GENERATE JACOBIAN ##############################
####################################################################################
####################################################################################


# Calculation Jacobian matrix of the model
function generate_jacobian_matrix(compressed_model::dt.OdeSorted, names_derivatives)
names_states = collect(keys(compressed_model.States))
Jacobian = Array(Union(Expr, Symbol, Number),(length(names_states), length(names_states)))
cs = 1
for j in names_states
cd = 1
for i in names_derivatives
@inbounds Jacobian[cd,cs] = differentiate(compressed_model.SortedEquations[2][i].Expr, parse(j))
cd += 1
end
cs += 1
end
return Jacobian
end

####################################################################################
####################################################################################
################### GENERATE EXTENDED SYSTEM ############################
####################################################################################
####################################################################################

# Calculate array of sensitivities
function generate_sensitivity_array(compressed_model::dt.OdeSorted)
names_states = collect(keys(compressed_model.States))
names_derivatives = ["d_"*x*"_dt" for x in names_states]
Sensitivity = Array{Union(Expr, Symbol, Number), 1}[]
names_parameters = collect(keys(compressed_model.Parameters))
for i in names_parameters
sens = Array(Union(Expr, Symbol, Number), length(names_states))
c = 1
for j in names_derivatives
sens[c] = differentiate(compressed_model.SortedEquations[2][j].Expr, parse(i))
c += 1
end
push!(Sensitivity, sens)
end
return Sensitivity
end
8 changes: 4 additions & 4 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,20 @@ end
# Reduces any model to level 2 (i.e. only input instruction and calculatio of observers and time derivatives)
#######
function compress_model(model::OdeSorted; level = 2)
equations = deepcopy(model.SortedEquations)
new_model = deepcopy(model)
equations = new_model.SortedEquations
if length(equations) < level + 1
return model
return new_model
end
for i in linrange(length(equations), level + 1, length(equations) - level)
for (key,val) in equations[i]
equations[i][key].Expr = expand_expression(val.Expr, equations)
equations[i-1][key] = equations[i][key]
end
end
new_model = deepcopy(model)
new_model.SortedEquations = equations[1:level]
lhs_names = collect(keys(new_model.SortedEquations[level]))
names_derivatives = ["d_$(i)_dt" for i in collect(keys(model.States))]
names_derivatives = ["d_$(i)_dt" for i in collect(keys(new_model.States))]
for i in setdiff(lhs_names, names_derivatives)
if !new_model.SortedEquations[level][i].Exported
delete!(new_model.SortedEquations[level], i)
Expand Down
Loading

0 comments on commit 119c97c

Please sign in to comment.