|
161 | 161 | @test SciMLBase.successful_retcode(sccsol)
|
162 | 162 | @test val[] == 1
|
163 | 163 | end
|
| 164 | + |
| 165 | +import ModelingToolkitStandardLibrary.Blocks as B |
| 166 | +import ModelingToolkitStandardLibrary.Mechanical.Translational as T |
| 167 | +import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC |
| 168 | + |
| 169 | +@testset "Caching of subexpressions of different types" begin |
| 170 | + liquid_pressure(rho, rho_0, bulk) = (rho / rho_0 - 1) * bulk |
| 171 | + gas_pressure(rho, rho_0, p_gas, rho_gas) = rho * ((0 - p_gas) / (rho_0 - rho_gas)) |
| 172 | + full_pressure(rho, rho_0, bulk, p_gas, rho_gas) = ifelse( |
| 173 | + rho >= rho_0, liquid_pressure(rho, rho_0, bulk), |
| 174 | + gas_pressure(rho, rho_0, p_gas, rho_gas)) |
| 175 | + |
| 176 | + @component function Volume(; |
| 177 | + #parameters |
| 178 | + area, |
| 179 | + direction = +1, |
| 180 | + x_int, |
| 181 | + name) |
| 182 | + pars = @parameters begin |
| 183 | + area = area |
| 184 | + x_int = x_int |
| 185 | + rho_0 = 1000 |
| 186 | + bulk = 1e9 |
| 187 | + p_gas = -1000 |
| 188 | + rho_gas = 1 |
| 189 | + end |
| 190 | + |
| 191 | + vars = @variables begin |
| 192 | + x(t) = x_int |
| 193 | + dx(t), [guess = 0] |
| 194 | + p(t), [guess = 0] |
| 195 | + f(t), [guess = 0] |
| 196 | + rho(t), [guess = 0] |
| 197 | + m(t), [guess = 0] |
| 198 | + dm(t), [guess = 0] |
| 199 | + end |
| 200 | + |
| 201 | + systems = @named begin |
| 202 | + port = IC.HydraulicPort() |
| 203 | + flange = T.MechanicalPort() |
| 204 | + end |
| 205 | + |
| 206 | + eqs = [ |
| 207 | + # connectors |
| 208 | + port.p ~ p |
| 209 | + port.dm ~ dm |
| 210 | + flange.v * direction ~ dx |
| 211 | + flange.f * direction ~ -f |
| 212 | + |
| 213 | + # differentials |
| 214 | + D(x) ~ dx |
| 215 | + D(m) ~ dm |
| 216 | + |
| 217 | + # physics |
| 218 | + p ~ full_pressure(rho, rho_0, bulk, p_gas, rho_gas) |
| 219 | + f ~ p * area |
| 220 | + m ~ rho * x * area] |
| 221 | + |
| 222 | + return ODESystem(eqs, t, vars, pars; name, systems) |
| 223 | + end |
| 224 | + |
| 225 | + systems = @named begin |
| 226 | + fluid = IC.HydraulicFluid(; bulk_modulus = 1e9) |
| 227 | + |
| 228 | + src1 = IC.Pressure(;) |
| 229 | + src2 = IC.Pressure(;) |
| 230 | + |
| 231 | + vol1 = Volume(; area = 0.01, direction = +1, x_int = 0.1) |
| 232 | + vol2 = Volume(; area = 0.01, direction = +1, x_int = 0.1) |
| 233 | + |
| 234 | + mass = T.Mass(; m = 10) |
| 235 | + |
| 236 | + sin1 = B.Sine(; frequency = 0.5, amplitude = +0.5e5, offset = 10e5) |
| 237 | + sin2 = B.Sine(; frequency = 0.5, amplitude = -0.5e5, offset = 10e5) |
| 238 | + end |
| 239 | + |
| 240 | + eqs = [connect(fluid, src1.port) |
| 241 | + connect(fluid, src2.port) |
| 242 | + connect(src1.port, vol1.port) |
| 243 | + connect(src2.port, vol2.port) |
| 244 | + connect(vol1.flange, mass.flange, vol2.flange) |
| 245 | + connect(src1.p, sin1.output) |
| 246 | + connect(src2.p, sin2.output)] |
| 247 | + |
| 248 | + initialization_eqs = [mass.s ~ 0.0 |
| 249 | + mass.v ~ 0.0] |
| 250 | + |
| 251 | + @mtkbuild sys = ODESystem(eqs, t, [], []; systems, initialization_eqs) |
| 252 | + prob = ODEProblem(sys, [], (0, 5)) |
| 253 | + sol = solve(prob) |
| 254 | + @test SciMLBase.successful_retcode(sol) |
| 255 | +end |
0 commit comments