Skip to content
Snippets Groups Projects
Commit 1ac41de9 authored by Nicolas Marsic's avatar Nicolas Marsic
Browse files

Edge and Node Basis (Tri and Quad)

parent f8674289
No related branches found
No related tags found
No related merge requests found
#ifndef _TRINEDELECBASIS_H_
#define _TRINEDELECBASIS_H_
#include "BasisVector.h"
/**
@class TriNedelecBasis
@brief Nedelec Basis for Triangles
This class can instantiate a Nedelec Basis
for Triangles.@n
*/
class TriNedelecBasis: public BasisVector{
public:
TriNedelecBasis(void);
virtual ~TriNedelecBasis(void);
};
/**
@fn TriNedelecBasis::TriNedelecBasis
@return Returns a new Nedelec Basis for Triangles
@fn TriNedelecBasis::~TriNedelecBasis(void)
@return Deletes this Basis
*/
#endif
#include "TriNodeBasis.h"
#include "Legendre.h"
TriNodeBasis::TriNodeBasis(const int order){
// Set Basis Type //
this->order = order;
type = 0;
size = (order + 1) * (order + 2) / 2;
nodeNbr = 3;
dim = 2;
// Alloc Temporary Space //
Polynomial* legendre = new Polynomial[order];
Polynomial* intLegendre = new Polynomial[order];
Polynomial* lagrangeSub = new Polynomial[3];
Polynomial* lagrangeSum = new Polynomial[3];
// Classical and Intrated-Scaled Legendre Polynomial //
const int orderMinus = order - 1;
Legendre::legendre(legendre, orderMinus);
Legendre::intScaled(intLegendre, order);
// Basis //
basis = new Polynomial[size];
// Vertex Based (Lagrange) //
basis[0] =
Polynomial(1, 0, 0, 0) - Polynomial(1, 1, 0, 0) - Polynomial(1, 0, 1, 0);
basis[1] =
Polynomial(1, 1, 0, 0);
basis[2] =
Polynomial(1, 0, 1, 0);
// Lagrange Sum //
for(int i = 0, j = 1; i < 3; i++, j = (j + 1) % 3)
lagrangeSum[i] = basis[i] + basis[j];
// Lagrange Sub //
for(int i = 0, j = 1; i < 3; i++, j = (j + 1) % 3)
lagrangeSub[i] = basis[j] - basis[i];
// Edge Based //
int i = 3;
for(int l = 1; l < order; l++){
for(int e = 0; e < 3; e++){
basis[i] =
intLegendre[l].compose(lagrangeSub[e], lagrangeSum[e]);
i++;
}
}
// Cell Based //
Polynomial p = basis[2] * 2 - Polynomial(1, 0, 0, 0);
const int orderMinusTwo = order - 2;
for(int l1 = 1; l1 < orderMinus; l1++){
for(int l2 = 0; l2 + l1 - 1 < orderMinusTwo; l2++){
basis[i] =
intLegendre[l1].compose(lagrangeSub[0], lagrangeSum[0]) *
legendre[l2].compose(p) * basis[2];
i++;
}
}
// Free Temporary Sapce //
delete[] legendre;
delete[] intLegendre;
delete[] lagrangeSub;
delete[] lagrangeSum;
}
TriNodeBasis::~TriNodeBasis(void){
delete[] basis;
}
/*
#include <cstdio>
int main(void){
const int P = 5;
const double d = 0.01;
TriNodeBasis b(P);
const Polynomial* basis = b.getBasis();
printf("\n");
printf("clear all;\n");
printf("\n");
printf("\n");
printf("Order = %d\n", b.getOrder());
printf("Type = %d\n", b.getType());
printf("Size = %d\n", b.getSize());
printf("NodeNumber = %d\n", b.getNodeNbr());
printf("Dimension = %d\n", b.getDim());
printf("\n");
printf("function r = p(i, x, y)\n");
printf("p = zeros(%d, 1);\n", b.getSize());
printf("\n");
for(int i = 0; i < b.getSize(); i++)
printf("p(%d) = %s;\n", i + 1, basis[i].toString().c_str());
printf("\n");
printf("r = p(i, 1);\n");
printf("end\n");
printf("\n");
printf("d = %lf;\nx = [0:d:1];\ny = x;\n\nlx = length(x);\nly = length(y);\n\n", d);
for(int i = 0; i < b.getSize(); i++)
printf("p%d = zeros(lx, ly);\n", i + 1);
printf("\n");
printf("for i = 1:lx\n");
printf("for j = 1:ly - i + 1\n");
printf("\n");
for(int i = 0; i < b.getSize(); i++)
printf("p%d(j, i) = p(%d, x(i), y(j));\n", i + 1, i + 1, i + 1);
printf("end\n");
printf("end\n");
printf("\n");
printf("SizeOfBasis = %lu\n", sizeof(b) + sizeof(basis) * b.getSize());
printf("\n");
printf("\n");
for(int i = 0; i < b.getSize(); i++)
printf("figure;\ncontourf(x, y, p%d);\ncolorbar;\n", i + 1, i + 1);
printf("\n");
return 0;
}
*/
#ifndef _TRINODEBASIS_H_
#define _TRINODEBASIS_H_
#include "BasisScalar.h"
/**
@class TriNodeBasis
@brief A Node-Basis for Triangles
This class can instantiate a Node-Based Basis
(high or low order) for Triangles.@n
It uses
<a href="http://www.hpfem.jku.at/publications/szthesis.pdf">Zaglmayr's</a>
Basis for @em high @em order Polynomial%s generation.@n
*/
class TriNodeBasis: public BasisScalar{
public:
TriNodeBasis(const int order);
virtual ~TriNodeBasis(void);
};
/**
@fn TriNodeBasis::TriNodeBasis(const int order)
@param order The order of the Basis
@return Returns a new Node-Basis for Triangles of the given order
@fn TriNodeBasis::~TriNodeBasis(void)
@return Deletes this Basis
*/
#endif
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <string>
extern "C"{
#include <cblas.h>
}
#include "Exception.h"
/**
@class Vector
@brief Handles vectors
This class represents a vector of type < T >
and of dimention @c n.
@todo
Use Inheritance instead of template@n
|@n
|@n
---> We have methods that changes between types
(e.g: at(...), 3D automatic allocation, scalar mult)
*/
class Solver;
template<class T>
class Vector{
private:
int N;
T* v;
friend class Solver;
friend class Matrix;
public:
Vector(const int a);
Vector(void);
~Vector(void);
int dim(void) const;
T& operator()(const int i);
T operator()(const int i) const;
void operator=(const Vector<T>& other);
T get(const int i) const;
void set(const int i, const T a);
Vector<double> at(const double x, const double y, const double z) const;
Vector<T> operator+(const Vector<T>& other);
Vector<T> operator-(const Vector<T>& other);
Vector<T> operator*(const T& other);
//Vector<T> operator*(const double alpha);
void add(const Vector<T>& b);
void sub(const Vector<T>& b);
void mul(const T& other);
//void mul(const double alpha);
double dot(const Vector<T>& v) const;
void allToZero(void);
std::string toString(void) const;
};
/**
@fn Vector::Vector(int)
@param a Size of the futur Vector
@return Returns a new Vector of size @c a
@fn Vector::Vector(void)
@return Returns a new Vector of size @em @c 3
@fn Vector::~Vector
@return Deletes this Vector
@fn Vector::dim
@return Returns the @em dimention of this vector
@fn T& Vector::operator()(const int)
@param i An index of this Vector
@return Returns a @em reference to
the element at position @c i
@fn T Vector::operator()(const int) const
@param i An index of this Vector
@return Returns the @em value of
the element at position @c i
@fn Vector::get
@param i An index in this Vector
@return Returns the @em value of the element
at position @c i
@fn Vector::set
@param i An index in this Vector
@param a A value
@return Sets the given value at position @c i
in this Vector
@fn Vector::at
@param x The @em first dimesion of a @c 3D Polynomial
@param y The @em second dimesion of a @c 3D Polynomial
@param z The @em third dimesion of a @c 3D Polynomial
@return Retuns the @c 3D Vector resulting of the @em evaluation
of this @em @c 3D Vector @em of @em Polynomial%s
@warning
This works only for @em @c 3D Vector @em of @em Polynomial%s
@fn Vector<T> Vector::operator+(const Vector<T>&)
@param other An other Vector
@return Returns the Vector containing
the @em sum of this Vector with the other
@fn Vector<T> Vector::operator-(const Vector<T>&)
@param other An other Vector
@return Returns the Vector containing
the @em substraction of this Vector with the other
@fn Vector<T> Vector::operator*(const T&)
@param other A value
@return Returns the Vector containing
the @em product (@em element @em by @em element)
of this Vector with the given value
@fn Vector::add
@param b An other Vector
@return Adds the given Vector with this one
@note
The result is stored in this Vector
@fn Vector::sub
@param b An other Vector
@return Substracts the given Vector with this one
@note
The result is stored in this Vector
@fn Vector::mul
@param other A value
@return Multiplies all the elements of this Vector
with the given value
@note
The result is stored in this Vector
@fn Vector::dot
@param v An Vector
@return Returns the @em dot @em product of this
Vector with the given one
@fn Vector::allToZero
@return Sets all the elements of this Vector
to the @em zero @em element
@fn Vector::toString
@return Returns a string representing this Vector
*/
//////////////////////////////////////////////////////////////////////
// Templated Implementations //
//////////////////////////////////////////////////////////////////////
template<class T>
Vector<T>::Vector(const int a){
if(!a)
throw Exception("Vector must by of dimension bigger than 0");
N = a;
v = new T[N];
}
template<class T>
Vector<T>::Vector(void){
N = 3;
v = new T[N];
}
template<class T>
Vector<T>::~Vector(void){
delete[] v;
}
//////////////////////////////////////////////////////////////////////
// Inline Templated Implementations //
//////////////////////////////////////////////////////////////////////
template<class T>
inline int Vector<T>::dim(void) const{
return N;
}
template<class T>
inline T& Vector<T>::operator()(const int i){
return v[i];
}
template<class T>
inline T Vector<T>::operator()(const int i) const{
return v[i];
}
template<class T>
void Vector<T>::operator=(const Vector<T>& other){
if(N != other.N)
throw Exception("Vectors must be of the same dimension");
for(int i = 0; i < N; i++)
v[i] = other.v[i];
}
template<class T>
inline T Vector<T>::get(const int i) const{
return v[i];
}
template<class T>
inline void Vector<T>::set(const int i, const T a){
v[i] = a;
}
//////////////////////////////////////////////////////////////////////
// Inline Vector<double> Implementations //
//////////////////////////////////////////////////////////////////////
template<>
inline double Vector<double>::dot(const Vector<double>& v) const{
return cblas_ddot(N, (*this).v, 1, v.v, 1);
}
#endif
#include <sstream>
#include "Vector.h"
using namespace std;
template<>
void Vector<double>::allToZero(void){
for(int i = 0; i < N; i++)
v[i] = 0.0;
}
template<>
void Vector<double>::add(const Vector<double>& b){
if(this->N != b.N)
throw Exception("Vectors must be with of the same size");
for(int i = 0; i < N; i++)
v[i] += b.v[i];
}
template<>
void Vector<double>::sub(const Vector<double>& b){
if(this->N != b.N)
throw Exception("Vectors must be with of the same size");
for(int i = 0; i < N; i++)
v[i] -= b.v[i];
}
template<>
void Vector<double>::mul(const double& alpha){
for(int i = 0; i < N; i++)
v[i] *= alpha;
}
template<>
string Vector<double>::toString(void) const{
stringstream s;
for(int i = 0; i < N; i++)
s << scientific << showpos << v[i] << endl;
return s.str();
}
#include <sstream>
#include "Vector.h"
#include "Polynomial.h"
using namespace std;
template<>
Vector<double> Vector<Polynomial>::at(const double x,
const double y,
const double z) const{
Vector<double> val(N);
for(int i = 0; i < N; i++)
val(i) = v[i].at(x, y, z);
return val;
}
template<>
void Vector<Polynomial>::add(const Vector<Polynomial>& other){
for(int i = 0; i < N; i++)
v[i].add(other.v[i]);
}
template<>
void Vector<Polynomial>::sub(const Vector<Polynomial>& other){
for(int i = 0; i < N; i++)
v[i].sub(other.v[i]);
}
template<>
void Vector<Polynomial>::mul(const Polynomial& other){
for(int i = 0; i < N; i++)
v[i].mul(other);
}
/*
template<>
void Vector<Polynomial>::mul(const double alpha){
for(int i = 0; i < N; i++)
v[i].mul(alpha);
}
*/
template<>
string Vector<Polynomial>::toString(void) const{
stringstream s;
s << endl;
for(int i = 0; i < N; i++)
s << "[" << v[i].toString() << "]" << endl;
return s.str();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment