Skip to content

Commit 6b50fda

Browse files
authored
fixed polynomial fit derivative error (#105)
1 parent 0c99ece commit 6b50fda

3 files changed

Lines changed: 28 additions & 18 deletions

File tree

source/equilibrium.f90

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,18 +2725,12 @@ subroutine EqDerivatives_assemble_jacobian(self, solver, solution)
27252725
type(EqSolver), intent(in) :: solver
27262726
type(EqSolution), intent(inout) :: solution
27272727

2728-
if (solver%smooth_truncation) then
2729-
call EqDerivatives_assemble_effective_jacobian(self, solver, solution)
2730-
else
2731-
call solver%assemble_matrix(solution)
2732-
self%J = solution%G(:self%m, :self%m)
2733-
end if
2728+
call EqDerivatives_assemble_effective_jacobian(self, solver, solution)
27342729
end subroutine
27352730

27362731
subroutine EqDerivatives_assemble_effective_jacobian(self, solver, solution)
2737-
! Assemble the derivative-only Jacobian for smooth problems using the
2738-
! converged effective residual linearization rather than the forward
2739-
! Newton update matrix.
2732+
! Assemble the derivative-only Jacobian from the converged effective
2733+
! residual rather than reusing the forward Newton update matrix.
27402734

27412735
! Arguments
27422736
class(EqDerivatives), intent(inout) :: self
@@ -2752,6 +2746,7 @@ subroutine EqDerivatives_assemble_effective_jacobian(self, solver, solution)
27522746
real(dp) :: ln_n
27532747
real(dp) :: ln_threshold
27542748
real(dp) :: n_delta
2749+
real(dp) :: hsu_delta
27552750
real(dp) :: tmp(solver%num_gas)
27562751
real(dp) :: mu_g(solver%num_gas)
27572752
real(dp) :: ln_nj_eff(solver%num_gas)
@@ -2811,6 +2806,14 @@ subroutine EqDerivatives_assemble_effective_jacobian(self, solver, solution)
28112806
nj_eval = solution%nj
28122807
nj_eval(:ng) = nj_eff_g
28132808
n_delta = n - sum(nj_eff_g)
2809+
hsu_delta = 0.0d0
2810+
if (const_h) then
2811+
hsu_delta = solution%constraints%state1/solution%T - &
2812+
dot_product(nj_eval, solution%thermo%enthalpy)
2813+
else if (const_u) then
2814+
hsu_delta = solution%constraints%state1/solution%T - &
2815+
dot_product(nj_eval, solution%thermo%energy)
2816+
end if
28142817

28152818
self%J = 0.0d0
28162819
r = 0
@@ -2913,9 +2916,9 @@ subroutine EqDerivatives_assemble_effective_jacobian(self, solver, solution)
29132916

29142917
c = c + 1
29152918
if (const_p) then
2916-
self%J(r, c) = dot_product(nj_eval, cp) + dot_product(tmp, h_g)
2919+
self%J(r, c) = dot_product(nj_eval, cp) + dot_product(tmp, h_g) + hsu_delta
29172920
else
2918-
self%J(r, c) = dot_product(nj_eval, cv) + dot_product(tmp, u_g)
2921+
self%J(r, c) = dot_product(nj_eval, cv) + dot_product(tmp, u_g) + hsu_delta
29192922
if (const_s) then
29202923
self%J(r, c) = self%J(r, c) - dot_product(nj_eff_g, u_g)
29212924
end if
@@ -3125,9 +3128,6 @@ subroutine EqDerivatives_assemble_Rx(self, solver, solution)
31253128
end if
31263129
else
31273130
self%Rx(r, c) = 0.0d0
3128-
if (.not. const_p) then
3129-
self%Rx(r, c) = self%Rx(r, c) - sum(tmp)/T
3130-
end if
31313131
end if
31323132

31333133
! dR/dx3...n (x3...n: element amounts)

source/fits.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ elemental function calc_denergy_dT(self,T) result(de_dT)
143143
elemental function calc_denthalpy_dT(self,T) result(dh_dT)
144144
class(ThermoFit), intent(in) :: self
145145
real(dp), intent(in) :: T
146-
real(dp) :: dh_dT ! h/R
146+
real(dp) :: dh_dT ! d(H/R)/dT = cp/R
147147
dh_dT = self%a7
148148
dh_dT = T*dh_dT + self%a6
149149
dh_dT = T*dh_dT + self%a5
150150
dh_dT = T*dh_dT + self%a4
151151
dh_dT = T*dh_dT + self%a3
152152
dh_dT = T*dh_dT + self%a2
153-
dh_dT = T*dh_dT + 2.0d0*self%a1
153+
dh_dT = T*dh_dT + self%a1
154154
dh_dT = dh_dT/(T*T)
155155
end function
156156

@@ -175,4 +175,4 @@ elemental function calc_transport_value(self,T) result(value)
175175
value = self%A*log(T) + self%B/T + self%C/(T*T) + self%D
176176
end function
177177

178-
end module
178+
end module

source/fits_test.pf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,14 @@ contains
4747
@assertRelativelyEqual(g, h - T*s, tol)
4848
end subroutine
4949

50-
end module
50+
@test
51+
subroutine test_thermo_derivatives
52+
real(dp), parameter :: T = 600.0d0
53+
real(dp), parameter :: tol = 1.0d-12
54+
real(dp) :: cp
55+
cp = fit%calc_cp(T)
56+
@assertRelativelyEqual(cp, fit%calc_denthalpy_dT(T), tol)
57+
@assertRelativelyEqual(cp/T, fit%calc_dentropy_dT(T), tol)
58+
end subroutine
59+
60+
end module

0 commit comments

Comments
 (0)