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 @@
*/
//#include <string>
#include <string>
#include "QuadratureIntegrator1D.h"
class FunctionType1D;
......@@ -25,7 +27,18 @@ namespace NumA {
class ChebyshevAIntegrator1D: public QuadratureIntegrator1D {
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* clone() const;
......@@ -38,7 +51,7 @@ public:
protected:
ChebyshevAIntegrator1D(const ChebyshevAIntegrator1D &other);
void setNodesAndWeights();
virtual void setNodesAndWeights();
};
} /* namespace NumA */
......
......@@ -9,6 +9,8 @@
*/
//#include <string>
#include <string>
#include "QuadratureIntegrator1D.h"
class FunctionType1D;
......@@ -25,7 +27,18 @@ namespace NumA {
class ChebyshevBIntegrator1D: public QuadratureIntegrator1D {
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* clone() const;
......
......@@ -10,14 +10,16 @@
*
*/
#include <ElementaryUtils/parameters/Parameters.h>
#include <string>
#include <vector>
#include "Integrator1D.h"
class FunctionType1D;
#include "Integrator1DModule.h"
namespace NumA {
class FunctionType1D;
/**
* @class DExpIntegrator1D
* @brief This is an implementation of the double exponential rule.
......@@ -54,12 +56,17 @@ namespace NumA {
*
* See Integrator1D documentation for an example.
*/
class DExpIntegrator1D: public Integrator1D {
class DExpIntegrator1D: public Integrator1DModule {
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.
......@@ -68,6 +75,8 @@ public:
DExpIntegrator1D* clone() const;
virtual void configure(const ElemUtils::Parameters &parameters);
virtual double integrate(FunctionType1D* pFunction, double a, double b,
std::vector<double> &parameters);
......@@ -79,6 +88,9 @@ protected:
*/
DExpIntegrator1D(const DExpIntegrator1D &other);
virtual void isModuleWellConfigured();
virtual void initModule();
private:
////////////////////////////////////////////////////////////
......
......@@ -12,6 +12,8 @@
*/
//#include <string>
#include <string>
#include "QuadratureIntegrator1D.h"
namespace NumA {
......@@ -26,11 +28,22 @@ namespace NumA {
class GaussLegendreIntegrator1D: public QuadratureIntegrator1D {
public:
static const unsigned int classId; ///< Unique ID to automatically register the class in the registry.
/**
* 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.
*/
......@@ -47,7 +60,7 @@ protected:
* @param other GaussLegendreIntegrator1D object to copy.
*/
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 */
......
......@@ -8,7 +8,8 @@
* @version 1.0
*/
//#include <string>
#include <string>
#include "QuadratureIntegrator1D.h"
namespace NumA {
......@@ -23,11 +24,21 @@ namespace NumA {
class GaussLegendreSeStIntegrator1D: public QuadratureIntegrator1D {
public:
static const unsigned int classId; ///< Unique ID to automatically register the class in the registry.
/**
* 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.
*/
......
#ifndef INTEGRATOR_1D_H
#define INTEGRATOR_1D_H
#ifndef INTEGRATOR_1D_MODULE_H
#define INTEGRATOR_1D_MODULE_H
/**
* @file Integrator1D.h
* @author Bryan BERTHOU (SPhN / CEA Saclay)
* @date February 15, 2016
* @date August 03, 2018
* @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 <vector>
#include "../../functor/one_dimension/Functor1D.h"
#include "../../utils/Errors.h"
#include "../../utils/Tolerances.h"
#include "IntegratorType1D.h"
namespace ElemUtils {
class Parameters;
} /* namespace ElemUtils */
namespace NumA {
class FunctionType1D;
/**
* @class Integrator1D
*
* @brief Abstract class for all integration routines.
* @class Integrator1DModule
*
* Example:
* ```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].
* ```
* @brief
*/
class Integrator1D {
class Integrator1DModule: public ElemUtils::ModuleObject {
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_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.
*/
virtual ~Integrator1D();
virtual ~Integrator1DModule();
virtual Integrator1D* 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;
virtual Integrator1DModule* clone() const = 0;
/**
* Provides a generic method to configure all types of integrations by passing a Parameters object.
......@@ -71,26 +57,19 @@ public:
*/
virtual void configure(const ElemUtils::Parameters &parameters);
//TODO moved to FunctorUtils ; remove this definition
/**
* 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);
}
virtual void prepareSubModules(
const std::map<std::string, ElemUtils::BaseObjectData>& subModulesData);
/**
* Instantiates an Integrator object.
* Will be replaced by a Factory/Registry system in the future.
* @param oneDimIntegratorType Type of integrator.
* @return Integrator1D pointer.
* 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.
*/
static Integrator1D* newIntegrator(
const IntegratorType1D::Type &oneDimIntegratorType);
virtual double integrate(FunctionType1D* pFunction, double a, double b,
std::vector<double> &parameters) = 0;
// ##### GETTERS & SETTERS #####
......@@ -116,17 +95,15 @@ public:
void setErrors(const Errors& errors);
protected:
Tolerances m_tolerances; ///< Absolute and relative tolerances.
Errors m_errors; ///< Absolute and relative errors estimations.
/**
* Copy constructor.
* Called by clone().
* @param other Integrator1D object to copy.
* Copy constructor
*/
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);
/**
* Default destructor.
*/
virtual ~IntegratorType1D();
/**
* Operator that casts back into an enum.
*/
operator Type() const;
/**
* Returns a formatted string displaying the type.
* @return std::string
*/
std::string toString() const;
/**
* Returns a short name for the type.
* @return std::string
*/
std::string getShortName();
/**
* Returns the wrapped enum.
* @return IntegratorType1D::Type
*/
IntegratorType1D::Type getType() const;
/**
* Set the type of integration.
* @param type Integration type to be set.
*/
void setType(Type type);
private:
IntegratorType1D::Type m_type; ///< Type of integration (enum).
};
} /* namespace NumA */
#endif /* INTEGRATOR_TYPE_1D_H */
......@@ -8,10 +8,15 @@
* @version 1.0
*/
#include <ElementaryUtils/parameters/Parameters.h>
#include <string>
#include <vector>
#include "Integrator1D.h"
#include "Integrator1DModule.h"
namespace NumA {
class FunctionType1D;
} /* namespace NumA */
class FunctionType1D;
......@@ -29,15 +34,24 @@ namespace NumA {
* See Integrator1D documentation for an example.
*/
class QuadratureIntegrator1D: public Integrator1D {
class QuadratureIntegrator1D: public Integrator1DModule {
public:
static const std::string PARAM_NAME_N; ///< Parameter used in the configure() to set the order of the quadrature.
/**
* 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.
*/
QuadratureIntegrator1D(unsigned int N = 0);
QuadratureIntegrator1D(const std::string &className);
// /**
// * Constructor.
// * @param N Order of the quadrature (number of nodes).
// */
// QuadratureIntegrator1D(unsigned int N = 0);
/**
* Default destructor.
*/
......@@ -82,6 +96,11 @@ protected:
* @param other QuadratureIntegrator1D object to copy.
*/
QuadratureIntegrator1D(const QuadratureIntegrator1D &other);
virtual void setNodesAndWeights() = 0;
virtual void isModuleWellConfigured();
virtual void initModule();
};
} // namespace NumA
......