• 84.46% Rate
  • 125 Hits
  • 23 Missed
  • 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
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 19x
  • 36x
  • 1x
  • 76x
  • 76x
  • 76x
  • 1x
  • 19x
  • 36x
  • 18x
  • 1x
  • 1x
  • 1x
  • 30x
  • 30x
  • 30x
  • 3x
  • 6x
  • 1x
  • 1x
  • 29x
  • 15x
  • 10x
  • 10x
  • 29x
  • 1x
  • 2x
  • 1x
  • 1x
  • 3x
  • 1x
  • 4x
  • 2x
  • 2x
  • 1x
  • 1x
  • 3x
  • 1x
  • 4x
  • 2x
  • 2x
  • 1x
  • 1x
  • 1x
  • 1x
  • 63x
  • 6x
  • 57x
  • 3x
  • 54x
  • 3x
  • 51x
  • 51x
  • 6x
  • 45x
  • 1x
  • 15x
  • 15x
  • 2x
  • 13x
  • 9x
  • 4x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 5x
  • 4x
  • 4x
  • 4x
  • 4x
  • 15x
  • 22x
  • 11x
  • 11x
  • 10x
  • 10x
  • 10x
  • 10x
  • 10x
  • 20x
  • 16x
  • 14x
  • 10x
  • 1x
  • 20x
  • 20x
  • 4x
  • 16x
  • 40x
  • 40x
  • 16x
  • 16x
  • 16x
  • 16x
  • 1x
  • 1x
  • 5x
  • 25x
  • 20x
  • 5x
  • 11x
  • 11x
  • 55x
  • 44x
  • 11x
  • 1x
  • 2x
  • 1x
  • 1x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Wrapper for PUMAS enums
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local ffi = require('ffi')
  • local error = require('pumas.error')
  • local metatype = require('pumas.metatype')
  • local enum = {}
  • -------------------------------------------------------------------------------
  • -- The Event metatype
  • -------------------------------------------------------------------------------
  • do
  • ffi.cdef('struct pumas_event_w {enum pumas_event _value;}')
  • local event_t = ffi.typeof('struct pumas_event_w')
  • local tags = {
  • 'none', 'start', 'medium', 'weight', 'stop', 'limit', 'vertex',
  • 'vertex_decay', 'vertex_del', 'limit_distance', 'limit_grammage',
  • 'limit_energy', 'limit_time', 'vertex_coulomb',
  • 'vertex_bremsstrahlung', 'vertex_delta_ray', 'vertex_pair_creation',
  • 'vertex_photonuclear'
  • }
  • local raise_error = error.ErrorFunction{['type'] = 'Event'}
  • local get_value
  • do
  • local mapping = {}
  • for _, k in ipairs(tags) do
  • mapping[k] = tonumber(ffi.C['PUMAS_EVENT_'..k:upper()])
  • end
  • function get_value (k)
  • local v = mapping[k]
  • if v then
  • return v
  • else
  • raise_error{bad_member = k}
  • end
  • end
  • end
  • local get_string
  • do
  • local mapping = {}
  • for _, k in ipairs(tags) do
  • local v = tonumber(ffi.C['PUMAS_EVENT_'..k:upper()])
  • mapping[v] = k
  • end
  • function get_string (k)
  • return mapping[k]
  • end
  • end
  • local Event = {}
  • function Event.__new (ct, ...)
  • local self = ffi.new(ct)
  • local n = select('#', ...)
  • if n == 1 then
  • -- Copy constructor
  • local arg = select(1, ...)
  • if metatype(arg) == 'Event' then
  • self._value = arg._value
  • return self
  • end
  • end
  • if n > 0 then
  • for _, k in ipairs{...} do
  • local b = get_value(k)
  • self._value = bit.bor(tonumber(self._value), b)
  • end
  • end
  • return self
  • end
  • function Event.__eq (self, other)
  • if ffi.istype(event_t, other) then
  • return self._value == other._value
  • else
  • return self._value == other
  • end
  • end
  • local function clear (self)
  • if not self then
  • error.raise{fname = 'clear', argnum = 'bad', expected = 1, got = 0}
  • elseif metatype(self) ~= 'Event' then
  • error.raise{fname = 'clear', argnum = 1,
  • expected = 'an Event table', got = metatype.a(self)}
  • end
  • self._value = 0
  • return self
  • end
  • local function clone (self)
  • if self == nil then
  • error.raise{fname = 'clone', argnum = 'bad', expected = 1, got = 0}
  • elseif metatype(self) ~= 'Event' then
  • error.raise{fname = 'clone', argnum = 1,
  • expected = 'an Event table', got = metatype.a(self)}
  • end
  • local other = enum.Event()
  • other._value = self._value
  • return other
  • end
  • function Event.__index (self, k)
  • if k == '__metatype' then
  • return 'Event'
  • elseif k == 'clear' then
  • return clear
  • elseif k == 'clone' then
  • return clone
  • else
  • local b = get_value(k)
  • if b == 0 then
  • return self._value == 0
  • else
  • return bit.band(tonumber(self._value), b) ~= 0
  • end
  • end
  • end
  • function Event.__newindex (self, k, v)
  • local b = get_value(k)
  • if b == 0 then
  • self._value = 0
  • elseif v then
  • self._value = bit.bor(tonumber(self._value), b)
  • else
  • self._value = bit.band(tonumber(self._value), bit.bnot(b))
  • end
  • end
  • function Event.__tostring (self)
  • local v = tonumber(self._value)
  • local s = get_string(v)
  • if s then return s end
  • local t = {}
  • for _, k in ipairs(tags) do
  • local b = get_value(k)
  • if b ~= 0 then
  • if bit.band(tonumber(self._value), b) == b then
  • table.insert(t, k)
  • self._value = bit.band(tonumber(self._value), bit.bnot(b))
  • end
  • end
  • end
  • self._value = v
  • return table.concat(t, ' ')
  • end
  • enum.Event = ffi.metatype(event_t, Event)
  • end
  • -------------------------------------------------------------------------------
  • -- The Mode wrapper
  • -------------------------------------------------------------------------------
  • do
  • local Mode = {}
  • local tags = {
  • {'direction', {'forward', 'backward'}},
  • {'scattering', {'full_space', 'longitudinal'}},
  • {'energy_loss', {'virtual', 'csda', 'hybrid', 'detailed'}},
  • {'decay', {'stable', 'weight', 'decay'}}}
  • local strtoval, valtostr, categories = {}, {}, {}
  • for _, data in ipairs(tags) do
  • local category, subtags = unpack(data)
  • table.insert(categories, category)
  • local t = {}
  • valtostr[category] = t
  • for _, k in ipairs(subtags) do
  • local v = tonumber(ffi.C['PUMAS_MODE_'..k:upper()])
  • strtoval[k] = {category, v}
  • t[v] = k
  • end
  • end
  • local function set1 (self, s, category)
  • local r = strtoval[s]
  • if r then
  • local k, v = unpack(r)
  • if category and (category ~= k) then
  • for _, c in ipairs(categories) do
  • if c == category then
  • error.raise{fname = 'Mode.'..category, argname = s,
  • description = 'invalid value'}
  • end
  • end
  • error.raise{['type'] = 'Mode', bad_member = category}
  • end
  • self._c[k] = v
  • else
  • error.raise{['type'] = 'Mode', bad_member = s}
  • end
  • end
  • local function set (self, str)
  • for _, category in ipairs(categories) do
  • self._c[category] = self._default[category]
  • end
  • for s in str:gmatch('[%w_]+') do
  • set1(self, s)
  • end
  • end
  • function Mode:__index (k)
  • if k == '__metatype' then
  • return 'Mode'
  • elseif k == 'set' then
  • return set
  • else
  • local ok = false
  • for _, category in ipairs(categories) do
  • if k == category then
  • ok = true
  • break
  • end
  • end
  • if not ok then
  • error.raise{['type'] = 'Mode', bad_member = k}
  • else
  • return valtostr[k][tonumber(self._c[k])]
  • end
  • end
  • end
  • function Mode:__newindex (k, s)
  • set1(self, s, k)
  • end
  • function Mode:__tostring ()
  • local t = {}
  • for _, category in ipairs(categories) do
  • table.insert(t, valtostr[category][tonumber(self._c[category])])
  • end
  • return table.concat(t, ' ')
  • end
  • local function new (cls, c_context)
  • local c = c_context.mode
  • local default = {}
  • for _, category in ipairs(categories) do
  • default[category] = tonumber(c[category])
  • end
  • return setmetatable({_c = c, _default = default}, cls)
  • end
  • enum.Mode = setmetatable(Mode, {__call = new})
  • error.register('enum.Mode', Mode)
  • end
  • -------------------------------------------------------------------------------
  • -- Register enums to a table
  • -------------------------------------------------------------------------------
  • function enum.register_to (t)
  • t.Event = enum.Event
  • end
  • -------------------------------------------------------------------------------
  • -- Return the package
  • -------------------------------------------------------------------------------
  • return enum