MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Stencil.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2011 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include <vector>
11 #include <cassert>
12 using namespace std;
13 #include "Stencil.h"
14 
16 // utility funcs
18 void stencil1( vector< double >& f, int index, unsigned int n,
19  double invSq,
20  const vector< vector< double > >& S,
21  const vector< double >& diffConst )
22 {
23  const vector< double >& t0 = S[ index ];
24  if ( index - static_cast< int >( n ) < 0 ) {
25  const vector< double >& temp = S[ index + n ];
26  for ( unsigned int i = 0; i < f.size(); ++i ) {
27  f[i] += 2 * diffConst[i] * ( temp[i] - t0[i] ) * invSq;
28  }
29  } else if ( index + n >= S.size() ) {
30  const vector< double >& temp = S[ index - n ];
31  for ( unsigned int i = 0; i < f.size(); ++i ) {
32  f[i] += 2 * diffConst[i] * ( temp[i] - t0[i] ) * invSq;
33  }
34  } else {
35  const vector< double >& tminus = S[ index - n ];
36  const vector< double >& tplus = S[ index + n ];
37  for ( unsigned int i = 0; i < f.size(); ++i ) {
38  f[i] += diffConst[i] * ( tminus[i] + tplus[i] - 2 * t0[i] ) * invSq;
39  }
40  }
41 }
42 
43 
55 void stencilN( vector< double >& f, int index, unsigned int n, int offset,
56  double invSq, const vector< vector< double > >& S,
57  const vector< double >& diffConst )
58 {
59  const vector< double >& t0 = S[ index ];
60  int rem = index % n;
61  if ( rem < offset ) {
62  const vector< double >& temp = S[ index + offset];
63  for ( unsigned int i = 0; i < f.size(); ++i ) {
64  f[i] += 2 * diffConst[i] * ( temp[i] - t0[i] ) * invSq;
65  }
66  } else if ( rem >= static_cast< int >( n ) - offset ) {
67  const vector< double >& temp = S[ index - offset ];
68  for ( unsigned int i = 0; i < f.size(); ++i ) {
69  f[i] += 2 * diffConst[i] * ( temp[i] - t0[i] ) * invSq;
70  }
71  } else {
72  const vector< double >& tminus = S[ index - offset ];
73  const vector< double >& tplus = S[ index + offset ];
74  for ( unsigned int i = 0; i < f.size(); ++i ) {
75  f[i] += diffConst[i] * ( tminus[i] + tplus[i] - 2 * t0[i] ) * invSq;
76  }
77  }
78 }
79 
81 // Stencil constructor
83 
85 {;}
86 
88 {;}
89 
91 // dummy stencil.
94 {;}
95 
97 {;}
98 
99 void DummyStencil::addFlux( unsigned int meshIndex, vector< double >& f,
100  const vector< vector< double > >& S,
101  const vector< double >& diffConst ) const
102 {;}
103 
105 // 1-D stencil.
107 
108 
110  : h_( dx )
111 {
112  if ( dx <= 0 ) {
113  h_ = 1;
114  invHsq_ = 1;
115  } else {
116  invHsq_ = 1.0 / ( dx * dx );
117  }
118 }
119 
120 
122 {;}
123 
124 void LineStencil::addFlux( unsigned int meshIndex,
125  vector< double >& f, const vector< vector< double > >& S,
126  const vector< double >& diffConst ) const
127 {
128  assert( f.size() <= S[0].size() );
129  if ( S.size() < 2 )
130  return;
131  assert( meshIndex < S.size() );
132 
133  int index = meshIndex;
134  stencil1( f, index, 1, invHsq_, S, diffConst ); // Diffusion in x
135 }
136 
138 // 2-D stencil.
140 
141 RectangleStencil::RectangleStencil( double dx, double dy, unsigned int nx )
142  : dx_( dx ), dy_( dy ), nx_( nx )
143 {
144  if ( dx <= 0 ) {
145  dx_ = 1;
146  invDxSq_ = 1;
147  } else {
148  invDxSq_ = 1.0 / ( dx * dx );
149  }
150 
151  if ( dy <= 0 ) {
152  dy_ = 1;
153  invDySq_ = 1;
154  } else {
155  invDySq_ = 1.0 / ( dy * dy );
156  }
157 }
158 
160 {;}
161 
162 // Assumes diffusion in xy plane
163 void RectangleStencil::addFlux( unsigned int meshIndex,
164  vector< double >& f, const vector< vector< double > >& S,
165  const vector< double >& diffConst ) const
166 {
167  assert( f.size() <= S[0].size() );
168  if ( S.size() < 2 * nx_ )
169  return;
170  assert( meshIndex < S.size() );
171  int index = meshIndex;
172  stencilN( f, index, nx_, 1, invDxSq_, S, diffConst ); // Diffusion in x
173  stencil1( f, index, nx_, invDySq_, S, diffConst ); // Diffusion in y
174 }
175 
177 // 3-D stencil.
179 
180 CuboidStencil::CuboidStencil( double dx, double dy, double dz,
181  unsigned int nx, unsigned int ny )
182  : dx_( dx ), dy_( dy ), nx_( nx ), ny_( ny )
183 {
184  if ( dx <= 0 ) {
185  dx_ = 1;
186  invDxSq_ = 1;
187  } else {
188  invDxSq_ = 1.0 / ( dx * dx );
189  }
190 
191  if ( dy <= 0 ) {
192  dy_ = 1;
193  invDySq_ = 1;
194  } else {
195  invDySq_ = 1.0 / ( dy * dy );
196  }
197 
198  if ( dz <= 0 ) {
199  dz_ = 1;
200  invDzSq_ = 1;
201  } else {
202  invDzSq_ = 1.0 / ( dz * dz );
203  }
204 }
205 
206 
208 {;}
209 
210 // Assumes diffusion in xy plane
211 void CuboidStencil::addFlux( unsigned int meshIndex,
212  vector< double >& f, const vector< vector< double > >& S,
213  const vector< double >& diffConst ) const
214 {
215  assert( f.size() <= S[0].size() );
216  if ( S.size() < 2 * nx_ )
217  return;
218  assert( meshIndex < S.size() );
219  int index = meshIndex;
220  stencilN( f, index, nx_, 1, invDxSq_, S, diffConst ); // Diffusion in x
221  stencilN( f, index, nx_*ny_, nx_, invDxSq_, S, diffConst ); // Diff in y
222  stencil1( f, index, nx_*ny_, invDzSq_, S, diffConst ); // Diffusion in z
223 }
224 
double h_
Definition: Stencil.h:59
double invHsq_
Definition: Stencil.h:60
LineStencil(double h)
Definition: Stencil.cpp:109
RectangleStencil(double dx, double dy, unsigned int nx)
Definition: Stencil.cpp:141
void addFlux(unsigned int meshIndex, vector< double > &f, const vector< vector< double > > &S, const vector< double > &diffConst) const
Definition: Stencil.cpp:163
unsigned int ny_
Definition: Stencil.h:103
void addFlux(unsigned int meshIndex, vector< double > &f, const vector< vector< double > > &S, const vector< double > &diffConst) const
Definition: Stencil.cpp:211
CuboidStencil(double dx, double dy, double dz, unsigned int nx, unsigned int ny)
Definition: Stencil.cpp:180
void addFlux(unsigned int meshIndex, vector< double > &f, const vector< vector< double > > &S, const vector< double > &diffConst) const
Definition: Stencil.cpp:99
virtual ~Stencil()
Definition: Stencil.cpp:87
double invDxSq_
Definition: Stencil.h:99
double invDzSq_
Definition: Stencil.h:101
void stencil1(vector< double > &f, int index, unsigned int n, double invSq, const vector< vector< double > > &S, const vector< double > &diffConst)
Definition: Stencil.cpp:18
unsigned int nx_
Definition: Stencil.h:102
Stencil()
Definition: Stencil.cpp:84
void stencilN(vector< double > &f, int index, unsigned int n, int offset, double invSq, const vector< vector< double > > &S, const vector< double > &diffConst)
Definition: Stencil.cpp:55
double invDySq_
Definition: Stencil.h:79
double dx_
Definition: Stencil.h:76
void addFlux(unsigned int meshIndex, vector< double > &f, const vector< vector< double > > &S, const vector< double > &diffConst) const
Definition: Stencil.cpp:124
unsigned int nx_
Definition: Stencil.h:80
double dx_
Definition: Stencil.h:96
double dy_
Definition: Stencil.h:97
double invDySq_
Definition: Stencil.h:100
double dz_
Definition: Stencil.h:98
double invDxSq_
Definition: Stencil.h:78
double dy_
Definition: Stencil.h:77