This file is indexed.

/usr/include/palabos/particles/particleProcessingFunctional2D.hh is in libplb-dev 1.5~r1+repack1-2build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
258
259
260
261
262
263
264
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
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
530
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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/* This file is part of the Palabos library.
 *
 * Copyright (C) 2011-2015 FlowKit Sarl
 * Route d'Oron 2
 * 1010 Lausanne, Switzerland
 * E-mail contact: contact@flowkit.com
 *
 * The most recent release of Palabos can be downloaded at 
 * <http://www.palabos.org/>
 *
 * The library Palabos is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * The library 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PARTICLE_PROCESSING_FUNCTIONAL_2D_HH
#define PARTICLE_PROCESSING_FUNCTIONAL_2D_HH

#include "particles/particleProcessingFunctional2D.h"
#include "particles/particleField2D.h"
#include "dataProcessors/metaStuffFunctional2D.h"
#include "core/plbDebug.h"
#include "core/blockStatistics.h"
#include "atomicBlock/atomicBlock2D.h"
#include "atomicBlock/blockLattice2D.h"

namespace plb {

/* ******** CountParticlesFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
CountParticlesFunctional2D<T,Descriptor>::CountParticlesFunctional2D()
    : numParticlesId(this->getStatistics().subscribeIntSum())
{ }

template<typename T, template<typename U> class Descriptor>
void CountParticlesFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField
        = *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    std::vector<Particle2D<T,Descriptor>*> particles;
    particleField.findParticles(domain, particles);
    this->getStatistics().gatherIntSum(numParticlesId, (plint)particles.size());
}

template<typename T, template<typename U> class Descriptor>
CountParticlesFunctional2D<T,Descriptor>* CountParticlesFunctional2D<T,Descriptor>::clone() const {
    return new CountParticlesFunctional2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
void CountParticlesFunctional2D<T,Descriptor>::getTypeOfModification(std::vector<modif::ModifT>& modified) const {
    modified[0] = modif::nothing;
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT CountParticlesFunctional2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
plint CountParticlesFunctional2D<T,Descriptor>::getNumParticles() const {
    return this->getStatistics().getIntSum(numParticlesId);
}

/* ******** CountParticlesSelectiveFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
CountParticlesSelectiveFunctional2D<T,Descriptor>::CountParticlesSelectiveFunctional2D(util::SelectInt* tags_)
    : numParticlesId(this->getStatistics().subscribeIntSum()),
      tags(tags_)
{ }

template<typename T, template<typename U> class Descriptor>
CountParticlesSelectiveFunctional2D<T,Descriptor>::~CountParticlesSelectiveFunctional2D()
{
    delete tags;
}

template<typename T, template<typename U> class Descriptor>
CountParticlesSelectiveFunctional2D<T,Descriptor>::CountParticlesSelectiveFunctional2D(CountParticlesSelectiveFunctional2D<T,Descriptor> const& rhs)
    : numParticlesId(this->getStatistics().subscribeIntSum()),
      tags(rhs.tags->clone())
{ }

template<typename T, template<typename U> class Descriptor>
CountParticlesSelectiveFunctional2D<T,Descriptor>&
    CountParticlesSelectiveFunctional2D<T,Descriptor>::operator=(CountParticlesSelectiveFunctional2D<T,Descriptor> const& rhs)
{
    CountParticlesSelectiveFunctional2D<T,Descriptor>(rhs).swap(*this);
    return *this;
}

template<typename T, template<typename U> class Descriptor>
void CountParticlesSelectiveFunctional2D<T,Descriptor>::swap(CountParticlesSelectiveFunctional2D<T,Descriptor>& rhs) {
    std::swap(numParticlesId, rhs.numParticlesId);
    std::swap(tags, rhs.tags);
    PlainReductiveBoxProcessingFunctional2D::swap(rhs);
}

template<typename T, template<typename U> class Descriptor>
void CountParticlesSelectiveFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField
        = *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    std::vector<Particle2D<T,Descriptor>*> particles;
    particleField.findParticles(domain, particles);
    for (pluint iParticle=0; iParticle<particles.size(); ++iParticle) {
        if ((*tags)(particles[iParticle]->getTag())) {
	    this->getStatistics().gatherIntSum(numParticlesId, 1);
        }
    }
}

template<typename T, template<typename U> class Descriptor>
CountParticlesSelectiveFunctional2D<T,Descriptor>* CountParticlesSelectiveFunctional2D<T,Descriptor>::clone() const {
    return new CountParticlesSelectiveFunctional2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
void CountParticlesSelectiveFunctional2D<T,Descriptor>::getTypeOfModification(std::vector<modif::ModifT>& modified) const {
    modified[0] = modif::nothing;
}

template<typename T, template<typename U> class Descriptor>
plint CountParticlesSelectiveFunctional2D<T,Descriptor>::getNumParticles() const {
    return this->getStatistics().getIntSum(numParticlesId);
}


/* ******** AverageParticleVelocityFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
AverageParticleVelocityFunctional2D<T,Descriptor>::AverageParticleVelocityFunctional2D()
    : averageVelocityId (
            Array<T,2>( this->getStatistics().subscribeAverage(),
                        this->getStatistics().subscribeAverage() ) )
{ }

template<typename T, template<typename U> class Descriptor>
void AverageParticleVelocityFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField = *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    std::vector<Particle2D<T,Descriptor>*> particles;
    particleField.findParticles(domain, particles);
    for (pluint iParticle=0; iParticle<particles.size(); ++iParticle) {
        Array<T,2> velocity;
        particles[iParticle]->getVelocity(velocity);
        this->getStatistics().gatherAverage(averageVelocityId[0], velocity[0]);
        this->getStatistics().gatherAverage(averageVelocityId[1], velocity[1]);
        this->getStatistics().incrementStats();
    }
}

template<typename T, template<typename U> class Descriptor>
AverageParticleVelocityFunctional2D<T,Descriptor>* AverageParticleVelocityFunctional2D<T,Descriptor>::clone() const {
    return new AverageParticleVelocityFunctional2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
void AverageParticleVelocityFunctional2D<T,Descriptor>::getTypeOfModification(std::vector<modif::ModifT>& modified) const {
    modified[0] = modif::nothing;
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT AverageParticleVelocityFunctional2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
Array<T,2> AverageParticleVelocityFunctional2D<T,Descriptor>::getAverageParticleVelocity() const {
    return Array<T,2> (
        this->getStatistics().getAverage(averageVelocityId[0]),
        this->getStatistics().getAverage(averageVelocityId[1]) );
}


/* ******** InjectParticlesFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
InjectParticlesFunctional2D<T,Descriptor>::InjectParticlesFunctional2D(std::vector<Particle2D<T,Descriptor>*>& particles_)
    : particles(particles_)
{ }

template<typename T, template<typename U> class Descriptor>
void InjectParticlesFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField = *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    for (pluint iParticle=0; iParticle<particles.size(); ++iParticle) {
        // The function addParticle tests automatically if the particle corresponds to the domain
        //   and needs to be added. In either case, the particle is consumed by the function.
        particleField.addParticle(domain, particles[iParticle]->clone());
    }
}

template<typename T, template<typename U> class Descriptor>
InjectParticlesFunctional2D<T,Descriptor>* InjectParticlesFunctional2D<T,Descriptor>::clone() const {
    return new InjectParticlesFunctional2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT InjectParticlesFunctional2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
void InjectParticlesFunctional2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables;  // Particle field.
}


/* ******** InjectRandomPointParticlesFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
InjectRandomPointParticlesFunctional2D<T,Descriptor>::InjectRandomPointParticlesFunctional2D (
        plint tag_, T probabilityPerCell_ )
    : tag(tag_),
      probabilityPerCell(probabilityPerCell_)
{ }

template<typename T, template<typename U> class Descriptor>
void InjectRandomPointParticlesFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField = *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
        for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
            T randNumber = (T)rand() / (T)RAND_MAX;
            if(randNumber<probabilityPerCell) {
                T randX = (T)rand() / (T)RAND_MAX - (T)1;
                T randY = (T)rand() / (T)RAND_MAX - (T)1;
                Array<T,2> position (
                        particleField.getLocation().x + iX + randX,
                        particleField.getLocation().y + iY + randY );
                Array<T,2> velocity; velocity.resetToZero();
                particleField.addParticle(
                        domain,
                        new PointParticle2D<T,Descriptor>(tag, position, velocity) );
            }
        }
    }
}

template<typename T, template<typename U> class Descriptor>
InjectRandomPointParticlesFunctional2D<T,Descriptor>* InjectRandomPointParticlesFunctional2D<T,Descriptor>::clone() const {
    return new InjectRandomPointParticlesFunctional2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT InjectRandomPointParticlesFunctional2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
void InjectRandomPointParticlesFunctional2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables;  // Particle field.
}


/* ******** AnalyticalInjectRandomParticlesFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::
    AnalyticalInjectRandomParticlesFunctional2D (
         Particle2D<T,Descriptor>* particleTemplate_, T probabilityPerCell_, DomainFunctional functional_ )
    : particleTemplate(particleTemplate_),
      probabilityPerCell(probabilityPerCell_),
      functional(functional_)
{ }

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::AnalyticalInjectRandomParticlesFunctional2D (
        AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional> const& rhs)
    : particleTemplate(rhs.particleTemplate->clone()),
      probabilityPerCell(rhs.probabilityPerCell),
      functional(rhs.functional)
{ }

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>&
    AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::operator= (
        AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional> const& rhs )
{
    AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>(rhs).swap(*this);
    return *this;
}

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
void AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::swap (
        AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>& rhs )
{
    std::swap(particleTemplate, rhs.particleTemplate);
    std::swap(probabilityPerCell, rhs.probabilityPerCell);
    std::swap(functional, rhs.functional);
}

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::~AnalyticalInjectRandomParticlesFunctional2D()
{
    delete particleTemplate;
}

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
void AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField = *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
        for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
            T randNumber = (T)rand() / (T)RAND_MAX;
            if(randNumber<probabilityPerCell) {
                T randX = (T)rand() / (T)RAND_MAX - (T)1;
                T randY = (T)rand() / (T)RAND_MAX - (T)1;
                Array<T,2> position (
                        particleField.getLocation().x + iX + randX,
                        particleField.getLocation().y + iY + randY );
                if (functional(position)) {
                    Particle2D<T,Descriptor>* newparticle = particleTemplate->clone();
                    newparticle->getPosition() = position;
                    particleField.addParticle(domain, newparticle);
                }
            }
        }
    }
}

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>*
    AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::clone() const {
    return new AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>(*this);
}

template<typename T, template<typename U> class Descriptor, class DomainFunctional>
void AnalyticalInjectRandomParticlesFunctional2D<T,Descriptor,DomainFunctional>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables;  // Particle field.
}





/* ******** AbsorbParticlesFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
void AbsorbParticlesFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    particleField.removeParticles(domain);
}

template<typename T, template<typename U> class Descriptor>
AbsorbParticlesFunctional2D<T,Descriptor>* AbsorbParticlesFunctional2D<T,Descriptor>::clone() const {
    return new AbsorbParticlesFunctional2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT AbsorbParticlesFunctional2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
void AbsorbParticlesFunctional2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables;  // Particle field.
}


/* ******** FluidToParticleCoupling2D *********************************** */

