feat: added timestep adjustment for FTCS

[skip ci]
This commit is contained in:
nebmit 2023-12-01 11:36:50 +01:00
parent 51705e3eef
commit 6269805eba
No known key found for this signature in database

View File

@ -34,6 +34,36 @@ struct Simulation{T}
end
end
function _adjustTimestep(grid::Grid{T}, approach::APPROACH, timestep::T, iterations::Int)::Tuple{T,Int} where {T}
if approach == FTCS
if getDim(grid) == 1
deltaSquare = getDeltaCol(grid)
maxAlpha = maximum(getAlphaX(grid))
# Courant-Friedrichs-Lewy condition
cfl = deltaSquare / (4 * maxAlpha)
elseif getDim(grid) == 2
deltaColSquare = getDeltaCol(grid) * getDeltaCol(grid)
deltaRowSquare = getDeltaRow(grid) * getDeltaRow(grid)
minDeltaSquare = min(deltaColSquare, deltaRowSquare)
maxAlpha = min(maximum(getAlphaX(grid)), maximum(getAlphaY(grid)))
cfl = minDeltaSquare / (4 * maxAlpha)
end
if timestep > cfl
innerIterations = ceil(Int, timestep / cfl)
iterations = iterations * innerIterations
timestep = timestep / innerIterations
println("Warning: Timestep is too large for FTCS approach. Adjusting timestep to ", timestep, " and iterations to ", iterations, ".")
else
timestep = timestep
end
end
return timestep, iterations
end
function _createCSVfile(simulation::Simulation{T})::IOStream where {T}
approachString = string(simulation.approach)
rows = getRows(simulation.grid)
@ -107,7 +137,8 @@ function run(simulation::Simulation{T}) where {T}
if simulation.approach == BTCS
runBTCS(simulation.grid, simulation.bc, simulation.timestep, simulation.iterations, simulationStepCallback)
elseif simulation.approach == FTCS
runFTCS(simulation.grid, simulation.bc, simulation.timestep, simulation.iterations, simulationStepCallback)
timestep, iterations = _adjustTimestep(simulation.grid, simulation.approach, simulation.timestep, simulation.iterations)
runFTCS(simulation.grid, simulation.bc, timestep, iterations, simulationStepCallback)
else
error("Undefined approach!")
end