Commit 8cf4fc8a authored by Bryan Berthou's avatar Bryan Berthou
Browse files

- Modify one dimension integration routine classes to match module

pattern for registry/factory mechanism.
parent 0ee8bfdd
class Integrator {
public:
Integrator();
virtual ~Integrator();
};
#ifndef INTEGRATOR_REGISTRY_H
#define INTEGRATOR_REGISTRY_H
/**
* @file IntegratorRegistry.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date June 29, 2016
* @version 1.0
*/
#include <SFML/System/Mutex.hpp>
#include <map>
#include <string>
#include "Integrator.h"
namespace NumA {
/**
* @class IntegratorRegistry
*
* @brief
*/
class IntegratorRegistry {
public:
/**
* Static function to be able to retrieve a unique instance pointer of this class anywhere in the code.
*
* @return a unique instance of this class
*/
static IntegratorRegistry* getInstance();
/**
* Default destructor
*/
virtual ~IntegratorRegistry();
unsigned int registerIntegrator(Integrator * pIntegrator);
/**
* Try to find stored object identified by its unique class identifier.
*
* @param classId
* @return reference to object in memory or throw an exception
*/
Integrator* get(unsigned int classId) const;
/**
* Try to find stored object identified by its class name.
*
* @param className
* @return reference to object in memory or throw an exception
*/
Integrator* get(const std::string &className) const;
private:
/**
* Because NumA++ can be use in a multi-threading program we must ensure that only one object manipulates the registry at the same time to avoid memory access violation.
* It's performed by a mechanism called mutex or semaphore.
*/
mutable sf::Mutex m_mutex;
/**
* Private pointer of this class for a unique instance
*/
static IntegratorRegistry* m_pInstance;
/**
* Private default constructor for a unique instance
*/
IntegratorRegistry();
std::map<unsigned int, Integrator*> m_baseObjectList; ///< list of registered objects identified by their unique integer identifier
std::map<std::string, Integrator*> m_translateList; ///< list of registered objects identified by their class name.
};
} // namespace NumA
#endif /* INTEGRATOR_REGISTRY_H */
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
*/ */
//#include <string> //#include <string>
#include <string>
#include "QuadratureIntegrator1D.h" #include "QuadratureIntegrator1D.h"
class FunctionType1D; class FunctionType1D;
...@@ -25,7 +27,18 @@ namespace NumA { ...@@ -25,7 +27,18 @@ namespace NumA {
class ChebyshevAIntegrator1D: public QuadratureIntegrator1D { class ChebyshevAIntegrator1D: public QuadratureIntegrator1D {
public: public:
ChebyshevAIntegrator1D(unsigned int N = 48); static const unsigned int classId; ///< Unique ID to automatically register the class in the registry.
/**
* Constructor.
* See BaseObject::BaseObject and ModuleObject::ModuleObject for more details.
*
* @param className name of child class.
*/
ChebyshevAIntegrator1D(const std::string &className);
//ChebyshevAIntegrator1D(unsigned int N = 48);
virtual ~ChebyshevAIntegrator1D(); virtual ~ChebyshevAIntegrator1D();
virtual ChebyshevAIntegrator1D* clone() const; virtual ChebyshevAIntegrator1D* clone() const;
...@@ -38,7 +51,7 @@ public: ...@@ -38,7 +51,7 @@ public:
protected: protected:
ChebyshevAIntegrator1D(const ChebyshevAIntegrator1D &other); ChebyshevAIntegrator1D(const ChebyshevAIntegrator1D &other);
void setNodesAndWeights(); virtual void setNodesAndWeights();
}; };
} /* namespace NumA */ } /* namespace NumA */
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
*/ */
//#include <string> //#include <string>
#include <string>
#include "QuadratureIntegrator1D.h" #include "QuadratureIntegrator1D.h"
class FunctionType1D; class FunctionType1D;
...@@ -25,7 +27,18 @@ namespace NumA { ...@@ -25,7 +27,18 @@ namespace NumA {
class ChebyshevBIntegrator1D: public QuadratureIntegrator1D { class ChebyshevBIntegrator1D: public QuadratureIntegrator1D {
public: public:
ChebyshevBIntegrator1D(unsigned int N = 48); static const unsigned int classId; ///< Unique ID to automatically register the class in the registry.
/**
* Constructor.
* See BaseObject::BaseObject and ModuleObject::ModuleObject for more details.
*
* @param className name of child class.
*/
ChebyshevBIntegrator1D(const std::string &className);
//ChebyshevBIntegrator1D(unsigned int N = 48);
virtual ~ChebyshevBIntegrator1D(); virtual ~ChebyshevBIntegrator1D();
virtual ChebyshevBIntegrator1D* clone() const; virtual ChebyshevBIntegrator1D* clone() const;
......
...@@ -10,14 +10,16 @@ ...@@ -10,14 +10,16 @@
* *
*/ */
#include <ElementaryUtils/parameters/Parameters.h>
#include <string>
#include <vector> #include <vector>
#include "Integrator1D.h" #include "Integrator1DModule.h"
class FunctionType1D;
namespace NumA { namespace NumA {
class FunctionType1D;
/** /**
* @class DExpIntegrator1D * @class DExpIntegrator1D
* @brief This is an implementation of the double exponential rule. * @brief This is an implementation of the double exponential rule.
...@@ -54,12 +56,17 @@ namespace NumA { ...@@ -54,12 +56,17 @@ namespace NumA {
* *
* See Integrator1D documentation for an example. * See Integrator1D documentation for an example.
*/ */
class DExpIntegrator1D: public Integrator1D { class DExpIntegrator1D: public Integrator1DModule {
public: public:
static const unsigned int classId; ///< Unique ID to automatically register the class in the registry.
/** /**
* Default constructor. * Constructor.
* See BaseObject::BaseObject and ModuleObject::ModuleObject for more details.
*
* @param className name of child class.
*/ */
DExpIntegrator1D(); DExpIntegrator1D(const std::string &className);
/** /**
* Default destructor. * Default destructor.
...@@ -68,6 +75,8 @@ public: ...@@ -68,6 +75,8 @@ public:
DExpIntegrator1D* clone() const; DExpIntegrator1D* clone() const;
virtual void configure(const ElemUtils::Parameters &parameters);
virtual double integrate(FunctionType1D* pFunction, double a, double b, virtual double integrate(FunctionType1D* pFunction, double a, double b,
std::vector<double> &parameters); std::vector<double> &parameters);
...@@ -79,6 +88,9 @@ protected: ...@@ -79,6 +88,9 @@ protected:
*/ */
DExpIntegrator1D(const DExpIntegrator1D &other); DExpIntegrator1D(const DExpIntegrator1D &other);
virtual void isModuleWellConfigured();
virtual void initModule();
private: private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
*/ */
//#include <string> //#include <string>
#include <string>
#include "QuadratureIntegrator1D.h" #include "QuadratureIntegrator1D.h"
namespace NumA { namespace NumA {
...@@ -26,11 +28,22 @@ namespace NumA { ...@@ -26,11 +28,22 @@ namespace NumA {
class GaussLegendreIntegrator1D: public QuadratureIntegrator1D { class GaussLegendreIntegrator1D: public QuadratureIntegrator1D {
public: public:
static const unsigned int classId; ///< Unique ID to automatically register the class in the registry.
/** /**
* Constructor. * Constructor.
* @param N Order of the quadrature (number of nodes). * See BaseObject::BaseObject and ModuleObject::ModuleObject for more details.
*
* @param className name of child class.
*/ */
GaussLegendreIntegrator1D(unsigned int N = 50); // TODO how many steps by default ? GaussLegendreIntegrator1D(const std::string &className);
// /**
// * Constructor.
// * @param N Order of the quadrature (number of nodes).
// */
// GaussLegendreIntegrator1D(unsigned int N = 50); // TODO how many steps by default ?
/** /**
* Default destructor. * Default destructor.
*/ */
...@@ -47,7 +60,7 @@ protected: ...@@ -47,7 +60,7 @@ protected:
* @param other GaussLegendreIntegrator1D object to copy. * @param other GaussLegendreIntegrator1D object to copy.
*/ */
GaussLegendreIntegrator1D(const GaussLegendreIntegrator1D &other); GaussLegendreIntegrator1D(const GaussLegendreIntegrator1D &other);
void setNodesAndWeights(); ///< Computes the nodes and weights of the Gauss-Legendre quadrature. Called when N is set. virtual void setNodesAndWeights(); ///< Computes the nodes and weights of the Gauss-Legendre quadrature. Called when N is set.
}; };
} /* namespace NumA */ } /* namespace NumA */
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* @version 1.0 * @version 1.0
*/ */
//#include <string> #include <string>
#include "QuadratureIntegrator1D.h" #include "QuadratureIntegrator1D.h"
namespace NumA { namespace NumA {
...@@ -23,11 +24,21 @@ namespace NumA { ...@@ -23,11 +24,21 @@ namespace NumA {
class GaussLegendreSeStIntegrator1D: public QuadratureIntegrator1D { class GaussLegendreSeStIntegrator1D: public QuadratureIntegrator1D {
public: public:
static const unsigned int classId; ///< Unique ID to automatically register the class in the registry.
/** /**
* Constructor. * Constructor.
* @param N Order of the quadrature (number of nodes). * See BaseObject::BaseObject and ModuleObject::ModuleObject for more details.
*
* @param className name of child class.
*/ */
GaussLegendreSeStIntegrator1D(unsigned int N = 50); // TODO how many steps by default ? GaussLegendreSeStIntegrator1D(const std::string &className);
// /**
// * Constructor.
// * @param N Order of the quadrature (number of nodes).
// */
// GaussLegendreSeStIntegrator1D(unsigned int N = 50); // TODO how many steps by default ?
/** /**
* Default destructor. * Default destructor.
*/ */
......
#ifndef INTEGRATOR_1D_H #ifndef INTEGRATOR_1D_MODULE_H
#define INTEGRATOR_1D_H #define INTEGRATOR_1D_MODULE_H
/** /**
* @file Integrator1D.h * @file Integrator1D.h
* @author Bryan BERTHOU (SPhN / CEA Saclay) * @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date February 15, 2016 * @date August 03, 2018
* @version 1.0 * @version 1.0
*/ */
#include <ElementaryUtils/object_factory/BaseObjectData.h>
#include <ElementaryUtils/object_factory/ModuleObject.h>
#include <ElementaryUtils/parameters/Parameters.h>
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include "../../functor/one_dimension/Functor1D.h"
#include "../../utils/Errors.h" #include "../../utils/Errors.h"
#include "../../utils/Tolerances.h" #include "../../utils/Tolerances.h"
#include "IntegratorType1D.h"
namespace ElemUtils {
class Parameters;
} /* namespace ElemUtils */
namespace NumA { namespace NumA {
class FunctionType1D;
/** /**
* @class Integrator1D * @class Integrator1DModule
*
* @brief Abstract class for all integration routines.
* *
* Example: * @brief
* ```cpp
* NumA::Integrator1D* integrator = NumA::Integrator1D::newIntegrator(NumA::IntegratorType1D::GaussLegendre); // Using Gauss-Legendre quadrature.
* NumA::FunctionType1D* functor = NumA::FunctorUtils::newFunctor1D(pointerMyClass, &MyClass::myFunction); // See FunctionType1D documentation.
* double integral = integrator.integrate(functor, -1., 1.); // We integrate a function on [-1,1].
* ```
*/ */
class Integrator1D { class Integrator1DModule: public ElemUtils::ModuleObject {
public: public:
static const std::string INTEGRATOR_1D_MODULE_CLASS_NAME; ///< Type of the module in XML automation.
static const std::string PARAM_NAME_ABSOLUTE_TOLERANCE; ///< Parameter used in the configure() to set the absolute tolerance. static const std::string PARAM_NAME_ABSOLUTE_TOLERANCE; ///< Parameter used in the configure() to set the absolute tolerance.
static const std::string PARAM_NAME_RELATIVE_TOLERANCE; ///< Parameter used in the configure() to set the relative tolerance. static const std::string PARAM_NAME_RELATIVE_TOLERANCE; ///< Parameter used in the configure() to set the relative tolerance.
/** /**
* Default constructor. * Constructor.
* See BaseObject::BaseObject and ModuleObject::ModuleObject for more details.
*
* @param className name of child class.
*/ */
Integrator1D(); Integrator1DModule(const std::string &className);
/** /**
* Default destructor. * Default destructor.
*/ */
virtual ~Integrator1D(); virtual ~Integrator1DModule();
virtual Integrator1D* clone() const = 0; virtual Integrator1DModule* clone() const = 0;
/**
* Integration routine.
* @param pFunction Functor representing the one-dimensional function to integrate.
* @param a Lower bound.
* @param b Upper bound.
* @param parameters Parameters that can be passed to the function.
* @return Integral.
*/
virtual double integrate(FunctionType1D* pFunction, double a, double b,
std::vector<double> &parameters) = 0;
/** /**
* Provides a generic method to configure all types of integrations by passing a Parameters object. * Provides a generic method to configure all types of integrations by passing a Parameters object.
...@@ -71,26 +57,19 @@ public: ...@@ -71,26 +57,19 @@ public:
*/ */
virtual void configure(const ElemUtils::Parameters &parameters); virtual void configure(const ElemUtils::Parameters &parameters);
//TODO moved to FunctorUtils ; remove this definition virtual void prepareSubModules(
/** const std::map<std::string, ElemUtils::BaseObjectData>& subModulesData);
* Use FunctorUtils::newFunctor1D instead.
*
* TODO: Remove this method.
*/
template<typename PointerToObj, typename PointerToMemFn>
static Functor1D<PointerToObj, PointerToMemFn>* newIntegrationFunctor(
PointerToObj* object, PointerToMemFn function) {
return new Functor1D<PointerToObj, PointerToMemFn>(object, function);
}
/** /**
* Instantiates an Integrator object. * Integration routine.
* Will be replaced by a Factory/Registry system in the future. * @param pFunction Functor representing the one-dimensional function to integrate.
* @param oneDimIntegratorType Type of integrator. * @param a Lower bound.
* @return Integrator1D pointer. * @param b Upper bound.
* @param parameters Parameters that can be passed to the function.
* @return Integral.
*/ */
static Integrator1D* newIntegrator( virtual double integrate(FunctionType1D* pFunction, double a, double b,
const IntegratorType1D::Type &oneDimIntegratorType); std::vector<double> &parameters) = 0;
// ##### GETTERS & SETTERS ##### // ##### GETTERS & SETTERS #####
...@@ -116,17 +95,15 @@ public: ...@@ -116,17 +95,15 @@ public:
void setErrors(const Errors& errors); void setErrors(const Errors& errors);
protected: protected:
Tolerances m_tolerances; ///< Absolute and relative tolerances.
Errors m_errors; ///< Absolute and relative errors estimations.
/** /**
* Copy constructor. * Copy constructor
* Called by clone().
* @param other Integrator1D object to copy.
*/ */
Integrator1D(const Integrator1D &other); Integrator1DModule(const Integrator1DModule &other);
Tolerances m_tolerances; ///< Absolute and relative tolerances.
Errors m_errors; ///< Absolute and relative errors estimations.
}; };
} // namespace NumA } /* namespace NumA */
#endif /* INTEGRATOR_1D_H */ #endif /* INTEGRATOR_1D_MODULE_H */
#ifndef INTEGRATOR_TYPE_1D_H
#define INTEGRATOR_TYPE_1D_H
/**
* @file IntegratorType1D.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date 17 February 2016
* @version 1.0
*/
#include <string>
namespace NumA {
/**
* @class IntegratorType1D
*
* @brief Type of one-dimensional integrations (wrapper for enum).
*
* See Integrator1D documentation for an example.
*/
class IntegratorType1D {
public:
/**
* Type of integration.
*/
enum Type {
UNDEFINED = 0, //!< Undefined type.
TRAPEZOIDAL = 1, //!< Trapezoidal method with a fixed number of equidistant steps.
TRAPEZOIDALLOG = 2, //!< Trapezoidal method with a fixed number with equidistantly distributed steps in [ln(min), ln(max)] range.
GL = 3, //!< Gauss-Legendre method with a fixed number of steps.
DEXP = 4, //!< Adaptive double exponential method.
GK21_ADAPTIVE = 5 //!< Adaptive Gauss-Kronrod method with 21 point rules.
};
/**
* Default constructor.
*/
IntegratorType1D();
/**
* Assignment constructor.
* @param type Type to be assigned.
*/
IntegratorType1D(Type type);
/**
* Assignment constructor trying to match integration type from given string. If unable to match set IntegratorType1D::UNDEFINED.
* @param integratorType1DString String to be matched.
*/
IntegratorType1D(const std::string& integratorType1DString);