template<typename T, template<typename U> class Descriptor>
FluidToParticleCoupling2D<T,Descriptor>::FluidToParticleCoupling2D(T scaling_)
    : scaling(scaling_)
{ }

template<typename T, template<typename U> class Descriptor>
void FluidToParticleCoupling2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==2 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    BlockLattice2D<T,Descriptor>& fluid =
        *dynamic_cast<BlockLattice2D<T,Descriptor>*>(blocks[1]);
    particleField.fluidToParticleCoupling(domain, fluid, scaling);
}

template<typename T, template<typename U> class Descriptor>
FluidToParticleCoupling2D<T,Descriptor>* FluidToParticleCoupling2D<T,Descriptor>::clone() const {
    return new FluidToParticleCoupling2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT FluidToParticleCoupling2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk; // Important: must be bulk-only,
    // because non-local access to fluid is made.
}

template<typename T, template<typename U> class Descriptor>
void FluidToParticleCoupling2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables; // Particle field.
    modified[1] = modif::nothing;  // Fluid.
}


/* ******** VelocityToParticleCoupling2D *********************************** */

template<typename T, template<typename U> class Descriptor>
VelocityToParticleCoupling2D<T,Descriptor>::VelocityToParticleCoupling2D(T scaling_)
    : scaling(scaling_)
{ }

