From: Wolfgang Bangerth Date: Tue, 2 Jul 2013 19:10:38 +0000 (+0000) Subject: Patch by Denis Davydov: Provide functions to unroll and re-roll indices of Tensor... X-Git-Tag: v8.0.0~201 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1150a542097b2af8716f1e4cb265ac1e14f79e3b;p=dealii.git Patch by Denis Davydov: Provide functions to unroll and re-roll indices of Tensor, much the same as we already have for SymmetricTensor. git-svn-id: https://svn.dealii.org/trunk@29917 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index a7526a6ac3..1049ce5462 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -147,6 +147,13 @@ this function.
    +
  1. New: There are now functions Tensor::component_to_unrolled_index() +and Tensor::unrolled_to_component_indices() in the same way as they +already exist for the SymmetricTensor class. +
    +(Denis Davydov, 2013/07/02) +
  2. +
  3. New: There is now a read-write version of TableIndices::operator[].
    (Denis Davydov, 2013/07/02) diff --git a/deal.II/include/deal.II/base/tensor.h b/deal.II/include/deal.II/base/tensor.h index c151cb197f..dc5dd68407 100644 --- a/deal.II/include/deal.II/base/tensor.h +++ b/deal.II/include/deal.II/base/tensor.h @@ -1,7 +1,7 @@ //--------------------------------------------------------------------------- // $Id$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2011, 2012 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2011, 2012, 2013 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -71,6 +71,14 @@ public: */ static const unsigned int rank = rank_; + /** + * Number of independent components of a + * tensor of current rank. This is dim times the + * number of independent components of each sub-tensor. + */ + static const unsigned int + n_independent_components = Tensor::n_independent_components * dim; + /** * Type of stored objects. This * is a tensor of lower rank. @@ -242,6 +250,24 @@ public: template void unroll (Vector &result) const; + /** + * Returns an unrolled index in + * the range [0,dim^rank-1] for the element of the tensor indexed by + * the argument to the function. + */ + static + unsigned int + component_to_unrolled_index(const TableIndices &indices); + + /** + * Opposite of component_to_unrolled_index: For an index in the + * range [0,dim^rank-1], return which set of indices it would + * correspond to. + */ + static + TableIndices unrolled_to_component_indices(const unsigned int i); + + /** * Reset all values to zero. @@ -574,6 +600,40 @@ Tensor::unroll_recursion (Vector &result, } } +template +inline +unsigned int +Tensor::component_to_unrolled_index(const TableIndices &indices) +{ + TableIndices indices1; + for (unsigned int i = 0; i < rank_-1;i++) + indices1[i] = indices[i]; + + Assert (indices[rank_-1] < dim, + ExcIndexRange (indices[rank_-1], 0, dim)); + return ( Tensor::component_to_unrolled_index(indices1) * dim + indices[rank_-1]); + +} + +template +inline +TableIndices +Tensor::unrolled_to_component_indices(const unsigned int i) +{ + TableIndices indices1; + TableIndices indices; + + indices[rank_-1] = (i % dim); + const unsigned int i1 = i / dim; + + indices1 = Tensor::unrolled_to_component_indices(i1); + + for (unsigned int i = 0; i < rank-1;i++) + indices[i] = indices1[i]; + + return indices; +} + template diff --git a/deal.II/include/deal.II/base/tensor_base.h b/deal.II/include/deal.II/base/tensor_base.h index 6574ddc6f2..6a56daa4dd 100644 --- a/deal.II/include/deal.II/base/tensor_base.h +++ b/deal.II/include/deal.II/base/tensor_base.h @@ -1,7 +1,7 @@ //--------------------------------------------------------------------------- // $Id$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012, 2013 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -348,6 +349,13 @@ public: */ static const unsigned int rank = 1; + /** + * Number of independent components of a + * tensor of rank 1. + */ + static const unsigned int + n_independent_components = dim; + /** * Type of stored objects. This * is a Number for a rank 1 tensor. @@ -568,6 +576,31 @@ public: template void unroll (Vector &result) const; + /** + * Returns an unrolled index in + * the range [0,dim-1] for the element of the tensor indexed by + * the argument to the function. + * + * Given that this is a rank-1 object, the returned value + * is simply the value of the only index stored by the argument. + */ + static + unsigned int + component_to_unrolled_index(const TableIndices<1> &indices); + + /** + * Opposite of component_to_unrolled_index: For an index in the + * range [0,dim-1], return which set of indices it would + * correspond to. + * + * Given that this is a rank-1 object, the returned set of indices + * consists of only a single element with value equal to the argument + * to this function. + */ + static + TableIndices<1> unrolled_to_component_indices(const unsigned int i); + + /** * Determine an estimate for * the memory consumption (in @@ -1187,6 +1220,23 @@ Tensor<1,dim,Number>::unroll_recursion (Vector &result, } +template +inline +unsigned int +Tensor<1, dim, Number>::component_to_unrolled_index (const TableIndices<1> &indices) +{ + return indices[0]; +} + +template +inline +TableIndices<1> +Tensor<1, dim, Number>::unrolled_to_component_indices (const unsigned int i) +{ + return TableIndices<1>(i); +} + + template inline