From c55fe09951f368ad517806b567c4849995089861 Mon Sep 17 00:00:00 2001 From: marcbasquensmunoz <150438807+marcbasquensmunoz@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:03:21 +0200 Subject: [PATCH] Provide the whole history in operator (#20) * Provide the whole history in operator * Fix example --- docs/src/nonhistory.jl | 2 +- examples/Braedstrup/main.jl | 2 +- examples/tekniska/prop_m.jl | 2 +- examples/tekniska/toggle.jl | 2 +- src/BoreholeNetworksSimulator.jl | 1 + src/modular/interfaces/Operator.jl | 12 +++++++++--- src/modular/operators/SimpleOperator.jl | 2 +- src/modular/simulate.jl | 2 +- src/modular/utils/solution_extract.jl | 6 ++++++ 9 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 src/modular/utils/solution_extract.jl diff --git a/docs/src/nonhistory.jl b/docs/src/nonhistory.jl index 0ccba1f..3023614 100644 --- a/docs/src/nonhistory.jl +++ b/docs/src/nonhistory.jl @@ -15,7 +15,7 @@ using BoreholeNetworksSimulator # Δt = 3600. -Nt = 8760 +Nt = 2#8760 medium = GroundMedium(α=1e-6, λ=3., T0=10.) borehole = SingleUPipeBorehole(H=100., D=10.) diff --git a/examples/Braedstrup/main.jl b/examples/Braedstrup/main.jl index c12f047..c796cf6 100644 --- a/examples/Braedstrup/main.jl +++ b/examples/Braedstrup/main.jl @@ -58,7 +58,7 @@ options = SimulationOptions( seasonal_configuration end -function BoreholeNetworksSimulator.operate(operator::SeasonalOperator, i, options, Tfin, Tfout, Tb, q) +function BoreholeNetworksSimulator.operate(operator::SeasonalOperator, i, options, X) active_network = options.configurations[operator.seasonal_configuration[i]] BoreholeOperation(active_network, operator.mass_flows) end diff --git a/examples/tekniska/prop_m.jl b/examples/tekniska/prop_m.jl index 96e6da2..5ca9bc8 100644 --- a/examples/tekniska/prop_m.jl +++ b/examples/tekniska/prop_m.jl @@ -8,7 +8,7 @@ struct VariableMFOperator{T <: Number} <: Operator mass_flows::Vector{T} end -function BoreholeNetworksSimulator.operate(operator::VariableMFOperator, step, options, Tin, Tout, Tb, q) +function BoreholeNetworksSimulator.operate(operator::VariableMFOperator, step, options, X) operator.mass_flows .= operator.mass_flow_series[step] BoreholeOperation(options.configurations[1], operator.mass_flows) end diff --git a/examples/tekniska/toggle.jl b/examples/tekniska/toggle.jl index c4b9095..0623b55 100644 --- a/examples/tekniska/toggle.jl +++ b/examples/tekniska/toggle.jl @@ -13,7 +13,7 @@ include("defs.jl") mass_flow_containers::Vector{T} end -function BoreholeNetworksSimulator.operate(operator::ToggleOperator, step, options, Tin, Tout, Tb, q) +function BoreholeNetworksSimulator.operate(operator::ToggleOperator, step, options, X) @unpack mass_flow, Q_threshold, single_branch, hours_used, toggle, mass_flow_containers = operator current_load = options.constraint.Q_tot[step] diff --git a/src/BoreholeNetworksSimulator.jl b/src/BoreholeNetworksSimulator.jl index 0c6b182..fe64023 100644 --- a/src/BoreholeNetworksSimulator.jl +++ b/src/BoreholeNetworksSimulator.jl @@ -41,5 +41,6 @@ export BoreholeOperation, Operator, SimpleOperator, operate export simulate!, compute_parameters, load_cache!, save_cache, initialize export n_branches export ThermophysicalProperties +export extract_Tfin, extract_Tfout, extract_Tb, extract_q end # module \ No newline at end of file diff --git a/src/modular/interfaces/Operator.jl b/src/modular/interfaces/Operator.jl index a6c697c..ed22827 100644 --- a/src/modular/interfaces/Operator.jl +++ b/src/modular/interfaces/Operator.jl @@ -5,10 +5,16 @@ Interface for operation strategies. Required functions: -- `operate(::Operator, i, options, Tfin, Tfout, Tb, q)`: Return a `BoreholeOperation`. +- `operate(::Operator, step, options, X)` + +The implementation of `operate` must return an instance of `BoreholeOperation`, specifying the network topology and the mass flows per branch for the current time step. +`step` is the current time step, `options` contains the provided simulation options and `X` contains the time series up to the time step `step-1` of +the inlet fluid temperature, the outlet fluid temperature, the wall temperature and the heat extraction rate, for each borehole in the borefield. +To get `Tfin`, `Tfout`, `Tb`, and `q` from `X`, use the functions `extract_Tfin`, `extract_Tfout`, `extract_Tb`, `extract_q`, or get them manually at the +indices [1:2:2Nb, :], [2:2:2Nb, :], [2Nb+1:3Nb, :], [3Nb+1:end, :], respectively. """ abstract type Operator end -@required Operator begin - operate(::Operator, i, options, Tfin, Tfout, Tb, q) +@required Operator begin + operate(::Operator, step, options, X) end diff --git a/src/modular/operators/SimpleOperator.jl b/src/modular/operators/SimpleOperator.jl index bd9ad55..6d91114 100644 --- a/src/modular/operators/SimpleOperator.jl +++ b/src/modular/operators/SimpleOperator.jl @@ -11,7 +11,7 @@ struct SimpleOperator{T <: Number} <: Operator end SimpleOperator(;mass_flow, branches) = SimpleOperator(mass_flow .* ones(eltype(mass_flow), branches)) -function operate(operator::SimpleOperator, i, options, Tin, Tout, Tb, q) +function operate(operator::SimpleOperator, i, options, X) network = options.configurations[1] BoreholeOperation(network, operator.mass_flows) end diff --git a/src/modular/simulate.jl b/src/modular/simulate.jl index 219b18f..e500c61 100644 --- a/src/modular/simulate.jl +++ b/src/modular/simulate.jl @@ -34,7 +34,7 @@ function simulate!(;operator, options::SimulationOptions, containers::Simulation # Simulation loop for i = Ts:Nt - operation = @views operate(operator, i, options, X[1:2:2Nb, 1:i], X[2:2:2Nb, 1:i], X[2Nb+1:3Nb, 1:i], X[3Nb+1:end, 1:i]) + operation = @views operate(operator, i, options, X[:, 1:i-1]) operation = unwrap(operation) for (j, branch) in enumerate(operation.network.branches) diff --git a/src/modular/utils/solution_extract.jl b/src/modular/utils/solution_extract.jl new file mode 100644 index 0000000..b05c01b --- /dev/null +++ b/src/modular/utils/solution_extract.jl @@ -0,0 +1,6 @@ + +get_Nb(X) = Int(size(X)[1] / 4) +extract_Tfin(X) = @view X[1:2:2*get_Nb(X), :] +extract_Tfout(X) = @view X[2:2:2*get_Nb(X), :] +extract_Tb(X) = @view X[2*get_Nb(X)+1:3*get_Nb(X), :] +extract_q(X) = @view X[3*get_Nb(X)+1:end, :] \ No newline at end of file