Skip to content

Commit

Permalink
Refactor/style guide (#437)
Browse files Browse the repository at this point in the history
* Add configuration file for Julia Formatter

* Add format on save in vscode settings file

* Remove .vscode from gitignore

* Format all Julia files

* Sync wflow's style guide with ribasim's style guide

* Update formatting

* Update .vscode/settings.json

* Remove outdated comments

* Add workaround for VS Code bugs
  • Loading branch information
CFBaptista authored Jul 15, 2024
1 parent 5107824 commit d21c930
Show file tree
Hide file tree
Showing 39 changed files with 504 additions and 492 deletions.
12 changes: 12 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Options for the JuliaFormatter auto syntax formatting tool.
# https://domluna.github.io/JuliaFormatter.jl/stable/
# https://docs.sciml.ai/SciMLStyle/stable/

# Based on the default style we do pick these non-default options from SciML style:
whitespace_ops_in_indices = true
remove_extra_newlines = true
always_for_in = true
whitespace_typedefs = true

# And add other options we like:
separate_kwargs_with_semicolon = true
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# General
.DS_Store

# VS Code stuff
.vscode

# Packaging stuff
*.jl.*.cov
*.jl.cov
Expand Down
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"[julia]": {
"editor.formatOnSave": true,
},
"julia.lint.disabledDirs": [
".pixi"
],
"julia.lint.run": true,
}
18 changes: 10 additions & 8 deletions build/wflow_cli/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ using Test
using Wflow

extension = Sys.iswindows() ? ".exe" : ""
wflow_exe = normpath(@__DIR__, "../../create_binaries/wflow_bundle/bin/wflow_cli" * extension)
wflow_exe =
normpath(@__DIR__, "../../create_binaries/wflow_bundle/bin/wflow_cli" * extension)

# this assumes that the Wflow tests have already been run, so the data has been downloaded
testdir = abspath(dirname(pathof(Wflow)), "..", "test")
Expand All @@ -14,15 +15,15 @@ outputdir = joinpath(datadir, "output")

@testset "no_config" begin
# Clean output directory
rm(outputdir; force=true, recursive=true)
rm(outputdir; force = true, recursive = true)
@test_throws ProcessFailedException run(`$wflow_exe`)
# Check if no files are being created
@test !(isdir(outputdir))
end

@testset "wflow_sbm" begin
# Clean directory
rm(outputdir; force=true, recursive=true)
rm(outputdir; force = true, recursive = true)
# Run cli with the toml
toml = normpath(testdir, "sbm_config.toml")
run(`$wflow_exe $toml`)
Expand All @@ -43,7 +44,7 @@ end

@testset "wflow_sbm-gwf" begin
# Clean directory
rm(outputdir; force=true, recursive=true)
rm(outputdir; force = true, recursive = true)
# Run cli with the toml
toml = normpath(testdir, "sbm_gwf_config.toml")
run(`$wflow_exe $toml`)
Expand All @@ -63,7 +64,7 @@ end

@testset "wflow_sediment" begin
# Clean directory
rm(outputdir; force=true, recursive=true)
rm(outputdir; force = true, recursive = true)
# Run cli with the toml
toml = normpath(testdir, "sediment_config.toml")
run(`$wflow_exe $toml`)
Expand All @@ -82,11 +83,12 @@ end
end

@testset "wflow_sbm_timing" begin

toml = normpath(testdir, "sbm_config.toml")
time_sbm_1thread = @elapsed run(Cmd(`$wflow_exe $toml`, env=("JULIA_NUM_THREADS" => "1",)))
time_sbm_1thread =
@elapsed run(Cmd(`$wflow_exe $toml`; env = ("JULIA_NUM_THREADS" => "1",)))

time_sbm_4thread = @elapsed run(Cmd(`$wflow_exe $toml`, env=("JULIA_NUM_THREADS" => "4", )))
time_sbm_4thread =
@elapsed run(Cmd(`$wflow_exe $toml`; env = ("JULIA_NUM_THREADS" => "4",)))

# Test if run with more threads is indeed faster
@test time_sbm_4thread < time_sbm_1thread
Expand Down
8 changes: 4 additions & 4 deletions server/src/WflowServer.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module WflowServer
import ZMQ
import JSON3
import StructTypes
import Wflow
using ZMQ: ZMQ
using JSON3: JSON3
using StructTypes: StructTypes
using Wflow: Wflow

include("bmi_service.jl")
include("server.jl")
Expand Down
2 changes: 1 addition & 1 deletion server/src/bmi_service.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct SaveState
fn::String
end

function wflow_bmi(m::Initialize, model::Union{Wflow.Model,Nothing})
function wflow_bmi(m::Initialize, model::Union{Wflow.Model, Nothing})
model = getfield(Wflow.BMI, Symbol(m.fn))(Wflow.Model, m.config_file)
return model
end
Expand Down
14 changes: 7 additions & 7 deletions server/src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ const map_structs = Dict(
)

mutable struct ModelHandler
model::Union{Wflow.Model,Nothing}
model::Union{Wflow.Model, Nothing}
end

"Shutdown ZMQ server"
function shutdown(s::ZMQ.Socket, ctx::ZMQ.Context)
@info "Shutting down Wflow ZMQ server on request..."
ZMQ.close(s)
ZMQ.close(ctx)
return ZMQ.close(ctx)
end

