summaryrefslogtreecommitdiff
path: root/src/functors/lattice/superCalcF2D.hh
blob: cd63f395ebf71746f7020b6aba9d32a8b5c7e94a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*  This file is part of the OpenLB library
 *
 *  Copyright (C) 2014-2017 Albert Mink, Mathias J. Krause,
 *                          Adrian Kummerlaender
 *  E-mail contact: info@openlb.net
 *  The most recent release of OpenLB can be downloaded at
 *  <http://www.openlb.net/>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301, USA.
*/

#ifndef SUPER_CALC_F_2D_HH
#define SUPER_CALC_F_2D_HH

#include "superCalcF2D.h"
#include "blockCalcF2D.h"
#include "superConst2D.h"

namespace olb {


template <typename T, typename W, template<typename> class F>
SuperCalcF2D<T,W,F>::SuperCalcF2D(FunctorPtr<SuperF2D<T,W>>&& f,
                                  FunctorPtr<SuperF2D<T,W>>&& g)
  : SuperF2D<T,W>(
      f->getSuperStructure(),
      f->getTargetDim() > g->getTargetDim() ? f->getTargetDim() : g->getTargetDim()),
    _f(std::move(f)),
    _g(std::move(g))
{
  OLB_ASSERT(
    _f->getTargetDim() == _g->getTargetDim() || _f->getTargetDim() == 1 || _g->getTargetDim() == 1,
    "Componentwise operation must be well defined.");

  this->getName() = "(" + _f->getName() + F<T>::symbol + _g->getName() + ")";

  std::swap(_f->_ptrCalcC, this->_ptrCalcC);

  LoadBalancer<T>& load = _f->getSuperStructure().getLoadBalancer();

  if ( _f->getBlockFSize() == load.size() ) {
    if ( _g->getBlockFSize() == load.size() ) {
      // both functors expose the correct count of block level functors
      for (int iC = 0; iC < load.size(); ++iC) {
        this->_blockF.emplace_back(
          new BlockCalcF2D<W,F>(_f->getBlockF(iC), _g->getBlockF(iC))
        );
      }
    }
    else {
      // operate on super functor `g` and block level functors provided by `f`
      for (int iC = 0; iC < load.size(); ++iC) {
        this->_blockF.emplace_back(
          new BlockCalcF2D<W,F>(_f->getBlockF(iC), *g, load.glob(iC))
        );
      }
    }
  }
  else if ( _g->getBlockFSize() == load.size() ) {
    // operate on block level functors provided by `f` and super functor `g`
    for (int iC = 0; iC < load.size(); ++iC) {
      this->_blockF.emplace_back(
        new BlockCalcF2D<W,F>(*f, load.glob(iC), _g->getBlockF(iC))
      );
    }
  }
}

template <typename T, typename W, template<typename> class F>
SuperCalcF2D<T,W,F>::SuperCalcF2D(W scalar, FunctorPtr<SuperF2D<T,W>>&& g)
  : SuperCalcF2D(
      std::unique_ptr<SuperF2D<T,W>>(new SuperConst2D<T,W>(g->getSuperStructure(), scalar)),
      std::forward<decltype(g)>(g))
{ }

template <typename T, typename W, template<typename> class F>
SuperCalcF2D<T,W,F>::SuperCalcF2D(FunctorPtr<SuperF2D<T,W>>&& f, W scalar)
  : SuperCalcF2D(
      std::forward<decltype(f)>(f),
      std::unique_ptr<SuperF2D<T,W>>(new SuperConst2D<T,W>(f->getSuperStructure(), scalar)))
{ }

template <typename T, typename W, template<typename> class F>
bool SuperCalcF2D<T,W,F>::operator()(W output[], const int input[])
{
  if ( _f->getTargetDim() == 1 || _g->getTargetDim() == 1 ) {
    // scalar operation
    W scalar;
    if ( _f->getTargetDim() == 1 ) {
      // apply the scalar f to possibly multidimensional g
      _f(&scalar, input);
      _g(output, input);

      for (int i = 0; i < this->getTargetDim(); i++) {
        output[i] = F<T>()(scalar, output[i]);
      }
    }
    else {
      // apply scalar g to possibly multidimensional f
      _f(output, input);
      _g(&scalar, input);

      for (int i = 0; i < this->getTargetDim(); i++) {
        output[i] = F<T>()(output[i], scalar);
      }
    }
  }
  else {
    // componentwise operation on equidimensional functors
    W* outputF = output;
    W outputG[this->getTargetDim()];

    _f(outputF, input);
    _g(outputG, input);

    for (int i = 0; i < this->getTargetDim(); i++) {
      output[i] = F<T>()(outputF[i], outputG[i]);
    }
  }
  return true;
}


template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator+(std::shared_ptr<SuperF2D<T,W>> lhs, std::shared_ptr<SuperF2D<T,W>> rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcPlus2D<T,W>(std::move(lhs), std::move(rhs)));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator+(std::shared_ptr<SuperF2D<T,W>> lhs, W rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcPlus2D<T,W>(std::move(lhs), rhs));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator+(W lhs, std::shared_ptr<SuperF2D<T,W>> rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcPlus2D<T,W>(lhs, std::move(rhs)));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator-(std::shared_ptr<SuperF2D<T,W>> lhs, std::shared_ptr<SuperF2D<T,W>> rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcMinus2D<T,W>(std::move(lhs), std::move(rhs)));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator-(std::shared_ptr<SuperF2D<T,W>> lhs, W rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcMinus2D<T,W>(std::move(lhs), rhs));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator-(W lhs, std::shared_ptr<SuperF2D<T,W>> rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcMinus2D<T,W>(lhs, std::move(rhs)));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator*(std::shared_ptr<SuperF2D<T,W>> lhs, std::shared_ptr<SuperF2D<T,W>> rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcMultiplication2D<T,W>(std::move(lhs), std::move(rhs)));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator*(std::shared_ptr<SuperF2D<T,W>> lhs, W rhs)
{
  return std::shared_ptr<SuperF2D<T,W>>(
           new SuperCalcMultiplication2D<T,W>(std::move(lhs), rhs));
}

template <typename T, typename W>
std::shared_ptr<SuperF2D<T,W>> operator*(W lhs, std::shared_ptr<SuperF2D<T,W>> rhs)
{
  return std::shared_ptr<SuperF2D<