|
1 | | -using Base.Threads: @threads, nthreads, threadid |
2 | | - |
3 | | -function get_funs(Ps, vars) |
4 | | - @info "Converting to Num" |
5 | | - nums = Num.(Ps) |
6 | | - numsv = reduce(vcat, nums) |
7 | | - @info "Extracting variables" |
8 | | - found_vars = Symbolics.get_variables.(numsv) |
9 | | - found_vars = unique(reduce(vcat, found_vars)) |
10 | | - si = findfirst(v -> string(v) == "s", found_vars) |
11 | | - si === nothing && error("Didn't find s symbol") |
12 | | - s = found_vars[si] |
13 | | - svars = Set([vars; s]) |
14 | | - all(v ∈ svars for v in found_vars) || error( |
15 | | - "Found some variables that were not listed in vars. Found vars = $found_vars, expected vars: $svars", |
16 | | - ) |
17 | | - # all(any(isequal(v, v2) for v2 in [vars; s]) for v in found_vars) || error("Found some variables that were not listed in vars. Found vars = $found_vars") |
18 | | - @info "Building functions" |
19 | | - funs = [Symbolics.build_function(n, s, vars...; expression = Val(false)) for n in nums] # the first element in the tuple is out-of-place. If n is scalar, only a single function is returned. |
20 | | -end |
21 | | - |
22 | | - |
23 | | -""" |
24 | | - cost, funs = coeff_cost_freq(Pd::Vector{StateSpace}, Ps, freqs, vars, dist = abs2) |
25 | | - cost, funs = coeff_cost_freq(Pd::Vector{FRD}, Ps, vars, dist = abs2) |
26 | | -
|
27 | | -Return a cost function that if optimized fits the symbolic parameters in `Ps` to the numerical models in `Pd`. |
28 | | -
|
29 | | -# Arguments: |
30 | | -- `Pd`: Either a vector of statespace objects with numerical coefficients, or a vector with frequency-domain models `::FRD`. |
31 | | -- `Ps`: A vector of symbolic statespace objects |
32 | | -- `freqs`: the frequency grid to eval the cost on. If `Pd` is a set of `FRD` objects, this argument is omitted. |
33 | | -- `vars`: the symbolic variables to optimize |
34 | | -- `dist`: The distance measure. |
35 | | -""" |
36 | | -function coeff_cost_freq( |
37 | | - Pd::AbstractVector{<:AbstractStateSpace}, |
38 | | - Ps, |
39 | | - freqs, |
40 | | - vars, |
41 | | - dist::F = abs2, |
42 | | -) where {F} |
43 | | - funs = get_funs(Ps, vars) |
44 | | - local cost |
45 | | - logabs(x) = @fastmath log(abs(x)) |
46 | | - let Pd = Pd, funs = funs, freqs = freqs # closure bug trick |
47 | | - function cost(x::AbstractArray{T}, _ = nothing)::T where {T} |
48 | | - any(<=(0), x) && (return T(Inf)) |
49 | | - c = [zero(T) for _ = 1:nthreads()] |
50 | | - for (Pd, symfun) in zip(Pd, funs) |
51 | | - for f in freqs # @threads |
52 | | - freq = complex(0, f) |
53 | | - data_val = evalfr(Pd, freq) |
54 | | - model_val = symfun(freq, x...) |
55 | | - c[threadid()] += sum(dist.(logabs.(data_val) .- logabs.(model_val))) |
56 | | - end |
57 | | - end |
58 | | - sum(c) / (length(freqs) * length(Pd)) |
59 | | - end |
60 | | - end |
61 | | - cost, funs |
62 | | -end |
| 1 | +# using Base.Threads: @threads, nthreads, threadid |
| 2 | + |
| 3 | +# function get_funs(Ps, vars) |
| 4 | +# @info "Converting to Num" |
| 5 | +# nums = Num.(Ps) |
| 6 | +# numsv = reduce(vcat, nums) |
| 7 | +# @info "Extracting variables" |
| 8 | +# found_vars = Symbolics.get_variables.(numsv) |
| 9 | +# found_vars = unique(reduce(vcat, found_vars)) |
| 10 | +# si = findfirst(v -> string(v) == "s", found_vars) |
| 11 | +# si === nothing && error("Didn't find s symbol") |
| 12 | +# s = found_vars[si] |
| 13 | +# svars = Set([vars; s]) |
| 14 | +# all(v ∈ svars for v in found_vars) || error( |
| 15 | +# "Found some variables that were not listed in vars. Found vars = $found_vars, expected vars: $svars", |
| 16 | +# ) |
| 17 | +# # all(any(isequal(v, v2) for v2 in [vars; s]) for v in found_vars) || error("Found some variables that were not listed in vars. Found vars = $found_vars") |
| 18 | +# @info "Building functions" |
| 19 | +# funs = [Symbolics.build_function(n, s, vars...; expression = Val(false)) for n in nums] # the first element in the tuple is out-of-place. If n is scalar, only a single function is returned. |
| 20 | +# end |
| 21 | + |
| 22 | + |
| 23 | +# """ |
| 24 | +# cost, funs = coeff_cost_freq(Pd::Vector{StateSpace}, Ps, freqs, vars, dist = abs2) |
| 25 | +# cost, funs = coeff_cost_freq(Pd::Vector{FRD}, Ps, vars, dist = abs2) |
| 26 | + |
| 27 | +# Return a cost function that if optimized fits the symbolic parameters in `Ps` to the numerical models in `Pd`. |
| 28 | + |
| 29 | +# # Arguments: |
| 30 | +# - `Pd`: Either a vector of statespace objects with numerical coefficients, or a vector with frequency-domain models `::FRD`. |
| 31 | +# - `Ps`: A vector of symbolic statespace objects |
| 32 | +# - `freqs`: the frequency grid to eval the cost on. If `Pd` is a set of `FRD` objects, this argument is omitted. |
| 33 | +# - `vars`: the symbolic variables to optimize |
| 34 | +# - `dist`: The distance measure. |
| 35 | +# """ |
| 36 | +# function coeff_cost_freq( |
| 37 | +# Pd::AbstractVector{<:AbstractStateSpace}, |
| 38 | +# Ps, |
| 39 | +# freqs, |
| 40 | +# vars, |
| 41 | +# dist::F = abs2, |
| 42 | +# ) where {F} |
| 43 | +# funs = get_funs(Ps, vars) |
| 44 | +# local cost |
| 45 | +# logabs(x) = @fastmath log(abs(x)) |
| 46 | +# let Pd = Pd, funs = funs, freqs = freqs # closure bug trick |
| 47 | +# function cost(x::AbstractArray{T}, _ = nothing)::T where {T} |
| 48 | +# any(<=(0), x) && (return T(Inf)) |
| 49 | +# c = [zero(T) for _ = 1:nthreads()] |
| 50 | +# for (Pd, symfun) in zip(Pd, funs) |
| 51 | +# for f in freqs # @threads |
| 52 | +# freq = complex(0, f) |
| 53 | +# data_val = evalfr(Pd, freq) |
| 54 | +# model_val = symfun(freq, x...) |
| 55 | +# c[threadid()] += sum(dist.(logabs.(data_val) .- logabs.(model_val))) |
| 56 | +# end |
| 57 | +# end |
| 58 | +# sum(c) / (length(freqs) * length(Pd)) |
| 59 | +# end |
| 60 | +# end |
| 61 | +# cost, funs |
| 62 | +# end |
| 63 | + |
| 64 | + |
| 65 | + |
63 | 66 |
|
64 | 67 | # function coeff_cost_freq(Pd::AbstractVector{<:FRD}, Ps, vars, dist::F = abs2) where {F} |
65 | 68 | # funs = get_funs(Ps, vars) |
|
0 commit comments