OPAL (Object Oriented Parallel Accelerator Library)  2024.1
OPAL
DiscType.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef DISC_TYPE_H
12 #define DISC_TYPE_H
13 
14 /***************************************************************************
15  * DiscType<T> is a simple class that returns a string via the static method
16  * "str()" based on the type T. It is specialized for our standard types,
17  * and has a default value of "u" for "user" or "unknown" types.
18  ***************************************************************************/
19 
20 // include files
21 #include "AppTypes/Vektor.h"
22 #include "AppTypes/Tenzor.h"
23 #include "AppTypes/SymTenzor.h"
24 
26 // a base class for all DiscType's, that provides common routines to
27 // parse a type string and determine the apptype, the dimension, and the
28 // scalar type
29 struct DiscTypeBase {
30  // enums for scalar types and apptypes
33 
34  // determine the scalar data type for a given type string
35  static int scalarType(const std::string &s) {
36  if (s.length() == 0 || s.length() == 2 || s.length() > 3)
37  return UNKNOWN;
38  char c = static_cast<char>(tolower(s[s.length() - 1]));
39  if (c == 'c')
40  return CHAR;
41  else if (c == 's')
42  return SHORT;
43  else if (c == 'i')
44  return INT;
45  else if (c == 'l')
46  return LONG;
47  else if (c == 'f')
48  return FLOAT;
49  else if (c == 'd')
50  return DOUBLE;
51  else if (c == 'y')
52  return FCOMPLEX;
53  else if (c == 'z')
54  return DCOMPLEX;
55  else
56  return UNKNOWN;
57  }
58 
59  // determine the dimension of the apptype. If it is a scalar, return 0.
60  // if there is an error, return -1.
61  static int dim(const std::string &s) {
62  if (s.length() == 0 || s.length() == 2 || s.length() > 3)
63  return (-1);
64  if (s.length() == 1)
65  return 0;
66  char c = s[1];
67  if (c < '1' || c > '9')
68  return (-1);
69  return (c - '1' + 1);
70  }
71 
72  // determine the apptype of the string
73  static int appType(const std::string &s) {
74  if (s.length() == 0 || s.length() == 2 || s.length() > 3)
75  return UNKNOWN;
76  if (s.length() == 1)
77  return SCALAR;
78  char c = static_cast<char>(tolower(s[0]));
79  if (c == 'v')
80  return VEKTOR;
81  else if (c == 't')
82  return TENZOR;
83  else if (c == 's')
84  return SYMTENZOR;
85  else if (c == 'a')
86  return ANTISYMTENZOR;
87  else
88  return UNKNOWN;
89  }
90 };
91 
92 
94 // the general class, that is the default if the user does not specify
95 // one of the specialized types below
96 template<class T>
97 struct DiscType : public DiscTypeBase {
98  static std::string str() { return std::string("u"); }
99 };
100 
101 
103 // specializations for char, short, int, float, double
104 // codes:
105 // c = char
106 // s = short
107 // i = int
108 // l = long
109 // f = float
110 // d = double
111 // z = complex<double>
112 // y = complex<float>
113 #define DEFINE_DISCTYPE_SCALAR(TYPE, STRING) \
114 template<> \
115 struct DiscType< TYPE > : public DiscTypeBase { \
116  static std::string str() { return std::string(STRING); } \
117 };
118 
125 
126 DEFINE_DISCTYPE_SCALAR(unsigned char, "c")
127 DEFINE_DISCTYPE_SCALAR(unsigned short, "s")
128 DEFINE_DISCTYPE_SCALAR(unsigned int, "i")
129 DEFINE_DISCTYPE_SCALAR(unsigned long, "l")
130 
132 DEFINE_DISCTYPE_SCALAR(std::complex<double>, "z")
133 
135 // specializations for Vektor, Tenzor, SymTenzor, AntiSymTenzor
136 // codes:
137 // vNs = Vektor of dimension N and scalar type s
138 // tNs = Tenzor of dimension N and scalar type s
139 // sNs = SymTenzor of dimension N and scalar type s
140 // aNs = AntiSymTenzor of dimension N and scalar type s
141 #define DEFINE_DISCTYPE_APPTYPE(TYPE, STRING) \
142 template<class T, unsigned D> \
143 struct DiscType< TYPE<T, D> > : public DiscTypeBase { \
144  static std::string str() { \
145  CTAssert(D < 10); \
146  std::string retval = STRING; \
147  retval += "0"; \
148  retval += DiscType<T>::str(); \
149  retval[1] += D; \
150  return retval; \
151  } \
152 };
153 
158 
159 #endif
160 
161 /***************************************************************************
162  * $RCSfile: DiscType.h,v $ $Author: adelmann $
163  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
164  * IPPL_VERSION_ID: $Id: DiscType.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
165  ***************************************************************************/
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
#define DEFINE_DISCTYPE_APPTYPE(TYPE, STRING)
Definition: DiscType.h:141
Definition: TSVMeta.h:24
static int dim(const std::string &s)
Definition: DiscType.h:61
#define DEFINE_DISCTYPE_SCALAR(TYPE, STRING)
Definition: DiscType.h:113
static int scalarType(const std::string &s)
Definition: DiscType.h:35
Definition: Tenzor.h:34
static int appType(const std::string &s)
Definition: DiscType.h:73
static std::string str()
Definition: DiscType.h:98
MMatrix< m_complex > complex(MMatrix< double > real)
Definition: MMatrix.cpp:396