xAssembler.h 23.8 KB
Newer Older
1
/*
2 3
    This file is a part of eXlibris C++ Library
    under the GNU Lesser General Public License.
4
    See the NOTICE.md & LICENSE.md files for terms
5
    and conditions.
Greg's avatar
Greg committed
6
*/
7

Greg's avatar
Greg committed
8 9 10 11
#ifndef _ASSEMBLER_TEST_H
#define _ASSEMBLER_TEST_H

#include <vector>
12

Greg's avatar
Greg committed
13 14
#include "xAssemblerDispatcher.h"
#include "xAssemblerTraitPolicy.h"
15
#include "xDataType.h"
16
#include "xValue.h"
Greg's avatar
Greg committed
17

18
namespace xlinalg
Greg's avatar
Greg committed
19
{
20 21 22
class xCSRMatrix;
class xCSRVector;
}  // namespace xlinalg
Greg's avatar
Greg committed
23 24 25 26 27 28

namespace xfem
{
/*!
the following define the Assembler Concept. The function needed by the Assemble algorithm from xAlgorithm.h
Note that most member function are template, which prevent any of them to be virtual : No dynamic polymorphism allowed !
29
This way we can assemble any type of local data.
30
The only alternative to be able to do that was to use some kind of type earasure, for example by writting the interface in term of
31 32
std::function <double (int, int) > to have a uniform signature whatever the type of local Matrix/ Vector ...  The problem is that
then the access to a given term was taking too long, and no latter optimisation like block assembly for instance would be possible
Greg's avatar
Greg committed
33 34 35 36


class xAssemblerConcept {
public:
37 38 39 40 41 42
 template<typename LOCALMATRIX, typename ITERDOF>
 void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr);
 template<typename LOCALMATRIX, typename ITERDOF>
 void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr);
 template <typename LOCALVECTOR, typename ITERDOF>
 void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect);
Greg's avatar
Greg committed
43 44 45 46
 template <typename LOCALSCALAR>
 void assemble_scalar(const LOCALSCALAR& scal);
 void setCoeff(const double& c) { coeff = c; }
 };
47 48 49 50 51 52 53 54 55 56

 Here LOCALMATRIX concept must provides at least :
 A parentheses operator that takes two integers and return the term associated to these arguments in the matrix
 In one xAssembler (xAssemblerLumpedBlockEqu) two extra methods getNbRow() and getNbCol() are also required. To clarified this
 concept we may consider these methods as mandatory ones, for all xAssembler. Or xAssemblerLumpedBlockEqu may be changed to avoid
 use of getNbCol()

 Here LOCALVECTOR concept must provides :
 A [] operator that takes one integer and return the term associated to this argument in the vector

Greg's avatar
Greg committed
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
// Defining here the assemble methode give the possibility to optimize the loop on elementary matrix matr by just dispaching
// needed part of the matr (for sym, only half of the matix,...) This concerne only the "potential" symetric matrix as invocated
// by Assemble with only trial field pointeur value iterator. In case of a receiving global symetric matrix this implicitely act
// as if matr is symetric and it's upper triangle is defined. In case of a receiving global unsymetric matrix this implicitely act
// as if matr is unsymetric and it's fully defined. In case of a receiving global diagonal matrix this implicitely act as if matr
// is at least fully defined on it' diagonal. It's up to xFemMatrix matrix class to fullfill these requirements with for now a
// indexation of a full matrix. If this last point is changed policies and immplementation shoul be reconsidered
//

/*!This template class permits to define the data for the different implementation of the xAssembler concept defined in this file
 * : xAssemblerBasic, xAssembleBasicAndTranspose, xAssemblerLumped, xAssemblerLumpedEqu  !*/
template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerBaseData
{
  public:
   typedef M matrix_type;
   typedef V vector_type;
   typedef S scalar_type;
   xAssemblerBaseData(M* A, V* b, S* s) : mat(A), vec(b), sca(s), coeff(1.) {}
   void setCoeff(const S& c) { coeff = c; }
   void setTarget(M& A, V& b, S& s)
   {
      mat = &A;
      vec = &b;
      sca = &s;
   }
   void setTarget(M& A, V& b)
   {
      mat = &A;
      vec = &b;
      sca = nullptr;
   }
   void setTarget(M& A)
   {
      mat = &A;
      vec = nullptr;
      sca = nullptr;
   }
   void setTarget(V& b)
   {
      mat = 0;
      vec = &b;
      sca = 0;
   }
   void setTarget(S& s)
   {
      mat = 0;
      vec = 0;
      sca = &s;
   }

  protected:
   M* mat;
   V* vec;
   S* sca;
   S coeff;
};

/*! This is a basic implementation of the xAssembler concept. !*/
template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerBasic : public xAssemblerBaseData<M, V, S>
{
  public:
   xAssemblerBasic(M& A, V& b, S& s) : xAssemblerBaseData<M, V, S>(&A, &b, &s) {}
   xAssemblerBasic(M& A, V& b) : xAssemblerBaseData<M, V, S>(&A, &b, nullptr) {}
   xAssemblerBasic(M& A) : xAssemblerBaseData<M, V, S>(&A, nullptr, nullptr) {}
   xAssemblerBasic(V& b) : xAssemblerBaseData<M, V, S>(nullptr, &b, nullptr) {}
   xAssemblerBasic(S& s) : xAssemblerBaseData<M, V, S>(0, 0, &s) {}
   xAssemblerBasic() : xAssemblerBaseData<M, V, S>(nullptr, nullptr, nullptr) {}

   /// Functions from the xAssembleConcept
   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr)
   {
133
      ITERDOF firstj_bak = firstj;
134 135 136 137 138 139 140 141 142 143
      for (int i = 0; firsti != lasti; ++firsti, ++i)
      {
         firstj = firstj_bak;
         for (int j = 0; firstj != lastj; ++firstj, ++j)
         {
            auto val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firstj)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
Greg's avatar
Greg committed
144
      }
145
   }
146

147 148 149
   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr)
   {
150
      ITERDOF firsti = first, firstj, lastj;
Greg's avatar
Greg committed
151
      int j;
152 153 154 155 156 157 158 159 160 161 162
      for (int i = 0; firsti != last; ++firsti, ++i)
      {
         xPolicyAssemblerLoop<typename M::matrix_pattern>::firstForLoop(first, firsti, firstj, i, j);
         lastj = xPolicyAssemblerLoop<typename M::matrix_pattern>::lastForLoop(last, firsti);
         for (; firstj != lastj; ++firstj, ++j)
         {
            auto val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firstj)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
Greg's avatar
Greg committed
163
      }
164 165 166 167 168 169 170
   }

   template <typename LOCALVECTOR, typename ITERDOF>
   void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect)
   {
      for (int i = 0; first != last; ++first, ++i)
      {
171
         auto val = vect[i] * this->coeff;
172 173
         if (val != xtool::xDataType<S>::zero())
            xMatVecScaAssembler<M, V, S>::assemble((*first)->getState(), val, this->vec, this->sca);
Greg's avatar
Greg committed
174
      }
175
   }
