Commit 0a1c2fc9 authored by Pierre Navaro's avatar Pierre Navaro
Browse files

Push the notebook

parent b4385dac
docs/build
*.swp
Manifest.toml
notebooks/.ipynb
notebooks/.ipynb_checkpoints/.ipynb-checkpoint
notebooks/.ipynb_checkpoints/HermiteGF-checkpoint.ipynb
test/.ipynb_checkpoints/HermiteGF-checkpoint.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Plots.PyPlotBackend()"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Plots\n",
"pyplot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"HermiteGf is a julia package, install it with\n",
"\n",
"```julia\n",
"using Pkg\n",
"Pkg.add(\"https://gitlab.mpcdf.mpg.de/clapp/HermiteGF.jl\")\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"using HermiteGF"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NodesType"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\" Node positions (Uniform or Chebyshev) \"\n",
"abstract type NodesType end"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"import HermiteGF:generate_chebyshev_nodes"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Chebyshev"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
" Chebyshev( nx, xmin, xmax )\n",
"\n",
"Chebyshev nodes\n",
"\n",
"\"\"\"\n",
"struct Chebyshev <: NodesType\n",
"\n",
" nx :: Int64\n",
" xmin :: Float64\n",
" xmax :: Float64\n",
" xk :: Vector{Float64}\n",
"\n",
" function Chebyshev( nx, xmin, xmax )\n",
"\n",
" xk = zeros(Float64, nx)\n",
" xk .= generate_chebyshev_nodes(nx, xmin, xmax)\n",
" new( nx, xmin, xmax, xk )\n",
"\n",
" end\n",
"\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"import HermiteGF:generate_uniform_nodes"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Uniform"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
" Uniform( nx, xmin, xmax )\n",
"\n",
"Uniform nodes\n",
"\n",
"\"\"\"\n",
"struct Uniform <: NodesType\n",
"\n",
" nx :: Int64\n",
" xmin :: Float64\n",
" xmax :: Float64\n",
" xk :: Vector{Float64}\n",
"\n",
" function Uniform( nx, xmin, xmax )\n",
"\n",
" xk = zeros(Float64, nx)\n",
" xk .= generate_uniform_nodes(nx, xmin, xmax)\n",
" new( nx, xmin, xmax, xk )\n",
"\n",
" end\n",
"\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"abstract type InterpolationType end"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"import HermiteGF:evaluate_hermite"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Hermite"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
" Hermite( nodes::NodesType, epsilon, gamma )\n",
"\n",
"Hermite inteprolation\n",
"\n",
"\"\"\"\n",
"mutable struct Hermite <: InterpolationType\n",
"\n",
" nx :: Int64\n",
" dims :: Int64\n",
" epsilon :: Float64\n",
" gamma :: Float64\n",
" xk :: Array{Float64,1}\n",
" colloc_mat :: Array{Float64,2}\n",
"\n",
" function Hermite( nodes::NodesType, epsilon, gamma )\n",
"\n",
" dims = 1\n",
" xk = nodes.xk\n",
" nx = nodes.nx\n",
" colloc_mat = evaluate_hermite(xk, nx, epsilon, gamma)\n",
" new( nx, dims, epsilon, gamma, xk, colloc_mat )\n",
"\n",
" end\n",
"\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Hermite"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
" Hermite( xe )\n",
"\n",
"returns evaluation matrix\n",
"\"\"\"\n",
"function (interp::Hermite)( xe::Array{Float64,1} )\n",
"\n",
" nx = interp.nx\n",
" epsilon = interp.epsilon\n",
" gamma = interp.gamma\n",
" evaluate_hermite(xe, nx, epsilon, gamma)\n",
"\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"import HermiteGF:evaluate_radial"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Radial"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
" Radial( nodes::NodesType, epsilon, gamma )\n",
"\n",
"Radial inteprolation\n",
"\n",
"\"\"\"\n",
"mutable struct Radial <: InterpolationType\n",
"\n",
" nx :: Int64\n",
" epsilon :: Float64\n",
" gamma :: Float64\n",
" xk :: Array{Float64,1}\n",
" colloc_mat :: Array{Float64,2}\n",
"\n",
" function Radial( nodes::NodesType, epsilon, gamma )\n",
" \n",
" dims = 1\n",
" xk = nodes.xk\n",
" nx = nodes.nx\n",
" colloc_mat = evaluate_radial(xk, xk, nx, epsilon, gamma)\n",
" new( dims, epsilon, gamma, xk, colloc_mat )\n",
" \n",
" end\n",
"\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Radial"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"\n",
" Radial( xe )\n",
"\n",
"returns evaluation matrix\n",
"\"\"\"\n",
"function (interp::Radial)( xe::Array{Float64,1} )\n",
"\n",
" nx = interp.nx\n",
" epsilon = interp.epsilon\n",
" gamma = interp.gamma\n",
" xk = interp.xk\n",
" evaluate_radial(xk, xe, nx, epsilon, gamma)\n",
"\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"import HermiteGF:evaluate_s_1D"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"ename": "UndefVarError",
"evalue": "UndefVarError: Chebyshev not defined",
"output_type": "error",
"traceback": [
"UndefVarError: Chebyshev not defined",
"",
"Stacktrace:",
" [1] top-level scope at In[13]:22"
]
}
],
"source": [
"function interpolate(interp::InterpolationType, \n",
" f::Array{Float64,1}, xe::Array{Float64,1} )\n",
"\n",
" ϵ = interp.epsilon\n",
" nx = interp.nx\n",
" xk = interp.xk\n",
"\n",
" # Precompute 1D interpolation matrix\n",
" x_all = interp(xe) / interp.colloc_mat\n",
"\n",
" # Compute the interpolated values\n",
" evaluate_s(x_all, f)\n",
"\n",
"end\n",
"\n",
"\n",
"# interpolation domain.\n",
"xmin, xmax, nx = -1, 1, 32\n",
"\n",
"ϵ = 0.1\n",
"γ = 3\n",
"\n",
"hermite = Hermite(Chebyshev(nx, xmin, xmax), ϵ, γ)\n",
"radial = Radial(Chebyshev(nx, xmin, xmax), ϵ, γ)\n",
"xk = interpolation.xk\n",
"f = cos.(xk.^2)\n",
"xe = collect(range(xmin, stop=xmax, length=nx))\n",
"plot(xe, interpolate( hermite, f, xe ))\n",
"plot(xe, interpolate( radial, f, xe ))\n",
"scatter!(xk, f)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"using IterTools"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "syntax: invalid iteration specification",
"output_type": "error",
"traceback": [
"syntax: invalid iteration specification",
""
]
}
],
"source": [
"for k, v in product([:Hermite, :Radial],[:Uniform, :Chebyshev])\n",
" println(k, v)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"32-element Array{Float64,1}:\n",
" 0.54030230586814 \n",
" 0.6408970161773455\n",
" 0.7258102410433283\n",
" 0.7958633344738472\n",
" 0.8522796739163705\n",
" 0.8965416349514189\n",
" 0.9302692145972262\n",
" 0.9551209764487195\n",
" 0.9727162883451472\n",
" 0.9845767075724988\n",
" 0.99208373838787 \n",
" 0.9964499363706495\n",
" 0.9987003653073915\n",
" ⋮ \n",
" 0.9964499363706478\n",
" 0.9920837383878681\n",
" 0.984576707572498 \n",
" 0.9727162883451476\n",
" 0.95512097644872 \n",
" 0.9302692145972272\n",
" 0.896541634951419 \n",
" 0.8522796739163696\n",
" 0.7958633344738454\n",
" 0.7258102410433296\n",
" 0.6408970161773515\n",
" 0.540302305868137 "
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#l2_error = sqrt(trapz_1D((s - f_vals).^2, dx))\n",
"#l1_error = maximum(abs.(s .- f_vals))\n",
"#\n",
"#f_vals = h(xx_e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.0.1",
"language": "julia",
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.0.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
``` julia
using Plots
pyplot()
```
%%%% Output: execute_result
Plots.PyPlotBackend()
%% Cell type:markdown id: tags:
HermiteGf is a julia package, install it with
```julia
using Pkg
Pkg.add("https://gitlab.mpcdf.mpg.de/clapp/HermiteGF.jl")
```
%% Cell type:code id: tags:
``` julia
using HermiteGF
```
%% Cell type:code id: tags:
``` julia
" Node positions (Uniform or Chebyshev) "
abstract type NodesType end
```
%%%% Output: execute_result
NodesType
%% Cell type:code id: tags:
``` julia
import HermiteGF:generate_chebyshev_nodes
```
%% Cell type:code id: tags:
``` julia
"""
Chebyshev( nx, xmin, xmax )
Chebyshev nodes
"""
struct Chebyshev <: NodesType
nx :: Int64
xmin :: Float64
xmax :: Float64
xk :: Vector{Float64}
function Chebyshev( nx, xmin, xmax )
xk = zeros(Float64, nx)
xk .= generate_chebyshev_nodes(nx, xmin, xmax)
new( nx, xmin, xmax, xk )
end
end
```
%%%% Output: execute_result
Chebyshev
%% Cell type:code id: tags:
``` julia
import HermiteGF:generate_uniform_nodes
```
%% Cell type:code id: tags:
``` julia
"""
Uniform( nx, xmin, xmax )
Uniform nodes
"""
struct Uniform <: NodesType
nx :: Int64
xmin :: Float64
xmax :: Float64
xk :: Vector{Float64}
function Uniform( nx, xmin, xmax )
xk = zeros(Float64, nx)
xk .= generate_uniform_nodes(nx, xmin, xmax)
new( nx, xmin, xmax, xk )
end
end
```
%%%% Output: execute_result
Uniform
%% Cell type:code id: tags:
``` julia
abstract type InterpolationType end
```
%% Cell type:code id: tags:
``` julia
import HermiteGF:evaluate_hermite
```
%% Cell type:code id: tags:
``` julia
"""
Hermite( nodes::NodesType, epsilon, gamma )
Hermite inteprolation
"""
mutable struct Hermite <: InterpolationType
nx :: Int64
dims :: Int64
epsilon :: Float64
gamma :: Float64
xk :: Array{Float64,1}
colloc_mat :: Array{Float64,2}
function Hermite( nodes::NodesType, epsilon, gamma )
dims = 1
xk = nodes.xk
nx = nodes.nx
colloc_mat = evaluate_hermite(xk, nx, epsilon, gamma)
new( nx, dims, epsilon, gamma, xk, colloc_mat )
end
end
```
%%%% Output: execute_result
Hermite
%% Cell type:code id: tags:
``` julia
"""
Hermite( xe )
returns evaluation matrix
"""
function (interp::Hermite)( xe::Array{Float64,1} )