elec-1.00
 All Classes Namespaces Files Functions Variables Macros
elecParticle.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 /* This file is part of rl-lib
4  *
5  * Copyright (C) 2010, Herve FREZZA-BUET
6  *
7  * Author : Herve Frezza-Buet
8  *
9  * Contributor :
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public
13  * License (GPL) as published by the Free Software Foundation; either
14  * version 3 of the License, or any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24  *
25  * Contact : Herve.Frezza-Buet@supelec.fr
26  *
27  */
28 
29 #include <elecPoint.hpp>
30 #include <elecParams.hpp>
31 #include <cmath>
32 #include <utility>
33 #include <limits>
34 
35 namespace elec {
36  class Particle {
37  public:
39  double q; // -1, 1
40 
41 
42  Particle() : pos(), speed(), q(0) {}
43  Particle(const Point& p, double qq)
44  : pos(p), speed(0,0), q(qq) {}
45 
46  Particle(const Particle& cp)
47  : pos(cp.pos), speed(cp.speed), q(cp.q) {}
48  Particle& operator=(const Particle& cp) {
49  pos = cp.pos;
50  speed = cp.speed;
51  q = cp.q;
52  return *this;
53  }
54 
55  };
56 
57  inline std::ostream& operator<<(std::ostream& os,
58  const Particle& p) {
59  if(p.q > 0)
60  os << "p+@";
61  else
62  os << "e-@";
63  os << p.pos << '/' << p.speed;
64  return os;
65  }
66  inline Particle electron(const Point& pos) {
67  return {pos,-1};
68  }
69 
70  inline Particle proton(const Point& pos) {
71  return {pos,1};
72  }
73 
74  inline Point E(const Particle& p, const Point& at) {
75  double dist = d2(p.pos,at);
77  return Point(0,0);
78  dist = std::max(dist,elecMIN_E_RADIUS*elecMIN_E_RADIUS);
79  Point d = (at-p.pos);
80  return (*d)*(p.q*elecFIELD_COEF/dist);
81  }
82 
83  template<typename Iter>
84  Point E(const Iter& begin,
85  const Iter& end,
86  const Point& at) {
87  Point e = {0,0};
88  for(auto it = begin; it != end; ++it)
89  e += E(*it,at);
90  return e;
91  }
92 
93  template<typename Iter, typename PartOf>
94  Point E_(const Iter& begin,
95  const Iter& end,
96  const Point& at,
97  const PartOf& part_of) {
98  Point e = {0,0};
99  for(auto it = begin; it != end; ++it)
100  e += E(part_of(*it),at);
101  return e;
102  }
103 
104  template<typename PartIter, typename PosIter,
105  typename OutputIt>
106  double E(const PartIter& particle_begin,
107  const PartIter& particle_end,
108  const PosIter& position_begin,
109  const PosIter& position_end,
110  OutputIt out) {
111  double max_norm = 0;
112  for(auto it = position_begin; it != position_end; ++it) {
113  auto e = E(particle_begin,particle_end,*it);
114  double tmp = e*e;
115  if(tmp > max_norm)
116  max_norm = tmp;
117  *(out++) = e;
118  }
119  return std::sqrt(max_norm);
120  }
121 
122  template<typename PartIter, typename PosIter,
123  typename OutputIt, typename PartOf, typename PosOf>
124  void E_(const PartIter& particle_begin,
125  const PartIter& particle_end,
126  const PosIter& position_begin,
127  const PosIter& position_end,
128  OutputIt out,
129  const PartOf& part_of,
130  const PosOf& pos_of) {
131  for(auto it = position_begin; it != position_end; ++it)
132  *(out++) = E_(particle_begin,particle_end,pos_of(*it),part_of);
133  }
134  inline double V(const Particle& p, const Point& at) {
135  double dist = d(p.pos,at);
136  if(dist < elecEPS_E_RADIUS)
137  return 0;
138  dist = std::max(dist,elecMIN_E_RADIUS);
139  return p.q*elecFIELD_COEF/dist;
140  }
141 
142  template<typename Iter>
143  double V(const Iter& begin,
144  const Iter& end,
145  const Point& at) {
146  double v = 0;
147  for(auto it = begin; it != end; ++it)
148  v += V(*it,at);
149  return v;
150  }
151 
152  template<typename PartIter, typename PosIter,
153  typename OutputIt>
154  std::pair<double,double> V(const PartIter& particle_begin,
155  const PartIter& particle_end,
156  const PosIter& position_begin,
157  const PosIter& position_end,
158  OutputIt out) {
159  double min = std::numeric_limits<double>::max();
160  double max = std::numeric_limits<double>::lowest();
161  for(auto it = position_begin; it != position_end; ++it) {
162  double v = V(particle_begin,particle_end,*it);
163  if(v < min) min = v;
164  if(v > max) max = v;
165  *(out++) = v;
166  }
167 
168  return {min,max};
169  }
170 
171 }
172 
Definition: elecParticle.hpp:36
double V(const Particle &p, const Point &at)
Definition: elecParticle.hpp:134
Particle & operator=(const Particle &cp)
Definition: elecParticle.hpp:48
double q
Definition: elecParticle.hpp:39
Point E_(const Iter &begin, const Iter &end, const Point &at, const PartOf &part_of)
Definition: elecParticle.hpp:94
#define elecFIELD_COEF
Definition: elecParams.hpp:29
Point speed
Definition: elecParticle.hpp:38
Particle electron(const Point &pos)
Definition: elecParticle.hpp:66
Particle(const Particle &cp)
Definition: elecParticle.hpp:46
Definition: elecPoint.hpp:35
double d2(const Point &A, const Point &B)
Definition: elecPoint.hpp:131
Particle()
Definition: elecParticle.hpp:42
Point pos
Definition: elecParticle.hpp:38
#define elecEPS_E_RADIUS
Definition: elecParams.hpp:35
double d(const Point &A, const Point &B)
Definition: elecPoint.hpp:136
std::ostream & operator<<(std::ostream &os, const Particle &p)
Definition: elecParticle.hpp:57
Particle proton(const Point &pos)
Definition: elecParticle.hpp:70
#define elecMIN_E_RADIUS
Definition: elecParams.hpp:34
Point E(const Particle &p, const Point &at)
Definition: elecParticle.hpp:74
Particle(const Point &p, double qq)
Definition: elecParticle.hpp:43