Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Revising Chris Rackauckas's "exp_euler_test.jl" from Julia 0.6 so it works in Julia 1.1
########################################################
# Revising Chris Rackauckas's "exp_euler_test.jl" from
# Julia 0.6 so it works in Julia 1.1
########################################################
#
# Original 0.6 code from:
#
# ChrisRackauckas/exp_euler_test.jl
# https://gist.github.com/ChrisRackauckas/cf91f4575b0587a45c72620384612b82
# This works in Julia 0.6
# But DOESN'T WORK IN JULIA 1.1
########################################################
# Pkg.add("SpecialMatrices") # Only need to run the first time
using SpecialMatrices, DiffEqBase
A = Strang(11)
function g(t,u)
for i in eachindex(u)
2 - u[i]
end
u
end
u0 = zeros(11); u0[6] = 10
tspan = (0.0,10.0)
SplitODEProblem((A,g),u0,tspan)
########
# This represents the diffusion of some chemical which has a source
# A lot starts that the middle (u0[6])
# What you should see is it should diffuse to the other places
# And slowly evenly rise
# If it's run for long enough, it should become flat at u=2 across the domain
########################################################
# End of Chris's stuff
########################################################
#######################################################
# This version WORKS in Julia 1.1,
# as of 2019-04-18
#######################################################
#######################################################
# Mac Terminal command line:
# > julia --version
# julia version 1.1.0
#
# > which julia
# /usr/local/bin/julia
#
# # Show the symbolic link to make Julia 1.1 run from
# # Mac Terminal Command line
# > ls -l /usr/local/bin/julia
# lrwxr-xr-x 1 root wheel 63 1 Apr 09:52
# /usr/local/bin/julia ->
# /Applications/Julia-1.1a.app/Contents/Resources/julia/bin/julia
#
# # To run, type:
# > julia
#######################################################
# Install the libraries
using Pkg
# Only need to run the first time
Pkg.add("DifferentialEquations")
Pkg.add("DiffEqOperators")
Pkg.add(Pkg.PackageSpec(url = "https://github.com/ChrisRackauckas/EllipsisNotation.jl")) # seems to be dependency for SpecialMatrices?
Pkg.add(Pkg.PackageSpec(url = "https://github.com/JuliaMatrices/SpecialMatrices.jl")) # for Strang()
# Use the libraries
#using DiffEqBase # solve() crashes with just this
using DifferentialEquations # for solve() to not crash (?)
using DiffEqOperators # for DiffEqArrayOperator
using SpecialMatrices # for Strang()
using Plots # for plot()
# Define the linear and nonlinear part of the ODE as functions
# Explanation of "!" in function names:
# http://julia.cookbook.tips/doku.php?id=baffling
# Julia recommends a “collaborative” model, where functions that modify their arguments
# should be named with a trailing '!'. Functions without it may or may not modify their
# passed arguments -- well, unless the passed arguments are native primitives and not
# objects.
# Define the linear part of the problem
# A = Strang(11)
A = Matrix(Strang(11))
f1!(du,u,p,t) = DiffEqArrayOperator(A)
# Define the nonlinear part of the problem
function g1!(du,u,p,t)
for i in eachindex(u)
du[i] = 2 - u[i]
end
end
# Initial conditions of the changing vector "u"
u0 = zeros(11); u0[6] = 10
# Start and end of timespan over which to integrate
tspan = (0.0,10.0)
# This works in Julia 1.1
prob2 = SplitODEProblem(f1!,g1!,u0,tspan)
sol2 = solve(prob2)
plot(sol2)
# Print value of u[6] through timeseries
for i in eachindex(sol2.u)
print(sol2.u[i][6])
print("\n")
end
@nmatzke

This comment has been minimized.

Copy link
Owner Author

@nmatzke nmatzke commented Apr 18, 2019

This code modifies the original gist from Chris Rackauckas, originally posted at: https://gist.github.com/ChrisRackauckas/cf91f4575b0587a45c72620384612b82

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.