template<typename T, template<typename U> class Descriptor>
void VelocityToParticleCoupling2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==2 );
    ParticleField2D<T,Descriptor>* particleFieldPtr =
        dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    PLB_ASSERT( particleFieldPtr );
    ParticleField2D<T,Descriptor>& particleField = *particleFieldPtr;

    TensorField2D<T,2>* velocityPtr =
        dynamic_cast<TensorField2D<T,2>*>(blocks[1]);
    PLB_ASSERT( velocityPtr );
    TensorField2D<T,2>& velocity = *velocityPtr;

    particleField.velocityToParticleCoupling(domain, velocity, scaling);
}

template<typename T, template<typename U> class Descriptor>
VelocityToParticleCoupling2D<T,Descriptor>* VelocityToParticleCoupling2D<T,Descriptor>::clone() const {
    return new VelocityToParticleCoupling2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
void VelocityToParticleCoupling2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables; // Particle field.
    modified[1] = modif::nothing;  // TensorField velocity.
}


/* ******** RhoBarJtoParticleCoupling2D *********************************** */

template<typename T, template<typename U> class Descriptor>
RhoBarJtoParticleCoupling2D<T,Descriptor>::RhoBarJtoParticleCoupling2D (
        bool velIsJ_, T scaling_)
    : velIsJ(velIsJ_),
      scaling(scaling_)
{ }

template<typename T, template<typename U> class Descriptor>
void RhoBarJtoParticleCoupling2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==2 );
    ParticleField2D<T,Descriptor>* particleFieldPtr =
        dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    PLB_ASSERT( particleFieldPtr );
    ParticleField2D<T,Descriptor>& particleField = *particleFieldPtr;

    NTensorField2D<T>* rhoBarJptr =
        dynamic_cast<NTensorField2D<T>*>(blocks[1]);
    PLB_ASSERT( rhoBarJptr );
    NTensorField2D<T>& rhoBarJ = *rhoBarJptr;

    particleField.rhoBarJtoParticleCoupling(domain, rhoBarJ, velIsJ, scaling);
}

template<typename T, template<typename U> class Descriptor>
RhoBarJtoParticleCoupling2D<T,Descriptor>* RhoBarJtoParticleCoupling2D<T,Descriptor>::clone() const {
    return new RhoBarJtoParticleCoupling2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
void RhoBarJtoParticleCoupling2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables; // Particle field.
    modified[1] = modif::nothing;  // RhoBarJ;
}

/* ******** AdvanceParticlesFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
AdvanceParticlesFunctional2D<T,Descriptor>::AdvanceParticlesFunctional2D (
        T cutOffValue_ )
  : cutOffValue(cutOffValue_)
{ }

template<typename T, template<typename U> class Descriptor>
void AdvanceParticlesFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    particleField.advanceParticles(domain, cutOffValue);
}

template<typename T, template<typename U> class Descriptor>
AdvanceParticlesFunctional2D<T,Descriptor>* AdvanceParticlesFunctional2D<T,Descriptor>::clone() const {
    return new AdvanceParticlesFunctional2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT AdvanceParticlesFunctional2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulkAndEnvelope;  // Important: access envelope as well,
                                          // because particles are streamed from the
                                          // envelope into the bulk.
}

template<typename T, template<typename U> class Descriptor>
void AdvanceParticlesFunctional2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables;  // Particle field.
}


/* ******** AdvanceParticlesFunctional2D *********************************** */

template<typename T, template<typename U> class Descriptor>
AdvanceParticlesEveryWhereFunctional2D<T,Descriptor>::AdvanceParticlesEveryWhereFunctional2D (
        T cutOffValue_ )
  : cutOffValue(cutOffValue_)
{ }

template<typename T, template<typename U> class Descriptor>
void AdvanceParticlesEveryWhereFunctional2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    // Important: acts on the full domain, not only the provided one.
    particleField.advanceParticles(particleField.getBoundingBox(), cutOffValue);
}

template<typename T, template<typename U> class Descriptor>
AdvanceParticlesEveryWhereFunctional2D<T,Descriptor>* AdvanceParticlesEveryWhereFunctional2D<T,Descriptor>::clone() const {
    return new AdvanceParticlesEveryWhereFunctional2D<T,Descriptor>(*this);
}

// template<typename T, template<typename U> class Descriptor>
// BlockDomain::DomainT AdvanceParticlesEveryWhereFunctional2D<T,Descriptor>::appliesTo() const {
//     return BlockDomain::bulkAndEnvelope;  // Important: access envelope as well,
//                                           // because particles are streamed from the
//                                           // envelope into the bulk.}
// 
// }

template<typename T, template<typename U> class Descriptor>
void AdvanceParticlesEveryWhereFunctional2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables;  // Particle field.
}

