Nstrophy/julia/main.jl
2022-05-18 09:57:44 +02:00

151 lines
2.6 KiB
Julia

using Printf
using FFTW
include("navier_stokes.jl")
include("driving.jl")
include("print.jl")
function main()
## defaults
delta=0.0001220703125
nsteps=10000000
nu=0.00048828125
print_freq=1000
K1=16
K2=K1
N1=4*K1+1
N2=4*K2+1
# read cli arguments
(params,driving,command)=read_args(ARGS)
# whether N was set in parameters
setN1=false
setN2=false
# read params
if params!=""
for param in split(params,";")
terms=split(param,"=")
if length(terms)!=2
print(stderr,"error: could not read parameter '",param,"'.\n")
exit(-1)
end
lhs=string(terms[1])
rhs=string(terms[2])
if lhs=="K1"
K1=parse(Int64,rhs)
elseif lhs=="K2"
K2=parse(Int64,rhs)
elseif lhs=="K"
K1=parse(Int64,rhs)
K2=K1
elseif lhs=="N1"
N1=parse(Int64,rhs)
setN1=true
elseif lhs=="N2"
N2=parse(Int64,rhs)
setN2=true
elseif lhs=="N"
N1=parse(Int64,rhs)
N2=N1
setN1=true
setN2=true
elseif lhs=="nsteps"
nsteps=parse(Int64,rhs)
elseif lhs=="nu"
nu=parse(Float64,rhs)
elseif lhs=="delta"
delta=parse(Float64,rhs)
elseif lhs=="print_freq"
print_freq=parse(Int64,rhs)
else
print(stderr,"unrecognized parameter '",lhs,"'.\n")
exit(-1)
end
end
end
## read driving
if driving=="test"
g=(kx,ky)->g_test(kx,ky)
end
## set parameters
if ! setN1
N1=4*K1+1
end
if ! setN2
N2=4*K2+1
end
## run command
if command=="uk"
navier_stokes_uk(nsteps,nu,g,N1,N2,K1,K2,delta,print_freq)
elseif command=="alpha"
navier_stokes_alpha(nsteps,nu,g,N1,N2,K1,K2,delta,print_freq)
else
print(stderr,"unrecognized command '",command,"'.\n")
exit(-1)
end
end
# read cli arguments
function read_args(ARGS)
# flag
flag=""
# output strings
command=""
params=""
# default driving force
driving="test"
# loop over arguments
for arg in ARGS
# argument that starts with a dash
if arg[1]=='-'
# go through characters after dash
for char in arg[2:length(arg)]
# set params
if char=='p'
# raise flag
flag="params"
elseif char=='g'
# raise flag
flag="driving"
else
print_usage()
exit(-1)
end
end
# arguments that do not start with a dash
else
if flag=="params"
params=arg
elseif flag=="driving"
driving=arg
else
command=arg
end
# reset flag
flag=""
end
end
if command==""
print_usage()
exit(-1)
end
return (params,driving,command)
end
# usage
function print_usage()
print(stderr,"usage: nstrophy [-p params] [-g driving] <command>\n")
end
main()