176

177 178 179
   template <typename LOCALSCALAR>
   void assemble_scalar(const LOCALSCALAR& scal)
   {
180
      auto val = scal * this->coeff;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
      if (val != xtool::xDataType<S>::zero()) xMatVecScaAssembler<M, V, S>::assemble(val, this->sca);
   }
   /// End  Function for the xAssembleConcept
};

template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerBasicAndTranspose : public xAssemblerBaseData<M, V, S>
{
  public:
   xAssemblerBasicAndTranspose(M& A, V& b, S& s) : xAssemblerBaseData<M, V, S>(&A, &b, &s) {}
   xAssemblerBasicAndTranspose(M& A, V& b) : xAssemblerBaseData<M, V, S>(&A, &b, nullptr) {}
   xAssemblerBasicAndTranspose(M& A) : xAssemblerBaseData<M, V, S>(&A, nullptr, nullptr) {}
   xAssemblerBasicAndTranspose(V& b) : xAssemblerBaseData<M, V, S>(0, &b, 0) {}
   xAssemblerBasicAndTranspose(S& s) : xAssemblerBaseData<M, V, S>(0, 0, &s) {}
   xAssemblerBasicAndTranspose() : xAssemblerBaseData<M, V, S>(0, 0, 0) {}

   /// Functions from the xAssembleConcept
   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr)
   {
201
      ITERDOF firstj_bak = firstj;
202 203 204 205 206 207 208 209 210 211 212
      for (int i = 0; firsti != lasti; ++firsti, ++i)
      {
         firstj = firstj_bak;
         for (int j = 0; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
            {
               AssembleTermMatrix(*firsti, *firstj, val);
            }
         }
Greg's avatar
Greg committed
213
      }
214
   }