/* ******** VerletUpdateVelocity2D *********************************** */

template<typename T, template<typename U> class Descriptor>
void VerletUpdateVelocity2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==1 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);

    std::vector<Particle2D<T,Descriptor>*> found;
    particleField.findParticles(domain, found);

    for (pluint iParticle=0; iParticle<found.size(); ++iParticle) {
        Particle2D<T,Descriptor>* nonTypeParticle = found[iParticle];
        VerletParticle2D<T,Descriptor>* particle =
            dynamic_cast<VerletParticle2D<T,Descriptor>*>(nonTypeParticle);
        PLB_ASSERT( particle );

        Array<T,2> a(particle->get_a());
        particle->set_v(particle->get_vHalfTime() + (T)0.5*a);
        PLB_ASSERT( norm(particle->get_v()) < 1. );
    }
}

template<typename T, template<typename U> class Descriptor>
VerletUpdateVelocity2D<T,Descriptor>* VerletUpdateVelocity2D<T,Descriptor>::clone() const {
    return new VerletUpdateVelocity2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
void VerletUpdateVelocity2D<T,Descriptor>::getModificationPattern(std::vector<bool>& isWritten) const {
    isWritten[0] = true;  // Particle field.
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT VerletUpdateVelocity2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
void VerletUpdateVelocity2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::dynamicVariables; // Particle field.
}



/* ******** CountAndAccumulateParticles2D *********************************** */

template<typename T, template<typename U> class Descriptor>
void CountAndAccumulateParticles2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==2 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    ScalarField2D<plint>& numParticlefield =
        *dynamic_cast<ScalarField2D<plint>*>(blocks[1]);
    Dot2D offset = computeRelativeDisplacement(particleField, numParticlefield);
    std::vector<Particle2D<T,Descriptor>*> particles;
    for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
        for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
            particleField.findParticles(Box2D(iX,iX,iY,iY), particles);
            numParticlefield.get(iX+offset.x,iY+offset.y) += particles.size();
        }
    }
}

template<typename T, template<typename U> class Descriptor>
CountAndAccumulateParticles2D<T,Descriptor>* CountAndAccumulateParticles2D<T,Descriptor>::clone() const {
    return new CountAndAccumulateParticles2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT CountAndAccumulateParticles2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
void CountAndAccumulateParticles2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::nothing;          // Particle field.
    modified[1] = modif::staticVariables;  // Scalar field.
}

/* ******** CountAndAccumulateTaggedParticles2D *********************************** */

template<typename T, template<typename U> class Descriptor>
CountAndAccumulateTaggedParticles2D<T,Descriptor>::CountAndAccumulateTaggedParticles2D(plint tag_)
    : tag(tag_)
{ }

template<typename T, template<typename U> class Descriptor>
void CountAndAccumulateTaggedParticles2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==2 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    ScalarField2D<plint>& numParticlefield =
        *dynamic_cast<ScalarField2D<plint>*>(blocks[1]);
    Dot2D offset = computeRelativeDisplacement(particleField, numParticlefield);
    std::vector<Particle2D<T,Descriptor>*> particles;
    for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
        for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
            particleField.findParticles(Box2D(iX,iX,iY,iY), particles);
            for (pluint iParticle=0; iParticle<particles.size(); ++iParticle) {
                if (particles[iParticle]->getTag() == tag) {
                    ++numParticlefield.get(iX+offset.x,iY+offset.y);
                }
            }
        }
    }
}

template<typename T, template<typename U> class Descriptor>
CountAndAccumulateTaggedParticles2D<T,Descriptor>* CountAndAccumulateTaggedParticles2D<T,Descriptor>::clone() const {
    return new CountAndAccumulateTaggedParticles2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
BlockDomain::DomainT CountAndAccumulateTaggedParticles2D<T,Descriptor>::appliesTo() const {
    return BlockDomain::bulk;
}

template<typename T, template<typename U> class Descriptor>
void CountAndAccumulateTaggedParticles2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::nothing;          // Particle field.
    modified[1] = modif::staticVariables;  // Scalar field.
}

/* ******** CountTaggedParticles2D *********************************** */

template<typename T, template<typename U> class Descriptor>
CountTaggedParticles2D<T,Descriptor>::CountTaggedParticles2D(util::SelectInt* tags_)
    : tags(tags_)
{ }

template<typename T, template<typename U> class Descriptor>
CountTaggedParticles2D<T,Descriptor>::~CountTaggedParticles2D()
{
    delete tags;
}

