Skip to content

Commit

Permalink
Make symbolics a Module (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
oameye authored Oct 14, 2024
1 parent 46315bc commit fded42a
Show file tree
Hide file tree
Showing 31 changed files with 150 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/DifferentialEquation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Return the dependent variables of `diff_eom`.
Symbolics.get_variables(diff_eom::DifferentialEquation)::Vector{Num} =
collect(keys(diff_eom.equations))

is_harmonic(diff_eom::DifferentialEquation, t::Num)::Bool =
ExprUtils.is_harmonic(diff_eom::DifferentialEquation, t::Num)::Bool =
all([is_harmonic(eq, t) for eq in values(diff_eom.equations)])

"Pretty printing of the newly defined types"
Expand Down
42 changes: 42 additions & 0 deletions src/ExprUtils/ExprUtils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module ExprUtils

using DocStringExtensions
using OrderedCollections: OrderedDict

using SymbolicUtils:
SymbolicUtils,
Postwalk,
Sym,
BasicSymbolic,
isterm,
ispow,
isadd,
isdiv,
ismul,
add_with_div,
frac_maketerm,
@compactified,
issym

using Symbolics:
Symbolics,
Num,
unwrap,
wrap,
get_variables,
Equation,
Differential,
@variables,
arguments,
simplify_fractions,
substitute,
term,
expand,
operation

include("Symbolics_utils.jl")
include("exponentials.jl")
include("fourier.jl")
include("drop_powers.jl")

end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 4 additions & 9 deletions src/modules/HC_wrapper.jl → src/HC_wrapper/HC_wrapper.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
module HC_wrapper

using DocStringExtensions
using Symbolics: Num, @variables
using Symbolics: Num, @variables, expand_derivatives, get_variables
using Symbolics.SymbolicUtils: isterm
using LinearAlgebra: LinearAlgebra

using HarmonicBalance:
HarmonicBalance,
HarmonicEquation,
_remove_brackets,
expand_derivatives,
var_name,
get_variables,
Problem
HarmonicBalance, HarmonicEquation, _remove_brackets, var_name, Problem

using HomotopyContinuation
using HomotopyContinuation: Variable, System

include("HC_wrapper/homotopy_interface.jl")
include("homotopy_interface.jl")

export Problem

Expand Down
File renamed without changes.
88 changes: 28 additions & 60 deletions src/HarmonicBalance.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module HarmonicBalance

# default global settings
IM_TOL::Float64 = 1E-6
function set_imaginary_tolerance(x::Float64)
@eval(IM_TOL::Float64 = $x)
end

using DocStringExtensions
using JLD2: JLD2
using DelimitedFiles: DelimitedFiles, writedlm
Expand All @@ -16,54 +22,14 @@ const HC = HomotopyContinuation
using Plots: Plots, plot, plot!, savefig, heatmap, Plot
using Latexify: Latexify, latexify

using PrecompileTools: @setup_workload, @compile_workload

# default global settings
IM_TOL::Float64 = 1E-6
function set_imaginary_tolerance(x::Float64)
@eval(IM_TOL::Float64 = $x)
end
using Symbolics:
Symbolics, Num, Equation, @variables, expand_derivatives, get_variables, Differential
using SymbolicUtils: SymbolicUtils

using SymbolicUtils:
SymbolicUtils,
Postwalk,
Sym,
BasicSymbolic,
isterm,
ispow,
isadd,
isdiv,
ismul,
add_with_div,
frac_maketerm,
@compactified,
issym
include("ExprUtils/ExprUtils.jl")
using .ExprUtils: is_harmonic, substitute_all, drop_powers

using Symbolics:
Symbolics,
Num,
unwrap,
wrap,
get_variables,
simplify,
expand_derivatives,
build_function,
Equation,
Differential,
@variables,
arguments,
simplify_fractions,
substitute,
term,
expand,
operation

include("Symbolics/Symbolics_utils.jl")
include("Symbolics/exponentials.jl")
include("Symbolics/fourier.jl")
include("Symbolics/drop_powers.jl")

include("modules/extention_functions.jl")
include("extention_functions.jl")
include("utils.jl")
include("types.jl")

Expand All @@ -90,34 +56,36 @@ export plot, plot!, plot_phase_diagram, savefig, plot_spaghetti
export ParameterSweep, steady_state_sweep
export plot_1D_solutions_branch, follow_branch

include("modules/HC_wrapper.jl")
include("HC_wrapper/HC_wrapper.jl")
using .HC_wrapper

include("modules/LinearResponse.jl")
include("LinearResponse/LinearResponse.jl")
using .LinearResponse
export plot_linear_response, plot_rotframe_jacobian_response, get_Jacobian, plot_eigenvalues
export transform_solutions

include("modules/LimitCycles.jl")
include("LimitCycles/LimitCycles.jl")
using .LimitCycles
export get_cycle_variables, get_limit_cycles, add_pairs!

include("modules/KrylovBogoliubov.jl")
include("KrylovBogoliubov/KrylovBogoliubov.jl")
using .KrylovBogoliubov
export first_order_transform!, is_rearranged_standard, rearrange_standard!, get_equations
export get_krylov_equations

include("modules/FFTWExt.jl")
include("FFTWExt.jl")
using .FFTWExt

# @setup_workload begin
# # Putting some things in `@setup_workload` instead of `@compile_workload` can reduce the size of the
# # precompile file and potentially make loading faster.
# @compile_workload begin
# # all calls in this block will be precompiled, regardless of whether
# # they belong to your package or not (on Julia 1.8 and higher)
# include("precompilation.jl")
# end
# end
using PrecompileTools: @setup_workload, @compile_workload

@setup_workload begin
# Putting some things in `@setup_workload` instead of `@compile_workload` can reduce the size of the
# precompile file and potentially make loading faster.
@compile_workload begin
# all calls in this block will be precompiled, regardless of whether
# they belong to your package or not (on Julia 1.8 and higher)
include("precompilation.jl")
end
end

end # module
12 changes: 7 additions & 5 deletions src/HarmonicEquation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function slow_flow(
end

# Drop powers of `var` of degree >= `deg` from the equation set in `eom`.
function drop_powers(eom::HarmonicEquation, terms::Vector{Num}, deg::Int)
function ExprUtils.drop_powers(eom::HarmonicEquation, terms::Vector{Num}, deg::Int)
new_eom = deepcopy(eom)
new_eom.equations = drop_powers(eom.equations, terms, deg)
return new_eom
Expand Down Expand Up @@ -157,7 +157,9 @@ end
###

"Apply `rules` to both `equations` and `variables` field of `eom`"
function substitute_all(eom::HarmonicEquation, rules::Union{Dict,Pair})::HarmonicEquation
function ExprUtils.substitute_all(
eom::HarmonicEquation, rules::Union{Dict,Pair}
)::HarmonicEquation
new_eom = deepcopy(eom)
new_eom.equations = expand_derivatives.(substitute_all(eom.equations, rules))
return new_eom
Expand Down Expand Up @@ -205,11 +207,11 @@ function fourier_transform!(eom::HarmonicEquation, time::Num)
eq = eom.equations[eq_idx]
# "type" is usually "u" or "v" (harmonic) or ["a"] (zero-harmonic)
if hvar.type == "u"
avg_eqs[i] = fourier_cos_term(eq, hvar.ω, time)
avg_eqs[i] = ExprUtils.fourier_cos_term(eq, hvar.ω, time)
elseif hvar.type == "v"
avg_eqs[i] = fourier_sin_term(eq, hvar.ω, time)
avg_eqs[i] = ExprUtils.fourier_sin_term(eq, hvar.ω, time)
elseif hvar.type == "a"
avg_eqs[i] = fourier_cos_term(eq, 0, time) # pick out the constants
avg_eqs[i] = ExprUtils.fourier_cos_term(eq, 0, time) # pick out the constants
end
end
eom.equations = avg_eqs
Expand Down
8 changes: 4 additions & 4 deletions src/HarmonicVariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ end
###

# when HV is used for substitute, substitute its symbol
function substitute_all(eq::Union{Num,Equation}, rules::Dict{HarmonicVariable})
function ExprUtils.substitute_all(eq::Union{Num,Equation}, rules::Dict{HarmonicVariable})
return substitute(eq, Dict(zip(getfield.(keys(rules), :symbol), values(rules))))
end

function substitute_all(var::HarmonicVariable, rules)
function ExprUtils.substitute_all(var::HarmonicVariable, rules)
sym, freq = var.symbol, var.ω
return HarmonicVariable(
substitute_all(sym, rules),
Expand All @@ -39,7 +39,7 @@ function substitute_all(var::HarmonicVariable, rules)
)
end

function substitute_all(vars::Vector{HarmonicVariable}, rules)
function ExprUtils.substitute_all(vars::Vector{HarmonicVariable}, rules)
return [substitute_all(var, rules) for var in vars]
end

Expand Down Expand Up @@ -80,4 +80,4 @@ function var_name(x::Num)
return var isa Expr ? String(var.args[1]) : String(var)
end
# var_name(x::Term) = String(Symbolics._toexpr(x).args[1])
var_name(x::Sym) = String(x.name)
var_name(x::SymbolicUtils.Sym) = String(x.name)
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ using HarmonicBalance
using HarmonicBalance:
rearrange!,
flatten,
is_harmonic,
_create_harmonic_variable,
trig_reduce,
get_independent,
simplify_complex,
is_trig,
substitute_all,
slow_flow,
_remove_brackets,
get_all_terms,
get_variables_nums

include("KrylovBogoliubov/first_order_transform.jl")
include("KrylovBogoliubov/KrylovEquation.jl")
using HarmonicBalance.ExprUtils:
get_all_terms,
substitute_all,
trig_reduce,
get_independent,
simplify_complex,
is_trig,
is_harmonic
include("first_order_transform.jl")
include("KrylovEquation.jl")

export first_order_transform!,
is_rearranged_standard, rearrange_standard!, get_equations, get_krylov_equations
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/modules/LimitCycles.jl → src/LimitCycles/LimitCycles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ using HarmonicBalance:
_free_symbols,
_symidx,
_is_physical,
get_all_terms,
substitute_all,
var_name
using HarmonicBalance.LinearResponse: get_implicit_Jacobian
using HarmonicBalance.ExprUtils: get_all_terms

include("LimitCycles/gauge_fixing.jl")
include("LimitCycles/analysis.jl")
include("gauge_fixing.jl")
include("analysis.jl")

export get_cycle_variables, get_limit_cycles, add_pairs!

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Plots: heatmap, theme_palette, scatter, RGB, cgrad
using Latexify: Latexify, latexify, @L_str
using Latexify.LaTeXStrings: LaTeXStrings

using Symbolics: Num, build_function, Equation, substitute, unwrap
using Symbolics: Num, Equation, substitute, unwrap
using LinearAlgebra: norm, eigvals, eigen, eigvecs
using OrderedCollections: OrderedDict

Expand All @@ -32,12 +32,12 @@ using HarmonicBalance:
is_rearranged
using ..HC_wrapper

include("LinearResponse/types.jl")
include("LinearResponse/utils.jl")
include("LinearResponse/jacobians.jl")
include("LinearResponse/Lorentzian_spectrum.jl")
include("LinearResponse/response.jl")
include("LinearResponse/plotting.jl")
include("types.jl")
include("utils.jl")
include("jacobians.jl")
include("Lorentzian_spectrum.jl")
include("response.jl")
include("plotting.jl")

export get_Jacobian, get_response, show
export plot_linear_response, plot_rotframe_jacobian_response, plot_eigenvalues
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function ResponseMatrix(res::Result; rules=Dict())
M = get_response_matrix(res.problem.eom.natural_equation, Δ; order=2)
M = substitute_all(M, merge(res.fixed_parameters, rules))
symbols = _free_symbols(res)
compiled_M = [build_function(el, cat(symbols, [Δ]; dims=1)) for el in M]
compiled_M = [Symbolics.build_function(el, cat(symbols, [Δ]; dims=1)) for el in M]

return ResponseMatrix(eval.(compiled_M), symbols, res.problem.eom.variables)
end
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/plotting_Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function plot1D(
branches=1:branch_count(res),
add=false,
kwargs...,
)
)::Plots.Plot
if class == "default"
args = [:x => x, :y => y, :branches => branches]
if not_class == [] # plot stable full, unstable dashed
Expand Down Expand Up @@ -202,7 +202,7 @@ function plot2D(
not_class=[],
add=false,
kwargs...,
)
)::Plots.Plot
X, Y = values(res.swept_parameters)
Z =
getindex.(
Expand Down
2 changes: 1 addition & 1 deletion src/solve_homotopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Substitute the values according to `fixed_parameters` and compile into a functio
"""
function compile_matrix(mat, variables; rules=Dict(), postproc=x -> x)
J = substitute_all.(mat, Ref(rules))
matrix = build_function(J, variables)
matrix = Symbolics.build_function(J, variables)
matrix = eval(matrix[1]) # compiled allocating function, see Symbolics manual
m(vals::Vector) = postproc(matrix(vals))
m(s::OrderedDict) = m([s[var] for var in variables]) # for the UI
Expand Down
Loading

0 comments on commit fded42a

Please sign in to comment.