Skip to content

Commit 4d554b1

Browse files
authored
Cleanup of script testing (#62)
* some more elegant tests for the scripts * missed a little * with bsk
1 parent 8a527b6 commit 4d554b1

9 files changed

Lines changed: 36 additions & 270 deletions

File tree

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PoissonBoltzmannIPAM2025"
22
uuid = "480e0da0-c3fa-11f0-becb-6b90fb73a51e"
3-
version = "0.1.0"
43
authors = ["Jürgen Fuhrmann <juergen-fuhrmann@web.de>"]
4+
version = "0.1.0"
55

66
[deps]
77
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
@@ -13,6 +13,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1313
HypertextLiteral = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
1414
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1515
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
16+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
1617
JuliaMPBSolver = "d8b18f01-5396-498d-b34d-247825c18ff0"
1718
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
1819
LessUnitful = "f29f6376-6e90-4d80-80c9-fb8ec61203d5"
@@ -42,6 +43,7 @@ ForwardDiff = "1"
4243
HypertextLiteral = "0.9"
4344
InteractiveUtils = "1.11.0"
4445
Interpolations = "0.16"
46+
JLD2 = "0.6.3"
4547
LaTeXStrings = "1.0.1"
4648
LessUnitful = "1"
4749
Markdown = "1.11.0"

draftnotebooks/demo-notebook.jl

Lines changed: 0 additions & 34 deletions
This file was deleted.

scripts/simplecell-bsk.jl

Lines changed: 2 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using Interpolations
77
using VoronoiFVM
88
using PythonPlot
99
using JuliaMPBSolver
10+
using JLD2
1011

1112
const nel = 20.0 * JuliaMPBSolver.Units.el_surface_density # number of electrons/nm^2 at interfaces
1213
const M_bulk = 1 # (bulk) molarity at center of domain
@@ -51,120 +52,4 @@ solution, X, nv, ε_r =
5152
computed_parameters,
5253
)
5354

54-
function bee!(y, ϕ)
55-
N = length(user_parameters.charge_numbers)
56-
for i in 1:N
57-
y[i] =
58-
JuliaMPBSolver.Units.thermal_energy(user_parameters.temperature) *
59-
log(c_bulk[i] / computed_parameters.bulk_solvent_concentration) /
60-
JuliaMPBSolver.Units.F - user_parameters.charge_numbers[i] * ϕ
61-
end
62-
return nothing
63-
end
64-
65-
function bee(sol)
66-
n = size(sol, 2)
67-
N = length(user_parameters.charge_numbers)
68-
e = zeros(N, n)
69-
y = zeros(N)
70-
for i in 1:n
71-
bee!(y, sol[1, i])
72-
for j in 1:N
73-
e[j, i] = y[j]
74-
end
75-
end
76-
return e
77-
end
78-
79-
function plotsol(sol; size = (600, 400))
80-
PythonPlot.clf()
81-
fig, ax = pyplot.subplots(2, 1)
82-
ax1 = ax[0]
83-
ax2 = ax[1]
84-
ax1.grid()
85-
ax1r = ax1.twinx()
86-
87-
c = JuliaMPBSolver.Postprocess.compute_concentrations(
88-
sol[1, :],
89-
user_parameters,
90-
computed_parameters,
91-
)
92-
cm = c[1, :] nv / (JuliaMPBSolver.Units.M) / grid_parameters.domain_size
93-
cp = c[2, :] nv / (JuliaMPBSolver.Units.M) / grid_parameters.domain_size
94-
c0 = -(sum(c, dims = 1) .- c̄)
95-
e = bee(sol)
96-
ax1.set_title(
97-
"ϕ∈$(round.(Float64.(extrema(sol[1, :])), sigdigits = 3)), ε_r ∈$(round.(Float64.(extrema(ε_r)), sigdigits = 3))",
98-
)
99-
ax1.plot(
100-
X / JuliaMPBSolver.Units.nm,
101-
sol[1, :],
102-
color = "green",
103-
linewidth = 2,
104-
label = "ϕ",
105-
)
106-
ax1.plot(
107-
X / JuliaMPBSolver.Units.nm,
108-
e[1, :],
109-
color = "blue",
110-
linewidth = 2,
111-
label = L"ψ^+",
112-
linestyle = "dotted",
113-
)
114-
ax1.plot(
115-
X / JuliaMPBSolver.Units.nm,
116-
e[2, :],
117-
color = "red",
118-
linewidth = 2,
119-
linestyle = "dotted",
120-
label = L"ψ^+",
121-
)
122-
ax1r.plot(
123-
X[1:(end - 1)] / JuliaMPBSolver.Units.nm,
124-
ε_r,
125-
color = "pink",
126-
linewidth = 3,
127-
label = L"ε_r",
128-
)
129-
ax1.set_ylim(-10, 10)
130-
ax1.set_xlabel("z/nm")
131-
ax1.set_ylabel("ϕ/V")
132-
ax1.legend(loc = (0.1, 0.1))
133-
ax1r.legend(loc = (0.8, 0.1))
134-
ax1r.set_ylim(0, 80)
135-
136-
ax2.grid()
137-
ax2.set_title("M_avg=$(round.((cm, cp), sigdigits = 3))")
138-
ax2.set_xlabel("z/nm")
139-
ax2.set_ylabel("c/(mol/L)")
140-
ax2.set_ylim(0, 60)
141-
142-
ax2.plot(
143-
X / JuliaMPBSolver.Units.nm,
144-
c[1, :] / (JuliaMPBSolver.Units.M),
145-
color = "blue",
146-
linewidth = 2,
147-
label = L"c^-",
148-
)
149-
ax2.plot(
150-
X / JuliaMPBSolver.Units.nm,
151-
c[2, :] / (JuliaMPBSolver.Units.M),
152-
color = "red",
153-
linewidth = 2,
154-
label = L"c^+",
155-
)
156-
ax2.plot(
157-
X / JuliaMPBSolver.Units.nm,
158-
c0[1, :] / (JuliaMPBSolver.Units.M),
159-
color = "green",
160-
linewidth = 2,
161-
label = L"c_{solvent}",
162-
)
163-
ax2.legend(loc = (0.4, 0.1))
164-
165-
tight_layout()
166-
savefig("simplecell-bsk.jpg", dpi = 300)
167-
return PythonPlot.gcf()
168-
end
169-
170-
plotsol(solution)
55+
save("simplecell-bsk.jld2", "solution", solution, "X", X, "nv", nv, "ε_r", ε_r)