"Error response ZMQ server"
function response(err::AbstractString, s::ZMQ.Socket)
@info "Send error response"
resp = Dict{String,String}("status" => "ERROR", "error" => err)
ZMQ.send(s, JSON3.write(resp))
resp = Dict{String, String}("status" => "ERROR", "error" => err)
return ZMQ.send(s, JSON3.write(resp))
end

"Status response ZMQ server"
function response(s::ZMQ.Socket)
@info "Send status response"
resp = Dict{String,String}("status" => "OK")
ZMQ.send(s, JSON3.write(resp))
resp = Dict{String, String}("status" => "OK")
return ZMQ.send(s, JSON3.write(resp))
end

"Validate JSON request against mapped Struct"
Expand Down Expand Up @@ -128,7 +128,7 @@ function main(ARGS::Vector{String})
),
)
end
start(port)
return start(port)
end

"""
Expand Down
3 changes: 2 additions & 1 deletion server/test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ vwc_1_size = 0
@test request((fn = "get_var_itemsize", name = "lateral.subsurface.ssf")) ==
Dict("var_itemsize" => sizeof(Wflow.Float))
@test request((fn = "get_var_type", name = "vertical.n"))["status"] == "ERROR"
@test request((fn = "get_var_units", name = "vertical.theta_s")) == Dict("var_units" => "-")
@test request((fn = "get_var_units", name = "vertical.theta_s")) ==
Dict("var_units" => "-")
@test request((fn = "get_var_location", name = "lateral.river.q")) ==
Dict("var_location" => "node")
zi_nbytes = request((fn = "get_var_nbytes", name = "vertical.zi"))["var_nbytes"]
Expand Down
12 changes: 6 additions & 6 deletions server/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ZMQ
import JSON3
import StructTypes
import Wflow
import WflowServer
using ZMQ: ZMQ
using JSON3: JSON3
using StructTypes: StructTypes
using Wflow: Wflow
using WflowServer: WflowServer
import Statistics: mean
import Logging: with_logger, NullLogger
import Test: @testset, @test
import Downloads
using Downloads: Downloads

# ensure test data is present
testdir = @__DIR__
Expand Down
20 changes: 10 additions & 10 deletions src/Wflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ using IfElse

const BMI = BasicModelInterface
const Float = Float64
const CFDataset = Union{NCDataset,NCDatasets.MFDataset}
const CFVariable_MF = Union{NCDatasets.CFVariable,NCDatasets.MFCFVariable}
const CFDataset = Union{NCDataset, NCDatasets.MFDataset}
const CFVariable_MF = Union{NCDatasets.CFVariable, NCDatasets.MFCFVariable}
const version =
VersionNumber(TOML.parsefile(joinpath(@__DIR__, "..", "Project.toml"))["version"])

Expand All @@ -43,7 +43,7 @@ function Clock(config)
calendar = get(config, "calendar", "standard")::String
starttime = cftime(config.starttime, calendar)
dt = Second(config.timestepsecs)
Clock(starttime, 0, dt)
return Clock(starttime, 0, dt)
end

function Clock(config, reader)
Expand Down Expand Up @@ -77,7 +77,7 @@ function Clock(config, reader)
end
starttime = cftime(config.starttime, calendar)

Clock(starttime, 0, dt)
return Clock(starttime, 0, dt)
end

include("io.jl")
Expand All @@ -88,7 +88,7 @@ include("io.jl")
Composite type that represents all different aspects of a Wflow Model, such as the
network, parameters, clock, configuration and input and output.
"""
struct Model{N,L,V,R,W,T}
struct Model{N, L, V, R, W, T}
config::Config # all configuration options
network::N # connectivity information, directed graph
lateral::L # lateral model that holds lateral state, moves along network
Expand Down Expand Up @@ -184,7 +184,7 @@ function run(config::Config)
error("unknown model type")
end
load_fixed_forcing(model)
run(model)
return run(model)
end

function run_timestep(model::Model; update_func = update, write_model_output = true)
Expand Down Expand Up @@ -212,7 +212,7 @@ function run(model::Model; close_files = true)
starttime = clock.time
dt = clock.dt
endtime = cftime(config.endtime, calendar)
times = range(starttime + dt, endtime, step = dt)
times = range(starttime + dt, endtime; step = dt)

@info "Run information" model_type starttime dt endtime nthreads()
runstart_time = now()
Expand All @@ -233,7 +233,7 @@ function run(model::Model; close_files = true)
# option to support running function twice without re-initializing
# and thus opening the netCDF files
if close_files
Wflow.close_files(model, delete_output = false)
Wflow.close_files(model; delete_output = false)
end

# copy TOML to dir_output, to archive what settings were used
Expand All @@ -242,7 +242,7 @@ function run(model::Model; close_files = true)
dst = output_path(config, basename(src))
if src != dst
@debug "Copying TOML file." src dst
cp(src, dst, force = true)
cp(src, dst; force = true)
end
end
return model
Expand All @@ -258,7 +258,7 @@ function run()
if !isfile(toml_path)
throw(ArgumentError("File not found: $(toml_path)\n" * usage))
end
run(toml_path)
return run(toml_path)
end

end # module
Loading

0 comments on commit d21c930

Please sign in to comment.