-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parsing of time-dependent data,
plot
overload (#31)
* transform_solutions for time-dependent results * plot overloaded for Result and ODESolution * examples and docs changes * Jacobian bugfix
- Loading branch information
Showing
13 changed files
with
129 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "HarmonicBalance" | ||
uuid = "e13b9ff6-59c3-11ec-14b1-f3d2cc6c135e" | ||
authors = ["Jan Kosata <[email protected]>", "Javier del Pino <[email protected]>"] | ||
version = "0.5.1" | ||
version = "0.5.2" | ||
|
||
[deps] | ||
BijectiveHilbert = "91e7fc40-53cd-4118-bd19-d7fcd1de2a54" | ||
|
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
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
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
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,62 @@ | ||
export transform_solutions | ||
|
||
|
||
_parse_expression(exp) = exp isa String ? Num(eval(Meta.parse(exp))) : exp | ||
|
||
|
||
""" | ||
$(TYPEDSIGNATURES) | ||
|
||
Takes a `Result` object and a string `f` representing a Symbolics.jl expression. | ||
Returns an array with the values of `f` evaluated for the respective solutions. | ||
Additional substitution rules can be specified in `rules` in the format `("a" => val)` or `(a => val)` | ||
""" | ||
function transform_solutions(res::Result, f::String; rules=Dict()) | ||
# a string is used as input - a macro would not "see" the user's namespace while the user's namespace does not "see" the variables | ||
transformed = [Vector{ComplexF64}(undef, length(res.solutions[1])) for k in res.solutions] # preallocate | ||
|
||
# define variables in rules in this namespace | ||
new_keys = declare_variable.(string.(keys(Dict(rules)))) | ||
expr = f isa String ? _parse_expression(f) : f | ||
|
||
fixed_subs = merge(res.fixed_parameters, Dict(zip(new_keys, values(Dict(rules))))) | ||
expr = substitute_all(expr, Dict(fixed_subs)) | ||
|
||
vars = res.problem.variables | ||
all_symbols = cat(vars, collect(keys(res.swept_parameters)), dims=1) | ||
comp_func = build_function(expr, all_symbols) | ||
f = eval(comp_func) | ||
|
||
# preallocate an array for the numerical values, rewrite parts of it | ||
# when looping through the solutions | ||
vals = Vector{ComplexF64}(undef, length(all_symbols)) | ||
n_vars = length(vars) | ||
n_pars = length(all_symbols) - n_vars | ||
|
||
for idx in CartesianIndices(res.solutions) | ||
params_values = res.swept_parameters[Tuple(idx)...] | ||
vals[end-n_pars+1:end] .= params_values # param values are common to all branches | ||
for (branch,soln) in enumerate(res.solutions[idx]) | ||
vals[1:n_vars] .= soln | ||
transformed[idx][branch] = Base.invokelatest(f, vals) | ||
end | ||
end | ||
return transformed | ||
end | ||
|
||
|
||
# a simplified version meant to work with arrays of solutions | ||
# cannot parse parameter values mainly meant for time-dependent results | ||
function transform_solutions(soln::Vector, f::String, harm_eq::HarmonicEquation) | ||
|
||
vars = _remove_brackets(get_variables(harm_eq)) | ||
transformed = Vector{ComplexF64}(undef, length(soln)) | ||
|
||
# parse the input with Symbolics | ||
expr = HarmonicBalance._parse_expression(f) | ||
|
||
rule(u) = Dict(zip(vars, u)) | ||
|
||
transformed = map( x -> substitute_all(expr, rule(x)), soln) | ||
return convert(typeof(soln[1]), transformed) | ||
end |