Intrepid
Intrepid_CubatureTensorSortedDef.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Pavel Bochev (pbboche@sandia.gov)
38 // Denis Ridzal (dridzal@sandia.gov), or
39 // Kara Peterson (kjpeter@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
49 namespace Intrepid {
50 
51 template <class Scalar, class ArrayPoint, class ArrayWeight>
52 CubatureTensorSorted<Scalar,ArrayPoint,ArrayWeight>::CubatureTensorSorted(
53  int numPoints, int dimension) {
54  /*
55  This constructor initializes the nodes and weights for an Ndim quadrature
56  rule and sets the nodes and weights lists to zero.
57  */
58  points_.clear(); weights_.clear();
59  numPoints_ = numPoints;
60  dimension_ = dimension;
61 }
62 
63 template <class Scalar, class ArrayPoint, class ArrayWeight>
65  CubatureLineSorted<Scalar> & cubLine) {
66  /*
67  This constructor takes a 1D rule and casts it as a tensor product rule.
68  */
69  dimension_ = 1;
70  numPoints_ = cubLine.getNumPoints();
71  degree_.resize(1);
72  cubLine.getAccuracy(degree_);
73 
74  int loc = 0;
75  std::vector<Scalar> node(1,0.0);
76  typename std::map<Scalar,int>::iterator it;
77  points_.clear(); weights_.clear();
78  for (it = cubLine.begin(); it != cubLine.end(); it++) {
79  node[0] = cubLine.getNode(it);
80  points_.insert(std::pair<std::vector<Scalar>,int>(node,loc));
81  weights_.push_back(cubLine.getWeight(it->second));
82  loc++;
83  }
84 }
85 
86 template <class Scalar, class ArrayPoint, class ArrayWeight>
88  int dimension, std::vector<int> numPoints1D,
89  std::vector<EIntrepidBurkardt> rule1D, bool isNormalized) {
90  /*
91  This constructor builds a tensor product rule according to quadInfo myRule.
92  */
93  TEUCHOS_TEST_FOR_EXCEPTION((dimension!=(int)numPoints1D.size()||
94  dimension!=(int)rule1D.size()),std::out_of_range,
95  ">>> ERROR (CubatureTensorSorted): Dimension mismatch for inputs.");
96 
97  dimension_ = dimension;
98  degree_.resize(dimension);
99  std::vector<int> degree(1,0);
100  CubatureTensorSorted<Scalar> newRule(0,1);
101  for (int i=0; i<dimension; i++) {
102  // Compute 1D rules
103  CubatureLineSorted<Scalar> rule1(rule1D[i],numPoints1D[i],isNormalized);
104  rule1.getAccuracy(degree);
105  degree_[i] = degree[0];
106  // Build Tensor Rule
107  newRule = kron_prod<Scalar>(newRule,rule1);
108  }
109  numPoints_ = newRule.getNumPoints();
110  typename std::map<std::vector<Scalar>,int>::iterator it;
111  points_.clear(); weights_.clear();
112  int loc = 0;
113  std::vector<Scalar> node(dimension_,0.0);
114  for (it=newRule.begin(); it!=newRule.end(); it++) {
115  node = it->first;
116  points_.insert(std::pair<std::vector<Scalar>,int>(node,loc));
117  weights_.push_back(newRule.getWeight(node));
118  loc++;
119  }
120 }
121 
122 template <class Scalar, class ArrayPoint, class ArrayWeight>
124  int dimension, std::vector<int> numPoints1D,
125  std::vector<EIntrepidBurkardt> rule1D,
126  std::vector<EIntrepidGrowth> growth1D,
127  bool isNormalized) {
128  /*
129  This constructor builds a tensor product rule according to quadInfo myRule.
130  */
131  TEUCHOS_TEST_FOR_EXCEPTION((dimension!=(int)numPoints1D.size()||
132  dimension!=(int)rule1D.size()||
133  dimension!=(int)growth1D.size()),std::out_of_range,
134  ">>> ERROR (CubatureTensorSorted): Dimension mismatch for inputs.");
135  dimension_ = dimension;
136  degree_.resize(dimension);
137  std::vector<int> degree(1);
138  CubatureTensorSorted<Scalar> newRule(0,1);
139  for (int i=0; i<dimension; i++) {
140  // Compute 1D rules
141  int numPoints = growthRule1D(numPoints1D[i],growth1D[i],rule1D[i]);
142  CubatureLineSorted<Scalar> rule1(rule1D[i],numPoints,isNormalized);
143  rule1.getAccuracy(degree);
144  degree_[i] = degree[0];
145  // Build Tensor Rule
146  newRule = kron_prod<Scalar>(newRule,rule1);
147  }
148  numPoints_ = newRule.getNumPoints();
149 
150  typename std::map<std::vector<Scalar>,int>::iterator it;
151  points_.clear(); weights_.clear();
152  int loc = 0;
153  std::vector<Scalar> node;
154  for (it=newRule.begin(); it!=newRule.end(); it++) {
155  node = it->first;
156  points_.insert(std::pair<std::vector<Scalar>,int>(node,loc));
157  weights_.push_back(newRule.getWeight(node));
158  loc++;
159  }
160 }
161 
162 template <class Scalar, class ArrayPoint, class ArrayWeight>
164  int dimension, int maxNumPoints,
165  std::vector<EIntrepidBurkardt> rule1D,
166  std::vector<EIntrepidGrowth> growth1D,
167  bool isNormalized) {
168  /*
169  This constructor builds a tensor product rule according to quadInfo myRule.
170  */
171  TEUCHOS_TEST_FOR_EXCEPTION((dimension!=(int)rule1D.size()||
172  dimension!=(int)growth1D.size()),std::out_of_range,
173  ">>> ERROR (CubatureTensorSorted): Dimension mismatch for inputs.");
174  dimension_ = dimension;
175  degree_.resize(dimension);
176  std::vector<int> degree(1);
177  CubatureTensorSorted<Scalar> newRule(0,1);
178  for (int i=0; i<dimension; i++) {
179  // Compute 1D rules
180  int numPoints = growthRule1D(maxNumPoints,growth1D[i],rule1D[i]);
181  CubatureLineSorted<Scalar> rule1(rule1D[i],numPoints,isNormalized);
182  rule1.getAccuracy(degree);
183  degree_[i] = degree[0];
184  // Build Tensor Rule
185  newRule = kron_prod<Scalar>(newRule,rule1);
186  }
187  numPoints_ = newRule.getNumPoints();
188 
189  typename std::map<std::vector<Scalar>,int>::iterator it;
190  points_.clear(); weights_.clear();
191  int loc = 0;
192  std::vector<Scalar> node;
193  for (it=newRule.begin(); it!=newRule.end(); it++) {
194  node = it->first;
195  points_.insert(std::pair<std::vector<Scalar>,int>(node,loc));
196  weights_.push_back(newRule.getWeight(node));
197  loc++;
198  }
199 }
200 
201 /* =========================================================================
202  Access Operator - ruleTP
203  ========================================================================= */
204 template <class Scalar, class ArrayPoint, class ArrayWeight>
206  return numPoints_;
207 } // end getNumPoints
208 
209 template <class Scalar, class ArrayPoint, class ArrayWeight>
211  std::vector<int> & accuracy) const {
212  accuracy = degree_;
213 } // end getAccuracy
214 
215 template <class Scalar, class ArrayPoint, class ArrayWeight>
217  ArrayPoint & cubPoints, ArrayWeight & cubWeights) const {
218 
219  typename std::map<std::vector<Scalar>,int>::const_iterator it;
220  for (it=points_.begin(); it!=points_.end();it++) {
221  for (int j=0; j<dimension_; j++) {
222  cubPoints(it->second,j) = it->first[j];
223  }
224  cubWeights(it->second) = weights_[it->second];
225  }
226 
227  /*
228  typename std::map<std::vector<Scalar>,int>::const_iterator it =
229  points_.begin();
230  for (int i=0; i<numPoints_; i++) {
231  for (int j=0; j<dimension_; j++) {
232  cubPoints(i,j) = it->first[j];
233  }
234  cubWeights(i) = weights_[it->second];
235  it++;
236  }
237  */
238 } // end getCubature
239 
240 template <class Scalar, class ArrayPoint, class ArrayWeight>
242  return dimension_;
243 } // end getDimension
244 
245 template <class Scalar, class ArrayPoint, class ArrayWeight>
246 typename std::map<std::vector<Scalar>,int>::iterator CubatureTensorSorted<Scalar,ArrayPoint,ArrayWeight>::begin() {
247  return points_.begin();
248 }
249 
250 template <class Scalar, class ArrayPoint, class ArrayWeight>
251 typename std::map<std::vector<Scalar>,int>::iterator CubatureTensorSorted<Scalar,ArrayPoint,ArrayWeight>::end() {
252  return points_.end();
253 }
254 
255 template <class Scalar, class ArrayPoint, class ArrayWeight>
257  typename std::map<std::vector<Scalar>,int>::iterator it,
258  std::vector<Scalar> point,
259  Scalar weight) {
260  points_.insert(it,std::pair<std::vector<Scalar>,int>(point,
261  (int)points_.size()));
262  weights_.push_back(weight);
263  numPoints_++;
264  return;
265 }
266 
267 template <class Scalar, class ArrayPoint, class ArrayWeight>
268 std::vector<Scalar> CubatureTensorSorted<Scalar,ArrayPoint,ArrayWeight>::getNode(typename std::map<std::vector<Scalar>,int>::iterator it) {
269  /*
270  Access node for ruleTP
271  */
272  return it->first;
273 }
274 
275 template <class Scalar, class ArrayPoint, class ArrayWeight>
277  int node) {
278  /*
279  Access weight for ruleTP
280  */
281  return weights_[node];
282 }
283 
284 template <class Scalar, class ArrayPoint, class ArrayWeight>
286  std::vector<Scalar> point) {
287  //
288  // Access weight for ruleTP
289  //
290  return weights_[points_[point]];
291 }
292 
293 template <class Scalar, class ArrayPoint, class ArrayWeight>
295  Scalar alpha2,
296  CubatureTensorSorted<Scalar> & cubRule2,
297  Scalar alpha1) {
298 
299  // Initialize an iterator on std::map<std::vector<Scalar>,Scalar>
300  typename std::map<std::vector<Scalar>,int>::iterator it;
301 
302  // Temporary Container for updated rule
303  typename std::map<std::vector<Scalar>,int> newPoints;
304  std::vector<Scalar> newWeights(0,0.0);
305  std::vector<Scalar> node(dimension_,0.0);
306  int loc = 0;
307 
308  // Intersection of rule1 and rule2
309  typename std::map<std::vector<Scalar>,int> inter;
310  std::set_intersection(points_.begin(),points_.end(),
311  cubRule2.begin(),cubRule2.end(),
312  inserter(inter,inter.begin()),inter.value_comp());
313  for (it=inter.begin(); it!=inter.end(); it++) {
314  node = it->first;
315  newWeights.push_back( alpha1*weights_[it->second]
316  +alpha2*cubRule2.getWeight(node));
317  newPoints.insert(std::pair<std::vector<Scalar>,int>(node,loc));
318  //points_.erase(node); cubRule2.erase(node);
319  loc++;
320  }
321  int isize = inter.size();
322 
323  // Set Difference rule1 \ rule2
324  int size = points_.size();
325  if (isize!=size) {
326  typename std::map<std::vector<Scalar>,int> diff1;
327  std::set_difference(points_.begin(),points_.end(),
328  cubRule2.begin(),cubRule2.end(),
329  inserter(diff1,diff1.begin()),diff1.value_comp());
330  for (it=diff1.begin(); it!=diff1.end(); it++) {
331  node = it->first;
332  newWeights.push_back(alpha1*weights_[it->second]);
333  newPoints.insert(std::pair<std::vector<Scalar>,int>(node,loc));
334  loc++;
335  }
336  }
337 
338  // Set Difference rule2 \ rule1
339  size = cubRule2.getNumPoints();
340  if (isize!=size) {
341  typename std::map<std::vector<Scalar>,int> diff2;
342  std::set_difference(cubRule2.begin(),cubRule2.end(),
343  points_.begin(),points_.end(),
344  inserter(diff2,diff2.begin()),diff2.value_comp());
345  for (it=diff2.begin(); it!=diff2.end(); it++) {
346  node = it->first;
347  newWeights.push_back(alpha2*cubRule2.getWeight(it->second));
348  newPoints.insert(std::pair<std::vector<Scalar>,int>(node,loc));
349  loc++;
350  }
351  }
352 
353  points_.clear(); points_.insert(newPoints.begin(),newPoints.end());
354  weights_.clear(); weights_.assign(newWeights.begin(),newWeights.end());
355  numPoints_ = (int)points_.size();
356 }
357 
358 template <class Scalar, class ArrayPoint, class ArrayWeight>
360  Scalar sum = 0.0;
361 
362  typename std::vector<Scalar>::iterator it;
363  for (it=weights_.begin(); it!=weights_.end(); it++)
364  sum += *it;
365 
366  for (it=weights_.begin(); it!=weights_.end(); it++)
367  *it /= sum;
368 }
369 
370 
371 /* ======================================================================
372  Kronecker Products
373  ====================================================================== */
374 template <class Scalar>
376  CubatureLineSorted<Scalar> & rule2) {
377  /*
378  Compute the Kronecker Product of a Tensor Product rule and a 1D rule.
379  */
380  int s1 = rule1.getNumPoints();
381  // int s2 = rule2.getNumPoints();
382  int Ndim = rule1.getDimension();
383 
384  if (s1==0) {
385  CubatureTensorSorted<Scalar> TPrule(rule2);
386  return TPrule;
387  }
388  else {
389  // Initialize Arrays Containing Updated Nodes and Weights
390  CubatureTensorSorted<Scalar> TPrule(0,Ndim+1);
391 
392  Scalar weight = 0.0;
393  Scalar node2 = 0.0;
394 
395  // Perform Kronecker Products
396  // Compute Kronecker Product of Nodes
397  typename std::map<std::vector<Scalar>,int>::iterator it = TPrule.begin();
398  typename std::map<std::vector<Scalar>,int>::iterator it_i;
399  typename std::map<Scalar,int>::iterator it_j;
400  for (it_i=rule1.begin(); it_i!=rule1.end(); it_i++) {
401  for (it_j=rule2.begin(); it_j!=rule2.end(); it_j++) {
402  std::vector<Scalar> node = rule1.getNode(it_i);
403  node2 = rule2.getNode(it_j);
404  weight = rule1.getWeight(node)*rule2.getWeight(node2);
405  node.push_back(node2);
406  TPrule.insert(it,node,weight);
407  it = TPrule.end();
408  }
409  }
410  return TPrule;
411  }
412 }
413 } // end Intrepid namespace
int getNumPoints() const
Returns the number of cubature points.
void normalize()
Normalize CubatureLineSorted weights.
std::map< std::vector< Scalar >, int >::iterator begin()
Initiate iterator at the beginning of data.
Utilizes cubature (integration) rules contained in the library sandia_rules (John Burkardt...
std::map< Scalar, int >::iterator begin(void)
Initiate iterator at the beginning of data.
Scalar getWeight(int node)
Get a specific weight described by the integer location.
std::vector< Scalar > getNode(typename std::map< std::vector< Scalar >, int >::iterator it)
Get a specific node described by the iterator location.
void getAccuracy(std::vector< int > &accuracy) const
Returns max. degree of polynomials that are integrated exactly. The return vector has size 1...
void update(Scalar alpha2, CubatureTensorSorted< Scalar > &cubRule2, Scalar alpha1)
Replace CubatureLineSorted values with "this = alpha1*this+alpha2*cubRule2".
int getNumPoints() const
Returns the number of cubature points.
void insert(typename std::map< std::vector< Scalar >, int >::iterator it, std::vector< Scalar > point, Scalar weight)
Insert a node and weight into data near the iterator position.
int getDimension() const
Returns dimension of domain of integration.
Scalar getNode(typename std::map< Scalar, int >::iterator it)
Get a specific node described by the iterator location.
void getAccuracy(std::vector< int > &accuracy) const
Returns max. degree of polynomials that are integrated exactly. The return vector has size 1...
Utilizes 1D cubature (integration) rules contained in the library sandia_rules (John Burkardt...
void getCubature(ArrayPoint &cubPoints, ArrayWeight &cubWeights) const
Returns cubature points and weights (return arrays must be pre-sized/pre-allocated).
Scalar getWeight(int weight)
Get a specific weight described by the integer location.
std::map< std::vector< Scalar >, int >::iterator end()
Initiate iterator at the end of data.
std::map< Scalar, int >::iterator end(void)
Initiate iterator at the end of data.