Each cell in a notebook can be Markdown or code.
Julia is a modern (and still evolving) language for scientific computing. Some features:
In [7]:
# Basic arithmetic
7 + π
Out[7]:
In [8]:
# Basic variables
x = 3
7x
Out[8]:
In [9]:
# Vectors
y = [1, 5, 9]
Out[9]:
In [19]:
# Operations on vectors
abs.(y.^2 - 10)
Out[19]:
In [20]:
# Operations on matrices
y' * [y y]
Out[20]:
In [21]:
# Accessing values
y[2] = -3
y
Out[21]:
In [22]:
# Basic conditionals
if sum(y) > 0
println("It's positive.")
end
In [23]:
# Basic loops
for ii in 1:length(y)
println(y[ii])
end
In [24]:
# Basic functions
function square(x)
x^2
end
println(square(3))
println(square(3.0))
In [26]:
# Multiple dispatch
function square(x::Number)
x^2
end
function square(x::AbstractArray)
x.^2
end
println(square(x))
println(square(y))
Mimi is a modeling framework written in Julia and used by AWASH. A Mimi model consists of "components" that process inputs ("parameters") into outputs ("variables"). Several components can be hooked together, making a Mimi model.
After Mimi is installed in Julia, you can load it by calling using Mimi
.
In [1]:
using Mimi
Defining a component consists of defining its inputs and outputs, and defining how it performs its computation on each timestep.
The @defcomp
macro is used to define components. Here's an example:
In [2]:
@defcomp MyComponent begin
input = Parameter(index=[time])
output = Variable(index=[time])
end
Out[2]:
And we define the calculations as a run_timestep
function:
In [9]:
function run_timestep(state::MyComponent, tt::Int)
v = state.Variables
p = state.Parameters
v.output[tt] = p.input[tt]^2
end
Out[9]:
Finally, there's some model configuration, where you specify the time dimension (and other dimensions in more sophisticated models), add the components to the model, and set each of their parameters. Parameters can be set either to other component variables or to data. Here, we just use data.
In [7]:
# Create an empty model
model = Model()
# Specify the values for each index (here, just time)
setindex(model, :time, collect(2018:2037))
# Add our single component
mycomponent = addcomponent(model, MyComponent)
# Use the `mycomponent` object to set the input
mycomponent[:input] = linspace(0, 1, 20)
# Show what's in model
model
Out[7]:
In [10]:
# Run the model
run(model)
Now that the model is run, we can look at its output.
In [11]:
model[:MyComponent, :output]
Out[11]:
In [12]:
using Plots
In [15]:
plot(model[:MyComponent, :input], model[:MyComponent, :output])
Out[15]:
More information is available at https://github.com/davidanthoff/Mimi.jl
In [ ]: