Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit Testing for development #56

Open
3 tasks
jiweiqi opened this issue May 8, 2021 · 3 comments
Open
3 tasks

Add Unit Testing for development #56

jiweiqi opened this issue May 8, 2021 · 3 comments
Assignees

Comments

@jiweiqi
Copy link
Contributor

jiweiqi commented May 8, 2021

As the package becomes more and more complex, it is important to adopt unit testing. We can following the example of RMS.jl at https://github.com/ReactionMechanismGenerator/ReactionMechanismSimulator.jl/blob/ac2a3bd88504b86d5bc60dedc09533839a9c9cbb/src/Calculators/TestThermo.jl#L1-L60.

Some thoughts:

  • We could put the test files inside the src folder, with a consistent prefix, so that one can easily spot the actual source file.
  • We can test the functions against Cantera via PyCall. This should work well for most of the functionality.
  • @test Cps ≈ Cpexplist rtol=1e-3 where 1e-3 is a good default value for tolerance.
@jiweiqi jiweiqi self-assigned this May 8, 2021
@TJP-Karpowski
Copy link
Contributor

That sound like a good ideal.
I can add that for the thermo interface.

I think the naming "test_.jl" sounds great. I also agree, that we can test a lot against Cantera.
But could you explain, why you would put the test files in the source folder? I would prefer those test files either in the toplevel test dir (e.g. test/test_Thermo.jl) or in a separate folder in the src directory (src/tests/test_Thermo.jl) as I fear, that the src dir becomes cluttered over time. I also find it conceptually odd to place things which are not actually src files in the src toplevel. But maybe I am just unaware of some advantages of the shared folder (especially if they are unique to Julia). In that case please let me know :).

@jiweiqi
Copy link
Contributor Author

jiweiqi commented May 9, 2021

Hi @TJP-Karpowski , I agree with you that we should make the top level of src being clean. Indeed, putting them into either ./test/ or ./src/test is a good idea.

As for the current test files, which are put at the level of src, the reason is that I haven't figured out a good pipeline for debugging code during the development phase. As you probably know, if I import Arrhenius.jl via using Arrhenius, every time I change the source code, I will have to restart the Julia terminal. Instead, I currently import Arrhenius.jl by directly executing the module file as done in

module Arrhenius
using YAML
using NPZ
using LinearAlgebra
using SparseArrays
include("Constants.jl")
include("DataStructure.jl")
include("Solution.jl")
include("Magic.jl")
include("Thermo.jl")
include("Kinetics.jl")
include("Transport.jl")
end
```
Following codes are used during development phase only.
```
using LinearAlgebra
using DifferentialEquations
using Sundials
using Profile
using ForwardDiff
using SparseArrays
using PyCall
using Test
R = Arrhenius.R
one_atm = Arrhenius.one_atm
mech = "../mechanism/h2o2.yaml"
gas = Arrhenius.CreateSolution(mech);
ns = gas.n_species
Y0 = zeros(ns);
Y0[Arrhenius.species_index(gas, "H2")] = 0.1;
Y0[Arrhenius.species_index(gas, "O2")] = 0.2;
Y0[Arrhenius.species_index(gas, "AR")] = 0.7;
. I feel this is not a good way to do it. But at the current time being, I haven't tried other approaches.

Noting that, I have to call Arrhenius.R to access the member of the Arrhenius module. This is different from the standard test file, like

using Arrhenius
using ForwardDiff
using LinearAlgebra
using Test
@testset "jl" begin
# Write your tests here.
gas = CreateSolution("../mechanism/gri30.yaml")
ns = gas.n_species
Y0 = ones(ns) ./ ns
T0 = 1200.0
P = one_atm
wdot = set_states(gas, T0, P, Y0)
@show size(wdot)
@show wdot[1:3]
u0 = vcat(Y0, T0)
function f(u)
T = u[end]
Y = @view(u[1:ns])
mean_MW = 1.0 / dot(Y, 1 ./ gas.MW)
ρ_mass = P / R / T * mean_MW
X = Y2X(gas, Y, mean_MW)
C = Y2C(gas, Y, ρ_mass)
cp_mole, cp_mass = get_cp(gas, T, X, mean_MW)
h_mole = get_H(gas, T, Y, X)
S0 = get_S(gas, T, P, X)
wdot = wdot_func(gas.reaction, T, C, S0, h_mole)
Tdot = -dot(h_mole, wdot) / ρ_mass / cp_mass
return Tdot
end
grad = ForwardDiff.gradient(f, u0)
@show size(grad)
@show grad[1:3]
end
.

@TJP-Karpowski
Copy link
Contributor

I had the same problem with the Julia restart, luckily we are not the first to have that issue. You can use the revise package: https://timholy.github.io/Revise.jl/stable/ .
Just call using Revise before using Arrhenius and then revise will check for changes to the source files and will apply those changes in the running session :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants