-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01fb132
commit 284612b
Showing
6 changed files
with
99 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
struct AdiabaticStateSelector{M} <: NQCModels.AdiabaticModels.AdiabaticModel | ||
model::M | ||
state::Int | ||
function AdiabaticStateSelector(model, state) | ||
state < 1 && throw(DomainError(state, "selected state must be greater than 0")) | ||
state > NQCModels.nstates(model) && throw(DomainError(state, "selected state must be less than the total number of states of the diabatic model")) | ||
return new{typeof(model)}(model, state) | ||
end | ||
end | ||
|
||
NQCModels.ndofs(model::AdiabaticStateSelector) = NQCModels.ndofs(model.model) | ||
|
||
function NQCModels.potential(model::AdiabaticStateSelector, r::AbstractMatrix) | ||
V = NQCModels.potential(model.model, r) | ||
eigenvalues = LinearAlgebra.eigvals(V) | ||
return eigenvalues[model.state] | ||
end | ||
|
||
function NQCModels.derivative!(model::AdiabaticStateSelector, output::AbstractMatrix, r::AbstractMatrix) | ||
V = NQCModels.potential(model.model, r) | ||
U = LinearAlgebra.eigvecs(V) | ||
D = NQCModels.derivative(model.model, r) | ||
for I in eachindex(output, D) | ||
output[I] = (U' * D[I] * U)[model.state, model.state] | ||
end | ||
return output | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Test | ||
using NQCModels | ||
using LinearAlgebra: eigvals | ||
|
||
include("test_utils.jl") | ||
|
||
innermodel = DoubleWell() | ||
|
||
@testset "State index out of bounds" begin | ||
@test_throws DomainError AdiabaticStateSelector(innermodel, 0) | ||
@test_throws DomainError AdiabaticStateSelector(innermodel, 3) | ||
end | ||
|
||
@testset "Potential, state: $selected_state" for selected_state = 1:2 | ||
model = AdiabaticStateSelector(innermodel, selected_state) | ||
r = randn(1,1) | ||
|
||
diabatic_potential = potential(innermodel, r) | ||
correct_value = eigvals(diabatic_potential)[selected_state] | ||
new_value = potential(model, r) | ||
@test correct_value ≈ new_value | ||
end | ||
|
||
@testset "Potential, state: $selected_state" for selected_state = 1:2 | ||
model = AdiabaticStateSelector(innermodel, selected_state) | ||
@test test_model(model, 1) | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using FiniteDiff | ||
using NQCModels | ||
|
||
function finite_difference_gradient(model::NQCModels.AdiabaticModels.AdiabaticModel, R) | ||
f(x) = potential(model, x) | ||
FiniteDiff.finite_difference_gradient(f, R) | ||
end | ||
|
||
function finite_difference_gradient(model::NQCModels.DiabaticModels.DiabaticModel, R) | ||
f(x, j, i) = potential(model, x)[j,i] | ||
grad = [Hermitian(zeros(nstates(model), nstates(model))) for _ in CartesianIndices(R)] | ||
for i=1:nstates(model) | ||
for j=1:nstates(model) | ||
gradient = FiniteDiff.finite_difference_gradient(x->f(x,j,i), R) | ||
for k in eachindex(R) | ||
grad[k].data[j,i] = gradient[k] | ||
end | ||
end | ||
end | ||
grad | ||
end | ||
|
||
function test_model(model::NQCModels.Model, atoms; rtol=1e-5) | ||
R = rand(ndofs(model), atoms) | ||
D = derivative(model, R) | ||
finite_diff = finite_difference_gradient(model, R) | ||
return isapprox(finite_diff, D, rtol=rtol) | ||
end | ||
|
||
function test_model(model::NQCModels.FrictionModels.AdiabaticFrictionModel, atoms) | ||
R = rand(ndofs(model), atoms) | ||
D = derivative(model, R) | ||
friction(model, R) | ||
return finite_difference_gradient(model, R) ≈ D | ||
end |