ScalarProducts.hpp
Go to the documentation of this file.
1#ifndef OPM_SCALAR_PRODUCTS_HEADER_INCLUDED
2#define OPM_SCALAR_PRODUCTS_HEADER_INCLUDED
3
5
6namespace Dune
7{
8
14template<class Vector, class Comm>
15class GhostLastScalarProduct : public ScalarProduct<Vector>
16{
17 public:
18
24 GhostLastScalarProduct (std::shared_ptr<const Comm> com, SolverCategory::Category cat)
25 : _communication(com), _category(cat)
26 {
27 count_ = getLocalCount(); // number or local cells
28 int verify = verifyLocalCount(); // redundant check on numbef of local cells
29 if (count_ != verify) OPM_THROW(std::runtime_error, "Inconsistent local node count!!\n");
30 }
31
38 GhostLastScalarProduct (const Comm& com, SolverCategory::Category cat)
39 : GhostLastScalarProduct(stackobject_to_shared_ptr(com), cat)
40 {}
41
46 double dot (const Vector& vx, const Vector& vy) const override
47 {
48
49 // access underlying data
50 double const *x = &vx[0][0];
51 double const *y = &vy[0][0];
52
53 // total array length
54 int NN = block_size*count_;
55
56 auto cc = _communication->communicator();
57 return cc.sum(vec_dot(x,y,NN));
58 }
59
63 double norm (const Vector& vx) const override
64 {
65 return sqrt(dot(vx,vx));
66 }
67
69 virtual SolverCategory::Category category() const override
70 {
71 return _category;
72 }
73
74 private:
75
77 static constexpr auto block_size = Vector::block_type::dimension;
78
79 std::shared_ptr<const Comm> _communication;
80 SolverCategory::Category _category;
81 int count_;
82
85 int getLocalCount() const
86 {
87 int count = 0;
88 // Loop over index set
89 auto indexSet = _communication->indexSet();
90 for (auto idx = indexSet.begin(); idx!=indexSet.end(); ++idx) {
91 if (idx->local().attribute()==1) count++; // count non-local indices
92 }
93 return count;
94 }
95
98 int verifyLocalCount() const
99 {
100 auto indexSet = _communication->indexSet();
101
102 size_t is = 0;
103 // Loop over index set
104 for (auto idx = indexSet.begin(); idx!=indexSet.end(); ++idx) {
105 //Only take "owner" indices
106 if (idx->local().attribute()==1) {
107 //get local index
108 auto loc = idx->local().local();
109 // if loc is higher than "old interior size", update it
110 if (loc > is) {
111 is = loc;
112 }
113 }
114 }
115 return is + 1; //size is plus 1 since we start at 0
116 }
117
118};
119
120
121
124template<class Vector>
125class SeqOptmizedProduct : public Dune::SeqScalarProduct<Vector>
126{
127public:
128
133 double dot(const Vector& vx, const Vector& vy) const override
134 {
135 // access underlying data
136 double const *x = &vx[0][0];
137 double const *y = &vy[0][0];
138
139 // total array length
140 int NN = block_size*vx.N();
141
142 return vec_dot(x,y,NN);
143 }
144
148 double norm(const Vector& vx) const override {
149 return std::sqrt(this->dot(vx, vx));
150 }
151
152 private:
153
154 // extract block size
155 static constexpr auto block_size = Vector::block_type::dimension;
156
157
158};
159
160}
161
162#endif //OPM_SCALAR_PRODUCTS_HEADER_INCLUDED
163
Dune::OwnerOverlapCopyCommunication< int, int > Comm
Definition: FlexibleSolver_impl.hpp:394
Definition: ScalarProducts.hpp:16
double norm(const Vector &vx) const override
Vector L2-norm.
Definition: ScalarProducts.hpp:63
GhostLastScalarProduct(const Comm &com, SolverCategory::Category cat)
constructor
Definition: ScalarProducts.hpp:38
GhostLastScalarProduct(std::shared_ptr< const Comm > com, SolverCategory::Category cat)
constructor
Definition: ScalarProducts.hpp:24
virtual SolverCategory::Category category() const override
Category of the scalar product (see SolverCategory::Category)
Definition: ScalarProducts.hpp:69
double dot(const Vector &vx, const Vector &vy) const override
Dot product of two vectors.
Definition: ScalarProducts.hpp:46
Definition: ScalarProducts.hpp:126
double norm(const Vector &vx) const override
Vector L2-norm.
Definition: ScalarProducts.hpp:148
double dot(const Vector &vx, const Vector &vy) const override
Dot product of two vectors.
Definition: ScalarProducts.hpp:133
double vec_dot(double const *x, double const *y, int NN)
Definition: fvbaseprimaryvariables.hh:161