Skip to content

Commit

Permalink
Improve test reliability and some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgardner1421 committed Aug 15, 2023
1 parent 2b741f9 commit 4677ebe
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 285 deletions.
20 changes: 16 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
group: [Core, InitialConditions, Dynamics]
group:
- Core
- InitialConditions
- dynamics_classical
- dynamics_mdef
- dynamics_surface_hopping
- dynamics_cme
- dynamics_mapping
- dynamics_ehrenfest
- dynamics_ensembles
fail-fast: false

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@latest

- uses: actions/cache@v1
- uses: actions/cache@v3
env:
cache-name: cache-artifacts
with:
Expand All @@ -33,6 +42,9 @@ jobs:
- name: Add JuliaMolSim registry
run: julia -e 'using Pkg; Pkg.Registry.add(RegistrySpec(url="https://github.com/JuliaMolSim/MolSim"))'
shell: bash
- name: Add ACEregistry
run: julia -e 'using Pkg; Pkg.Registry.add(RegistrySpec(url="https://github.com/ACEsuit/ACEregistry"))'
shell: bash

- name: Install ase for IO tests
run: pip3 install ase
Expand All @@ -42,6 +54,6 @@ jobs:
env:
GROUP: ${{ matrix.group }}
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v2
- uses: codecov/codecov-action@v3
with:
file: lcov.info
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
UnitfulAtomic = "a7773ee8-282e-5fa2-be4e-bd808c38a91a"

[compat]
AdvancedHMC = "0.3, 0.4, 0.5"
AdvancedHMC = "0.5"
AdvancedMH = "0.6, 0.7"
ComponentArrays = "0.11, 0.12, 0.13, 0.14"
DEDataArrays = "0.2"
Expand Down
55 changes: 38 additions & 17 deletions src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct EffectivePotential{T,B,F}
end

function (effective_potential::EffectivePotential)(r)
(;μ, J, binding_curve) = effective_potential
rotational = J*(J+1) / (2μ*r^2)
(; μ, J, binding_curve) = effective_potential
rotational = J * (J + 1) / (2μ * r^2)

Check warning on line 54 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L53-L54

Added lines #L53 - L54 were not covered by tests

potential = binding_curve.fit(r)
return rotational + potential
Expand All @@ -63,7 +63,7 @@ struct RadialMomentum{T,B,F}
end

function (radial_momentum::RadialMomentum)(r)
(;total_energy, V) = radial_momentum
(; total_energy, V) = radial_momentum

Check warning on line 66 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L66

Added line #L66 was not covered by tests
return sqrt_avoid_negatives(2V.μ * (total_energy - V(r)))
end

Expand Down Expand Up @@ -120,7 +120,7 @@ function generate_1D_vibrations(model::AdiabaticModel, μ::Real, ν::Integer;
)

J = 0
environment = EvaluationEnvironment([1], (1,1), zeros(1,0), 0.0, [1.0])
environment = EvaluationEnvironment([1], (1, 1), zeros(1, 0), 0.0, [1.0])

Check warning on line 123 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L123

Added line #L123 was not covered by tests
binding_curve = calculate_binding_curve(bond_lengths, model, environment)
plot_binding_curve(binding_curve.bond_lengths, binding_curve.potential, binding_curve.fit)

Expand All @@ -144,7 +144,7 @@ Same as `sqrt` but returns `zero(x)` if `x` is negative.
Used here just in case the endpoints are a little off.
"""
function sqrt_avoid_negatives(x)
if x < zero(x)
if x < zero(x)

Check warning on line 147 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L147

Added line #L147 was not covered by tests
@warn """
`sqrt(x)` of negative number attempted: x = $x, returning zero instead.\
This is acceptable if x is very close to zero. Otherwise something has gone wrong.
Expand All @@ -170,27 +170,27 @@ function find_total_energy(V::EffectivePotential, ν)
function nᵣ(E, r₁, r₂)
kernel(r) = sqrt_avoid_negatives(E - V(r))
integral, _ = QuadGK.quadgk(kernel, r₁, r₂; maxevals=100)
return sqrt(2μ)/π * integral - 1/2
return sqrt(2μ) / π * integral - 1 / 2

Check warning on line 173 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L173

Added line #L173 was not covered by tests
end

"Derivative of the above"
function ∂nᵣ∂E(E, r₁, r₂)
kernel(r) = 1 / sqrt_avoid_negatives(E - V(r))
integral, _ = QuadGK.quadgk(kernel, r₁, r₂; maxevals=100)
return sqrt(2μ)/2π * integral
return sqrt(2μ) / 2π * integral

Check warning on line 180 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L180

Added line #L180 was not covered by tests
end

"Second derivative of the above"
function ∂²nᵣ∂E²(E, r₁, r₂)
kernel(r) = 1 / sqrt_avoid_negatives(E - V(r))^3
integral, _ = QuadGK.quadgk(kernel, r₁, r₂; maxevals=100)
return -sqrt(2μ)/4π * integral
return -sqrt(2μ) / 4π * integral

Check warning on line 187 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L187

Added line #L187 was not covered by tests
end

"""
Evaluates the function, gradient and hessian as described in the Optim documentation.
"""
function fgh!(F,G,H,x)
function fgh!(F, G, H, x)

Check warning on line 193 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L193

Added line #L193 was not covered by tests
E = x[1]
r₁, r₂ = find_integral_bounds(E, V)

Expand Down Expand Up @@ -224,6 +224,27 @@ function find_integral_bounds(total_energy::Real, V::EffectivePotential)
r₀ = V.binding_curve.equilibrium_bond_length
minimum_bond_length = first(V.binding_curve.bond_lengths)
maximum_bond_length = last(V.binding_curve.bond_lengths)

