value_expr.hpp 9.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/



/** \file value_expr.hpp */

#ifndef AMDIS_VALUE_EXPR_HPP
#define AMDIS_VALUE_EXPR_HPP

#include "LazyOperatorTerm.h"

namespace AMDiS 
{
  namespace expressions
  {  
    /// Expression that encapsulates a runtime value
    template<typename T>
    struct RValue : public LazyOperatorTermBase
    {
      typedef T value_type;
      T value;
      RValue(const T& value_) : value(value_) {}

      inline value_type operator()(const int& iq) const { return value; }
      
      std::string str() const { return boost::lexical_cast<std::string>(value); }
    };
  
  
    /// Expression that encapsulates a compiletime value
    template<int V>
    struct CValue : public LazyOperatorTermBase
    {
      typedef int value_type;
      inline value_type operator()(const int& iq) const { return V; }
      
      std::string str() const { return std::string("[") + boost::lexical_cast<std::string>(V) + "]"; }
    };
    
    
    /// Expression that encapsulates a compiletime vector
    template<int Size, int V0 = 0, int V1 = 0, int V2 = 0>
    struct CVector : public LazyOperatorTermBase
    {
      typedef Vector<int> value_type;
      value_type V;
65 66 67 68
      
      CVector() : V(Size) {
	int values[3] = {V0, V1, V2};
	V.setValues(values);
69 70 71 72 73 74 75 76
      }
		    
      inline value_type operator()(const int& iq) const { return V; }
      
      std::string str() const { return std::string("[V(") + boost::lexical_cast<std::string>(Size) + ")]"; }
    };
    
    
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    /// Expression that encapsulates a compiletime vector
    template<int V0 = 0, int V1 = 0, int V2 = 0>
    struct WVector : public LazyOperatorTermBase
    {
      typedef WorldVector<int> value_type;
      value_type V;
      
      WVector() {
	V[0] = V0;
	if (size(V) > 1) V[1] = V1;
	if (size(V) > 2) V[2] = V2;
      }
		    
      inline value_type operator()(const int& iq) const { return V; }
      
      std::string str() const { return std::string("[V(") + boost::lexical_cast<std::string>(size(V)) + ")]"; }
    };
    
    template<int I = -1> 
    struct E : public LazyOperatorTermBase
    {
      typedef WorldVector<int> value_type;
      value_type V;
      
      E(int I_ = -1) {
	assert((I >= 0 && I_ < 0) || (I < 0 && I_ >= 0));
	V = 0; V[std::max(I, I_)] = 1;
      }
		    
      inline value_type operator()(const int& iq) const { return V; }
      
      std::string str() const { return std::string("[V(") + boost::lexical_cast<std::string>(size(V)) + ")]"; }
    };
    
    template<> struct E<0> : WVector<1> {};
    template<> struct E<1> : WVector<0,1> {};
    template<> struct E<2> : WVector<0,0,1> {};
    
    
116 117 118 119 120 121 122
    /// Expression that encapsulates a compiletime matrix
    template<int Rows, int Cols, int V0 = 0, int V1 = 0, int V2 = 0,
				int V3 = 0, int V4 = 0, int V5 = 0,
				int V6 = 0, int V7 = 0, int V8 = 0>
    struct CMatrix : public LazyOperatorTermBase
    {
      typedef Matrix<int> value_type;
123 124 125 126 127
      value_type M;
      
      CMatrix() : M(Rows, Cols) {
	int values[9] = {V0, V1, V2, V3, V4, V5, V6, V7, V8};
	M.setValues(values);
128 129
      }
		    
130
      inline value_type operator()(const int& iq) const { return M; }
131 132 133 134
      
      std::string str() const { return std::string("[M(") + boost::lexical_cast<std::string>(Rows) + ","+ boost::lexical_cast<std::string>(Cols) + ")]"; }
    };
    
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    
    /// Expression that encapsulates a compiletime matrix
    template<int V0 = 0, int V1 = 0, int V2 = 0,
	     int V3 = 0, int V4 = 0, int V5 = 0,
	     int V6 = 0, int V7 = 0, int V8 = 0>
    struct WMatrix : public LazyOperatorTermBase
    {
      typedef WorldMatrix<int> value_type;
      value_type M;
      
      WMatrix() {
	M[0][0] = V0;
	if (Global::getGeo(WORLD) == 2) {
	                M[0][1] = V1;
	  M[1][0] = V2; M[1][1] = V3;
	} else if (Global::getGeo(WORLD) == 3) {
	                M[0][1] = V1; M[0][2] = V2;
	  M[1][0] = V3; M[1][1] = V4; M[1][2] = V5;
	  M[2][0] = V6; M[2][1] = V7; M[2][2] = V8;
	}
      }
		    
      inline value_type operator()(const int& iq) const { return M; }
      
