Overture  Version 25
Stencil.h
Go to the documentation of this file.
1 // Stencil.h
2 
3 #ifndef _STENCIL_H_
4 #define _STENCIL_H_
5 
6 /*
7 //============================================================================
8 This class represents a stencil.
9 It can act on gridfunctions and can be used to update a coefficient matrix
10 
11 
12 WTB: krister@tdb.uu.se
13 
14 //============================================================================
15 
16 */
17 
18 //kkc 040415 #include <iostream.h>
19 #include "OvertureDefine.h"
20 #include OV_STD_INCLUDE(iostream)
21 
22 #include "MappedGridFunction.h"
23 #include "MappedGridOperators.h"
24 #include "A++.h"
25 
26 //\begin{>StencilPublic.tex}{\subsubsection{Typedefs}}
27 //\no function header:
28 // These Typedefs are used for more readable code:
29 // {\footnotesize
30 // \begin{verbatim}
31 // typedef IntegerArray Point;
32 // \end{verbatim}
33 // }
34 //
35 // A Point is used for offsets.
36 // The dimension of the Point is internally 1x3,
37 // Methods accept Points 3x1,2x1,1x1,1x2, too.
38 //
39 //\end{StencilPublic.tex}
40 
42 
43 
44 //\begin{>>StencilPublic.tex}{}
45 //\no function header:
46 //
47 // {\footnotesize
48 // \begin{verbatim}
49 // typedef RealMappedGridFunction CoefficientMatrix;
50 // \end{verbatim}
51 // }
52 //
53 //
54 // A CoefficientMatrix is a RealMappedGridFunction which uses the Overture
55 // sparse format. It may be created by a Stencil and may be used by Oges for
56 // solving a system.
57 //
58 //\end{StencilPublic.tex}
59 
60 
62 
63 //\begin{>>StencilPublic.tex}{}
64 //\no function header:
65 //
66 // {\footnotesize
67 // \begin{verbatim}
68 // enum predefinedStencils {
69 // identity,
70 // displacex, // displacex(u_i) -> u_{i+1}
71 // inversedisplacex, // inversedisplacex(u_i) -> u_{i-1}
72 // dplusx,
73 // dminusx,
74 // dzerox,
75 // dnullx,
76 // dplusxdminusx
77 // };
78 // \end{verbatim}
79 // }
80 // {\ff \ST ::predefinedStencils} is an enumerate used for creating
81 // predefined stencils by a special constructor.
82 // Note that these stencils are defined on a grid with spacing 1.
83 //
84 //\end{StencilPublic.tex}
85 
86 class Stencil
87 {
88  friend
89  ostream &operator<<(ostream &os, const Stencil &s);
90 public:
91  // The existing predefined stencils are
94  displacex, // displacex(u_i) -> u_{i+1}
95  inversedisplacex, // inversedisplacex(u_i) -> u_{i-1}
101  };
102 
103 
104  // constructors, operators, and misc methods
105  //==============================================================
106  Stencil();
107  Stencil(RealArray weights_, IntegerArray offsets_); // weights nx1; offsets nx3
108  Stencil(const Stencil&);
109  Stencil(const predefinedStencils &choice);
110  ~Stencil();
111  Stencil &operator= (const Stencil&);
112  Stencil operator() (const Stencil&) const;
113  Stencil operator* (Real d) const;
114  Stencil operator+ (const Stencil &) const;
115  Stencil operator- (const Stencil &) const;
116  Stencil operator- () const;
117  Stencil operator/ (Real d) const;
118  void operator+= (const Stencil&);
119  void operator-= (const Stencil&);
120  void operator*= (Real d);
121  void operator/= (Real d);
122 
123  void addWeight(Real d,const Point &offset);
124 
125  // display shows things depending on 'what':
127 
128  void display(const aString &str="", displayWhat what = text,
129  ostream &os = cout) const;
130  void displayText(const aString &str="", ostream &os = cout) const;
131  void displayStructure(const aString &str="", ostream &os = cout,
132  int legend = FALSE) const;
133 
134 
135  // default rotate: 90 degrees anticlock looking from z-axis
136  Stencil rotate(int axis = axis3, int steps=1) const;
137 
138  // Methods dealing with the Stencil acting on a targetGF
139  //==============================================================
140 
141  // Values taken from sourceGF
142  // Returns reference to targetGF, allows some nicer syntax
144  applyStencil(const RealMappedGridFunction &sourceGF,
145  RealMappedGridFunction &targetGF,
146  const Index &I1,
147  const Index &I2,
148  const Index &I3,
149  const Index &N) const;
150 
151  // Following method used by Operators who assume RealArray, not GridFunction
152  RealArray &
153  applyStencil(const RealArray &sourceGF,
154  RealArray &targetGF,
155  const Index &I1,
156  const Index &I2,
157  const Index &I3,
158  const Index &N) const;
159 
160 
161  // Methods dealing with creation of a discretization Matrix
162  //==============================================================
163 
164  // asserts coeff is an initialized coefficient matrix
165  // It adds weights to coeff (that is, it does not nullify)
166  // For systems, mixed terms can be obtained via iE and iC
167  void
169  const Index &I1,
170  const Index &I2,
171  const Index &I3,
172  const int iE = 0, // Equation
173  const int iC = 0 // Component
174  ) const;
175 
176  // For systems, if the stencil shall be applied on a vector,
177  // this method returns correct operator
178  void
180  const Index &I1,
181  const Index &I2,
182  const Index &I3,
183  const Index &C
184  ) const;
185 
186  // conformOffsets decides how the offsets are numbered.
187  // Its purpose is to generate conform coefficient matrixes
188  // so that the A++ opertor+ works
189  // offsets_ must hold existing offsets
190  void conformOffsets(IntegerArray newOffsets);
191 
192 
193  // Methods giving some info
194  //==============================================================
195  IntegerArray getWidth() const;
196 
197  int offsetExist(const Point &p) const;
198  // if return is TRUE, i holds the index for the point
199  int offsetExist(const Point &p, int &i) const;
200 
201  // Method that estimates the order of the stencil for a known derivative
203 
204  // Remove zeros can be useful
205  void removeZeros();
206 
207  // Internal test routine - not intended to be official
208  void test();
209 
210 private:
211 //\begin{>StencilPrivate.tex}{}
212 //\no function header:
213 //
214 // This is the attributes
215 // { \footnotesize
216 // \begin{verbatim}
217  int nrOfWeights; // number of weights
218  IntegerArray width; // width is 3x2 array
219  RealArray weights; // the weights (nx1) ...
220  IntegerArray offsets; // ... have these offsets (nx3)
221  static int debug; // debug flag
222 // \end{verbatim}
223 // }
224 //\end{StencilPrivate.tex}
225 
226 //\begin{>>StencilPrivate.tex}{}
227 //\no function header:
228 // Here are some private methods:
229 // {\footnotesize
230 // \begin{verbatim}
231  void resizePoint(Point &p) const; // Used to transform p to 1x3
232  void updateWidth(); // Used to update width after some constructions
233  void removeWeight(int i); // Used to remove a (zero) weight
234 // \end{verbatim}
235 // }
236 };
237 //\end{StencilPrivate.tex}
238 
239 Stencil operator*(Real,const Stencil &);
240 
241 #endif