Add A New Element
To add a new Element into the interpreted applications, the developer must:
- provide a new C++ subclass of the Element class
- provide an interface function that will be used to parse the input and create the new element.
Element Class
The Element class itself is an abstract base class. It inherits from both the DomainComponent class, which is itself a subclass of TaggedObject class and the MovableObject class. The class has a large number of methods defined in the interface, not all these methods need to be included in a new Element class. The following is the minimal interface that should be considered:
The Element Class:
class Element : public DomainComponent
{
public:
Element(int tag, int classTag);
virtual ~Element();
// methods dealing with nodes and number of external dof
virtual int getNumExternalNodes(void) const =0;
virtual const ID &getExternalNodes(void) =0;
virtual Node **getNodePtrs(void) =0;
virtual int getNumDOF(void) =0;
// methods dealing with committed state and update
virtual int commitState(void);
virtual int revertToLastCommit(void) = 0;
virtual int revertToStart(void);
virtual int update(void);
virtual bool isSubdomain(void);
// methods dealing with element stiffness
virtual const Matrix &getTangentStiff(void) =0;
virtual const Matrix &getInitialStiff(void) =0;
// methods dealing with element forces
virtual void zeroLoad(void);
virtual int addLoad(ElementalLoad *theLoad, double loadFactor);
virtual const Vector &getResistingForce(void) =0;
// public methods for output
int sendSelf(int commitTag, Channel &theChannel);
int recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker);
void Print(OPS_Stream &s, int flag =0);
// method for obtaining information specific to an element
virtual Response *setResponse(const char **argv, int argc, OPS_Stream &theHandler);
virtual int getResponse(int responseID, Information &eleInformation);
}
Example - Truss2D
In the following section we will provide all necessary code to add a new 2d planar truss element into an OpenSees interpreter. To demonstrate the power of object-oriented programming, the stress-strain relationship will be provided by a UniaxialMaterial object.
Header
The header for thew new class, which we will call Truss2D is as follows:
// include directives
#include <Element.h>
#include <Matrix.h>
#include <Vector.h>
// forward declarations
class Node;
class UniaxialMaterial;
class Truss2D : public Element
{
public:
// constructors
Truss2D(int tag,
int Nd1, int Nd2,
UniaxialMaterial &theMaterial,
double A);
Truss2D();
// destructor
~Truss2D();
// public methods to obtain inforrmation about dof & connectivity
int getNumExternalNodes(void) const;
const ID &getExternalNodes(void);
Node **getNodePtrs(void);
int getNumDOF(void);
void setDomain(Domain *theDomain);
// public methods to set the state of the element
int commitState(void);
int revertToLastCommit(void);
int revertToStart(void);
int update(void);
// public methods to obtain stiffness, mass, damping and residual information
const Matrix &getTangentStiff(void);
const Matrix &getInitialStiff(void);
const Vector &getResistingForce(void);
// public methods for output
int sendSelf(int commitTag, Channel &theChannel);
int recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker);
void Print(OPS_Stream &s, int flag =0);
// method for obtaining information specific to an element
Response *setResponse(const char **argv, int argc, OPS_Stream &s);
int getResponse(int responseID, Information &eleInformation);
protected:
private:
// private member functions - only available to objects of the class
double computeCurrentStrain(void) const;
// private attributes - a copy for each object of the class
UniaxialMaterial *theMaterial; // pointer to a material
ID externalNodes; // contains the id's of end nodes
Matrix trans; // hold the transformation matrix
double L; // length of truss (undeformed configuration)
double A; // area of truss
Node *theNodes[2]; // node pointers
// static data - single copy for all objects of the class
static Matrix trussK; // class wide matrix for returning stiffness
static Vector trussR; // class wide vector for returning residual
};
#endif
The header file defines the interface and variables for the class Truss2D. It defines the new class to be a sublass of the Element class. In the public interface are 2 constructors and 1 destructor in addition to minimal set of methods we showed for the Element class. There are no protected data or methods as we do not expect this class to be subclassed. In the private section we define one private method, computeCurrentStrain(), we define a number of private variables, and we also define a number of static variables.
The header has a number of #include directives, one is needed for the base class and every class used as a variable in the list of data (except those that are used as pointers). For those classes that only appear as pointers in the header file (Node, UniaxialMaterial) a forward declaration is all that is needed (the include could also have been used, but using the forward declaration simplifies dependencies and reduces the amount of code that ha to be recompiled later if changes are made).
Implementation
It another file, Truss2D.cpp, we place the code that details what the constructors, destructor and methods do. In addition we provide one additional procedure OPS_Truss2D() (NOTE it has the same name as the class with an OPS_ prefix). We will go through each part of the file.
Include Directives
The first part of the file contains the list of includes. It is necessary to have an #include directive for each class and api file that is used within the .cpp file and is not included in the header.
#include "Truss2D.h"
#include <elementAPI.h>
#include <G3Globals.h>
#include <Matrix.h>
#include <Vector.h>
#include <Information.h>
#include <Domain.h>
#include <Node.h>
#include <Channel.h>
#include <Message.h>
#include <FEM_ObjectBroker.h>
#include <UniaxialMaterial.h>
#include <Renderer.h>
#include <ElementResponse.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
Static Variables
Next, we initialize the static variables. For this example we are using 2 static-variables (objects shared by each Truss2D object that is created), one to return the tangent matrix and the other to return the resisting force.
// initialise the class wide variables
Matrix Truss2D::trussK(4,4);
Vector Truss2D::trussR(4);
Constructors
After the list of includes, we provide the 2 constructors. The constructors are rather simple. They just initialize all the data variables defined in the header. Note it is very important to set all pointer values to 0.
The first constructor is the one most typically used. The arguments provide the elements tag, the tags of the two end nodes, the element's area and a copy of the element's material. The code in the constructor does the following:
- The elements tag and a 0 are passed to the Element constructor.
- The matreial pointer, theMaterial, is set to a copy of the material obtained from the material that is passed in the arguments.
- The externalNodes array is set to be an array of size 2 and it's values are set to the nodal tags of the 2 nodes.
- The theNodes array components are set to be 0.
TrussCPP::TrussCPP(int tag,
int Nd1, int Nd2,
UniaxialMaterial &theMat,
double a)
:Element(tag, 0),
externalNodes(2),
trans(1,4), L(0.0), A(a)
{
// get a copy of the material object for our own use
theMaterial = theMat.getCopy();
if (theMaterial == 0) {
opserr << "FATAL TrussCPP::TrussCPP() - out of memory, could not get a copy of the\
Material\n";
exit(-1);
}
// fill in the ID containing external node info with node id's
if (externalNodes.Size() != 2) {
opserr << "FATAL TrussCPP::TrussCPP() - out of memory, could not create an ID of \
size 2\n";
exit(-1);
}
externalNodes(0) = Nd1;
externalNodes(1) = Nd2;
theNodes[0] = 0;
theNodes[1] = 0;
}
The second constructor is called when paralell processing or the database feature of the OpenSees application is used. It's pupose is to create a blank Truss2D object, that will be filled in when the recvSelf() method is invoked on the object.
TrussCPP::TrussCPP()
:Element(0, 0),
theMaterial(0),
externalNodes(2),
trans(1,4), L(0.0), A(0.0)
{
theNodes[0] = 0;
theNodes[1] = 0;
}