Greg's avatar
Greg committed
215

216 217 218
   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr)
   {
219
      ITERDOF firsti = first, firstj, lastj;
Greg's avatar
Greg committed
220
      int j;
221 222 223 224 225 226 227 228 229 230
      for (int i = 0; firsti != last; ++firsti, ++i)
      {
         xPolicyAssemblerLoop<typename M::matrix_pattern>::firstForLoop(first, firsti, firstj, i, j);
         lastj = xPolicyAssemblerLoop<typename M::matrix_pattern>::lastForLoop(last, firsti);
         for (; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               AssembleTermMatrix(*firsti, *firstj, val);
         }
Greg's avatar
Greg committed
231
      }
232 233 234 235 236 237 238
   }

   template <typename LOCALVECTOR, typename ITERDOF>
   void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect)
   {
      for (int i = 0; first != last; ++first, ++i)
      {
239
         S val = vect[i] * this->coeff;
240 241
         if (val != xtool::xDataType<S>::zero())
            xMatVecScaAssembler<M, V, S>::assemble((*first)->getState(), val, this->vec, this->sca);
Greg's avatar
Greg committed
242
      }
243 244 245 246
   }
   template <typename LOCALSCALAR>
   void assemble_scalar(const LOCALSCALAR& scal)
   {
247
      S val = scal * this->coeff;
248 249 250 251 252 253 254 255 256 257 258 259 260 261
      if (val != xtool::xDataType<S>::zero()) xMatVecScaAssembler<M, V, S>::assemble(val, this->sca);
   }

  private:
   void AssembleTermMatrix(const xValue<S>* dofi, const xValue<S>* dofj, S val)
   {
      if (xTraitsAssembler<typename M::matrix_pattern>::do_transpose)
      {
         xMatVecScaAssembler<M, V, S>::assemble(dofi->getState(), dofj->getState(), val, this->mat, this->vec, this->sca);
         xMatVecScaAssembler<M, V, S>::assemble(dofj->getState(), dofi->getState(), val, this->mat, this->vec, this->sca);
      }
      else
      {
         xMatVecScaAssembler<M, V, S>::assemble(dofi->getState(), dofj->getState(), val, this->mat, this->vec, this->sca);
Greg's avatar
Greg committed
262
      }
263 264
   }
   /// End  Function for the xAssembleConcept
