GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
ElementFactory.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for FEM simulation.
2 //
3 // Author(s) : - Jean-luc CHARLES I2M-DuMAS/ENSAM Talence France
4 // <jean-luc.charles@ensam.eu>
5 // - Damien ANDRE SPCTS/ENS Ceramique industrielle, Limoges France
6 // <damien.andre@unilim.fr>
7 // - Jeremie GIRARDOT I2M-DuMAS/ENSAM Talence France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UVHC Valenciennes France
10 // <cedric.hubert@univ-valenciennes.fr>
11 // - Ivan IORDANOFF I2M-DuMAS-MPI/ENSAM Talence France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2016 JL. CHARLES, D. ANDRE, I. IORDANOFF, J. GIRARDOT
15 //
16 //
17 //
18 //
19 //
20 // This program is free software: you can redistribute it and/or modify
21 // it under the terms of the GNU General Public License as published by
22 // the Free Software Foundation, either version 3 of the License, or
23 // (at your option) any later version.
24 //
25 // This program is distributed in the hope that it will be useful,
26 // but WITHOUT ANY WARRANTY; without even the implied warranty of
27 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 // GNU General Public License for more details.
29 //
30 // You should have received a copy of the GNU General Public License
31 // along with this program. If not, see <http://www.gnu.org/licenses/>.
32 
33 #ifndef ELEMENTFACTORY_H
34 #define ELEMENTFACTORY_H
35 
36 #include <stdio.h>
37 #include <fstream>
38 #include <map>
39 #include "GranOO3/FEM/Element.hpp"
40 
41 // element declaration
42 #define DECLARE_ELEMENT(CLASS) \
43 public: \
44  static const bool IsRegistered; \
45  static CLASS *new_element(std::vector<unsigned int> connectivity) { return new CLASS(connectivity); };
46 
47 // element registration
48 #define RECORD_ELEMENT(CLASS) \
49  const bool CLASS::IsRegistered = ElementFactory::get().record<CLASS>();
50 
51 namespace GranOO3 {
52 
53  namespace FEM {
54 
56 
57  public:
59 
61 
62  static ElementFactory &get();
63 
69  template<typename T> bool record() {
70 
71  // assert the element being registered is not already registered
72  assert(_elements.find(T::element_name()) == _elements.end() && _elementsnode_number.find(T::element_name()) == _elementsnode_number.end() && "Cannot register an element class twice");
73 
74  // store a reference to the static constructor
75  // T::new_element is declared through the DECLARE_ELEMENT(CLASS) preprocessor macro above
76  _elements[T::element_name()] = &T::new_element;
77 
78  // store the number of nodes for this element
79  _elementsnode_number[T::element_name()] = T::node_number;
80 
81  // return true, for fake...
82  return true;
83  }
84 
87  Element *new_element(std::string elementName, std::ifstream &stream);
88 
91  Element *new_element(std::string elementName, const std::vector<unsigned int> &connectivity);
92 
93  private:
96 
98  std::map<std::string, std::function<Element *(const std::vector<unsigned int>)> > _elements;
99 
101  std::map<std::string, unsigned int> _elementsnode_number;
102  };
103  }
104 }
105 
106 #endif // ELEMENTFACTORY_H
Definition: ElementFactory.hpp:55
bool record()
Registers a new element.
Definition: ElementFactory.hpp:69
ElementFactory()
Definition: ElementFactory.cpp:45
static ElementFactory * _factory
The ElementFacrtory instance.
Definition: ElementFactory.hpp:95
std::map< std::string, unsigned int > _elementsnode_number
Map to access the number of nodes of the elements.
Definition: ElementFactory.hpp:101
Element * new_element(std::string elementName, std::ifstream &stream)
Creates a new element from its name and the stream being read.
Definition: ElementFactory.cpp:69
~ElementFactory()
Definition: ElementFactory.cpp:51
std::map< std::string, std::function< Element *(const std::vector< unsigned int >)> > _elements
Map to access elements static constructor method.
Definition: ElementFactory.hpp:98
static ElementFactory & get()
Definition: ElementFactory.cpp:57
Definition: Element.hpp:50
Definition: Common.hpp:198