Canoe
Comprehensive Atmosphere N' Ocean Engine
particle_to_mesh.cpp
Go to the documentation of this file.
1 
4 // C/C++ headers
5 #include <iostream>
6 
7 // Athena++ headers
8 #include <athena/coordinates/coordinates.hpp>
9 #include <athena/globals.hpp>
10 #include <athena/mesh/mesh.hpp>
11 
12 // climath
13 #include <climath/interpolation.h> // locate
14 
15 // n-body
16 #include "particle_data.hpp"
17 #include "particles.hpp"
18 
20  auto pmb = pmy_block_;
21  auto pcoord = pmb->pcoord;
22 
23  // initialize cell aggregated particle weight u and linked list
24  weight.ZeroClear();
25  charge.ZeroClear();
26 
27  std::fill(pd_in_cell_.data(), pd_in_cell_.data() + pd_in_cell_.GetSize(),
28  nullptr);
29  int i, j, k;
30 
31  auto dims = pcoord->GetDimensions();
32  auto x321f = pcoord->GetFaceCoords();
33 
34  // loop over particles
35  for (auto& qi : pc) {
36  k = locate(x321f, qi.x3, dims[0] + 1);
37  j = locate(x321f + dims[0] + 1, qi.x2, dims[1] + 1);
38  i = locate(x321f + dims[0] + dims[1] + 2, qi.x1, dims[2] + 1);
39 
40  auto tid = qi.tid;
41 
42  weight(tid, k, j, i) += qi.weight;
43  charge(tid, k, j, i) += qi.charge;
44 
45  if (pd_in_cell_(tid, k, j, i) == nullptr) {
46  pd_in_cell_(tid, k, j, i) = &qi;
47  pd_in_cell_(tid, k, j, i)->next = nullptr;
48  } else { // insert sort from low to high weight
49  ParticleData* tmp;
50  if (pd_in_cell_(tid, k, j, i)->weight < qi.weight) {
51  auto qj = pd_in_cell_(tid, k, j, i);
52  while (qj->next != nullptr && qj->next->weight < qi.weight)
53  qj = qj->next;
54  tmp = qj->next;
55  qj->next = &qi;
56  qj->next->next = tmp;
57  } else {
58  tmp = pd_in_cell_(tid, k, j, i);
59  pd_in_cell_(tid, k, j, i) = &qi;
60  pd_in_cell_(tid, k, j, i)->next = tmp;
61  }
62  }
63  }
64 
65  linked_flag_ = true;
66 }
AthenaArray< ParticleData * > pd_in_cell_
Definition: particles.hpp:75
ParticleContainer pc
Definition: particles.hpp:39
bool linked_flag_
linked flag
Definition: particles.hpp:78
void LinkMesh()
particle-mesh
AthenaArray< Real > charge
Definition: particles.hpp:42
MeshBlock const * pmy_block_
pointer to parent MeshBlock
Definition: particles.hpp:82
AthenaArray< Real > weight
mesh data container
Definition: particles.hpp:42
int locate(double const *xx, double x, int n)
Definition: locate.c:7
ParticleData * next
particle can form a linked list