I’m writing a chemistry simulation of a zinc-manganese alkaline battery, and of course I want to organize the overall structure of the code to optimize for speed. The battery will have data describing its current “state”, and there will be many functions to perform operations on that battery state. Each operator (aka function) will have parameters describing it, and each will be performed many times with slightly different parameters, and I want to save the details of each operation (aka of each function-call) and the effect of the operation on the battery state. Sorry for the run-on sentence.
Does the code below follow Julia best practices? Is it written in the way that Julia was designed to be best for? Did I create performance issues?
###### Contents of include_file.jl
#This file is a lonnng list of parameters to initialize the system state
#The user will edit this include_file.jl to define the initial state according to their desires
r=80
q=3
a=[80, 0, 160.0]/r
b=[29.3, 31.1, 3.3]*q
###### end of include_file.jl
module manage_system_state
using FileIO
using Dates
#Define a data type to hold the state of the battery
struct system_state_data_type
a::Array{Float64,1}
b::Array{Float64,1}
c::Float64
end
function create_system_state(include_filename)
include(include_filename)
system_state = system_state_data_type(
a,
b,
789.5)
timestamp = Dates.format(Dates.now(),"yyyymmddHHMMSS")
save(timestamp * "_initial_state.jld2", Dict("system_state"=>system_state))
return(system_state)
end
function load_system_state_from_saved_dictionary(dictionary_name)
system_state=get(load(dictionary_name), "system_state", 0)
return(system_state)
end
end
module simulate
using FileIO
using Dates
using Plots
#Define an data type to hold infomration about an operator that will manipulate the battery
struct operator_A_data_type
a::Float64
b::Int
c::Array{Float64,1}
d::Array{Float64,1}
end
function operation_A(system_state, a, b)
operator = operator_A_data_type(a, b, [99, 0, 22.0], [19.3, 4.5, 0])
system_state.a[1]=system_state.a[2] * operator.a * operator.c[1]
system_state.b[2]=system_state.c * operator.b * operator.d[2]
### Next, save information about this operation and it's effect on system state
timestamp = Dates.format(Dates.now(),"yyyymmddHHMMSS")
save(timestamp * "_operator_A_output.jld2", Dict("system_state"=>system_state, "operator_A_structure"=>operator ))
end
end
#User Workflow
system_state = manage_system_state.create_system_state("include_file.jl")
simulate.operation_A(system_state,13.5, 5)
resulting_system_state = system_state.load_system_state_from_saved_dictionary("20200802124716_operator_A_output.jld2")
using Plots
display(plot(resulting_system_state.a, resulting_system_state.b))```