From: guido Date: Wed, 24 Feb 1999 13:20:28 +0000 (+0000) Subject: VectorFunction and TensorFunction X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a058e07d86eeaa843e7bdd9781c7a02ae0b61cd7;p=dealii-svn.git VectorFunction and TensorFunction git-svn-id: https://svn.dealii.org/trunk@894 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/tensor.h b/deal.II/base/include/base/tensor.h index d9a394bce6..a9ca5b8d21 100644 --- a/deal.II/base/include/base/tensor.h +++ b/deal.II/base/include/base/tensor.h @@ -8,7 +8,6 @@ #include - /** * Provide a general tensor class with an arbitrary rank, i.e. with * an arbitrary number of indices. The Tensor class provides an @@ -130,11 +129,28 @@ class Tensor { */ Tensor operator - (const Tensor &) const; + /** + * Fill a vector with all tensor elements. + * + * Thsi function unrolls all + * tensor entries into a single, + * linearly numbered vector. As + * usual in C++, the rightmost + * index marches fastest. + */ + void unroll(vector & result) const; + + /** * Reset all values to zero. */ void clear (); + /** + * Exception + */ + DeclException2(ExcWrongVectorSize, unsigned, int, << "Tensor has " << arg1 + << " entries, but vector has size " << arg2); /** * Exception @@ -148,6 +164,14 @@ class Tensor { * subelements. */ Tensor subtensor[dim]; + + /** + * Help function for unroll. + */ + void unroll_recursion(vector & result, unsigned& start_index) const; + + template<> + friend void Tensor::unroll_recursion(vector &, unsigned&) const; }; diff --git a/deal.II/base/include/base/tensor_base.h b/deal.II/base/include/base/tensor_base.h index 1fd4fe4969..af991fbed6 100644 --- a/deal.II/base/include/base/tensor_base.h +++ b/deal.II/base/include/base/tensor_base.h @@ -12,6 +12,7 @@ #include #include +#include @@ -160,12 +161,31 @@ class Tensor<1,dim> { * Reset all values to zero. */ void clear (); - + + /** + * Fill a vector with all tensor elements. + * + * Thsi function unrolls all + * tensor entries into a single, + * linearly numbered vector. As + * usual in C++, the rightmost + * index marches fastest. + */ + void unroll(vector & result) const; + protected: /** * Stores the values in a simple array. */ double values[dim]; + + /** + * Help function for unroll. + */ + void unroll_recursion(vector & result, unsigned& start_index) const; + + template<> + friend void Tensor<2,dim>::unroll_recursion(vector &, unsigned&) const; }; diff --git a/deal.II/base/include/base/tensor_function.h b/deal.II/base/include/base/tensor_function.h index a2e5ae8772..f79825e2f2 100644 --- a/deal.II/base/include/base/tensor_function.h +++ b/deal.II/base/include/base/tensor_function.h @@ -16,6 +16,85 @@ template class Tensor; +/** + * Base class for multi-valued functions. + * + * + */ +template +class VectorFunction : + public FunctionTime +{ + public: + /** + * Constructor. May take an initial vakue + * for the time variable, which defaults + * to zero. + */ + VectorFunction (unsigned n_components, const double initial_time = 0.0); + + /** + * Virtual destructor; absolutely + * necessary in this case. + */ + virtual ~VectorFunction (); + +// /** +// * Return the value of the function +// * at the given point. +// */ +// virtual double operator () (const Point &p, unsigned component) const; + + /** + * Set #values# to the point values + * of the function at points #p#. + * It is assumed that #values# + * already has the right size, i.e. + * the same size as the #n_components# + * array. + */ + virtual void value (const Point &p, vector &values) const; + + /** + * Set #values# to the point values + * of the function at the #points#. + * It is assumed that #values# + * already has the right size, i.e. + * the same size as the #points# + * array. + */ + virtual void value_list (const vector > &points, + vector > &values) const; + + /** + * Set #gradients# to the gradients of + * the function at the #points#. + * It is assumed that #values# + * already has the right size, i.e. + * the same size as the #points# array. + */ + virtual void gradient_list (const vector > &points, + vector > > &gradients) const; + + /** + * Number of vector components. + */ + const unsigned n_components; + + /** + * Exception + */ + DeclException0 (ExcPureFunctionCalled); + /** + * Exception + */ + DeclException2 (ExcVectorHasWrongSize, + int, int, + << "The vector has size " << arg1 << " but should have " + << arg2 << " elements."); + +}; + /** * This class is a model for a tensor valued function. * It returns the value @@ -38,7 +117,7 @@ class Tensor; */ template class TensorFunction : - public FunctionTime + public VectorFunction { public: /** @@ -92,7 +171,19 @@ class TensorFunction : */ virtual void gradient_list (const vector > &points, vector > &gradients) const; - + + /** + * See #VectorFunction#. + */ + virtual void value_list (const vector > &points, + vector > &values) const; + + /** + * See #VectorFunction#. + */ + virtual void gradient_list (const vector > &points, + vector > > &gradients) const; + /** * Exception */ diff --git a/deal.II/base/source/tensor.cc b/deal.II/base/source/tensor.cc new file mode 100644 index 0000000000..696cf9f1f0 --- /dev/null +++ b/deal.II/base/source/tensor.cc @@ -0,0 +1,51 @@ +// $Id$ + +#include +#include + +template void +Tensor::unroll( vector& result) const +{ + Assert(result.size()==pow(dim,rank_), + ExcWrongVectorSize(pow(dim,rank_), result.size())); + + unsigned index = 0; + unroll_recursion(result,index); +} + + +template void +Tensor::unroll_recursion( vector& result, unsigned& index) const +{ + for (unsigned i=0; i void +Tensor<1,dim>::unroll_recursion( vector& result, unsigned& index) const +{ + for (unsigned i=0; i; +template class Tensor<1, 2>; +template class Tensor<1, 3>; +template class Tensor<2, 1>; +template class Tensor<2, 2>; +template class Tensor<2, 3>; +template class Tensor<3, 1>; +template class Tensor<3, 2>; +template class Tensor<3, 3>; +template class Tensor<4, 1>; +template class Tensor<4, 2>; +template class Tensor<4, 3>; diff --git a/deal.II/base/source/tensor_function.cc b/deal.II/base/source/tensor_function.cc index f3106ff4a5..4372c6ceb8 100644 --- a/deal.II/base/source/tensor_function.cc +++ b/deal.II/base/source/tensor_function.cc @@ -4,11 +4,61 @@ #include #include #include +#include +template +VectorFunction::VectorFunction(unsigned n_components, const double initial_time) + : + FunctionTime(initial_time), + n_components(n_components) +{} + + +template +VectorFunction::~VectorFunction() +{} + +/* +template double +VectorFunction::operator () (const Point &, unsigned) const + +{ + Assert (false, ExcPureFunctionCalled()); + return 0.; +} +*/ + +template void +VectorFunction::value (const Point &, vector &) const +{ + Assert (false, ExcPureFunctionCalled()); +} + + +template void +VectorFunction::value_list (const vector > &, + vector > &) const +{ + Assert (false, ExcPureFunctionCalled()); +} + + +template void +VectorFunction::gradient_list (const vector > &, + vector > > &) const +{ + Assert (false, ExcPureFunctionCalled()); +} + + +////////////////////////////////////////////////////////////////////// +// TensorFunction +////////////////////////////////////////////////////////////////////// template -TensorFunction::TensorFunction (const double initial_time) : - FunctionTime(initial_time) +TensorFunction::TensorFunction (const double initial_time) + : + VectorFunction(pow(dim,rank_), initial_time) {}; @@ -78,6 +128,37 @@ TensorFunction::gradient_list (const vector > &points, gradients[i] = gradient(points[i]); }; +/* +template void +TensorFunction::value (const Point &p, vector &erg) const +{ + Tensor h = operator()(p); + h.unroll(erg); +} +*/ + +template void +TensorFunction::value_list (const vector > & points, + vector > & values) const +{ + Assert (values.size() == points.size(), + ExcVectorHasWrongSize(values.size(), points.size())); + + for (unsigned int i=0; i void +TensorFunction::gradient_list (const vector > &, + vector > > &) const +{ + Assert (false, ExcPureFunctionCalled()); +} + + + template class TensorFunction<1,1>; template class TensorFunction<2,1>; template class TensorFunction<3,1>;