The ChainCon Software (Release 0.03)
wrapping.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// Tools for coordinate wrapping, a.k.a. periodic boundary conditions.
6 ///
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
10 //
11 // This file is part of my research software package. This is free software:
12 // you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation,
14 // either version 3 of the License, or (at your option) any later version.
15 //
16 // This software is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this software; see the file "license.txt". If not,
23 // please, see <http://www.gnu.org/licenses/>.
24 
25 // Started on March 24, 2009. Last revision: January 13, 2013.
26 
27 
28 #ifndef _CHAINCON_WRAPPING_H_
29 #define _CHAINCON_WRAPPING_H_
30 
31 
32 // include some standard C++ header files
33 #include <vector>
34 #include <string>
35 #include <istream>
36 #include <sstream>
37 
38 // include selected header files from the CHomP library
39 //#include "chomp/system/config.h"
40 //#include "chomp/system/textfile.h"
41 
42 
43 // --------------------------------------------------
44 // ------------------ no wrapping -------------------
45 // --------------------------------------------------
46 
47 /// A simple class for wrapping cubical cell coordinstes,
48 /// which does not wrap anything.
49 template <class CoordT>
51 {
52 public:
53  /// The type of coordinates for wrapping.
54  typedef CoordT CoordType;
55 
56  /// A dummy wrapping function that does nothing.
57  template <class ArrayT>
58  static void wrap (ArrayT &coords, int dim);
59 
60  /// A dummy function for setting wrapping that throws an exception
61  /// if called.
62  template <class ArrayT>
63  static void setWrapping (const ArrayT &coords, int dim);
64 
65 }; /* class NoWrapping */
66 
67 // --------------------------------------------------
68 
69 template <class CoordT>
70 template <class ArrayT>
71 inline void NoWrapping<CoordT>::wrap (ArrayT &, int)
72 {
73  return;
74 } /* NoWrapping::wrap */
75 
76 template <class CoordT>
77 template <class ArrayT>
78 inline void NoWrapping<CoordT>::setWrapping (const ArrayT &, int)
79 {
80  throw "Coordinate wrapping not supported.";
81  return;
82 } /* SettableWrapping::setWrapping */
83 
84 
85 // --------------------------------------------------
86 // --------------- settable wrapping ----------------
87 // --------------------------------------------------
88 
89 /// A simple class for wrapping cubical cell coordinstes,
90 /// using the one defined in the CHomP package.
91 template <class CoordT>
93 {
94 public:
95  /// The type of coordinates for wrapping.
96  typedef CoordT CoordType;
97 
98  /// Makes a correction to the given array of coordinates
99  /// if wrapping is in effect.
100  template <class ArrayT>
101  static void wrap (ArrayT &coords, int dim);
102 
103  /// Sets wrapping for all spaces of the given dimension.
104  template <class ArrayT>
105  static void setWrapping (const ArrayT &coords, int dim);
106 
107 private:
108  /// An array of wrapping vectors for each dimension where defined.
109  static std::vector<std::vector<CoordT> > wrapping;
110 
111 }; /* class SettableWrapping */
112 
113 // --------------------------------------------------
114 
115 template <class CoordT>
116 std::vector<std::vector<CoordT> > SettableWrapping<CoordT>::wrapping;
117 
118 // --------------------------------------------------
119 
120 template <class CoordT>
121 template <class ArrayT>
122 inline void SettableWrapping<CoordT>::wrap (ArrayT &coords, int dim)
123 {
124  // if wrapping was not defined for this dimension then do nothing
125  if (static_cast<int> (wrapping. size ()) <= dim)
126  return;
127  const std::vector<CoordT> &w (wrapping [dim]);
128  if (static_cast<int> (w. size ()) != dim)
129  return;
130 
131  // wrap those coordinates which require this
132  for (int i = 0; i < dim; ++ i)
133  {
134  if (!w [i])
135  continue;
136  if (coords [i] >= 0)
137  {
138  if (coords [i] < w [i])
139  {
140  }
141  else if (coords [i] == w [i])
142  {
143  coords [i] = 0;
144  }
145  else if (coords [i] > w [i])
146  {
147  coords [i] %= w [i];
148  }
149  }
150  else if (coords [i] == -1)
151  {
152  coords [i] = w [i] - 1;
153  }
154  else
155  {
156  coords [i] = (w [i] - 1) -
157  ((-coords [i] - 1) % w [i]);
158  }
159  }
160 
161  return;
162 } /* SettableWrapping::wrap */
163 
164 template <class CoordT>
165 template <class ArrayT>
167  (const ArrayT &coords, int dim)
168 {
169  // extend the wrapping vector if necessary
170  while (static_cast<int> (wrapping. size ()) <= dim)
171  {
172  std::vector<CoordT> dummy;
173  wrapping. push_back (dummy);
174  }
175 
176  // create a new vector for wrapping coordinates at the given dim
177  std::vector<CoordT> w (dim);
178  for (int i = 0; i < dim; ++ i)
179  w [i] = coords [i];
180 
181  // put the new vector in the wrapping array
182  wrapping [dim] = w;
183 
184  return;
185 } /* SettableWrapping::setWrapping */
186 
187 // --------------------------------------------------
188 
189 /// Sets wrapping based on a text with comma-separated numbers.
190 /// The dimension in which the wrapping is set is determined automatically
191 /// as the number of the provided numbers.
192 /// All the numbers must be non-negative.
193 template <class WrapT>
194 inline void setWrapping (const std::string &s)
195 {
196  typedef typename WrapT::CoordType CoordType;
197  std::vector<CoordType> coords;
198  std::istringstream in (s);
199  while (in. peek () != EOF)
200  {
201  CoordType n (-1);
202  in >> n;
203  if (n == static_cast<CoordType> (-1))
204  break;
205  coords. push_back (n);
206  if (in. peek () == ',')
207  in. get ();
208  }
209  if (!coords. empty ())
210  WrapT::setWrapping (coords, coords. size ());
211  return;
212 } /* setWrapping */
213 
214 
215 #endif // _CHAINCON_WRAPPING_H_
216 
CoordT CoordType
The type of coordinates for wrapping.
Definition: wrapping.h:54
A simple class for wrapping cubical cell coordinstes, which does not wrap anything.
Definition: wrapping.h:50
static void setWrapping(const ArrayT &coords, int dim)
Sets wrapping for all spaces of the given dimension.
Definition: wrapping.h:167
CoordT CoordType
The type of coordinates for wrapping.
Definition: wrapping.h:96
void setWrapping(const std::string &s)
Sets wrapping based on a text with comma-separated numbers.
Definition: wrapping.h:194
A simple class for wrapping cubical cell coordinstes, using the one defined in the CHomP package...
Definition: wrapping.h:92
static void wrap(ArrayT &coords, int dim)
A dummy wrapping function that does nothing.
Definition: wrapping.h:71
static void setWrapping(const ArrayT &coords, int dim)
A dummy function for setting wrapping that throws an exception if called.
Definition: wrapping.h:78
static std::vector< std::vector< CoordT > > wrapping
An array of wrapping vectors for each dimension where defined.
Definition: wrapping.h:109
static void wrap(ArrayT &coords, int dim)
Makes a correction to the given array of coordinates if wrapping is in effect.
Definition: wrapping.h:122