template<typename T, template<typename U> class Descriptor>
CountTaggedParticles2D<T,Descriptor>::CountTaggedParticles2D(CountTaggedParticles2D<T,Descriptor> const& rhs)
    : tags(rhs.tags->clone())
{ }

template<typename T, template<typename U> class Descriptor>
CountTaggedParticles2D<T,Descriptor>&
    CountTaggedParticles2D<T,Descriptor>::operator=(CountTaggedParticles2D<T,Descriptor> const& rhs)
{
    CountTaggedParticles2D<T,Descriptor>(rhs).swap(*this);
    return *this;
}

template<typename T, template<typename U> class Descriptor>
void CountTaggedParticles2D<T,Descriptor>::swap(CountTaggedParticles2D<T,Descriptor>& rhs) {
    std::swap(tags, rhs.tags);
}

template<typename T, template<typename U> class Descriptor>
void CountTaggedParticles2D<T,Descriptor>::processGenericBlocks (
        Box2D domain, std::vector<AtomicBlock2D*> blocks )
{
    PLB_PRECONDITION( blocks.size()==2 );
    ParticleField2D<T,Descriptor>& particleField =
        *dynamic_cast<ParticleField2D<T,Descriptor>*>(blocks[0]);
    ScalarField2D<plint>& numParticlefield =
        *dynamic_cast<ScalarField2D<plint>*>(blocks[1]);
    Dot2D offset = computeRelativeDisplacement(particleField, numParticlefield);
    std::vector<Particle2D<T,Descriptor>*> particles;
    for (plint iX=domain.x0; iX<=domain.x1; ++iX) {
        for (plint iY=domain.y0; iY<=domain.y1; ++iY) {
            particleField.findParticles(Box2D(iX,iX,iY,iY), particles);
            numParticlefield.get(iX+offset.x,iY+offset.y) = 0;
            for (pluint iParticle=0; iParticle<particles.size(); ++iParticle) {
                if ((*tags)(particles[iParticle]->getTag())) {
                    ++numParticlefield.get(iX+offset.x,iY+offset.y);
                }
            }
        }
    }
}

template<typename T, template<typename U> class Descriptor>
CountTaggedParticles2D<T,Descriptor>* CountTaggedParticles2D<T,Descriptor>::clone() const {
    return new CountTaggedParticles2D<T,Descriptor>(*this);
}

template<typename T, template<typename U> class Descriptor>
void CountTaggedParticles2D<T,Descriptor>::getTypeOfModification (
        std::vector<modif::ModifT>& modified ) const
{
    modified[0] = modif::nothing;          // Particle field.
    modified[1] = modif::staticVariables;  // Scalar field.
}


template< typename T, template<typename U> class Descriptor,
          template<typename T_, template<typename U_> class Descriptor_> class ParticleFieldT >
plint countParticles (
                MultiParticleField2D<ParticleFieldT<T,Descriptor> >& particles, Box2D const& domain )
{
    std::vector<MultiBlock2D*> particleArg;
    particleArg.push_back(&particles);

    CountParticlesFunctional2D<T,Descriptor> functional;
    applyProcessingFunctional(functional, domain, particleArg);
    return functional.getNumParticles();
}

template< typename T, template<typename U> class Descriptor,
          template<typename T_, template<typename U_> class Descriptor_> class ParticleFieldT >
plint countParticles (
                MultiParticleField2D<ParticleFieldT<T,Descriptor> >& particles, Box2D const& domain, util::SelectInt* tags )
{
    std::vector<MultiBlock2D*> particleArg;
    particleArg.push_back(&particles);

    CountParticlesSelectiveFunctional2D<T,Descriptor> functional(tags);
    applyProcessingFunctional(functional, domain, particleArg);
    return functional.getNumParticles();
}

template<typename T, template<typename U> class Descriptor>
void injectParticles(std::vector<Particle2D<T,Descriptor>*>& injectedParticles,
                     MultiParticleField2D<DenseParticleField2D<T,Descriptor> >& particles, Box2D domain)
{
    std::vector<MultiBlock2D*> particleArg;
    particleArg.push_back(&particles);
    applyProcessingFunctional (
            new InjectParticlesFunctional2D<T,Descriptor>(injectedParticles), domain, particleArg );
}


}  // namespace plb

#endif  // PARTICLE_PROCESSING_FUNCTIONAL_2D_HH