Skip to content

Commit 57715be

Browse files
refactor(fem): remove TimestampLogger, add calc_domain_size and use setup_logging!
1 parent 9343a44 commit 57715be

2 files changed

Lines changed: 21 additions & 46 deletions

File tree

src/FEMTools.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module FEMTools
2424
# Export public API
2525
export MeshTransition
2626
export compute!, preview_results
27-
export FEMElectrodynamics, FEMDarwin
27+
export FEMElectrodynamics, FEMDarwin, FormulationSet, calc_domain_size
2828

2929
# Load common dependencies
3030
include("common_deps.jl")
@@ -40,14 +40,9 @@ import ..Engine: AbstractFormulationSet, AbstractImpedanceFormulation, AbstractA
4040

4141
# Module-specific dependencies
4242
using Gmsh
43-
using Printf
44-
using Dates
4543
using Measurements
4644
using LinearAlgebra
4745
using Colors
48-
using Logging
49-
using Logging: AbstractLogger, LogLevel, Info, global_logger
50-
using LoggingExtras: TeeLogger, FileLogger
5146

5247
# GetDP.jl
5348
using GetDP
@@ -423,7 +418,7 @@ struct FEMFormulation <: AbstractFormulationSet
423418
)
424419

425420

426-
setup_fem_logging(options.verbosity, options.logfile)
421+
setup_logging!(options.verbosity, options.logfile)
427422

428423
return new(
429424
domain_radius, domain_radius_inf,

src/FEMTools/utilities.jl

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,6 @@ Utility functions for the FEMTools.jl module.
33
These functions provide various utilities for file management, logging, etc.
44
"""
55

6-
struct TimestampLogger <: AbstractLogger
7-
logger::AbstractLogger
8-
end
9-
10-
Logging.min_enabled_level(logger::TimestampLogger) = Logging.min_enabled_level(logger.logger)
11-
Logging.shouldlog(logger::TimestampLogger, level, _module, group, id) =
12-
Logging.shouldlog(logger.logger, level, _module, group, id)
13-
14-
function Logging.handle_message(logger::TimestampLogger, level, message, _module, group, id,
15-
filepath, line; kwargs...)
16-
timestamp = Dates.format(now(), "yyyy-mm-dd HH:MM:SS")
17-
new_message = "[$timestamp] $message"
18-
Logging.handle_message(logger.logger, level, new_message, _module, group, id,
19-
filepath, line; kwargs...)
20-
end
21-
22-
function setup_fem_logging(verbosity::Int, logfile::Union{String,Nothing}=nothing)
23-
level = verbosity >= 2 ? Logging.Debug :
24-
verbosity == 1 ? Logging.Info : Logging.Warn
25-
26-
# Create console logger
27-
console_logger = ConsoleLogger(stderr, level)
28-
29-
if isnothing(logfile)
30-
# Log to console only
31-
global_logger(TimestampLogger(console_logger))
32-
else
33-
# Try to set up file logging with fallback to console-only
34-
try
35-
file_logger = FileLogger(logfile, level)
36-
combined_logger = TeeLogger(console_logger, file_logger)
37-
global_logger(TimestampLogger(combined_logger))
38-
catch e
39-
@warn "Failed to set up file logging to $logfile: $e"
40-
global_logger(TimestampLogger(console_logger))
41-
end
42-
end
43-
end
446

457
"""
468
$(TYPEDSIGNATURES)
@@ -247,4 +209,22 @@ function map_verbosity_to_gmsh(verbosity::Int)
247209
else # Warn
248210
return 1 # Gmsh Errors level
249211
end
250-
end
212+
end
213+
214+
function calc_domain_size(earth_params::EarthModel, f::Vector{<:Float64}; min_radius=5.0, max_radius=5000.0)
215+
# Find the earth layer with the highest resistivity to determine the domain size
216+
if isempty(earth_params.layers)
217+
error("EarthModel has no layers defined.")
218+
end
219+
220+
# Find the index of the layer with the maximum resistivity at the first frequency
221+
max_rho_idx = argmax([layer.rho_g[1] for layer in earth_params.layers])
222+
target_layer = earth_params.layers[max_rho_idx]
223+
224+
rho_g = target_layer.rho_g[1]
225+
mu_g = target_layer.mu_g[1]
226+
freq = first(f) # Use the first frequency for the calculation
227+
skin_depth_earth = abs(sqrt(rho_g / (1im * 2 * pi * freq * mu_g)))
228+
return clamp(skin_depth_earth, min_radius, max_radius)
229+
end
230+

0 commit comments

Comments
 (0)