From dab6f8d67448fd2ed2487126d4af8a992c2add44 Mon Sep 17 00:00:00 2001 From: Boris Martin <boris.martin.be@gmail.com> Date: Fri, 24 Mar 2023 15:50:53 +0100 Subject: [PATCH] Objective and inner product are now handled through unique_ptr --- common/statefunctional.cpp | 2 ++ directional.cpp | 5 ++--- directional_field.cpp | 5 ++--- gradient.cpp | 5 ++--- ip_comparison.cpp | 8 +++----- multiscale.cpp | 5 ++--- specific/data/objective/newObjective.h | 9 +++++---- specific/model/innerproduct/newInnerProduct.cpp | 10 +++++----- specific/model/innerproduct/newInnerProduct.h | 3 ++- .../optimization/globalminimumsearch/sequential.cpp | 8 +++----- 10 files changed, 28 insertions(+), 32 deletions(-) diff --git a/common/statefunctional.cpp b/common/statefunctional.cpp index 16fdf15..56bcc58 100644 --- a/common/statefunctional.cpp +++ b/common/statefunctional.cpp @@ -301,6 +301,8 @@ double StateFunctional<T_Physic>::performance_regularization() template<Physic T_Physic> const ModelField& StateFunctional<T_Physic>::gradient() { + // Update the model adjoint state and return it. + // TODO: no model sent but FS is updated -> Is it always up to date ? std::array<bool,4> ToBeUpdated = {false,false,true,false}; return _mu.get(ToBeUpdated).field(Type::AS); } diff --git a/directional.cpp b/directional.cpp index a8a6cd2..1ddc153 100644 --- a/directional.cpp +++ b/directional.cpp @@ -139,7 +139,7 @@ int directional(const GmshFem& gmshFem) } const Data<T_Physic> d0g(freq_idx,d0); - ObjectiveInterface<T_Physic>* const objective = newObjective<T_Physic>(d0g,configuration,gmshFem); + auto objective = newObjective<T_Physic>(d0g,configuration,gmshFem); std::vector<EquationInterface<T_Physic>*> pequation(n_freq); std::vector<double> subfrequency(n_freq); @@ -152,7 +152,7 @@ int directional(const GmshFem& gmshFem) pequation[f] = new ParametrizedEquation<T_Physic>(parametrization,f,2.*M_PI*subfrequency[f],configuration,w_discret,gmshFem,suffix_f); } - StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,nullptr,nullptr,subfrequency,pequation,objective); + StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,nullptr,nullptr,subfrequency,pequation,objective.get()); FunctionalInterface* const functional = statefunctional; double eps; @@ -344,7 +344,6 @@ int directional(const GmshFem& gmshFem) { delete pequation[f]; } - delete objective; msg::unindent(); } diff --git a/directional_field.cpp b/directional_field.cpp index 20d08f5..2636e2a 100644 --- a/directional_field.cpp +++ b/directional_field.cpp @@ -113,7 +113,7 @@ int directional(const GmshFem& gmshFem) } const Data<T_Physic> d0g(freq_idx,d0); - ObjectiveInterface<T_Physic>* const objective = (d0g.nf()!=0 ? newObjective<T_Physic>(d0g,configuration,gmshFem) : nullptr); + std::unique_ptr<ObjectiveInterface<T_Physic>> objective = (d0g.nf()!=0 ? newObjective<T_Physic>(d0g,configuration,gmshFem) : nullptr); std::vector<EquationInterface<T_Physic>*> pequation(n_freq); std::vector<double> subfrequency(n_freq); @@ -132,7 +132,7 @@ int directional(const GmshFem& gmshFem) RegularizationInterface* const regularization = newRegularization(configuration,(*sweeper_m)(0,0), gmshFem); - StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,nullptr,regularization,subfrequency,pequation,objective); + StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,nullptr,regularization,subfrequency,pequation,objective.get()); FunctionalInterface* const functional = statefunctional; @@ -214,7 +214,6 @@ int directional(const GmshFem& gmshFem) { delete pequation[f]; } - delete objective; delete regularization; msg::unindent(); } diff --git a/gradient.cpp b/gradient.cpp index bb6bb2b..8923fa9 100644 --- a/gradient.cpp +++ b/gradient.cpp @@ -134,7 +134,7 @@ int gradient(const GmshFem& gmshFem) } const Data<T_Physic> d0g(freq_idx,d0); - ObjectiveInterface<T_Physic>* const objective = newObjective<T_Physic>(d0g,configuration,gmshFem); + auto objective = newObjective<T_Physic>(d0g,configuration,gmshFem); std::vector<EquationInterface<T_Physic>*> pequation(n_freq); std::vector<double> subfrequency(n_freq); @@ -147,7 +147,7 @@ int gradient(const GmshFem& gmshFem) pequation[f] = new ParametrizedEquation<T_Physic>(parametrization,f,2.*M_PI*subfrequency[f],configuration,w_discret,gmshFem,suffix_f); } - StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,&innerproduct,nullptr,subfrequency,pequation,objective); + StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,&innerproduct,nullptr,subfrequency,pequation,objective.get()); FunctionalInterface* const functional = statefunctional; functional->setModel(configuration->m0()); @@ -203,7 +203,6 @@ int gradient(const GmshFem& gmshFem) msg::unindent(); delete statefunctional; - delete objective; for (unsigned int f = 0; f < n_freq; f++) { delete pequation[f]; diff --git a/ip_comparison.cpp b/ip_comparison.cpp index 969785a..7b848e3 100644 --- a/ip_comparison.cpp +++ b/ip_comparison.cpp @@ -126,7 +126,7 @@ int ip_comparison(const GmshFem& gmshFem) ModelField m = laplace_filter(configuration->m0(),filter_scale,configuration,m_discret,integrationType); m.write(name+"_m_smooth"+suffix_g); - ObjectiveInterface<T_Physic>* const objective = newObjective<T_Physic>(d0g,configuration,gmshFem); + auto objective = newObjective<T_Physic>(d0g,configuration,gmshFem); std::vector<EquationInterface<T_Physic>*> pequation(n_freq); std::vector<double> subfrequency(n_freq); @@ -139,9 +139,9 @@ int ip_comparison(const GmshFem& gmshFem) pequation[f] = new ParametrizedEquation<T_Physic>(parametrization,f,2.*M_PI*subfrequency[f],configuration,w_discret,gmshFem,suffix_f); } - InnerProductInterface* const innerproduct = newInnerProduct(configuration,m_discret,gmshFem,suffix_g); + auto innerproduct = newInnerProduct(configuration,m_discret,gmshFem,suffix_g); RegularizationInterface* const regularization = newRegularization(configuration,m,gmshFem,suffix_g); - StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,innerproduct,regularization,subfrequency,pequation,objective); + StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,innerproduct.get(),regularization,subfrequency,pequation,objective.get()); statefunctional->setModel(m); double j = statefunctional->performance(); msg::print << "performance = " << j << msg::endl; @@ -206,12 +206,10 @@ int ip_comparison(const GmshFem& gmshFem) msg::unindent(); delete statefunctional; - delete objective; for (unsigned int f = 0; f < n_freq; f++) { delete pequation[f]; } - delete innerproduct; delete regularization; msg::unindent(); } diff --git a/multiscale.cpp b/multiscale.cpp index 8982bac..97763bc 100644 --- a/multiscale.cpp +++ b/multiscale.cpp @@ -70,7 +70,7 @@ int multiscale(const GmshFem& gmshFem) } const Data<T_Physic> d0(filename,frequency,configuration); - ObjectiveInterface<T_Physic>* const objective = newObjective<T_Physic>(d0,configuration,gmshFem); + auto objective = newObjective<T_Physic>(d0,configuration,gmshFem); std::vector<EquationInterface<T_Physic>*> pequation(n_freq); for (unsigned int f = 0; f < n_freq; f++) @@ -80,7 +80,7 @@ int multiscale(const GmshFem& gmshFem) wave::Discretization<T_Physic> w_discret(gmshFem,suffix_f); pequation[f] = new ParametrizedEquation<T_Physic>(parametrization,f,2.*M_PI*frequency[f],configuration,w_discret,gmshFem,suffix_f); } - StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,nullptr,nullptr,frequency,pequation,objective); + StateFunctional<T_Physic>* const statefunctional = new StateFunctional<T_Physic>(configuration,nullptr,nullptr,frequency,pequation,objective.get()); FunctionalInterface* const functional = statefunctional; msg::print << "Perform multiscale analysis" << msg::endl; @@ -124,7 +124,6 @@ int multiscale(const GmshFem& gmshFem) delete statefunctional; - delete objective; for (unsigned int f = 0; f < n_freq; f++) { delete pequation[f]; diff --git a/specific/data/objective/newObjective.h b/specific/data/objective/newObjective.h index caf7812..04fd773 100644 --- a/specific/data/objective/newObjective.h +++ b/specific/data/objective/newObjective.h @@ -3,6 +3,7 @@ //Standard Library #include <string> +#include <memory> //GmshFEM Library #include "Exception.h" @@ -12,16 +13,16 @@ #include "phase.h" template<Physic T_Physic> -ObjectiveInterface<T_Physic>* newObjective(const Data<T_Physic>& d0, const ConfigurationInterface* const config, const gmshfem::common::GmshFem& gmshFem,std::string suffix = "") +std::unique_ptr<ObjectiveInterface<T_Physic>> newObjective(const Data<T_Physic>& d0, const ConfigurationInterface* const config, const gmshfem::common::GmshFem& gmshFem,std::string suffix = "") { std::string objective; if(!gmshFem.userDefinedParameter(objective, "objective")) { throw gmshfem::common::Exception("Objective type could not be found."); } - if(objective=="l2distance"){return new l2distance::Objective<T_Physic>(d0,gmshFem);} - else if(objective=="conventional_phase"){return new conventional_phase::Objective<T_Physic>(d0,gmshFem);} - else if(objective=="logarithmic_phase"){return new logarithmic_phase::Objective<T_Physic>(d0,gmshFem);} + if(objective=="l2distance"){return std::make_unique< l2distance::Objective<T_Physic> >(d0,gmshFem);} + else if(objective=="conventional_phase"){return std::make_unique< conventional_phase::Objective<T_Physic> >(d0,gmshFem);} + else if(objective=="logarithmic_phase"){return std::make_unique< logarithmic_phase::Objective<T_Physic> >(d0,gmshFem);} else { throw gmshfem::common::Exception("Objective " + objective + " is not valid."); diff --git a/specific/model/innerproduct/newInnerProduct.cpp b/specific/model/innerproduct/newInnerProduct.cpp index 0e9195e..ad44c72 100644 --- a/specific/model/innerproduct/newInnerProduct.cpp +++ b/specific/model/innerproduct/newInnerProduct.cpp @@ -8,15 +8,15 @@ #include "sobolev.h" template<class M> -InnerProductInterface* newInnerProduct(const ConfigurationInterface* const config, const M& m_discret, const gmshfem::common::GmshFem& gmshFem, std::string suffix) +std::unique_ptr<InnerProductInterface> newInnerProduct(const ConfigurationInterface* const config, const M& m_discret, const gmshfem::common::GmshFem& gmshFem, std::string suffix) { std::string innerproduct; if(!gmshFem.userDefinedParameter(innerproduct, "innerproduct")) { throw gmshfem::common::Exception("Inner product type could not be found."); } - if(innerproduct=="sobolev_l2"){return new sobolev_l2::InnerProduct(config,m_discret,gmshFem, suffix);} - else if(innerproduct=="sobolev_df"){return new sobolev_df::InnerProduct(config,m_discret,gmshFem, suffix);} + if(innerproduct=="sobolev_l2"){return std::make_unique< sobolev_l2::InnerProduct>(config,m_discret,gmshFem, suffix);} + else if(innerproduct=="sobolev_df"){return std::make_unique< sobolev_df::InnerProduct>(config,m_discret,gmshFem, suffix);} else { throw gmshfem::common::Exception("Inner product " + innerproduct + " is not valid."); @@ -24,5 +24,5 @@ InnerProductInterface* newInnerProduct(const ConfigurationInterface* const confi } } -template InnerProductInterface* newInnerProduct<model::Discretization>(const ConfigurationInterface* const, const model::Discretization&, const gmshfem::common::GmshFem&, std::string); -template InnerProductInterface* newInnerProduct<ModelField>(const ConfigurationInterface* const, const ModelField&, const gmshfem::common::GmshFem&, std::string); +template std::unique_ptr<InnerProductInterface> newInnerProduct<model::Discretization>(const ConfigurationInterface* const, const model::Discretization&, const gmshfem::common::GmshFem&, std::string); +template std::unique_ptr<InnerProductInterface> newInnerProduct<ModelField>(const ConfigurationInterface* const, const ModelField&, const gmshfem::common::GmshFem&, std::string); diff --git a/specific/model/innerproduct/newInnerProduct.h b/specific/model/innerproduct/newInnerProduct.h index b56447a..4bfe837 100644 --- a/specific/model/innerproduct/newInnerProduct.h +++ b/specific/model/innerproduct/newInnerProduct.h @@ -2,12 +2,13 @@ #define H_SPECIFIC_MODEL_NEWINNERPRODUCT //Standard Library +#include <memory> //GmshFEM Library #include "GmshFem.h" //GmshFWI Library #include "../../../common/model/innerproduct/innerproduct.h" template<class M> -InnerProductInterface* newInnerProduct(const ConfigurationInterface* const config, const M& m_discret, const gmshfem::common::GmshFem& gmshFem, std::string suffix); +std::unique_ptr<InnerProductInterface> newInnerProduct(const ConfigurationInterface* const config, const M& m_discret, const gmshfem::common::GmshFem& gmshFem, std::string suffix); #endif //H_SPECIFIC_MODEL_NEWINNERPRODUCT diff --git a/specific/optimization/globalminimumsearch/sequential.cpp b/specific/optimization/globalminimumsearch/sequential.cpp index c7c1532..e208373 100644 --- a/specific/optimization/globalminimumsearch/sequential.cpp +++ b/specific/optimization/globalminimumsearch/sequential.cpp @@ -145,10 +145,10 @@ namespace sequential throw Exception("Data and equations do not have the same size."); } - ObjectiveInterface<T_Physic>* const objective = newObjective<T_Physic>(d0,_config,_gmshFem,_suffix); - InnerProductInterface* const innerproduct = newInnerProduct(_config,*m,_gmshFem,_suffix); + auto objective = newObjective<T_Physic>(d0,_config,_gmshFem,_suffix); + auto const innerproduct = newInnerProduct(_config,*m,_gmshFem,_suffix); RegularizationInterface* const regularization = newRegularization(_config,*m,_gmshFem,_suffix); - auto statefunctional = make_unique<StateFunctional<T_Physic>>(_config,innerproduct,regularization,d0.frequency(),equation,objective); + auto statefunctional = make_unique<StateFunctional<T_Physic>>(_config,innerproduct.get(),regularization,d0.frequency(),equation,objective.get()); statefunctional->setModel(*m); LocalMinimumSearchInterface* const minimumsearch = newLocalMinimumSearch(_gmshFem); @@ -208,9 +208,7 @@ namespace sequential delete descentsearch; delete linesearch; - delete innerproduct; delete regularization; - delete objective; msg::unindent(); } -- GitLab