From 1cfdf501aaaf63f63f82802d889dfbe141d690b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mas=C3=B3?= Date: Mon, 9 Mar 2026 16:03:25 +0100 Subject: [PATCH 1/7] initial commit --- test/data/StaggeredElectroMechanical.jl | 92 +++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 test/data/StaggeredElectroMechanical.jl diff --git a/test/data/StaggeredElectroMechanical.jl b/test/data/StaggeredElectroMechanical.jl new file mode 100644 index 0000000..cc2984b --- /dev/null +++ b/test/data/StaggeredElectroMechanical.jl @@ -0,0 +1,92 @@ +using Gridap, GridapGmsh, HyperFEM, GridapSolvers, DrWatson, TimerOutputs +using GridapSolvers.NonlinearSolvers +using Gridap.FESpaces +using HyperFEM: jacobian, IterativeSolver, solve! +using WriteVTK +using Revise +using TimerOutputs +using HyperFEM + + +pname = "Static_ElectroMechanical_staggered" +meshfile = "ex2_mesh.msh" +simdir = datadir("sims", pname) +setupfolder(simdir) + +geomodel = GmshDiscreteModel(datadir("models", meshfile)) + +physmodel_mec = NeoHookean3D(λ=10.0, μ=1.0) +physmodel_elec = IdealDielectric(ε=1.0) +physmodel = ElectroMechModel(Mechano=physmodel_mec, Electro=physmodel_elec) + +# Setup integration +order = 2 +degree = 2 * order +Ω = Triangulation(geomodel) +dΩ = Measure(Ω, degree) + +# Dirichlet conditions +evolu(Λ) = 1.0 +dir_u_tags = ["fixedup"] +dir_u_values = [[0.0, 0.0, 0.0]] +dir_u_timesteps = [evolu] +Du = DirichletBC(dir_u_tags, dir_u_values, dir_u_timesteps) + +electrodes = (x)->(x[1] > 4 ? 0.15 : 0.05) +evolφ(Λ) = Λ +dir_φ_tags = ["midsuf", "topsuf"] +dir_φ_values = [0.0, electrodes] +dir_φ_timesteps = [evolφ, evolφ] +Dφ = DirichletBC(dir_φ_tags, dir_φ_values, dir_φ_timesteps) + +# FE spaces +reffeu = ReferenceFE(lagrangian, VectorValue{3,Float64}, order) +reffeφ = ReferenceFE(lagrangian, Float64, order) + +# Test FE Spaces +Vu = TestFESpace(geomodel, reffeu, Du, :H1) +Vφ = TestFESpace(geomodel, reffeφ, Dφ, :H1) + +# Trial FE Spaces and state variables +Uu = TrialFESpace(Vu, Du, 1.0) +uh⁺ = FEFunction(Uu, zero_free_values(Uu)) + +Uu⁻ = TrialFESpace(Vu, Du, 1.0) +uh⁻ = FEFunction(Uu⁻, zero_free_values(Uu⁻)) + +Uφ = TrialFESpace(Vφ, Dφ, 1.0) +φh⁺ = FEFunction(Uφ, zero_free_values(Uφ)) + +Uφ⁻ = TrialFESpace(Vφ, Dφ, 1.0) +φh⁻ = FEFunction(Uφ⁻, zero_free_values(Uφ⁻)) + +# Electro +Mechano_coupling(Λ) = uh⁻ + (uh⁺ - uh⁻) * Λ +res_elec(Λ) = (φ, vφ) -> residual(physmodel, Electro, (Mechano_coupling(Λ), φ), vφ, dΩ) +jac_elec(Λ) = (φ, dφ, vφ) -> jacobian(physmodel, Electro, (Mechano_coupling(Λ), φ), dφ, vφ, dΩ) + +# Mechano +Electro_coupling(Λ) = φh⁻ + (φh⁺ - φh⁻) * Λ +res_mec(Λ) = (u, v) -> residual(physmodel, Mechano, (u, Electro_coupling(Λ)), v, dΩ) +jac_mec(Λ) = (u, du, v) -> jacobian(physmodel, Mechano, (u, Electro_coupling(Λ)), du, v, dΩ) + +# nonlinear solver electro +ls = LUSolver() +nls_ = NewtonSolver(ls; maxiter=20, atol=1.e-10, rtol=1.e-8, verbose=true) +comp_model_elec = StaticNonlinearModel(res_elec, jac_elec, Uφ, Vφ, Dφ, dΩ; nls=nls_, xh=φh⁺) + +# nonlinear solver mechano +comp_model_mec = StaticNonlinearModel(res_mec, jac_mec, Uu, Vu, Du, dΩ; nls=nls_, xh=uh⁺) + +# nonlinear staggered model +comp_model= StaggeredModel((comp_model_elec,comp_model_mec), (φh⁺,uh⁺), (φh⁻,uh⁻)) + +args_elec = Dict(:stepping => (nsteps=1,maxbisec=5)) +args_mec = Dict(:stepping => (nsteps=5,maxbisec=5)) +args=(args_elec,args_mec) + +solve!(comp_model; stepping=(nsteps=5, maxbisec=15), kargsolve=args) + +writevtk(Ω, simdir * "/result2_end", cellfields=["φh" => φh⁺, "uh" => uh⁺]) + + \ No newline at end of file From 7b62e797118cd1fec83d6864ba378dded3f7e4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mas=C3=B3?= Date: Mon, 9 Mar 2026 16:57:12 +0100 Subject: [PATCH 2/7] fixed script --- test/data/StaggeredElectroMechanical.jl | 92 ---------------- .../StaggeredElectroMechanicalSimulation.jl | 101 ++++++++++++++++++ 2 files changed, 101 insertions(+), 92 deletions(-) delete mode 100644 test/data/StaggeredElectroMechanical.jl create mode 100644 test/data/StaggeredElectroMechanicalSimulation.jl diff --git a/test/data/StaggeredElectroMechanical.jl b/test/data/StaggeredElectroMechanical.jl deleted file mode 100644 index cc2984b..0000000 --- a/test/data/StaggeredElectroMechanical.jl +++ /dev/null @@ -1,92 +0,0 @@ -using Gridap, GridapGmsh, HyperFEM, GridapSolvers, DrWatson, TimerOutputs -using GridapSolvers.NonlinearSolvers -using Gridap.FESpaces -using HyperFEM: jacobian, IterativeSolver, solve! -using WriteVTK -using Revise -using TimerOutputs -using HyperFEM - - -pname = "Static_ElectroMechanical_staggered" -meshfile = "ex2_mesh.msh" -simdir = datadir("sims", pname) -setupfolder(simdir) - -geomodel = GmshDiscreteModel(datadir("models", meshfile)) - -physmodel_mec = NeoHookean3D(λ=10.0, μ=1.0) -physmodel_elec = IdealDielectric(ε=1.0) -physmodel = ElectroMechModel(Mechano=physmodel_mec, Electro=physmodel_elec) - -# Setup integration -order = 2 -degree = 2 * order -Ω = Triangulation(geomodel) -dΩ = Measure(Ω, degree) - -# Dirichlet conditions -evolu(Λ) = 1.0 -dir_u_tags = ["fixedup"] -dir_u_values = [[0.0, 0.0, 0.0]] -dir_u_timesteps = [evolu] -Du = DirichletBC(dir_u_tags, dir_u_values, dir_u_timesteps) - -electrodes = (x)->(x[1] > 4 ? 0.15 : 0.05) -evolφ(Λ) = Λ -dir_φ_tags = ["midsuf", "topsuf"] -dir_φ_values = [0.0, electrodes] -dir_φ_timesteps = [evolφ, evolφ] -Dφ = DirichletBC(dir_φ_tags, dir_φ_values, dir_φ_timesteps) - -# FE spaces -reffeu = ReferenceFE(lagrangian, VectorValue{3,Float64}, order) -reffeφ = ReferenceFE(lagrangian, Float64, order) - -# Test FE Spaces -Vu = TestFESpace(geomodel, reffeu, Du, :H1) -Vφ = TestFESpace(geomodel, reffeφ, Dφ, :H1) - -# Trial FE Spaces and state variables -Uu = TrialFESpace(Vu, Du, 1.0) -uh⁺ = FEFunction(Uu, zero_free_values(Uu)) - -Uu⁻ = TrialFESpace(Vu, Du, 1.0) -uh⁻ = FEFunction(Uu⁻, zero_free_values(Uu⁻)) - -Uφ = TrialFESpace(Vφ, Dφ, 1.0) -φh⁺ = FEFunction(Uφ, zero_free_values(Uφ)) - -Uφ⁻ = TrialFESpace(Vφ, Dφ, 1.0) -φh⁻ = FEFunction(Uφ⁻, zero_free_values(Uφ⁻)) - -# Electro -Mechano_coupling(Λ) = uh⁻ + (uh⁺ - uh⁻) * Λ -res_elec(Λ) = (φ, vφ) -> residual(physmodel, Electro, (Mechano_coupling(Λ), φ), vφ, dΩ) -jac_elec(Λ) = (φ, dφ, vφ) -> jacobian(physmodel, Electro, (Mechano_coupling(Λ), φ), dφ, vφ, dΩ) - -# Mechano -Electro_coupling(Λ) = φh⁻ + (φh⁺ - φh⁻) * Λ -res_mec(Λ) = (u, v) -> residual(physmodel, Mechano, (u, Electro_coupling(Λ)), v, dΩ) -jac_mec(Λ) = (u, du, v) -> jacobian(physmodel, Mechano, (u, Electro_coupling(Λ)), du, v, dΩ) - -# nonlinear solver electro -ls = LUSolver() -nls_ = NewtonSolver(ls; maxiter=20, atol=1.e-10, rtol=1.e-8, verbose=true) -comp_model_elec = StaticNonlinearModel(res_elec, jac_elec, Uφ, Vφ, Dφ, dΩ; nls=nls_, xh=φh⁺) - -# nonlinear solver mechano -comp_model_mec = StaticNonlinearModel(res_mec, jac_mec, Uu, Vu, Du, dΩ; nls=nls_, xh=uh⁺) - -# nonlinear staggered model -comp_model= StaggeredModel((comp_model_elec,comp_model_mec), (φh⁺,uh⁺), (φh⁻,uh⁻)) - -args_elec = Dict(:stepping => (nsteps=1,maxbisec=5)) -args_mec = Dict(:stepping => (nsteps=5,maxbisec=5)) -args=(args_elec,args_mec) - -solve!(comp_model; stepping=(nsteps=5, maxbisec=15), kargsolve=args) - -writevtk(Ω, simdir * "/result2_end", cellfields=["φh" => φh⁺, "uh" => uh⁺]) - - \ No newline at end of file diff --git a/test/data/StaggeredElectroMechanicalSimulation.jl b/test/data/StaggeredElectroMechanicalSimulation.jl new file mode 100644 index 0000000..b0ab85a --- /dev/null +++ b/test/data/StaggeredElectroMechanicalSimulation.jl @@ -0,0 +1,101 @@ +using Gridap, Gridap.FESpaces, GridapSolvers, GridapSolvers.NonlinearSolvers +using HyperFEM +using HyperFEM.ComputationalModels.CartesianTags +using HyperFEM: jacobian + + +function staggered_electro_mechanical_simulation(; is_vtk=true, verbose=true) + + # Problem name + pname = stem(@__FILE__) + folder = projdir("data", "sims", pname) + outpath = joinpath(folder, pname) + setupfolder(folder) + + # Geometry and discrete model + domain = (0.0, 0.1, 0.0, 0.01, 0.0, 0.002) + partition = (8, 2, 2) + geometry = CartesianDiscreteModel(domain, partition) + labels = get_face_labeling(geometry) + add_tag_from_tags!(labels, "fixedu", CartesianTags.faceX0) + add_tag_from_tags!(labels, "topsuf", CartesianTags.faceZ1) + add_tag_from_vertex_filter!(labels, geometry, "midsuf", x -> x[3] ≈ 0.001) + + # Constitutive model + physmodel_mec = NeoHookean3D(λ=10.0, μ=1.0) + physmodel_elec = IdealDielectric(ε=1.0) + physmodel = ElectroMechModel(physmodel_elec, physmodel_mec) + + # Setup integration + order = 2 + degree = 2 * order + Ω = Triangulation(geometry) + dΩ = Measure(Ω, degree) + + # Dirichlet conditions + evolu(Λ) = 1.0 + dir_u_tags = ["fixedu"] + dir_u_values = [[0.0, 0.0, 0.0]] + dir_u_timesteps = [evolu] + Du = DirichletBC(dir_u_tags, dir_u_values, dir_u_timesteps) + + evolφ(Λ) = Λ + dir_φ_tags = ["midsuf", "topsuf"] + dir_φ_values = [0.0, 0.00005] + dir_φ_timesteps = [evolφ, evolφ] + Dφ = DirichletBC(dir_φ_tags, dir_φ_values, dir_φ_timesteps) + + # FE spaces + reffeu = ReferenceFE(lagrangian, VectorValue{3,Float64}, order) + reffeφ = ReferenceFE(lagrangian, Float64, order) + + # Test FE Spaces + Vu = TestFESpace(geometry, reffeu, Du, conformity=:H1) + Vφ = TestFESpace(geometry, reffeφ, Dφ, conformity=:H1) + + # Trial FE Spaces and state variables + Uu = TrialFESpace(Vu, Du, 1.0) + uh⁺ = FEFunction(Uu, zero_free_values(Uu)) + + Uu⁻ = TrialFESpace(Vu, Du, 1.0) + uh⁻ = FEFunction(Uu⁻, zero_free_values(Uu⁻)) + + Uφ = TrialFESpace(Vφ, Dφ, 1.0) + φh⁺ = FEFunction(Uφ, zero_free_values(Uφ)) + + Uφ⁻ = TrialFESpace(Vφ, Dφ, 1.0) + φh⁻ = FEFunction(Uφ⁻, zero_free_values(Uφ⁻)) + + # Kinematics + k = (Kinematics(Mechano, Solid), Kinematics(Electro, Solid)) + + # Electro + Mechano_coupling(Λ) = uh⁻ + (uh⁺ - uh⁻) * Λ + res_elec(Λ) = (φ, vφ) -> residual(physmodel, Electro, k, (Mechano_coupling(Λ), φ), vφ, dΩ) + jac_elec(Λ) = (φ, dφ, vφ) -> jacobian(physmodel, Electro, k, (Mechano_coupling(Λ), φ), dφ, vφ, dΩ) + + # Mechano + Electro_coupling(Λ) = φh⁻ + (φh⁺ - φh⁻) * Λ + res_mec(Λ) = (u, v) -> residual(physmodel, Mechano, k, (u, Electro_coupling(Λ)), v, dΩ) + jac_mec(Λ) = (u, du, v) -> jacobian(physmodel, Mechano, k, (u, Electro_coupling(Λ)), du, v, dΩ) + + # nonlinear solver + ls = LUSolver() + nls_ = NewtonSolver(ls; maxiter=20, atol=1.e-10, rtol=1.e-8, verbose=verbose) + comp_model_elec = StaticNonlinearModel(res_elec, jac_elec, Uφ, Vφ, Dφ; nls=nls_, xh=φh⁺) + comp_model_mec = StaticNonlinearModel(res_mec, jac_mec, Uu, Vu, Du; nls=nls_, xh=uh⁺) + comp_model= StaggeredModel((comp_model_elec,comp_model_mec), (φh⁺,uh⁺), (φh⁻,uh⁻)) + + args_elec = Dict(:stepping => (nsteps=1, maxbisec=5)) + args_mec = Dict(:stepping => (nsteps=5, maxbisec=5)) + args=(args_elec,args_mec) + + solve!(comp_model; stepping=(nsteps=5, nsubsteps=1, maxbisec=15), kargsolve=args) + + if is_vtk + writevtk(Ω, outpath, cellfields=["φh" => φh⁺, "uh" => uh⁺]) + end +end + + +# staggered_electro_mechanical_simulation() From 1bb7c5c2c0c940cc645e270b467ead6da0728dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mas=C3=B3?= Date: Mon, 9 Mar 2026 17:07:09 +0100 Subject: [PATCH 3/7] return solve --- test/data/StaggeredElectroMechanicalSimulation.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/data/StaggeredElectroMechanicalSimulation.jl b/test/data/StaggeredElectroMechanicalSimulation.jl index b0ab85a..bfb896f 100644 --- a/test/data/StaggeredElectroMechanicalSimulation.jl +++ b/test/data/StaggeredElectroMechanicalSimulation.jl @@ -90,11 +90,13 @@ function staggered_electro_mechanical_simulation(; is_vtk=true, verbose=true) args_mec = Dict(:stepping => (nsteps=5, maxbisec=5)) args=(args_elec,args_mec) - solve!(comp_model; stepping=(nsteps=5, nsubsteps=1, maxbisec=15), kargsolve=args) + x = solve!(comp_model; stepping=(nsteps=5, nsubsteps=1, maxbisec=15), kargsolve=args) if is_vtk writevtk(Ω, outpath, cellfields=["φh" => φh⁺, "uh" => uh⁺]) end + + return x end From cd34acd5eaa6793127f2201eeeca6e3161f1c8be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mas=C3=B3?= Date: Mon, 9 Mar 2026 17:08:03 +0100 Subject: [PATCH 4/7] added test --- .../SimulationsTests/StaggeredElectroMechanicalTest.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/SimulationsTests/StaggeredElectroMechanicalTest.jl diff --git a/test/SimulationsTests/StaggeredElectroMechanicalTest.jl b/test/SimulationsTests/StaggeredElectroMechanicalTest.jl new file mode 100644 index 0000000..00e8c85 --- /dev/null +++ b/test/SimulationsTests/StaggeredElectroMechanicalTest.jl @@ -0,0 +1,10 @@ +using HyperFEM +using Test + +filename = projdir("test/data/StaggeredElectroMechanicalSimulation.jl") +include(filename) + +φh, uh = staggered_electro_mechanical_simulation(is_vtk=false, verbose=false) + +@test norm(uh[1]) ≈ 0.01492384116 +@test norm(φh[1]) ≈ 0.00023052926 From 09bfb3d22480e74c7e30bbb312b59726e2234efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mas=C3=B3?= Date: Mon, 9 Mar 2026 17:08:35 +0100 Subject: [PATCH 5/7] runtest --- test/SimulationsTests/runtests.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/SimulationsTests/runtests.jl b/test/SimulationsTests/runtests.jl index bda1b4f..8627f68 100644 --- a/test/SimulationsTests/runtests.jl +++ b/test/SimulationsTests/runtests.jl @@ -10,6 +10,8 @@ using Test include("StaticMechanicalNeumannTest.jl") + include("StaggeredElectroMechanicalTest.jl") + # include("Stokes.jl") # include("BoundaryConditions.jl") From 75ebc865a4612117a27a3077421de7b87a4e3b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mas=C3=B3?= Date: Mon, 9 Mar 2026 17:08:49 +0100 Subject: [PATCH 6/7] restore tests --- test/SimulationsTests/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/SimulationsTests/runtests.jl b/test/SimulationsTests/runtests.jl index 8627f68..b80bde7 100644 --- a/test/SimulationsTests/runtests.jl +++ b/test/SimulationsTests/runtests.jl @@ -12,8 +12,8 @@ using Test include("StaggeredElectroMechanicalTest.jl") - # include("Stokes.jl") + include("Stokes.jl") - # include("BoundaryConditions.jl") + include("BoundaryConditions.jl") end From ec807dde6ddb6975e9fc413d446e80c7f69ec6eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Mas=C3=B3?= Date: Mon, 9 Mar 2026 17:10:31 +0100 Subject: [PATCH 7/7] minor --- test/data/StaggeredElectroMechanicalSimulation.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/data/StaggeredElectroMechanicalSimulation.jl b/test/data/StaggeredElectroMechanicalSimulation.jl index bfb896f..5058a05 100644 --- a/test/data/StaggeredElectroMechanicalSimulation.jl +++ b/test/data/StaggeredElectroMechanicalSimulation.jl @@ -86,11 +86,11 @@ function staggered_electro_mechanical_simulation(; is_vtk=true, verbose=true) comp_model_mec = StaticNonlinearModel(res_mec, jac_mec, Uu, Vu, Du; nls=nls_, xh=uh⁺) comp_model= StaggeredModel((comp_model_elec,comp_model_mec), (φh⁺,uh⁺), (φh⁻,uh⁻)) - args_elec = Dict(:stepping => (nsteps=1, maxbisec=5)) - args_mec = Dict(:stepping => (nsteps=5, maxbisec=5)) + args_elec = Dict(:stepping => (nsteps=1, maxbisec=1)) + args_mec = Dict(:stepping => (nsteps=5, maxbisec=1)) args=(args_elec,args_mec) - x = solve!(comp_model; stepping=(nsteps=5, nsubsteps=1, maxbisec=15), kargsolve=args) + x = solve!(comp_model; stepping=(nsteps=5, nsubsteps=1, maxbisec=1), kargsolve=args) if is_vtk writevtk(Ω, outpath, cellfields=["φh" => φh⁺, "uh" => uh⁺])