Skip to content

Commit

Permalink
obvious bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
warisa-r committed Oct 14, 2024
1 parent 22bfc6c commit 96a7bf8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
6 changes: 3 additions & 3 deletions ext/TrixiNLsolveExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function Trixi.solve_a_butcher_coeffs_unknown!(a_unknown, num_stages, num_stage_
# Due to the nature of the nonlinear solver, different initial guesses can lead to
# small numerical differences in the solution.

x0 = convert(RealT, 0.1) .* rand(rng, RealT, num_stages - 2)
x0 = convert(RealT, 0.1) .* rand(rng, RealT, num_stage_evals - 2)

sol = nlsolve(objective_function!, x0, method = :trust_region,
ftol = 4.0e-16, # Enforce objective up to machine precision
Expand All @@ -112,8 +112,8 @@ function Trixi.solve_a_butcher_coeffs_unknown!(a_unknown, num_stages, num_stage_
# and also c[i] - a[i, i-1] >= 0.0 since all coefficients should be non-negative
# to avoid downwinding of numerical fluxes.
is_sol_valid = all(x -> !isnan(x) && x >= 0, a_unknown) &&
all(!isnan(c[i] - a_unknown[i - 2]) &&
c[i] - a_unknown[i - 2] >= 0 for i in eachindex(c) if i > 2)
all(!isnan(c[i] - a_unknown[i - num_stage_evals]) &&
c[i] - a_unknown[i - num_stage_evals] >= 0 for i in eachindex(c) if i > num_stage_evals)

if is_sol_valid
return a_unknown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ function compute_EmbeddedPairedRK3_butcher_tableau(num_stages, num_stage_evals,
c = compute_c_coeffs(num_stages, cS2)

# Initialize the array of our solution
a_unknown = zeros(num_stages - 2)
a_unknown = zeros(num_stage_evals - 2)

# Special case of e = 3
if num_stages == 3
if num_stage_evals == 3
a_unknown = [0.25] # Use classic SSPRK33 (Shu-Osher) Butcher Tableau
else
# Calculate coefficients of the stability polynomial in monomial form
Expand All @@ -45,8 +45,8 @@ function compute_EmbeddedPairedRK3_butcher_tableau(num_stages, num_stage_evals,
verbose)
end
# Fill A-matrix in P-ERK style
a_matrix = zeros(num_stages - 2, 2)
a_matrix[:, 1] = c[3:end]
a_matrix = zeros(num_stage_evals - 2, 2)
a_matrix[:, 1] = c[num_stages - num_stage_evals + 3:end] # issue
a_matrix[:, 1] -= a_unknown
a_matrix[:, 2] = a_unknown

Expand Down Expand Up @@ -303,14 +303,13 @@ function step!(integrator::EmbeddedPairedRK3Integrator)
end

# k_e (k at posoition num_stage_evals)

# Construct current state
@threaded for i in eachindex(integrator.du)
integrator.u_tmp[i] = integrator.u[i] + alg.c[stage] * integrator.k1[i]
integrator.u_tmp[i] = integrator.u[i] + alg.c[alg.num_stage_evals] * integrator.k1[i]
end

integrator.f(integrator.du, integrator.u_tmp, prob.p,
integrator.t + alg.c[stage] * integrator.dt)
integrator.t + alg.c[alg.num_stage_evals] * integrator.dt)

@threaded for i in eachindex(integrator.du)
integrator.k_higher[i] = integrator.du[i] * integrator.dt
Expand All @@ -326,9 +325,9 @@ function step!(integrator::EmbeddedPairedRK3Integrator)
# Construct current state
@threaded for i in eachindex(integrator.du)
integrator.u_tmp[i] = integrator.u[i] +
alg.a_matrix[stage - 2, 1] * #TODO: work on the indexing here and below
alg.a_matrix[stage - alg.num_stage_evals, 1]
integrator.k1[i] +
alg.a_matrix[stage - 2, 2] *
alg.a_matrix[stage - alg.num_stage_evals, 2] *
integrator.k_higher[i]
end

Expand All @@ -341,24 +340,22 @@ function step!(integrator::EmbeddedPairedRK3Integrator)

@threaded for i in eachindex(integrator.u)
integrator.u[i] += integrator.k_higher[i] *
alg.b[stage - alg.num_stages + alg.num_stage_evals - 1]
alg.b[stage - alg.num_stage_evals + 1]
end
end

#TODO: Check if commenting this is equal to not commenting
#=
# Last stage
@threaded for i in eachindex(integrator.du)
integrator.u_tmp[i] = integrator.u[i] +
alg.a_matrix[alg.num_stages - 2, 1] *
alg.a_matrix[alg.num_stages - alg.num_stage_evals - 1, 1] *
integrator.k1[i] +
alg.a_matrix[alg.num_stages - 2, 2] *
alg.a_matrix[alg.num_stages - alg.num_stage_evals - 1, 2] *
integrator.k_higher[i]
end

integrator.f(integrator.du, integrator.u_tmp, prob.p,
integrator.t + alg.c[alg.num_stages] * integrator.dt)
=#

@threaded for i in eachindex(integrator.u)
# add the contribution of the first stage
integrator.u[i] += (1 - sum(alg.b)) * integrator.k1[i]
Expand Down

0 comments on commit 96a7bf8

Please sign in to comment.