Skip to content

Commit

Permalink
Merge pull request #165 from ModiaSim/release0.10.0
Browse files Browse the repository at this point in the history
Release0.10.0
  • Loading branch information
MartinOtter authored May 29, 2023
2 parents ab99c83 + 863b13d commit ff2a4e2
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.8.1'
- '1.8.5'
os:
- ubuntu-latest
- windows-latest
Expand Down
68 changes: 34 additions & 34 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ version = "0.1.29"

[[deps.ArrayLayouts]]
deps = ["FillArrays", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "4aff5fa660eb95c2e0deb6bcdabe4d9a96bc4667"
git-tree-sha1 = "4efc22e4c299e49995a38d503d9dbb0544a37838"
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
version = "0.8.18"
version = "1.0.4"

[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[deps.BandedMatrices]]
deps = ["ArrayLayouts", "FillArrays", "LinearAlgebra", "SnoopPrecompile", "SparseArrays"]
git-tree-sha1 = "6ef8fc1d77b60f41041d59ce61ef9eb41ed97a83"
deps = ["ArrayLayouts", "FillArrays", "LinearAlgebra", "PrecompileTools", "SparseArrays"]
git-tree-sha1 = "b18febf0a34ba9192fdcd4fd2c57b535a48d3dd4"
uuid = "aae01518-5342-5314-be14-df237901396f"
version = "0.17.18"
version = "0.17.24"

[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand All @@ -73,10 +73,10 @@ uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82"
version = "0.4.2"

[[deps.CPUSummary]]
deps = ["CpuId", "IfElse", "Static"]
git-tree-sha1 = "2c144ddb46b552f72d7eafe7cc2f50746e41ea21"
deps = ["CpuId", "IfElse", "PrecompileTools", "Static"]
git-tree-sha1 = "89e0654ed8c7aebad6d5ad235d6242c2d737a928"
uuid = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"
version = "0.2.2"
version = "0.2.3"

[[deps.Calculus]]
deps = ["LinearAlgebra"]
Expand Down Expand Up @@ -227,9 +227,9 @@ uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[deps.Distributions]]
deps = ["ChainRulesCore", "DensityInterface", "FillArrays", "LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SparseArrays", "SpecialFunctions", "Statistics", "StatsAPI", "StatsBase", "StatsFuns", "Test"]
git-tree-sha1 = "5eeb2bd01e5065090ad591a205d8cad432ae6cb6"
git-tree-sha1 = "c72970914c8a21b36bbc244e9df0ed1834a0360b"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
version = "0.25.93"
version = "0.25.95"

[[deps.DocStringExtensions]]
deps = ["LibGit2"]
Expand Down Expand Up @@ -286,15 +286,15 @@ uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"

[[deps.FillArrays]]
deps = ["LinearAlgebra", "Random", "SparseArrays", "Statistics"]
git-tree-sha1 = "7072f1e3e5a8be51d525d64f63d3ec1287ff2790"
git-tree-sha1 = "ed569cb9e7e3590d5ba884da7edc50216aac5811"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.13.11"
version = "1.1.0"

[[deps.FiniteDiff]]
deps = ["ArrayInterface", "LinearAlgebra", "Requires", "Setfield", "SparseArrays", "StaticArrays"]
git-tree-sha1 = "6604e18a0220650dbbea7854938768f15955dd8e"
git-tree-sha1 = "abfd952bdf92f6d7195c45dc46d50043bd0d7dbe"
uuid = "6a86dc24-6348-571c-b903-95158fe2bd41"
version = "2.20.0"
version = "2.21.0"

[[deps.Formatting]]
deps = ["Printf"]
Expand Down Expand Up @@ -325,9 +325,9 @@ uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820"

[[deps.GPUArraysCore]]
deps = ["Adapt"]
git-tree-sha1 = "1cd7f0af1aa58abc02ea1d872953a97359cb87fa"
git-tree-sha1 = "2d6ca471a6c7b536127afccfa7564b5b39227fe0"
uuid = "46192b85-c4d5-4398-a991-12ede77f4527"
version = "0.1.4"
version = "0.1.5"

[[deps.GenericSchur]]
deps = ["LinearAlgebra", "Printf"]
Expand Down Expand Up @@ -630,10 +630,10 @@ uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.6.0"

[[deps.OrdinaryDiffEq]]
deps = ["Adapt", "ArrayInterface", "DataStructures", "DiffEqBase", "DocStringExtensions", "ExponentialUtilities", "FastBroadcast", "FastClosures", "FiniteDiff", "ForwardDiff", "FunctionWrappersWrappers", "IfElse", "LineSearches", "LinearAlgebra", "LinearSolve", "Logging", "LoopVectorization", "MacroTools", "MuladdMacro", "NLsolve", "NonlinearSolve", "Polyester", "PreallocationTools", "PrecompileTools", "Preferences", "RecursiveArrayTools", "Reexport", "SciMLBase", "SciMLNLSolve", "SimpleNonlinearSolve", "SimpleUnPack", "SparseArrays", "SparseDiffTools", "StaticArrayInterface", "StaticArrays", "TruncatedStacktraces"]
git-tree-sha1 = "6ffebfa8971546bace3fc312f9a703795f79f5b9"
deps = ["ADTypes", "Adapt", "ArrayInterface", "DataStructures", "DiffEqBase", "DocStringExtensions", "ExponentialUtilities", "FastBroadcast", "FastClosures", "FiniteDiff", "ForwardDiff", "FunctionWrappersWrappers", "IfElse", "LineSearches", "LinearAlgebra", "LinearSolve", "Logging", "LoopVectorization", "MacroTools", "MuladdMacro", "NLsolve", "NonlinearSolve", "Polyester", "PreallocationTools", "PrecompileTools", "Preferences", "RecursiveArrayTools", "Reexport", "SciMLBase", "SciMLNLSolve", "SciMLOperators", "SimpleNonlinearSolve", "SimpleUnPack", "SparseArrays", "SparseDiffTools", "StaticArrayInterface", "StaticArrays", "TruncatedStacktraces"]
git-tree-sha1 = "7f758238ce4202ced5e08aa2903d19d3a4f0dd7c"
uuid = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
version = "6.51.2"
version = "6.52.0"

[[deps.PDMats]]
deps = ["LinearAlgebra", "SparseArrays", "SuiteSparse"]
Expand Down Expand Up @@ -791,9 +791,9 @@ version = "0.4.0+0"

[[deps.RuntimeGeneratedFunctions]]
deps = ["ExprTools", "SHA", "Serialization"]
git-tree-sha1 = "d7d9ebe28062161c1e314ed643097b0c6fe657d9"
git-tree-sha1 = "92fa1dd437690a5e2ac39c81b6353ef106affc0e"
uuid = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
version = "0.5.7"
version = "0.5.8"

[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Expand Down Expand Up @@ -824,9 +824,9 @@ version = "0.1.6"

[[deps.SciMLOperators]]
deps = ["ArrayInterface", "DocStringExtensions", "Lazy", "LinearAlgebra", "Setfield", "SparseArrays", "StaticArraysCore", "Tricks"]
git-tree-sha1 = "90163ebc767cba9f126ea00aeef1d75ed74fe7b0"
git-tree-sha1 = "d9f0f6ce9bb899a657c4d218a846533910e9dea9"
uuid = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
version = "0.2.8"
version = "0.2.9"

[[deps.SentinelArrays]]
deps = ["Dates", "Random"]
Expand Down Expand Up @@ -854,10 +854,10 @@ uuid = "3201582d-3078-4276-ba5d-0a1254d79d7c"
version = "0.4.2"

[[deps.SimpleNonlinearSolve]]
deps = ["ArrayInterface", "DiffEqBase", "FiniteDiff", "ForwardDiff", "LinearAlgebra", "Reexport", "Requires", "SciMLBase", "SnoopPrecompile", "StaticArraysCore"]
git-tree-sha1 = "54c78ac3cc0343a16785adabe5bbf4063c737967"
deps = ["ArrayInterface", "DiffEqBase", "FiniteDiff", "ForwardDiff", "LinearAlgebra", "PrecompileTools", "Reexport", "Requires", "SciMLBase", "StaticArraysCore"]
git-tree-sha1 = "7c55a3e65aad4ce6e610409cdd564b8d590b9726"
uuid = "727e6d20-b764-4bd8-a329-72de5adea6c7"
version = "0.1.14"
version = "0.1.15"

[[deps.SimpleTraits]]
deps = ["InteractiveUtils", "MacroTools"]
Expand Down Expand Up @@ -890,10 +890,10 @@ deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[deps.SparseDiffTools]]
deps = ["Adapt", "ArrayInterface", "Compat", "DataStructures", "FiniteDiff", "ForwardDiff", "Graphs", "LinearAlgebra", "Requires", "SparseArrays", "StaticArrays", "VertexSafeGraphs"]
git-tree-sha1 = "e19ac47477c9a8fcca06dab5e5471417d5d9d723"
deps = ["ADTypes", "Adapt", "ArrayInterface", "Compat", "DataStructures", "FiniteDiff", "ForwardDiff", "Graphs", "LinearAlgebra", "Reexport", "Requires", "SciMLOperators", "SparseArrays", "StaticArrayInterface", "StaticArrays", "Tricks", "VertexSafeGraphs"]
git-tree-sha1 = "04f060e66a61a909ed59efd79f64943688d7568d"
uuid = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
version = "1.31.0"
version = "2.3.0"

[[deps.Sparspak]]
deps = ["Libdl", "LinearAlgebra", "Logging", "OffsetArrays", "Printf", "SparseArrays", "Test"]
Expand Down Expand Up @@ -959,10 +959,10 @@ uuid = "9672c7b4-1e72-59bd-8a11-6ac3964bc41f"
version = "1.15.0"

[[deps.StochasticDiffEq]]
deps = ["Adapt", "ArrayInterface", "DataStructures", "DiffEqBase", "DiffEqNoiseProcess", "DocStringExtensions", "FillArrays", "FiniteDiff", "ForwardDiff", "JumpProcesses", "LevyArea", "LinearAlgebra", "Logging", "MuladdMacro", "NLsolve", "OrdinaryDiffEq", "Random", "RandomNumbers", "RecursiveArrayTools", "Reexport", "SciMLBase", "SparseArrays", "SparseDiffTools", "StaticArrays", "UnPack"]
git-tree-sha1 = "073da86200349ddf4ef8bc3e3f3acd62e1d554f7"
deps = ["Adapt", "ArrayInterface", "DataStructures", "DiffEqBase", "DiffEqNoiseProcess", "DocStringExtensions", "FillArrays", "FiniteDiff", "ForwardDiff", "JumpProcesses", "LevyArea", "LinearAlgebra", "Logging", "MuladdMacro", "NLsolve", "OrdinaryDiffEq", "Random", "RandomNumbers", "RecursiveArrayTools", "Reexport", "SciMLBase", "SciMLOperators", "SparseArrays", "SparseDiffTools", "StaticArrays", "UnPack"]
git-tree-sha1 = "619c846726548b7b2e8728b72f288acf10390121"
uuid = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
version = "6.60.0"
version = "6.61.0"

[[deps.StrideArraysCore]]
deps = ["ArrayInterface", "CloseOpenIntervals", "IfElse", "LayoutPointers", "ManualMemory", "SIMDTypes", "Static", "StaticArrayInterface", "ThreadingUtilities"]
Expand All @@ -986,9 +986,9 @@ version = "5.10.1+0"

[[deps.Sundials]]
deps = ["CEnum", "DataStructures", "DiffEqBase", "Libdl", "LinearAlgebra", "Logging", "PrecompileTools", "Reexport", "SciMLBase", "SparseArrays", "Sundials_jll"]
git-tree-sha1 = "4289695031d663252f38fd114d3e0020d1461cee"
git-tree-sha1 = "a982ee85e1908d39f58d7fff670e60f991ca2ddb"
uuid = "c3572dad-4567-51f8-b174-8c6c989267f4"
version = "4.18.0"
version = "4.19.0"

[[deps.Sundials_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "OpenBLAS_jll", "Pkg", "SuiteSparse_jll"]
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
authors = ["Hilding Elmqvist <[email protected]>", "Martin Otter <[email protected]>"]
name = "Modia"
uuid = "cb905087-75eb-5f27-8515-1ce0ec8e839e"
version = "0.9.4"
version = "0.10.0"

[deps]
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
Expand Down
17 changes: 17 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ functionalities of these packages.

## Release Notes

### Version 0.10.0

- Initial support of segmented simulations where the number of states can change during simulation.
For examples, see `Modia/test/TestHeatTransfer2.jl` and models in directory `Modia3D/test/Segmented`
(of release 0.12.0 and later). The tutorial will be updated for this feature in an upcoming version.


**Non-backwards** compatible changes

These changes should usually not influence user models.

- `_buildFunction = <functionName>` changed to `_buildFunction = Par(functionName = <functionName>)` and
changed argument list of `<functionName>`.
- `_instantiateFunction = Par(..)` changed to `_initSegmentFunction = Par(functionName = <functionName>)`
and changed argument list of `<functionName>`.


### Version 0.9.4

- Precompile statements included (compilation of Modia package takes more time, but startup of Modia model simulations is faster).
Expand Down
36 changes: 36 additions & 0 deletions models/HeatTransfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,40 @@ InsulatedRod = Model(
]
)


include("HeatTransfer/InsulatedRod2.jl")

"""
insulatedRod = InsulatedRod2(; L, A, rho=7500.0u"kg/m^3", lambda=74.0u"W/(m*K)", c=450.0u"J/(kg*K), T0=293.15u"K", nT=1")
Generate a Model(..) instance of an insulated rod with length `L` and cross sectional area `A` that
models 1D heat transfer from port_a to port_b. The rod is discretized with `nT` internal temperature nodes that are initialized with `T0`.
This is an acausal built-in component where the code size does not depend on `nT` and
`nT` can still be changed after model code is generated and compiled.
For more details see Appendix B1 of [DOI: 10.3390/electronics12030500](https://doi.org/10.3390/electronics12030500).
# Optional Arguments:
- `rho`: density of rod material
- `lambda`: thermal conductivity of rod material
- `c`: specific heat capacity of rod material
- `T0`: initial temperature of internal temperature nodes
- `nT`: number of temperature nodes (`nT > 0`).
# Internal variables that can be inquired/plotted:
- `T`: Vector of temperatures at the internal temperature nodes
- `der(T)': Vector of derivatives of `T`.
"""
InsulatedRod2 = Model(;
_buildFunction = Par(functionName = :(build_InsulatedRod2!)), # Called once in @instantiateModel(..) before getDerivatives!(..) is generated
_initSegmentFunction = Par(functionName = :(initSegment_InsulatedRod2!)), # Called once before initialization of a new simulation segment
L = 1.0u"m", # Length of rod
A = 0.0004u"m^2", # Rod area
rho = 7500.0u"kg/m^3", # Density of rod material
lambda = 74.0u"W/(m*K)", # Thermal conductivity of rod material
c = 450.0u"J/(kg*K)", # Specific heat capacity of rod material
T0 = 293.15u"K", # Initial temperature of internal temperature nodes
nT = 1, # Number of temperature nodes (nT > 0).
port_a = HeatPort, # heat port on left side
port_b = HeatPort # heat port on right side
)
131 changes: 131 additions & 0 deletions models/HeatTransfer/InsulatedRod2.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# License for this file: MIT (expat)
# Copyright 2023, DLR Institute of System Dynamics and Control
#
# This file is included in Modia/models/HeatTranser.jl


# Structure holding the internal memory of the insulated rod
mutable struct InsulatedRodStruct{FloatType}
# Parameters
Ge::FloatType # = lambda*A/dx
Ge2::FloatType # = 2*Ge
k::FloatType # = Ge / (c*rho*A*dx)
T_init::Vector{FloatType} # initial states

# Internal states and state deriatives
T::Vector{FloatType} # states
der_T::Vector{FloatType} # state derivatives

# Start index of state and state derivative vectors
T_startIndex::Int

InsulatedRodStruct{FloatType}() where {FloatType} = new()
end


# Called from @instantiateModel(..) before getDerivatives!(..) is generated
function build_InsulatedRod2!(model::AbstractDict, FloatType, TimeType, unitless::Bool, ID, pathAST)
model = model | Model(
insRod = Var(hideResult=true), # InsulatedRodStruct instance (an equation to return this variable is generated by _buildFunction)
success = Var(hideResult=true), # Dummy return argument
equations = :[
# copy states into insRod.T and return insRod
insRod = openInsulatedRod!(instantiatedModel, $ID)

# equations at the boundaries
port_a.Q_flow = getGe2(insRod, Val($unitless))*(port_a.T - getT1( insRod, Val($unitless)))
port_b.Q_flow = getGe2(insRod, Val($unitless))*(port_b.T - getTend(insRod, Val($unitless)))

# compute insRod.der_T and copy it into state derivatives
success = computeInsulatedRodDerivatives!(instantiatedModel, insRod, port_a.T, port_b.T) # instantiatedModel is provided in the generated code
])

return (model, InsulatedRodStruct{FloatType}())
end


# Called once before initialization of first simulation segment
function initFirstSegment_InsulatedRod2!(obj::InsulatedRodStruct{FloatType}; L, A, rho, lambda, c, T0, nT)::Nothing where {FloatType}
# Convert to SI units, strip units and check that values are positives
#=
@show L
@Modia.strippedPositive!(path, L)
@Modia.strippedPositive!(path, A)
@Modia.strippedPositive!(path, rho)
@Modia.strippedPositive!(path, lambda)
@Modia.strippedPositive!(path, c)
@Modia.strippedPositive!(path, T0)
@Modia.strippedPositive!(path, nT)
@show L
@show A
@show rho
@show lambda
=#
L = stripUnit(L)
A = stripUnit(A)
rho = stripUnit(rho)
lambda = stripUnit(lambda)
c = stripUnit(c)
T0 = stripUnit(T0)

# Compute derived parameters
dx = L/nT
obj.Ge = lambda*A/dx
obj.Ge2 = 2*obj.Ge
obj.k = obj.Ge / ( c*rho*A*dx )
obj.T_init = fill(T0, nT)

# Allocate and initialize internal states
obj.T = copy(obj.T_init)
obj.der_T = zeros(nT)
return nothing
end


# Called once before initialization of a new simulation segment
function initSegment_InsulatedRod2!(instantiatedModel::SimulationModel{FloatType,TimeType}, path::String, ID,
parameters::AbstractDict; log=false)::Nothing where {FloatType,TimeType}
obj::InsulatedRodStruct{FloatType} = Modia.get_instantiatedSubmodel(instantiatedModel, ID)

if Modia.isFirstInitialOfAllSegments(instantiatedModel)
initFirstSegment_InsulatedRod2!(obj; parameters...)
end

# Define a vector of new state and state derivative variables
obj.T_startIndex = Modia.new_x_segmented_variable!(instantiatedModel, path*".T", path*".der(T)", obj.T, "K")
return nothing
end


# Open an initialized InsulatedRod2 model and return a reference to it
function openInsulatedRod!(instantiatedModel::SimulationModel{FloatType,TimeType}, ID)::InsulatedRodStruct{FloatType} where {FloatType,TimeType}
obj::InsulatedRodStruct{FloatType} = Modia.get_instantiatedSubmodel(instantiatedModel, ID)
Modia.get_Vector_x_segmented_value!(instantiatedModel, obj.T_startIndex, obj.T)
return obj
end


# Functions to inquire values from InsulatedRodStruct
getT1( insRod::InsulatedRodStruct, Unitless::Val{true}) = insRod.T[1]
getT1( insRod::InsulatedRodStruct, Unitless::Val{false}) = insRod.T[1]*u"K"

getTend(insRod::InsulatedRodStruct, Unitless::Val{true}) = insRod.T[end]
getTend(insRod::InsulatedRodStruct, Unitless::Val{false}) = insRod.T[end]*u"K"

getGe2( insRod::InsulatedRodStruct, Unitless::Val{true}) = insRod.Ge2
getGe2( insRod::InsulatedRodStruct, Unitless::Val{false}) = insRod.Ge2*u"W/K"

T_grad1(T,Ta,i) = if i == 1 ; (Ta - T[1])*2 else T[i-1] - T[i] end
T_grad2(T,Tb,i) = if i == length(T); (T[i] - Tb)*2 else T[i] - T[i+1] end

function computeInsulatedRodDerivatives!(instantiatedModel, obj::InsulatedRodStruct{FloatType}, Ta, Tb)::Bool where {FloatType}
Ta::FloatType = stripUnit(Ta)
Tb::FloatType = stripUnit(Tb)
T = obj.T
k = obj.k
for i in 1:length(T)
obj.der_T[i] = k*(T_grad1(T,Ta,i) - T_grad2(T,Tb,i))
end
Modia.add_der_x_segmented_value!(instantiatedModel, obj.T_startIndex, obj.der_T)
return true
end
Loading

0 comments on commit ff2a4e2

Please sign in to comment.