TugJulia/julia/tug/AbstractSimulation.jl
nebmit 7331e45eea
feat: added DynamicSimulation.jl
Added dynamic simulation for use with distributed systems and incremental simulations
Added module based exports
Added test for distributed systems

[skip ci]
2023-12-04 08:23:11 +01:00

68 lines
2.4 KiB
Julia

using Printf
include("Boundary.jl")
include("Grid.jl")
@enum APPROACH BTCS FTCS
@enum CONSOLE_OUTPUT CONSOLE_OUTPUT_OFF CONSOLE_OUTPUT_ON CONSOLE_OUTPUT_VERBOSE
@enum CSV_OUTPUT CSV_OUTPUT_OFF CSV_OUTPUT_ON CSV_OUTPUT_VERBOSE CSV_OUTPUT_XTREME
abstract type AbstractSimulation{T} end
function createCSVfile(simulation::AbstractSimulation{T}, grid::Union{Grid{T},Nothing}=nothing)::IOStream where {T}
grid = grid === nothing ? simulation.grid : grid
approachString = string(simulation.approach)
rows = getRows(grid)
cols = getCols(grid)
numIterations = simulation.iterations
filename = string(approachString, "_", rows, "_", cols, "_", numIterations, ".csv")
appendIdent = 0
while isfile(filename)
appendIdent += 1
filename = string(approachString, "_", rows, "_", cols, "_", numIterations, "-", appendIdent, ".csv")
end
# Write boundary conditions if required
if simulation.csvOutput >= CSV_OUTPUT_XTREME
open(filename, "w") do file
writeBoundarySideValuesToCSV(file, simulation.bc, LEFT)
writeBoundarySideValuesToCSV(file, simulation.bc, RIGHT)
if getDim(grid) == 2
writeBoundarySideValuesToCSV(file, simulation.bc, TOP)
writeBoundarySideValuesToCSV(file, simulation.bc, BOTTOM)
end
write(file, "\n\n")
end
end
file = open(filename, "a")
return file
end
function writeBoundarySideValuesToCSV(file::IOStream, bc::Boundary{T}, side) where {T}
values::Vector{BoundaryElement} = getBoundarySide(bc, side)
formatted_values = join(map(getValue, values), " ")
write(file, formatted_values, "\n")
end
function writeConcentrationsToCLI(simulation::AbstractSimulation{T}, grid::Union{Grid{T},Nothing}=nothing) where {T}
grid = grid === nothing ? simulation.grid : grid
println(getConcentrations(grid))
end
function writeConcentrationsToCSV(file::IOStream, simulation::AbstractSimulation{T}, grid::Union{Grid{T},Nothing}=nothing) where {T}
grid = grid === nothing ? simulation.grid : grid
concentrations = getConcentrations(grid)
for row in eachrow(concentrations)
formatted_row = [Printf.@sprintf("%.6g", x) for x in row] # Format each element like is done in the C++ version using Eigen3
println(file, join(formatted_row, " "))
end
println(file)
println(file)
end