aboutsummaryrefslogtreecommitdiff
path: root/Python/pywarpx/PGroup.py
blob: fa8572c0a33c3930b7dd560d3f301bceb554a0ba (plain) (blame)
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
import numpy as np
from . import WarpX
from . import _libwarpx

class PGroup(object):
    """Implements a class that has the same API as a warp ParticleGroup instance.
    """

    def __init__(self, igroup, ispecie):
        self.igroup = igroup
        self.ispecie = ispecie
        self.ns = 1 # Number of species

        self.gallot()

    def name(self):
        return 'WarpXParticleGroup'

    def gallot(self):
        self.lebcancel_pusher = 0 # turns on/off cancellation of E+VxB within V push (logical type)
        self.lebcancel = 0 # turns on/off cancellation of E+VxB before V push (logical type)

        self.sm = np.zeros(self.ns) # Species mass [kg]
        self.sq = np.zeros(self.ns) # Species charge [C]
        self.sw = np.zeros(self.ns) # Species weight, (real particles per simulation particles)

        self.sid = np.arange(self.ns, dtype=int) # Global species index for each species
        self.ndts = np.ones(self.ns, dtype=int) # Stride for time step advance for each species
        self.ldts = np.ones(self.ns, dtype=int) # (logical type)
        self.lvdts = np.ones(self.ns, dtype=int) # (logical type)
        self.iselfb = np.zeros(self.ns, dtype=int) # Group number for particles that are affected by
                                                   # their own magnetic field, using the 1/gamma**2
                                                   # approximation. The correction is not applied to
                                                   # group number -1.
        self.fselfb = np.zeros(self.ns) # The scaling factor, vz.
        self.l_maps = np.zeros(self.ns)
        self.dtscale = np.ones(self.ns) # Scale factor applied to time step size for each
                                        # species. Only makes sense in steaday and and
                                        # transverse slice modes.

        self.limplicit = np.zeros(self.ns, dtype=int) # Flags implicit particle species (logical type)
        self.iimplicit = np.full(self.ns, -1, dtype=int) # Group number for implicit particles
        self.ldoadvance = np.ones(self.ns, dtype=int) # Flags whether particles are time advanced (logical type)
        self.lboundaries = np.ones(self.ns, dtype=int) # Flags whether boundary conditions need to be applied (logical type)
        self.lparaxial = np.zeros(self.ns, dtype=int) # Flags to turn on/off paraxial approximation (logical type)

        self.zshift = np.zeros(self.ns)
        self.gamma_ebcancel_max = np.ones(self.ns) # maximum value allowed for ExB cancellation

    # --- Temporary fix
    gchange = gallot

    def allocated(self, name):
        return True

    def addspecies(self):
        pass

    def getnpid(self):
        return _libwarpx.get_nattr()
    npid = property(getnpid)

    def getnps(self):
        return np.array([len(self.xp)], dtype='l')
    nps = property(getnps)

    def getins(self):
        return np.ones(self.ns, dtype='l')
    ins = property(getins)

    def getipmax(self):
        return np.array([0, len(self.xp)], dtype='l')
    ipmax = property(getipmax)

    def getnpmax(self):
        return self.nps.sum()
    npmax = property(getnpmax)

    def getxp(self):
        return _libwarpx.get_particle_x(self.ispecie)[self.igroup]
    xp = property(getxp)

    def getyp(self):
        return _libwarpx.get_particle_y(self.ispecie)[self.igroup]
    yp = property(getyp)

    def getzp(self):
        return _libwarpx.get_particle_z(self.ispecie)[self.igroup]
    zp = property(getzp)

    def getuxp(self):
        return _libwarpx.get_particle_ux(self.ispecie)[self.igroup]
    uxp = property(getuxp)

    def getuyp(self):
        return _libwarpx.get_particle_uy(self.ispecie)[self.igroup]
    uyp = property(getuyp)

    def getuzp(self):
        return _libwarpx.get_particle_uz(self.ispecie)[self.igroup]
    uzp = property(getuzp)

    def getpid(self):
        id = _libwarpx.get_particle_id(self.ispecie)[self.igroup]
        pid = np.array([id]).T
        return pid
    pid = property(getpid)

    def getgaminv(self):
        uxp = self.getuxp()
        uyp = self.getuyp()
        uzp = self.getuzp()
        return np.sqrt(1. - (uxp**2 + uyp**2 + uzp**2)/_libwarpx.clight**2)
    gaminv = property(getgaminv)

    def getex(self):
        return _libwarpx.get_particle_Ex(self.ispecie)[self.igroup]
    ex = property(getex)

    def getey(self):
        return _libwarpx.get_particle_Ey(self.ispecie)[self.igroup]
    ey = property(getey)

    def getez(self):
        return _libwarpx.get_particle_Ez(self.ispecie)[self.igroup]
    ez = property(getez)

    def getbx(self):
        return _libwarpx.get_particle_Bx(self.ispecie)[self.igroup]
    bx = property(getbx)

    def getby(self):
        return _libwarpx.get_particle_By(self.ispecie)[self.igroup]
    by = property(getby)

    def getbz(self):
        return _libwarpx.get_particle_Bz(self.ispecie)[self.igroup]
    bz = property(getbz)

class PGroups(object):
    def __init__(self, ispecie=0):
        self.ispecie = ispecie

    def setuppgroups(self):
        xall = _libwarpx.get_particle_x(self.ispecie)
        self.ngroups = len(xall)

        self._pgroups = []
        for igroup in range(self.ngroups):
            self._pgroups.append(PGroup(igroup, self.ispecie))
        
    def __iter__(self):
        self.setuppgroups()
        for igroup in range(self.ngroups):
            yield self._pgroups[igroup]

    def __getitem__(self, key):
        self.setuppgroups()
        return self._pgroups[key]