scripts/simplecell.jl

Lines changed: 2 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using Interpolations
77
using VoronoiFVM
88
using PythonPlot
99
using JuliaMPBSolver
10+
using JLD2
1011

1112
const nel = 20.0 * JuliaMPBSolver.Units.el_surface_density # number of electrons/nm^2 at interfaces
1213
const M_bulk = 1 # (bulk) molarity at center of domain
@@ -52,120 +53,4 @@ solution, X, nv, ε_r =
5253
computed_parameters,
5354
)
5455

55-
function bee!(y, ϕ)
56-
N = length(user_parameters.charge_numbers)
57-
for i in 1:N
58-
y[i] =
59-
JuliaMPBSolver.Units.thermal_energy(user_parameters.temperature) *
60-
log(c_bulk[i] / computed_parameters.bulk_solvent_concentration) /
61-
JuliaMPBSolver.Units.F - user_parameters.charge_numbers[i] * ϕ
62-
end
63-
return nothing
64-
end
65-
66-
function bee(sol)
67-
n = size(sol, 2)
68-
N = length(user_parameters.charge_numbers)
69-
e = zeros(N, n)
70-
y = zeros(N)
71-
for i in 1:n
72-
bee!(y, sol[1, i])
73-
for j in 1:N
74-
e[j, i] = y[j]
75-
end
76-
end
77-
return e
78-
end
79-
80-
function plotsol(sol; size = (600, 400))
81-
PythonPlot.clf()
82-
fig, ax = pyplot.subplots(2, 1)
83-
ax1 = ax[0]
84-
ax2 = ax[1]
85-
ax1.grid()
86-
ax1r = ax1.twinx()
87-
88-
c = JuliaMPBSolver.Postprocess.compute_concentrations(
89-
sol[1, :],
90-
user_parameters,
91-
computed_parameters,
92-
)
93-
cm = c[1, :] nv / (JuliaMPBSolver.Units.M) / grid_parameters.domain_size
94-
cp = c[2, :] nv / (JuliaMPBSolver.Units.M) / grid_parameters.domain_size
95-
c0 = -(sum(c, dims = 1) .- c̄)
96-
e = bee(sol)
97-
ax1.set_title(
98-
"ϕ∈$(round.(Float64.(extrema(sol[1, :])), sigdigits = 3)), ε_r ∈$(round.(Float64.(extrema(ε_r)), sigdigits = 3))",
99-
)
100-
ax1.plot(
101-
X / JuliaMPBSolver.Units.nm,
102-
sol[1, :],
103-
color = "green",
104-
linewidth = 2,
105-
label = "ϕ",
106-
)
107-
ax1.plot(
108-
X / JuliaMPBSolver.Units.nm,
109-
e[1, :],
110-
color = "blue",
111-
linewidth = 2,
112-
label = L"ψ^+",
113-
linestyle = "dotted",
114-
)
115-
ax1.plot(
116-
X / JuliaMPBSolver.Units.nm,
117-
e[2, :],
118-
color = "red",
119-
linewidth = 2,
120-
linestyle = "dotted",
121-
label = L"ψ^+",
122-
)
123-
ax1r.plot(
124-
X[1:(end - 1)] / JuliaMPBSolver.Units.nm,
125-
ε_r,
126-
color = "pink",
127-
linewidth = 3,
128-
label = L"ε_r",
129-
)
130-
ax1.set_ylim(-10, 10)
131-
ax1.set_xlabel("z/nm")
132-
ax1.set_ylabel("ϕ/V")
133-
ax1.legend(loc = (0.1, 0.1))
134-
ax1r.legend(loc = (0.8, 0.1))
135-
ax1r.set_ylim(0, 80)
136-
137-
ax2.grid()
138-
ax2.set_title("M_avg=$(round.((cm, cp), sigdigits = 3))")
139-
ax2.set_xlabel("z/nm")
140-
ax2.set_ylabel("c/(mol/L)")
141-
ax2.set_ylim(0, 60)
142-
143-
ax2.plot(
144-
X / JuliaMPBSolver.Units.nm,
145-
c[1, :] / (JuliaMPBSolver.Units.M),
146-
color = "blue",
147-
linewidth = 2,
148-
label = L"c^-",
149-
)
150-
ax2.plot(
151-
X / JuliaMPBSolver.Units.nm,
152-
c[2, :] / (JuliaMPBSolver.Units.M),
153-
color = "red",
154-
linewidth = 2,
155-
label = L"c^+",
156-
)
157-
ax2.plot(
158-
X / JuliaMPBSolver.Units.nm,
159-
c0[1, :] / (JuliaMPBSolver.Units.M),
160-
color = "green",
161-
linewidth = 2,
162-
label = L"c_{solvent}",
163-
)
164-
ax2.legend(loc = (0.4, 0.1))
165-
166-
tight_layout()
167-
savefig("simplecell.jpg", dpi = 300)
168-
return PythonPlot.gcf()
169-
end
170-
171-
plotsol(solution)
56+
save("simplecell.jld2", "solution", solution, "X", X, "nv", nv, "ε_r", ε_r)

