## 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 :

# Basic arithmetic
7 + π

``````
``````

Out:

10.141592653589793

``````
``````

In :

# Basic variables
x = 3
7x

``````
``````

Out:

21

``````
``````

In :

# Vectors
y = [1, 5, 9]

``````
``````

Out:

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

``````
``````

In :

# Operations on vectors
abs.(y.^2 - 10)

``````
``````

Out:

3-element Array{Int64,1}:
9
15
71

``````
``````

In :

# Operations on matrices
y' * [y y]

``````
``````

Out:

1×2 RowVector{Int64,Array{Int64,1}}:
107  107

``````
``````

In :

# Accessing values
y = -3
y

``````
``````

Out:

3-element Array{Int64,1}:
1
-3
9

``````
``````

In :

# Basic conditionals
if sum(y) > 0
println("It's positive.")
end

``````
``````

It's positive.

``````
``````

In :

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

``````
``````

1
-3
9

``````
``````

In :

# Basic functions
function square(x)
x^2
end
println(square(3))
println(square(3.0))

``````
``````

9
9.0

``````
``````

In :

# 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 :

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 :

@defcomp MyComponent begin
input = Parameter(index=[time])
output = Variable(index=[time])
end

``````
``````

Out:

MyComponent

``````

And we define the calculations as a `run_timestep` function:

``````

In :

function run_timestep(state::MyComponent, tt::Int)
v = state.Variables
p = state.Parameters

v.output[tt] = p.input[tt]^2
end

``````
``````

Out:

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 :

# Create an empty model
model = Model()
# Specify the values for each index (here, just time)
setindex(model, :time, collect(2018:2037))
# Use the `mycomponent` object to set the input
mycomponent[:input] = linspace(0, 1, 20)
# Show what's in model
model

``````
``````

Out:

showing model component connections:
1. MyComponent component
incoming parameters:
none
outgoing variables:
none

``````
``````

In :

# Run the model
run(model)

``````

Now that the model is run, we can look at its output.

``````

In :

model[:MyComponent, :output]

``````
``````

Out:

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 :

using Plots

``````
``````

In :

plot(model[:MyComponent, :input], model[:MyComponent, :output])

``````
``````

Out:

PLOT = document.getElementById('243fed07-3c55-436b-8663-21ed27f9057d');
Plotly.plot(PLOT, [{"showlegend":true,"mode":"lines","xaxis":"x","colorbar":{"title":""},"line":{"color":"rgba(0, 154, 250, 1.000)","shape":"linear","dash":"solid","width":1},"y":[0.0,0.0027700831024930744,0.011080332409972297,0.02493074792243767,0.04432132963988919,0.06925207756232686,0.09972299168975068,0.13573407202216065,0.17728531855955676,0.22437673130193903,0.27700831024930744,0.33518005540166207,0.39889196675900274,0.46814404432132967,0.5429362880886426,0.6232686980609419,0.709141274238227,0.8005540166204986,0.8975069252077561,1.0],"type":"scatter","name":"y1","yaxis":"y","x":[0.0,0.05263157894736842,0.10526315789473684,0.15789473684210525,0.21052631578947367,0.2631578947368421,0.3157894736842105,0.3684210526315789,0.42105263157894735,0.47368421052631576,0.5263157894736842,0.5789473684210527,0.631578947368421,0.6842105263157895,0.7368421052631579,0.7894736842105263,0.8421052631578947,0.8947368421052632,0.9473684210526315,1.0]}], {"showlegend":true,"xaxis":{"showticklabels":true,"gridwidth":0.5,"tickvals":[0.0,0.2,0.4,0.6000000000000001,0.8,1.0],"visible":true,"ticks":"inside","range":[0.0,1.0],"domain":[0.05100612423447069,0.9934383202099737],"tickmode":"array","linecolor":"rgba(0, 0, 0, 1.000)","showgrid":true,"title":"","mirror":false,"tickangle":0,"showline":true,"gridcolor":"rgba(0, 0, 0, 0.100)","titlefont":{"color":"rgba(0, 0, 0, 1.000)","family":"sans-serif","size":15},"tickcolor":"rgb(0, 0, 0)","ticktext":["0.0","0.2","0.4","0.6","0.8","1.0"],"zeroline":false,"type":"-","tickfont":{"color":"rgba(0, 0, 0, 1.000)","family":"sans-serif","size":11},"zerolinecolor":"rgba(0, 0, 0, 1.000)","anchor":"y"},"paper_bgcolor":"rgba(255, 255, 255, 1.000)","annotations":[],"height":400,"margin":{"l":0,"b":20,"r":0,"t":20},"plot_bgcolor":"rgba(255, 255, 255, 1.000)","yaxis":{"showticklabels":true,"gridwidth":0.5,"tickvals":[0.0,0.2,0.4,0.6000000000000001,0.8,1.0],"visible":true,"ticks":"inside","range":[0.0,1.0],"domain":[0.03762029746281716,0.9901574803149606],"tickmode":"array","linecolor":"rgba(0, 0, 0, 1.000)","showgrid":true,"title":"","mirror":false,"tickangle":0,"showline":true,"gridcolor":"rgba(0, 0, 0, 0.100)","titlefont":{"color":"rgba(0, 0, 0, 1.000)","family":"sans-serif","size":15},"tickcolor":"rgb(0, 0, 0)","ticktext":["0.0","0.2","0.4","0.6","0.8","1.0"],"zeroline":false,"type":"-","tickfont":{"color":"rgba(0, 0, 0, 1.000)","family":"sans-serif","size":11},"zerolinecolor":"rgba(0, 0, 0, 1.000)","anchor":"x"},"legend":{"bordercolor":"rgba(0, 0, 0, 1.000)","bgcolor":"rgba(255, 255, 255, 1.000)","font":{"color":"rgba(0, 0, 0, 1.000)","family":"sans-serif","size":11},"y":1.0,"x":1.0},"width":600});

``````

``````

In [ ]:

``````