dynLattice
Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
1
7#pragma once
8
9#include "utils/testing.h"
10
11#include <jem/base/Array.h>
12#include <jem/base/CString.h>
13#include <jem/base/tuple/Tuple.h>
14#include <jem/numeric/Quaternion.h>
15#include <jem/numeric/algebra.h>
16#include <jem/numeric/func/Function.h>
17
18#include <jive/Array.h>
19#include <jive/util/Constraints.h>
20#include <jive/util/FuncUtils.h>
21
22#define WARN(key) jem::System::warn() << key << "\n";
23#define ERR(key) jem::System::err() << key << "\n";
24
25using jem::ALL;
26using jem::Array;
27using jem::idx_t;
28using jem::SliceFromTo;
29using jem::String;
30using jem::numeric::dotProduct;
31using jem::numeric::Function;
32using jem::numeric::matmul;
33using jem::numeric::MatmulChain;
34using jem::numeric::norm2;
35using jem::numeric::Quaternion;
36
37using jive::Cubix;
38using jive::IdxVector;
39using jive::Matrix;
40using jive::Ref;
41using jive::Vector;
42using jive::util::FuncUtils;
43
45namespace jive_helpers
46{
47typedef Array<double, 4> Quadix;
48typedef Array<IdxVector, 2> IdxVectorMatrix;
49
50const Vector e1 = {1., 0., 0.};
51const Vector e2 = {0., 1., 0.};
52const Vector e3 = {0., 0., 1.};
53const Vector vec0 = {0., 0., 0.};
54const double TINY = __DBL_EPSILON__ * 1e6;
55
61Vector funcGrad(const Ref<Function> func, const Vector &args);
62
68Vector evalFuncs(const FuncUtils::FuncArray funcs, const Vector &args);
69
75Matrix funcHessian(const Ref<Function> func, const Vector &args);
76
82Matrix gradFuncs(const FuncUtils::FuncArray funcs, const Vector &args);
83
88Matrix eye(const idx_t dim = 3);
89
99void logMat(const Vector &rv, const Matrix &R);
100
105void vec2mat(const Matrix &mat, const Vector &vec);
106
111void mat2vec(const Vector &vec, const Matrix &mat);
112
113// -----------------------------------------------------------------------
114// Temporary arrays: why and how we avoid them
115//
116// A JEM array (Vector, Matrix, Cubix) is a small handle that points to a
117// block of numbers. Copying a handle ("Vector a = b;") is cheap and does NOT
118// copy the numbers: a and b then show the same numbers. Creating a new array,
119// or writing "x = matmul(A, v)", asks the memory manager for a fresh block of
120// numbers and gives it back afterwards. For the 3 to 6 numbers of a rod
121// element this bookkeeping costs more than the arithmetic itself, and in the
122// force assembly it happens millions of times per run. Therefore the code that
123// is executed for every element and every time step ("hot code") follows
124// three rules; everywhere else (set-up, output, rarely used code) the short
125// forms matmul(A, v) and skew(v) stay, because they are easier to read.
126//
127// 1. Work arrays: arrays that live as (mutable) members of the class, are
128// created once in the constructor/configure and overwritten in every
129// call. A work array is always written completely before it is read
130// within the same call, and it is never used to carry values from one
131// call to the next. A class with work arrays must not be used by several
132// threads at once.
133// 2. 3 x 3 products: mul3(C, A, B) and mul3(y, A, x). The result may be one
134// of the factors.
135// 3. All other matrix products: a MatmulChain member (jem::numeric), which
136// keeps its result array and reuses it. The result is only valid until
137// the next call of the same chain, so use it directly or copy it first.
138// -----------------------------------------------------------------------
139
148void mul3(const Matrix &C, const Matrix &A, const Matrix &B);
149
157void mul3(const Vector &y, const Matrix &A, const Vector &x);
158
168void expVec(const Matrix &Exp, const Vector &v);
169
178void expVecP(const Matrix &ExpP, const Vector &v, const Vector &vP);
179
184double trace(const Matrix &mat);
185
190idx_t factorial(const idx_t n);
191
197idx_t binom(const idx_t n, const idx_t k);
198
203double matrixNorm2(const Matrix &mat);
204
211Matrix skew(const Vector &vec);
212
218void skew(const Matrix &S, const Vector &vec);
219
225Vector unskew(const Matrix &mat);
226
232void setDofValue(jive::util::Constraints &cons, const idx_t idof, const double rval);
233
240void linkDofs(jive::util::Constraints &cons, const idx_t idof, const idx_t jdof, const double coeff);
241}; // namespace jive_helpers
namespace for all helper functions
Definition helpers.cpp:28
double trace(const Matrix &mat)
calculate the trace of a matrix.
Definition helpers.cpp:269
Vector unskew(const Matrix &mat)
construct a axial vector from a given skew-symmetric matrix
Definition helpers.cpp:332
void expVec(const Matrix &Exp, const Vector &psi)
compute the exponential of a axial vector.
Definition helpers.cpp:185
void setDofValue(jive::util::Constraints &cons, const idx_t idof, const double rval)
prescribe the value of a dof, i.e. constrain idof = rval.
Definition helpers.cpp:358
idx_t factorial(const idx_t n)
calculates the factorial of an integer.
Definition helpers.cpp:281
void vec2mat(const Matrix &mat, const Vector &vec)
rearrange a vector into a matrix.
Definition helpers.cpp:136
Vector funcGrad(const Ref< Function > func, const Vector &args)
gets the gradient of a multivariate function.
Definition helpers.cpp:29
idx_t binom(const idx_t n, const idx_t k)
calculate the binomial coefficient.
Definition helpers.cpp:292
void logMat(const Vector &rv, const Matrix &R)
compute rotational vector from rotation matrix.
Definition helpers.cpp:117
Matrix eye(const idx_t dim)
generates an identity matrix of the given dimension.
Definition helpers.cpp:106
const double TINY
Definition helpers.h:54
Vector evalFuncs(const FuncUtils::FuncArray funcs, const Vector &args)
evaluates an array of multivariate functions.
Definition helpers.cpp:42
const Vector e3
Definition helpers.h:52
const Vector vec0
Definition helpers.h:53
Array< double, 4 > Quadix
Definition helpers.h:47
const Vector e1
Definition helpers.h:50
void linkDofs(jive::util::Constraints &cons, const idx_t idof, const idx_t jdof, const double coeff)
link a slave dof to a master dof, i.e. constrain idof = coeff * jdof.
Definition helpers.cpp:366
Matrix funcHessian(const Ref< Function > func, const Vector &args)
gets the Hessian of a multivariate function.
Definition helpers.cpp:55
Matrix gradFuncs(const FuncUtils::FuncArray funcs, const Vector &args)
gets the gradients of an array of multivariate functions.
Definition helpers.cpp:89
Matrix skew(const Vector &vec)
construct a skew symetric matrix from a given vector.
Definition helpers.cpp:312
double matrixNorm2(const Matrix &mat)
calculates the 2-norm of a matrix.
Definition helpers.cpp:301
void expVecP(const Matrix &ExpP, const Vector &psi, const Vector &psiP)
derivative of jive_helpers::expVec.
Definition helpers.cpp:218
void mul3(const Matrix &C, const Matrix &A, const Matrix &B)
C = A * B for 3 x 3 matrices without heap allocation.
Definition helpers.cpp:162
const Vector e2
Definition helpers.h:51
void mat2vec(const Vector &vec, const Matrix &mat)
rearrange a matrix into a vector.
Definition helpers.cpp:149
Array< IdxVector, 2 > IdxVectorMatrix
Definition helpers.h:48
several compiler macros for faster testing of variables