      std::string str() const { return std::string("[M(") + boost::lexical_cast<std::string>(Global::getGeo(WORLD)) + ","+ boost::lexical_cast<std::string>(Global::getGeo(WORLD)) + ")]"; }
    };
    
    
    template<int N = -1> 
    struct Eye : public LazyOperatorTermBase
    {
      typedef Matrix<int> value_type;
      size_t n;
Praetorius, Simon's avatar
Praetorius, Simon committed
168
      value_type M;
169
      
Praetorius, Simon's avatar
Praetorius, Simon committed
170
      Eye(int n_) : n(n_ < 0 ? N : n_), M(n,n) {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	M = 0;
	for (size_t i = 0; i < n; ++i)
	  M[i][i] = 1;
      }
		    
      inline value_type operator()(const int& iq) const { return M; }
      
      std::string str() const { return std::string("[M(") + boost::lexical_cast<std::string>(n) + ","+ boost::lexical_cast<std::string>(n) + ")]"; }
    };
    
    template<> 
    struct Eye<-2> : public LazyOperatorTermBase
    {
      typedef WorldMatrix<int> value_type;
      value_type M;
      
      Eye() {
	M = 0;
189
	for (size_t i = 0; i < (size_t)Global::getGeo(WORLD); ++i)
190 191 192 193 194 195 196 197 198 199 200 201
	  M[i][i] = 1;
      }
		    
      inline value_type operator()(const int& iq) const { return M; }
      
      std::string str() const { return std::string("[M(") + boost::lexical_cast<std::string>(Global::getGeo(WORLD)) + ","+ boost::lexical_cast<std::string>(Global::getGeo(WORLD)) + ")]"; }
    };
    
    template<> struct Eye<1> : CMatrix<1,1, 1> {};
    template<> struct Eye<2> : CMatrix<2,2, 1,0, 0,1> {};
    template<> struct Eye<3> : CMatrix<3,3, 1,0,0, 0,1,0, 0,0,1> {};
    
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  } // end namespace expressions
  
  
  namespace traits 
  {
    template<typename T>
    struct remove_all_qualifiers
    {
      typedef typename boost::remove_cv
	< typename boost::remove_pointer
	  < typename boost::remove_reference<T>::type >::type
	>::type type;
    };
	  
	
    template<typename T>
    struct pure_value { 
      typedef typename remove_all_qualifiers<T>::type type;
      static type eval(const T& t) { return t; }
    };
    
    template<typename T>
    struct pure_value<T&> { 
      typedef typename remove_all_qualifiers<T>::type type;
      static type eval(T& t) { return t; }
    };
    
    template<typename T>
    struct pure_value<T*> { 
      typedef typename remove_all_qualifiers<T>::type type;
      static type eval(T* t) { return pure_value<T>::eval(*t); }
    };
    
    template<typename T>
    struct pure_value<const T> { 
      typedef typename remove_all_qualifiers<T>::type type;
      static type eval(const T& t) { return pure_value<T>::eval(t); }
    };
    
  } // end namespace traits
  
  namespace expressions
  {    
    /// Expression that points to a value given by reference
    template<typename T>
    struct Reference : public LazyOperatorTermBase
    {
      typedef typename traits::pure_value<T>::type value_type;
      const value_type& value;
      Reference(const T& value_) : value(value_) {}
      Reference(const T* value_) : value(*value_) {}

      template<typename List>
      void insertFeSpaces(List& feSpaces) const {}
      
      int getDegree() const
      {
	return 0;
      }

      template<typename OT>
      void initElement(OT* ot, const ElInfo* elInfo,
		      SubAssembler* subAssembler, Quadrature *quad, 
		    const BasisFunction *basisFct = NULL) {}

      template<typename OT>
      void initElement(OT* ot, const ElInfo* smallElInfo, const ElInfo* largeElInfo,
	      SubAssembler* subAssembler, Quadrature *quad, 
		    const BasisFunction *basisFct = NULL) {}

      inline value_type operator()(const int& iq) const { return traits::pure_value<T>::eval(value); }
      
      std::string str() const { return std::string("&(") + boost::lexical_cast<std::string>(traits::pure_value<T>::eval(value)) + ")"; }
    };
    
  } // end namespace expressions

  
  // generator functions
  // _____________________________________________________________________________

  template<typename T>
  inline expressions::RValue<T> constant(T value) { return expressions::RValue<T>(value); }

  template<int I>
  inline expressions::CValue<I> constant() { return expressions::CValue<I>(); }

  template<typename T>
  inline expressions::Reference<T> ref_(T& value) { return expressions::Reference<T>(value); }

  template<typename T>
  inline expressions::Reference<T> ref_(T* value) { return expressions::Reference<T>(value); }

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  
  // unit vectors
  template<int I>
  inline expressions::E<I> E() 
  { return expressions::E<I>(); }
  
  inline expressions::E<-1> E(int i) 
  { return expressions::E<-1>(i); }
  
  // unit matrix
  template<int N>
  inline expressions::Eye<N> eye() 
  { return expressions::Eye<N>(); }  // NxN unit-matrix 
  
  inline expressions::Eye<-1> eye(int n) 
  { return expressions::Eye<-1>(n); } // nxn unit-matrix
  
  inline expressions::Eye<-2> eye() 
  { return expressions::Eye<-2>(); } // dow x dow unit-matrix
  
  
316 317 318
} // end namespace AMDiS

#endif // AMDIS_VALUE_EXPR_HPP