ROL
ROL_QuadratureDef.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) 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 lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 
50 namespace ROL {
51 
52 template <class Real>
53 Quadrature<Real>::Quadrature(int dimension) : dimension_(dimension) {
54  points_.clear(); weights_.clear(); accuracy_.clear();
55  accuracy_.resize(dimension);
56 }
57 
58 template <class Real>
59 void Quadrature<Real>::addPointAndWeight(std::vector<Real> point, Real weight, int loc) {
60  this->points_.insert(std::pair<std::vector<Real>,int>(point,loc));
61  this->weights_.push_back(weight);
62 }
63 
64 template <class Real>
66  return this->weights_.size();
67 }
68 
69 template <class Real>
70 void Quadrature<Real>::getAccuracy(std::vector<int> & accuracy) const {
71  accuracy = this->accuracy_;
72 }
73 
74 template <class Real>
75 void Quadrature<Real>::getCubature(std::vector<std::vector<Real> >& points,
76  std::vector<Real>& weights) const {
77  points.resize(this->weights_.size());
78  weights.resize(this->weights_.size());
79  typename std::map<std::vector<Real>,int>::const_iterator it;
80  for (it=(this->points_).begin(); it!=(this->points_).end();it++) {
81  points[it->second] = it->first;
82  weights[it->second] = (this->weights_)[it->second];
83  }
84 }
85 
86 template <class Real>
88  return this->dimension_;
89 }
90 
91 template <class Real>
92 typename std::map<std::vector<Real>,int>::iterator Quadrature<Real>::begin() {
93  return (this->points_).begin();
94 }
95 
96 template <class Real>
97 typename std::map<std::vector<Real>,int>::iterator Quadrature<Real>::end() {
98  return (this->points_).end();
99 }
100 
101 template <class Real>
102 void Quadrature<Real>::insert(typename std::map<std::vector<Real>,int>::iterator it,
103  std::vector<Real> point, Real weight) {
104  (this->points_).insert(it,std::pair<std::vector<Real>,int>(point,(int)(this->points_).size()));
105  (this->weights_).push_back(weight);
106 }
107 
108 template <class Real>
109 std::vector<Real> Quadrature<Real>::getNode(typename std::map<std::vector<Real>,int>::iterator it) {
110  return it->first;
111 }
112 
113 template <class Real>
115  return (this->weights_)[node];
116 }
117 
118 template <class Real>
119 Real Quadrature<Real>::getWeight(std::vector<Real> point) {
120  return (this->weights_)[(this->points_)[point]];
121 }
122 
123 template <class Real>
125  if ( rule.begin() != rule.end() ) {
126  // Initialize an iterator on std::map<std::vector<Real>,Real>
127  typename std::map<std::vector<Real>,int>::iterator it;
128  typename std::map<std::vector<Real>,int>::iterator it2;
129 
130  // Get location of last point and weight
131  int loc = this->points_.size();
132 
133  // Intersection of rule1 and rule2 and set difference rule2 \ rule1
134  for ( it2=rule.begin(); it2!=rule.end(); it2++ ) {
135  it = (this->points_).find(it2->first);
136  if ( it != (this->points_).end() ) {
137  (this->weights_)[it->second] += alpha*rule.getWeight(it2->second);
138  }
139  else {
140  (this->points_).insert(std::pair<std::vector<Real>,int>(it2->first,loc));
141  (this->weights_).push_back(alpha*rule.getWeight(it2->second));
142  loc++;
143  }
144  }
145  }
146 }
147 
148 template <class Real>
150  Real sum = 0.0;
151  typename std::vector<Real>::iterator it;
152  for (it=weights_.begin(); it!=weights_.end(); it++) {
153  sum += *it;
154  }
155  for (it=weights_.begin(); it!=weights_.end(); it++) {
156  *it /= sum;
157  }
158 }
159 
160 } // end ROL namespace
virtual std::vector< Real > getNode(typename std::map< std::vector< Real >, int >::iterator it)
const double weights[4][5]
Definition: ROL_Types.hpp:1001
virtual void getCubature(std::vector< std::vector< Real > > &cubPoints, std::vector< Real > &cubWeights) const
virtual void insert(typename std::map< std::vector< Real >, int >::iterator it, std::vector< Real > point, Real weight)
virtual int getDimension() const
virtual void getAccuracy(std::vector< int > &accuracy) const
std::vector< Real > weights_
virtual void normalize()
virtual void update(Real alpha, Quadrature< Real > &rule)
virtual std::map< std::vector< Real >, int >::iterator end()
void addPointAndWeight(std::vector< Real > point, Real weight, int loc)
std::map< std::vector< Real >, int > points_
Quadrature(int dimension=1)
virtual Real getWeight(int node)
std::vector< int > accuracy_
virtual int getNumPoints() const
virtual std::map< std::vector< Real >, int >::iterator begin()