Greg's avatar
Greg committed
265 266
};

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
/// classical lumped assembler, all entries are summed on the diagonal
template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerLumped : public xAssemblerBaseData<M, V, S>
{
  public:
   xAssemblerLumped(M& A) : xAssemblerBaseData<M, V, S>(&A, nullptr, nullptr) {}
   xAssemblerLumped(V& b) : xAssemblerBaseData<M, V, S>(0, &b, 0) {}
   xAssemblerLumped() : xAssemblerBaseData<M, V, S>(0, 0, 0) {}

   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr)
   {
      ITERDOF firstj_bak = firstj;
      for (int i = 0; firsti != lasti; ++firsti, ++i)
      {
         firstj = firstj_bak;
         for (int j = 0; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firstj)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
Greg's avatar
Greg committed
290
      }
291 292 293 294
   }
   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr)
   {
295
      ITERDOF firsti = first, firstj, lastj;
Greg's avatar
Greg committed
296
      int j;
297 298 299 300 301 302 303 304 305 306 307
      for (int i = 0; firsti != last; ++firsti, ++i)
      {
         xPolicyAssemblerLoop<typename M::matrix_pattern>::firstForLoop(first, firsti, firstj, i, j);
         lastj = xPolicyAssemblerLoop<typename M::matrix_pattern>::lastForLoop(last, firsti);
         for (; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firsti)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
Greg's avatar
Greg committed
308
      }
309 310 311 312 313 314
   }
   template <typename LOCALVECTOR, typename ITERDOF>
   void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect)
   {
      for (int i = 0; first != last; ++first, ++i)
      {
315
         S val = vect[i] * this->coeff;
316 317
         if (val != xtool::xDataType<S>::zero())
            xMatVecScaAssembler<M, V, S>::assemble((*first)->getState(), val, this->vec, this->sca);
Greg's avatar
Greg committed
318
      }
319 320 321 322
   }
   template <typename LOCALSCALAR>
   void assemble_scalar(const LOCALSCALAR& scal)
   {
323
      S val = scal * this->coeff;
324 325
      if (val != xtool::xDataType<S>::zero()) xMatVecScaAssembler<M, V, S>::assemble(val, this->sca);
   }
Greg's avatar
Greg committed
326 327
};

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
/// same as xAssemblerLumped, but coeffcients of lumped matrix are assembled in vector
template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerLumpedInVector : public xAssemblerBaseData<M, V, S>
{
  public:
   xAssemblerLumpedInVector(M& A) : xAssemblerBaseData<M, V, S>(&A, nullptr, nullptr) {}
   xAssemblerLumpedInVector(V& b) : xAssemblerBaseData<M, V, S>(0, &b, 0) {}
   xAssemblerLumpedInVector() : xAssemblerBaseData<M, V, S>(0, 0, 0) {}
   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr)
   {
      ITERDOF firstj_bak = firstj;
      for (int i = 0; firsti != lasti; ++firsti, ++i)
      {
         firstj = firstj_bak;
         for (int j = 0; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firstj)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
      }
   }
   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr)
   {
      ITERDOF firsti = first, firstj, lastj;
      int j;
      for (int i = 0; firsti != last; ++firsti, ++i)
      {
         xPolicyAssemblerLoop<typename M::matrix_pattern>::firstForLoop(first, firsti, firstj, i, j);
         lastj = xPolicyAssemblerLoop<typename M::matrix_pattern>::lastForLoop(last, firsti);
         for (; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), val, this->vec, this->sca);
         }
      }
   }
   template <typename LOCALVECTOR, typename ITERDOF>
   void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect)
   {
      std::cout << "xAssemblerLumpedInVector is not supposed to be used to assemble vectors !!! " << std::endl;
      throw -1;
   }
   template <typename LOCALSCALAR>
   void assemble_scalar(const LOCALSCALAR& scal)
   {
      S val = scal * this->coeff;
      if (val != xtool::xDataType<S>::zero()) xMatVecScaAssembler<M, V, S>::assemble(val, this->sca);
   }
};