min_energy = energy_difference(minimum_bond_length)
mid_energy = energy_difference(r₀)
max_energy = energy_difference(maximum_bond_length)

Check warning on line 230 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L228-L230

Added lines #L228 - L230 were not covered by tests

if min_energy > 0
@error "Energy difference at minimum bond length must be negative!" minimum_bond_length min_energy
@info "Try reducing the minimum bond length with the `bond_lengths` keyword."

Check warning on line 234 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L232-L234

Added lines #L232 - L234 were not covered by tests
end

if mid_energy < 0
@error "Energy difference at equilibrium bond length must be positive!" r₀ mid_energy
@info "The current atomic positions and velocities are incompatible with the provided potential. \

Check warning on line 239 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L237-L239

Added lines #L237 - L239 were not covered by tests
Is there something wrong with the structure?"
end

if max_energy > 0
@error "Energy difference at maximum bond length must be negative!" maximum_bond_length max_energy
@info "Try increasing the maximum bond length with the `bond_lengths` keyword."

Check warning on line 245 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L243-L245

Added lines #L243 - L245 were not covered by tests
end

r₁ = @timeit TIMER "Lower bound" Roots.find_zero(energy_difference, (minimum_bond_length, r₀))
r₂ = @timeit TIMER "Upper bound" Roots.find_zero(energy_difference, (r₀, maximum_bond_length))

Expand Down Expand Up @@ -261,7 +282,7 @@ function quantise_diatomic(sim::Simulation, v::Matrix, r::Matrix;
E = k + p

L = total_angular_momentum(r_com, p_com)
J = round(Int, (sqrt(1+4*L^2) - 1) / 2) # L^2 = J(J+1)ħ^2
J = round(Int, (sqrt(1 + 4 * L^2) - 1) / 2) # L^2 = J(J+1)ħ^2

Check warning on line 285 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L285

Added line #L285 was not covered by tests

μ = reduced_mass(masses(sim)[atom_indices])

Expand All @@ -274,7 +295,7 @@ function quantise_diatomic(sim::Simulation, v::Matrix, r::Matrix;
function nᵣ(E, r₁, r₂)
kernel(r) = sqrt_avoid_negatives(E - V(r))
integral, _ = QuadGK.quadgk(kernel, r₁, r₂; maxevals=100)
return sqrt(2μ)/π * integral - 1/2
return sqrt(2μ) / π * integral - 1 / 2

Check warning on line 298 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L298

Added line #L298 was not covered by tests
end

ν = @timeit TIMER "Calculating ν" nᵣ(E, r₁, r₂)
Expand All @@ -290,7 +311,7 @@ function quantise_1D_vibration(model::AdiabaticModel, μ::Real, r::Real, v::Real
)
reset_timer && TimerOutputs.reset_timer!(TIMER)

environment = EvaluationEnvironment([1], (1,1), zeros(1,0), 0.0, [1.0])
environment = EvaluationEnvironment([1], (1, 1), zeros(1, 0), 0.0, [1.0])

Check warning on line 314 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L314

Added line #L314 was not covered by tests

kinetic_energy = μ * v^2 / 2
potential_energy = calculate_diatomic_energy(r, model, environment)
Expand All @@ -306,7 +327,7 @@ function quantise_1D_vibration(model::AdiabaticModel, μ::Real, r::Real, v::Real
function nᵣ(E, r₁, r₂)
kernel(r) = sqrt_avoid_negatives(E - V(r))
integral, _ = QuadGK.quadgk(kernel, r₁, r₂; maxevals=100)
return sqrt(2μ)/π * integral - 1/2
return sqrt(2μ) / π * integral - 1 / 2

Check warning on line 330 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L330

Added line #L330 was not covered by tests
end

ν = @timeit TIMER "Calculating ν" nᵣ(E, r₁, r₂)
Expand All @@ -318,20 +339,20 @@ function quantise_1D_vibration(model::AdiabaticModel, μ::Real, r::Real, v::Real
end

subtract_centre_of_mass(x, m) = x .- centre_of_mass(x, m)
@views centre_of_mass(x, m) = (x[:,1].*m[1] .+ x[:,2].*m[2]) ./ (m[1]+m[2])
@views centre_of_mass(x, m) = (x[:, 1] .* m[1] .+ x[:, 2] .* m[2]) ./ (m[1] + m[2])

Check warning on line 342 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L342

Added line #L342 was not covered by tests
reduced_mass(atoms::Atoms) = reduced_mass(atoms.masses)

function reduced_mass(m::AbstractVector)
if length(m) == 1
return m[1]
elseif length(m) == 2
m[1]*m[2]/(m[1]+m[2])
m[1] * m[2] / (m[1] + m[2])

Check warning on line 349 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L349

Added line #L349 was not covered by tests
else
throw(error("Mass vector of incorrect length."))
end
end

@views bond_length(r) = norm(r[:,1] .- r[:,2])
@views total_angular_momentum(r, p) = norm(cross(r[:,1], p[:,1]) + cross(r[:,2], p[:,2]))
@views bond_length(r) = norm(r[:, 1] .- r[:, 2])
@views total_angular_momentum(r, p) = norm(cross(r[:, 1], p[:, 1]) + cross(r[:, 2], p[:, 2]))

Check warning on line 356 in src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl

View check run for this annotation

Codecov / codecov/patch

src/InitialConditions/QuantisedDiatomic/QuantisedDiatomic.jl#L355-L356

Added lines #L355 - L356 were not covered by tests

end # module
Loading

0 comments on commit 4677ebe

Please sign in to comment.