simplecell-bsk.jpg

-217 KB
Binary file not shown.

simplecell.jpg

-162 KB
Binary file not shown.

test/runtests.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using Pkg, JuliaMPBSolver, ExampleJuggler, Test, Markdown
1+
using Pkg
2+
using JuliaMPBSolver
3+
using ExampleJuggler
4+
using Test
5+
using Markdown
6+
using JLD2
27

38
ExampleJuggler.verbose!(true)
49

@@ -11,8 +16,31 @@ notebooks = [
1116
"SymmetricCellSurfaceCharge.jl",
1217
]
1318

19+
scripts = [
20+
"simplecell.jl",
21+
"simplecell-bsk.jl",
22+
]
23+
1424
@testset "Notebooks" begin
1525
@testscripts(joinpath(@__DIR__, "..", "notebooks"), notebooks)
1626
end
1727

28+
@testset "Scripts" begin
29+
@testscripts(joinpath(@__DIR__, "..", "scripts"), scripts)
30+
31+
for script in scripts
32+
script_name = script[1:(end - 3)]
33+
solution, X, nv, ε_r = load(script_name * ".jld2", "solution", "X", "nv", "ε_r")
34+
solution_reference, X_reference, nv_reference, ε_r_reference = load(script_name * "-reference.jld2", "solution", "X", "nv", "ε_r")
35+
36+
@test solution solution_reference
37+
@test X X_reference
38+
@test nv nv_reference
39+
@test ε_r ε_r_reference
40+
41+
rm(script_name * ".jld2")
42+
end
43+
44+
end
45+
1846
Pkg.test("JuliaMPBSolver")

test/simplecell-bsk-reference.jld2

22.4 KB
Binary file not shown.

test/simplecell-reference.jld2

19.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)