Skip to content

Commit

Permalink
comment + fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
warisa-r committed Apr 16, 2024
1 parent f6332fb commit c5fa58b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
31 changes: 24 additions & 7 deletions ext/TrixiConvexECOSExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,29 @@ else
using ..ECOS
end

# Use other necessary libraries
using LinearAlgebra: eigvals
const MOI = Convex.MOI

# Use all exported symbols to avoid having to rewrite `recipes_makie.jl`
using Trixi

# Use additional symbols that are not exported
using Trixi: PolynomialOptimizer

# Import functions such that they can be extended with new methods
import .PolynomialOptimizer: filter_eigvals, bisection, undo_normalization!

using LinearAlgebra: eigvals
const MOI = Convex.MOI
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).
# Since these FMAs can increase the performance of many numerical algorithms,
# we need to opt-in explicitly.
# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.
@muladd begin
#! format: noindent

function stability_polynomials(cons_order, num_stage_evals, num_eig_vals,
normalized_powered_eigvals_scaled, pnoms, gamma::Variable)
normalized_powered_eigvals_scaled, pnoms,
gamma::Variable)
# Initialize with zero'th order (z^0) coefficient
for i in 1:num_eig_vals
pnoms[i] = 1.0
Expand Down Expand Up @@ -87,13 +100,16 @@ function bisection(cons_order, num_eig_vals, num_stage_evals, dtmax, dteps, eig_
dt_k = dt^k
for i in 1:num_eig_vals
normalized_powered_eigvals_scaled[i, k] = dt_k *
normalized_powered_eigvals[i, k]
normalized_powered_eigvals[i,
k]
end
end

# Use last optimal values for gamma in (potentially) next iteration
problem = minimize(stability_polynomials(cons_order, num_stage_evals, num_eig_vals,
normalized_powered_eigvals_scaled, pnoms,
problem = minimize(stability_polynomials(cons_order, num_stage_evals,
num_eig_vals,
normalized_powered_eigvals_scaled,
pnoms,
gamma))

Convex.solve!(problem,
Expand Down Expand Up @@ -123,5 +139,6 @@ function bisection(cons_order, num_eig_vals, num_stage_evals, dtmax, dteps, eig_

return evaluate(gamma), dt
end
end # @muladd

end # module
end # module TrixiConvexECOSExt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
using DelimitedFiles: readdlm
using LinearAlgebra: eigvals
using .PolynomialOptimizer
import .PolynomialOptimizer: filter_eigvals, bisection, undo_normalization!

#Fake change
import .PolynomialOptimizer: filter_eigvals, undo_normalization!, bisection

@muladd begin
#! format: noindent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module PolynomialOptimizer

#using ECOS
#using Convex
#const MOI = Convex.MOI
# implemented here
export filter_eigvals, undo_normalization!

# implemented in TrixiConvexExt and only usable when user adds Convex
export stability_polynomials, bisection

export filter_eigvals
function filter_eigvals(eig_vals, threshold)
filtered_eigvals_counter = 0
filtered_eig_vals = Complex{Float64}[]
Expand All @@ -26,22 +27,15 @@ function filter_eigvals(eig_vals, threshold)
return length(filtered_eig_vals), filtered_eig_vals
end

# implemented in TrixiConvexExt
export stability_polynomials
function stability_polynomials end

export bisection
function bisection end



export undo_normalization!

function undo_normalization!(cons_order, num_stage_evals, gamma_opt)
for k in (cons_order + 1):num_stage_evals
gamma_opt[k - cons_order] = gamma_opt[k - cons_order] / factorial(k)
end
return gamma_opt
end

end # module PolynomialOptimizer
function stability_polynomials end

function bisection end

end # module PolynomialOptimizer

0 comments on commit c5fa58b

Please sign in to comment.