Skip to content

Commit dad8c94

Browse files
skip ci
1 parent dcef526 commit dad8c94

5 files changed

Lines changed: 244 additions & 96 deletions

File tree

examples/tutorial3.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ addto_linecablesystem!(
3636
display(cable_system)
3737

3838
# Define a LineParametersProblem with the cable system and earth model
39+
f = 50.0
3940
problem = LineParametersProblem(
4041
cable_system,
4142
temperature=20.0, # Operating temperature
4243
earth_props=earth_params,
43-
frequencies=[50.0], # Frequency for the analysis
44+
frequencies=[f], # Frequency for the analysis
4445
)
4546

4647
# Create a FEMFormulation with custom mesh definitions
@@ -69,11 +70,15 @@ opts = FEMOptions(
6970
preview_geo=false, # Preview geometry
7071
preview_mesh=false, # Preview the mesh
7172
base_path=joinpath(@__DIR__, "fem_output"),
72-
verbosity=0, # Verbose output
73+
verbosity=3, # Verbose output
7374
getdp_executable=joinpath("/home/amartins/Applications/onelab-Linux64", "getdp"), # Path to GetDP executable
7475
)
7576

7677
# Run the FEM model
77-
@time workspace = compute!(problem, formulation, opts)
78+
@time workspace, line_params = compute!(problem, formulation, opts)
7879

79-
println("FEM model run completed.")
80+
println("R = $(round(real(line_params.Z[1,1,1])*1000, sigdigits=4)) Ω/km")
81+
println("L = $(round(imag(line_params.Z[1,1,1])/(2π*f)*1e6, sigdigits=4)) mH/km")
82+
println("C = $(round(imag(line_params.Y[1,1,1])/(2π*f)*1e9, sigdigits=4)) nF/km")
83+
84+
println("FEM model run completed.")

src/FEMTools.jl

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ using Gmsh
3333
using Printf
3434
using Dates
3535
using Measurements
36+
using LinearAlgebra
3637
using Colors
3738
using Logging
3839
using Logging: AbstractLogger, LogLevel, Info, global_logger
@@ -174,11 +175,8 @@ end
174175
mutable struct FEMElectrodynamics <: AbstractAdmittanceFormulation
175176
problem::GetDP.Problem
176177
resolution_name::String
177-
# sources::Int64 # New field for linecablesystem
178-
function FEMElectrodynamics()
179-
# electro = new(problem, "Electrodynamics", num_sources)
180-
# create_electrodynamic_problem(electro.problem, num_sources, electro.resolution_name)
181178

179+
function FEMElectrodynamics()
182180
return new(GetDP.Problem(), "Electrodynamics")
183181
end
184182
end
@@ -332,20 +330,22 @@ This contains the execution-related parameters.
332330
$(TYPEDFIELDS)
333331
"""
334332
struct FEMOptions <: FormulationOptions
335-
"Flag to force remeshing \\[dimensionless\\]."
333+
"Flag to force remeshing."
336334
force_remesh::Bool
337-
"Flag to run the solver \\[dimensionless\\]."
335+
"Flag to run the solver."
338336
run_solver::Bool
339-
"Flag to overwrite existing results \\[dimensionless\\]."
337+
"Flag to overwrite existing results."
340338
overwrite_results::Bool
339+
"Flag to remove or keep all files for each frequency run."
340+
cleanup_files::Bool
341341

342-
"Flag to run postprocessing \\[dimensionless\\]."
342+
"Flag to run postprocessing."
343343
run_postprocessing::Bool
344-
"Flag to preview the mesh \\[dimensionless\\]."
344+
"Flag to preview the mesh."
345345
preview_mesh::Bool
346-
"Flag to preview the geometry \\[dimensionless\\]."
346+
"Flag to preview the geometry."
347347
preview_geo::Bool
348-
"Flag to plot results \\[dimensionless\\]."
348+
"Flag to plot results."
349349
plot_results::Bool
350350

351351
"Base path for output files."
@@ -354,7 +354,7 @@ struct FEMOptions <: FormulationOptions
354354
gmsh_executable::String
355355
"Path to GetDP executable."
356356
getdp_executable::String
357-
"Verbosity level \\[dimensionless\\]."
357+
"Verbosity level."
358358
verbosity::Int
359359
"Path to the log file."
360360
logfile::Union{String,Nothing}
@@ -400,6 +400,7 @@ struct FEMOptions <: FormulationOptions
400400
force_remesh::Bool=false,
401401
run_solver::Bool=true,
402402
overwrite_results::Bool=false,
403+
cleanup_files::Bool=false,
403404
run_postprocessing::Bool=true,
404405
preview_mesh::Bool=false,
405406
preview_geo::Bool=false,
@@ -430,7 +431,7 @@ struct FEMOptions <: FormulationOptions
430431
setup_fem_logging(verbosity, logfile)
431432

432433
return new(
433-
force_remesh, run_solver, overwrite_results,
434+
force_remesh, run_solver, overwrite_results, cleanup_files,
434435
run_postprocessing, preview_mesh, preview_geo, plot_results,
435436
base_path, gmsh_executable, getdp_executable, verbosity, logfile
436437
)
@@ -510,28 +511,13 @@ mutable struct FEMWorkspace
510511
)
511512

512513
# Set up paths
513-
workspace.paths = setup_paths(problem.system, opts)
514+
workspace.paths = setup_paths(problem.system, formulation, opts)
514515
cleanup_files(workspace.paths, opts)
515516

516517
return workspace
517518
end
518519
end
519520

520-
function set_problem!(problem::GetDP.Problem, workspace::FEMWorkspace, ::Val{:baseparams})::Vector{<:AbstractFEMFormulation}
521-
darwin_prob = deepcopy(problem)
522-
electr_prob = deepcopy(problem)
523-
524-
# Darwin
525-
darwin_obj = FEMDarwin(darwin_prob)
526-
527-
# Electrodynamics
528-
num_sources = length(workspace.problem_def.system.cables)
529-
electro_obj = FEMElectrodynamics(electr_prob, num_sources)
530-
531-
make_file!(darwin_obj.problem)
532-
make_file!(electro_obj.problem)
533-
return [darwin_obj, electro_obj]
534-
end
535521

536522
"""
537523
$(TYPEDSIGNATURES)
@@ -587,6 +573,8 @@ function compute!(problem::LineParametersProblem,
587573
# Determine if we need to run post-processing
588574
run_postproc = opts.run_postprocessing && run_solver
589575

576+
line_params = nothing
577+
590578
try
591579
# Initialize Gmsh for meshing phase
592580
if run_mesh
@@ -609,40 +597,81 @@ function compute!(problem::LineParametersProblem,
609597
# Run solver if requested and/or needed
610598
if run_solver
611599
@info "Running GetDP solver..."
600+
601+
# Get dimensions for preallocation
602+
n_phases = problem.system.num_phases
603+
n_frequencies = length(problem.frequencies)
604+
605+
# Preallocate 3D arrays for Z and Y matrices
606+
Z = zeros(ComplexF64, n_phases, n_phases, n_frequencies)
607+
Y = zeros(ComplexF64, n_phases, n_phases, n_frequencies)
608+
612609
for (i, frequency) in enumerate(problem.frequencies)
613610
@info "Solving for frequency $i: $frequency Hz"
614-
for fem_formulation in formulation.analysis_type
615-
@debug "Processing formulation: $(fem_formulation.resolution_name)"
616-
make_fem_problem!(fem_formulation, frequency, workspace)
617-
solve_cmd = "$(opts.getdp_executable) $(fem_formulation.problem.filename) -msh $(workspace.paths[:mesh_file]) -solve $(fem_formulation.resolution_name) -v$(opts.verbosity == 0 ? 2 : 3)"
618-
619-
@info "Solving... (Resolution = $(fem_formulation.resolution_name))"
611+
try
612+
for fem_formulation in formulation.analysis_type
613+
@debug "Processing formulation: $(fem_formulation.resolution_name)"
614+
make_fem_problem!(fem_formulation, frequency, workspace)
615+
616+
solve_cmd = "$(opts.getdp_executable) $(fem_formulation.problem.filename) -msh $(workspace.paths[:mesh_file]) -solve $(fem_formulation.resolution_name) -v$(opts.verbosity == 0 ? 2 : 3)"
617+
618+
@info "Solving... (Resolution = $(fem_formulation.resolution_name))"
619+
try
620+
gmsh.onelab.run("GetDP", solve_cmd)
621+
@info "Solve successful!"
622+
catch e
623+
println("Solve failed: ", e)
624+
end
625+
end
626+
# Read results and store in 3D arrays
627+
Z[:, :, i] = read_results_file(formulation.analysis_type[1], frequency, workspace)
628+
Y[:, :, i] = read_results_file(formulation.analysis_type[2], frequency, workspace)
629+
catch e
630+
@error "Failed to compute matrices for frequency $frequency Hz" exception = e
631+
rethrow(e)
632+
end
620633

634+
if !opts.cleanup_files
621635
try
622-
gmsh.onelab.run("GetDP", solve_cmd)
623-
@info "Solve successful!"
624-
# gmsh.fltk.run()
636+
# Create frequency-specific results directory
637+
freq_dir = joinpath(workspace.paths[:case_dir], "results_f=$(round(frequency, sigdigits=4))")
638+
mkpath(freq_dir)
639+
640+
# Move selected files from case_dir
641+
for ext in [".res", ".pre"]
642+
# Move all files with extension from case_dir
643+
case_files = filter(f -> endswith(f, ext),
644+
readdir(workspace.paths[:case_dir], join=true))
645+
for f in case_files
646+
mv(f, joinpath(freq_dir, basename(f)), force=true)
647+
end
648+
end
649+
650+
# Move all files from results_dir
651+
if isdir(workspace.paths[:results_dir])
652+
result_files = readdir(workspace.paths[:results_dir], join=true)
653+
if !isempty(result_files)
654+
mv.(result_files,
655+
joinpath.(freq_dir, basename.(result_files)),
656+
force=true)
657+
end
658+
end
659+
625660
catch e
626-
println("Solve failed: ", e)
661+
@error "Failed to store results for frequency $frequency Hz" exception = e
662+
rethrow(e)
627663
end
628-
629664
end
630665
end
666+
667+
# Create LineParameters object with computed matrices
668+
line_params = LineParameters(Z, Y)
669+
631670
@info "All solver runs completed."
632671
else
633672
@info "Skipping solver run."
634673
end
635674

636-
# Run post-processing if needed
637-
if run_postproc
638-
@info "Running post-processing..."
639-
# To be implemented
640-
# _run_postprocessing(workspace)
641-
@info "Post-processing completed."
642-
else
643-
@info "Skipping post-processing."
644-
end
645-
646675
catch e
647676
@warn "Error in FEM simulation: $e"
648677

@@ -668,7 +697,7 @@ function compute!(problem::LineParametersProblem,
668697
end
669698
end
670699

671-
return workspace
700+
return workspace, line_params
672701
end
673702

674703
# Include auxiliary files

0 commit comments

Comments
 (0)