383 384 385 386 387 388 389 390 391 392 393 394 395 396
/// special lumped assembler, it assembles the same thing at each node.
/// It does the same job as  xAssemblerLumped except when the element
/// has a void part
template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerLumpedEqu : public xAssemblerLumped<M, V, S>
{
  public:
   xAssemblerLumpedEqu(M& A) : xAssemblerLumped<M, V, S>(A) {}
   xAssemblerLumpedEqu(V& b) : xAssemblerLumped<M, V, S>(b) {}
   xAssemblerLumpedEqu() : xAssemblerLumped<M, V, S>() {}

   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr)
   {
397
      ITERDOF firstj_bak = firstj;
398 399 400 401 402 403 404 405 406 407
      for (int i = 0; firsti != lasti; ++firsti, ++i)
      {
         firstj = firstj_bak;
         for (int j = 0; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firstj)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
Greg's avatar
Greg committed
408
      }
409 410 411 412 413
   }

   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr)
   {
414
      S sum = xtool::xDataType<S>::zero();
Greg's avatar
Greg committed
415
      int i;
416
      ITERDOF firsti = first, firstj;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
      for (i = 0; firsti != last; ++firsti, ++i)
      {
         firstj = first;
         for (int j = 0; firstj != last; ++firstj, ++j)
         {
            sum += this->coeff * matr(i, j);
         }
      }
      sum /= (S)i;
      if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(sum)) return;
      for (firsti = first; firsti != last; ++firsti)
         xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firsti)->getState(), sum, this->mat, this->vec,
                                                this->sca);
   }

   template <typename LOCALVECTOR, typename ITERDOF>
   void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect)
   {
      S sum = xtool::xDataType<S>::zero();
Greg's avatar
Greg committed
436
      int i;
437
      ITERDOF firsti = first, firstj;
438 439 440 441 442 443 444 445
      for (i = 0; firsti != last; ++firsti, ++i)
      {
         sum += this->coeff * vect[i];
      }
      sum /= (S)i;
      for (firsti = first; firsti != last; ++firsti)
         if (sum != xtool::xDataType<S>::zero())
            xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), sum, this->vec, this->sca);
Greg's avatar
Greg committed
446
      return;
447
   }
Greg's avatar
Greg committed
448

449 450 451
   template <typename LOCALSCALAR>
   void assemble_scalar(const LOCALSCALAR& scal)
   {
452
      S val = scal * this->coeff;
453 454
      if (val != xtool::xDataType<S>::zero()) xMatVecScaAssembler<M, V, S>::assemble(val, this->sca);
   }
