Tutorial 0 - Julia and Jupyter


Welcome to the Jupyter Notebook system

Jupyter Notebooks are a way to create documents that have both interactive (REPL) code and documentation! Jupyter works with Julia, Python, R, and many other languages.

Each cell in a notebook can be Markdown or code.

  • [Enter] Puts you in cell-editing mode, and [Escape] brings you to document-editing mode.
  • [Shift-Enter] Evaluates the cell.
  • [M] Switches the cell to Markdown mode, and [Y] switches it to code.
  • [H] Shows you help with other commands.

A short introduction to Julia

Julia is a modern (and still evolving) language for scientific computing. Some features:

  • A syntax very like Matlab
  • An active community of developers
  • Call R, Python and pass data back and forth
  • "Multiple dispatch": Object-oriented programming through functions
  • Super fast, and just-in-time compiled:

Some basic commands


In [7]:
# Basic arithmetic
7 + π


Out[7]:
10.141592653589793

In [8]:
# Basic variables
x = 3
7x


Out[8]:
21

In [9]:
# Vectors
y = [1, 5, 9]


Out[9]:
3-element Array{Int64,1}:
 1
 5
 9

In [19]:
# Operations on vectors
abs.(y.^2 - 10)


Out[19]:
3-element Array{Int64,1}:
  9
 15
 71

In [20]:
# Operations on matrices
y' * [y y]


Out[20]:
1×2 RowVector{Int64,Array{Int64,1}}:
 107  107

In [21]:
# Accessing values
y[2] = -3
y


Out[21]:
3-element Array{Int64,1}:
  1
 -3
  9

In [22]:
# Basic conditionals
if sum(y) > 0
    println("It's positive.")
end


It's positive.

In [23]:
# Basic loops
for ii in 1:length(y)
    println(y[ii])
end


1
-3
9

In [24]:
# Basic functions
function square(x)
    x^2
end
println(square(3))
println(square(3.0))


9
9.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))


9
[1, 9, 81]

Mimi Introduction

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]:
MyComponent

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]:
run_timestep (generic function with 4 methods)

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]:
showing model component connections:
1. MyComponent component
    incoming parameters:
      none
    outgoing variables:
      none

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]:
20-element Array{Float64,1}:
 0.0       
 0.00277008
 0.0110803 
 0.0249307 
 0.0443213 
 0.0692521 
 0.099723  
 0.135734  
 0.177285  
 0.224377  
 0.277008  
 0.33518   
 0.398892  
 0.468144  
 0.542936  
 0.623269  
 0.709141  
 0.800554  
 0.897507  
 1.0       

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 [ ]: