From 0a0a2b2771531d6b32427710688b4f31f830fba5 Mon Sep 17 00:00:00 2001 From: LucasLessa1 Date: Tue, 6 May 2025 22:15:08 -0300 Subject: [PATCH] now all the methods can be put in a vector inside problem --- examples/electrodynamic_formulation.jl | 15 ++- examples/problem_definition.jl | 11 ++- src/problem_definition.jl | 121 +++++++++++++------------ test/test_problem_formulation.jl | 15 ++- 4 files changed, 91 insertions(+), 71 deletions(-) diff --git a/examples/electrodynamic_formulation.jl b/examples/electrodynamic_formulation.jl index 3e96474..7e34939 100644 --- a/examples/electrodynamic_formulation.jl +++ b/examples/electrodynamic_formulation.jl @@ -25,7 +25,8 @@ add_constraint!(functionspace, "vn2", "EdgesOf", "ZeroElectricScalarPotential"; condition="If (Flag_Degree_v == 2)", endCondition="EndIf") -problem.functionspace = functionspace +# problem.functionspace = functionspace +push!(problem.functionspace, functionspace) # Formulation section formulation = Formulation() @@ -36,7 +37,8 @@ eq = add_equation!(form) add!(eq, "Galerkin", "[ sigma[] * Dof{d v} , {d v} ]", In="Domain_Ele", Jacobian="Vol", Integration="I1") add!(eq, "Galerkin", "DtDof[ epsilon[] * Dof{d v} , {d v} ]", In="Domain_Ele", Jacobian="Vol", Integration="I1") -problem.formulation = formulation +# problem.formulation = formulation +push!(problem.formulation, formulation) # Resolution section resolution = Resolution() @@ -53,7 +55,8 @@ add!(resolution, "Electrodynamics", "Sys_Ele", "PostOperation[Ele_Cuts]" ]) -problem.resolution = resolution +# problem.resolution = resolution +push!(problem.resolution, resolution) ## PostProcessing section postprocessing = PostProcessing() @@ -97,7 +100,8 @@ add!(q, "Term", "V0 * F_Cos_wt_p[]{2*Pi*Freq, Pc}"; Type="Global", In="Ind_3") q = add!(pp, "C_from_Energy") add!(q, "Term", "2*\$We/SquNorm[\$voltage]"; Type="Global", In="DomainDummy") -problem.postprocessing = postprocessing +# problem.postprocessing = postprocessing +push!(problem.postprocessing, postprocessing) # PostOperation section postoperation = PostOperation() @@ -133,7 +137,8 @@ op2 = add_operation!(po2) add_operation!(op2, "Print[ em, OnLine { {x2,y2,0} {x2+dc/2+ti+txlpe+to+tapl,y2,0} } {100}, Name \"|E| [V/m] cut in phase 2\", File \"res/em_cut.pos\" ]") add_operation!(op2, "Echo[Str[\"View[PostProcessing.NbViews-1].Type = 4;\",\n \"View[PostProcessing.NbViews-1].Axes = 3;\",\n \"View[PostProcessing.NbViews-1].AutoPosition = 3;\",\n \"View[PostProcessing.NbViews-1].ShowTime = 0;\",\n \"View[PostProcessing.NbViews-1].LineWidth = 3;\",\n \"View[PostProcessing.NbViews-1].NbIso = 5;\"],\n File \"res/em_cut.opt\" ]") -problem.postoperation = postoperation +# problem.postoperation = postoperation +push!(problem.postoperation, postoperation) # Generate and write the .pro file make_file!(problem) diff --git a/examples/problem_definition.jl b/examples/problem_definition.jl index 76e0978..6e9e899 100644 --- a/examples/problem_definition.jl +++ b/examples/problem_definition.jl @@ -179,7 +179,7 @@ add!(group, "DomainDummy", [12345], "Region") # add_space!(group) # Assign Group to Problem -problem.group = group +push!(problem.group, group) # Add the second Function definition func2 = GetDP.Function() # Use qualified name as established previously @@ -389,8 +389,9 @@ add_constant!(func2, "Flag_Degree_v", "1") add_space!(func2) -problem.function_obj = [func1, func2] - +# problem.function_obj = [func1, func2] +push!(problem.function_obj, func1) +push!(problem.function_obj, func2) # Add the Constraint definition constraint = GetDP.Constraint() @@ -428,8 +429,8 @@ case!(current, "Ind_2", value="I", time_function="F_Cos_wt_p[]{2*Pi*Freq, Pb}") case!(current, "Ind_3", value="I", time_function="F_Cos_wt_p[]{2*Pi*Freq, Pc}") # Assign Constraint to Problem -problem.constraint = constraint - +# problem.constraint = constraint +push!(problem.constraint, constraint) # Generate and write the .pro file make_file!(problem) diff --git a/src/problem_definition.jl b/src/problem_definition.jl index cea1f7d..bf1f997 100644 --- a/src/problem_definition.jl +++ b/src/problem_definition.jl @@ -8,37 +8,37 @@ using Dates """ Problem -The main problem definition class that brings together all the components. +A classe principal de definição de problema que reúne todos os componentes. """ mutable struct Problem _GETDP_CODE::Vector{String} filename::Union{String,Nothing} - group::Group + group::Vector{Group} function_obj::Vector{Function} - constraint::Constraint - functionspace::FunctionSpace - jacobian::Jacobian - integration::Integration - formulation::Formulation - resolution::Resolution - postprocessing::PostProcessing - postoperation::PostOperation + constraint::Vector{Constraint} + functionspace::Vector{FunctionSpace} + jacobian::Vector{Jacobian} + integration::Vector{Integration} + formulation::Vector{Formulation} + resolution::Vector{Resolution} + postprocessing::Vector{PostProcessing} + postoperation::Vector{PostOperation} objects::Vector{String} function Problem(; gmsh_major_version=nothing) version = GetDP.VERSION _GETDP_CODE = ["// This code was created by GetDP.jl v$(version).\n"] - group = Group() - function_obj = Function[] # Initialize as empty vector - constraint = Constraint() - functionspace = FunctionSpace() - jacobian = Jacobian() - integration = Integration() - formulation = Formulation() - resolution = Resolution() - postprocessing = PostProcessing() - postoperation = PostOperation() + group = Group[] + function_obj = Function[] + constraint = Constraint[] + functionspace = FunctionSpace[] + jacobian = Jacobian[] + integration = Integration[] + formulation = Formulation[] + resolution = Resolution[] + postprocessing = PostProcessing[] + postoperation = PostOperation[] objects = [ "group", @@ -74,7 +74,7 @@ end """ get_code(problem::Problem) -Returns properly formatted GetDP code. +Retorna o código GetDP formatado adequadamente. """ function get_code(problem::Problem) return join(problem._GETDP_CODE, "") @@ -83,7 +83,7 @@ end """ add_raw_code!(problem::Problem, raw_code, newline=true) -Add raw code to the Problem object. +Adiciona código bruto ao objeto Problem. """ function add_raw_code!(problem::Problem, raw_code, newline=true) problem._GETDP_CODE = [add_raw_code(get_code(problem), raw_code, newline)] @@ -92,7 +92,7 @@ end """ add_comment!(problem::Problem, comment_text, newline=true) -Add a comment to the Problem object. +Adiciona um comentário ao objeto Problem. """ function add_comment!(problem::Problem, comment_text, newline=true) add_raw_code!(problem, comment(comment_text; newline=false), newline) @@ -101,60 +101,70 @@ end """ make_file!(problem::Problem) -Generate the GetDP code for all objects in the Problem, including only non-empty components. +Gera o código GetDP para todos os objetos no Problem, incluindo apenas componentes não vazios. """ function make_file!(problem::Problem) for attr in problem.objects + components = getfield(problem, Symbol(attr)) if attr == "function_obj" - for func in problem.function_obj # Iterate over all Function objects - if !isempty(func.content) # Check if functions are defined + for func in components + if !isempty(func.content) push!(problem._GETDP_CODE, code(func)) end end elseif attr == "group" - p = getfield(problem, :group) - if !isempty(p.content) # Check if groups are defined - push!(problem._GETDP_CODE, code(p)) + for grp in components + if !isempty(grp.content) + push!(problem._GETDP_CODE, code(grp)) + end end elseif attr == "constraint" - p = getfield(problem, :constraint) - if !isempty(p.constraints) # Check if constraints are defined - push!(problem._GETDP_CODE, code(p)) + for constr in components + if !isempty(constr.constraints) + push!(problem._GETDP_CODE, code(constr)) + end end elseif attr == "functionspace" - p = getfield(problem, :functionspace) - if !isempty(p.content) # Check if items are defined - push!(problem._GETDP_CODE, code(p)) + for fs in components + if !isempty(fs.content) + push!(problem._GETDP_CODE, code(fs)) + end end elseif attr == "jacobian" - p = getfield(problem, :jacobian) - if !isempty(p.content) # Check if jacobians are defined - push!(problem._GETDP_CODE, code(p)) + for jac in components + if !isempty(jac.content) + push!(problem._GETDP_CODE, code(jac)) + end end elseif attr == "integration" - p = getfield(problem, :integration) - if !isempty(p.content) # Check if integrations are defined - push!(problem._GETDP_CODE, code(p)) + for integ in components + if !isempty(integ.content) + push!(problem._GETDP_CODE, code(integ)) + end end elseif attr == "formulation" - p = getfield(problem, :formulation) - if !isempty(p.items) # Check if formulations are defined - push!(problem._GETDP_CODE, code(p)) + for form in components + if !isempty(form.items) + push!(problem._GETDP_CODE, code(form)) + end end elseif attr == "resolution" - p = getfield(problem, :resolution) - if !isempty(p.content) # Check if resolutions are defined - push!(problem._GETDP_CODE, code(p)) + for res in components + if !isempty(res.content) + push!(problem._GETDP_CODE, code(res)) + end end elseif attr == "postprocessing" - p = getfield(problem, :postprocessing) - if !isempty(p.content) # Check if postprocessings are defined - push!(problem._GETDP_CODE, code(p)) + for pp in components + if !isempty(pp.content) + push!(problem._GETDP_CODE, code(pp)) + end end elseif attr == "postoperation" - p = getfield(problem, :postoperation) - if !isempty(p.content) # Check if postoperations are defined - push!(problem._GETDP_CODE, code(p)) + for po in components + if !isempty(po.content) + push!(problem._GETDP_CODE, code(po)) + end end end end @@ -163,7 +173,7 @@ end """ write_file!(problem::Problem) -Write the GetDP code to a file. +Escreve o código GetDP em um arquivo. """ function write_file!(problem::Problem) if problem.filename === nothing @@ -178,13 +188,12 @@ end """ include!(problem::Problem, incl_file) -Include another GetDP file. +Inclui outro arquivo GetDP. """ function include!(problem::Problem, incl_file) push!(problem._GETDP_CODE, "\nInclude \"$(incl_file)\";") end - """ write_multiple_problems(problems::Vector{Problem}, filename::String) diff --git a/test/test_problem_formulation.jl b/test/test_problem_formulation.jl index d435df6..4cda7da 100644 --- a/test/test_problem_formulation.jl +++ b/test/test_problem_formulation.jl @@ -24,7 +24,8 @@ include("../test/normalized.jl") condition="If (Flag_Degree_v == 2)", endCondition="EndIf") - problem.functionspace = functionspace + # problem.functionspace = functionspace + push!(problem.functionspace, functionspace) # Formulation section formulation = Formulation() @@ -35,7 +36,8 @@ include("../test/normalized.jl") add!(eq, "Galerkin", "[ sigma[] * Dof{d v} , {d v} ]", In="Domain_Ele", Jacobian="Vol", Integration="I1") add!(eq, "Galerkin", "DtDof[ epsilon[] * Dof{d v} , {d v} ]", In="Domain_Ele", Jacobian="Vol", Integration="I1") - problem.formulation = formulation + # problem.formulation = formulation + push!(problem.formulation, formulation) # Resolution section resolution = Resolution() @@ -52,7 +54,8 @@ include("../test/normalized.jl") "PostOperation[Ele_Cuts]" ]) - problem.resolution = resolution + # problem.resolution = resolution + push!(problem.resolution, resolution) ## PostProcessing section postprocessing = PostProcessing() @@ -96,7 +99,8 @@ include("../test/normalized.jl") q = add!(pp, "C_from_Energy") add!(q, "Term", "2*\$We/SquNorm[\$voltage]"; Type="Global", In="DomainDummy") - problem.postprocessing = postprocessing + # problem.postprocessing = postprocessing + push!(problem.postprocessing, postprocessing) # PostOperation section postoperation = PostOperation() @@ -132,7 +136,8 @@ include("../test/normalized.jl") add_operation!(op2, "Print[ em, OnLine { {x2,y2,0} {x2+dc/2+ti+txlpe+to+tapl,y2,0} } {100}, Name \"|E| [V/m] cut in phase 2\", File \"res/em_cut.pos\" ]") add_operation!(op2, "Echo[Str[\"View[PostProcessing.NbViews-1].Type = 4;\",\n \"View[PostProcessing.NbViews-1].Axes = 3;\",\n \"View[PostProcessing.NbViews-1].AutoPosition = 3;\",\n \"View[PostProcessing.NbViews-1].ShowTime = 0;\",\n \"View[PostProcessing.NbViews-1].LineWidth = 3;\",\n \"View[PostProcessing.NbViews-1].NbIso = 5;\"],\n File \"res/em_cut.opt\" ]") - problem.postoperation = postoperation + # problem.postoperation = postoperation + push!(problem.postoperation, postoperation) # Generate and write the .pro file make_file!(problem)