Greg's avatar
Greg committed
455 456
};

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/// special lumped assembler, for block matrices
/// takes each block of the assembled matrix, compute the average and assemble it on the corresponding diagonal coefficients
/// not supposed to be used to assemble vectors
template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerLumpedBlockEqu : public xAssemblerLumped<M, V, S>
{
  public:
   xAssemblerLumpedBlockEqu(M& A) : xAssemblerLumped<M, V, S>(A) {}
   xAssemblerLumpedBlockEqu(V& b) : xAssemblerLumped<M, V, S>(b) {}
   xAssemblerLumpedBlockEqu() : xAssemblerLumped<M, V, S>() {}

   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr)
   {
      ITERDOF firstj_bak = firstj;
      for (int i = 0; firsti != lasti; ++firsti, ++i)
      {
         firstj = firstj_bak;
         for (int j = 0; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firstj)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
      }
   }

   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr)
   {
      if (matr.getNbRow() != matr.getNbCol())
      {
         std::cout << "Error : xAssemblerLumpedBlockEqu works only for square matrices ! " << std::endl;
         throw -1;
      }
      std::vector<S> sum(matr.getNbRow());
Greg's avatar
Greg committed
494

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
      ITERDOF firsti = first, firstj;
      for (int i = 0; firsti != last; ++firsti, ++i)
      {
         S val = xtool::xDataType<S>::zero();
         std::vector<int> indexes;

         firstj = first;
         for (int j = 0; firstj != last; ++firstj, ++j)
         {
            val += this->coeff * matr(i, j);
            indexes.push_back(j);
         }
         val /= indexes.size();
         for (int k = 0; k < indexes.size(); k++)
         {
            sum[indexes[k]] += val;
         }
      }
      int l = 0;
      for (firsti = first; firsti != last; ++firsti, ++l)
         xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firsti)->getState(), sum[l], this->mat, this->vec,
                                                this->sca);
   }

   template <typename LOCALVECTOR, typename ITERDOF>
   void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect)
   {
      std::cout << "xAssemblerLumpedBlockEqu is not supposed to be used to assemble vectors !!! " << std::endl;
      throw -1;
   }

   template <typename LOCALSCALAR>
   void assemble_scalar(const LOCALSCALAR& scal)
   {
      S val = scal * this->coeff;
530
      if (val != xtool::xDataType<S>::zero()) xMatVecScaAssembler<M, V, S>::assemble(val, this->sca);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
   }
};
/// does the same thing as xAssemblerLumpedBlockEqu, but the coefficients of the lumped matrix are put in a
/// vector
template <class M = xlinalg::xCSRMatrix, class V = xlinalg::xCSRVector, class S = double>
class xAssemblerLumpedBlockEquInVector : public xAssemblerLumped<M, V, S>
{
  public:
   xAssemblerLumpedBlockEquInVector(M& A) : xAssemblerLumped<M, V, S>(A) {}
   xAssemblerLumpedBlockEquInVector(V& b) : xAssemblerLumped<M, V, S>(b) {}
   xAssemblerLumpedBlockEquInVector() : xAssemblerLumped<M, V, S>() {}

   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF firsti, ITERDOF lasti, ITERDOF firstj, ITERDOF lastj, const LOCALMATRIX& matr)
   {
      ITERDOF firstj_bak = firstj;
      for (int i = 0; firsti != lasti; ++firsti, ++i)
      {
         firstj = firstj_bak;
         for (int j = 0; firstj != lastj; ++firstj, ++j)
         {
            S val = this->coeff * matr(i, j);
            if (!xPolicyAssemblerOnZero<typename M::matrix_assembly_on_zero>::isNull(val))
               xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), (*firstj)->getState(), val, this->mat, this->vec,
                                                      this->sca);
         }
      }
   }

   template <typename LOCALMATRIX, typename ITERDOF>
   void assemble_matrix(ITERDOF first, ITERDOF last, const LOCALMATRIX& matr)
   {
      if (matr.getNbRow() != matr.getNbCol())
      {
         std::cout << "Error : xAssemblerLumpedBlockEquInVector works only for square matrices ! " << std::endl;
         throw -1;
      }
      std::vector<S> sum(matr.getNbRow());
Greg's avatar
Greg committed
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
      ITERDOF firsti = first, firstj;
      for (int i = 0; firsti != last; ++firsti, ++i)
      {
         S val = xtool::xDataType<S>::zero();
         std::vector<int> indexes;

         firstj = first;
         for (int j = 0; firstj != last; ++firstj, ++j)
         {
            val += this->coeff * matr(i, j);
            indexes.push_back(j);
         }
         val /= indexes.size();
         for (int k = 0; k < indexes.size(); k++)
         {
            sum[indexes[k]] += val;
         }
      }
      int l = 0;
      for (firsti = first; firsti != last; ++firsti, ++l)
         xMatVecScaAssembler<M, V, S>::assemble((*firsti)->getState(), sum[l], this->vec, this->sca);
   }

   template <typename LOCALVECTOR, typename ITERDOF>
   void assemble_vector(ITERDOF first, ITERDOF last, const LOCALVECTOR& vect)
   {
      std::cout << "xAssemblerLumpedBlockEquInVector is not supposed to be used to assemble vectors !!! " << std::endl;
      throw -1;
   }

   template <typename LOCALSCALAR>
   void assemble_scalar(const LOCALSCALAR& scal)
   {
      S val = scal * this->coeff;
604
      if (val != xtool::xDataType<S>::zero()) xMatVecScaAssembler<M, V, S>::assemble(val, this->sca);
605 606
   }
};
Greg's avatar
Greg committed
607

608
}  // namespace xfem
Greg's avatar
Greg committed
609 610

#endif