• 91.62% Rate
  • 153 Hits
  • 14 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
  • 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
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 15x
  • 15x
  • 12x
  • 3x
  • 3x
  • 3x
  • 6x
  • 3x
  • 6x
  • 6x
  • 3x
  • 3x
  • 5x
  • 5x
  • 4x
  • 2x
  • 2x
  • 2x
  • 2x
  • 2x
  • 2x
  • 2x
  • 1x
  • 1x
  • 1x
  • 17x
  • 17x
  • 15x
  • 2x
  • 2x
  • 2x
  • 2x
  • 8x
  • 6x
  • 12x
  • 6x
  • 6x
  • 12x
  • 6x
  • 6x
  • 12x
  • 4x
  • 2x
  • 2x
  • 11x
  • 11x
  • 8x
  • 3x
  • 3x
  • 3x
  • 3x
  • 9x
  • 6x
  • 12x
  • 6x
  • 3x
  • 3x
  • 10x
  • 10x
  • 8x
  • 2x
  • 2x
  • 2x
  • 4x
  • 2x
  • 2x
  • 4x
  • 4x
  • 2x
  • 2x
  • 4x
  • 2x
  • 2x
  • 4x
  • 1x
  • 1x
  • 1x
  • 1x
  • 4x
  • 2x
  • 2x
  • 2x
  • 2x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 104x
  • 25x
  • 79x
  • 13x
  • 66x
  • 4x
  • 62x
  • 4x
  • 58x
  • 15x
  • 43x
  • 5x
  • 38x
  • 17x
  • 21x
  • 11x
  • 10x
  • 10x
  • 1x
  • 4x
  • 16x
  • 16x
  • 4x
  • 2x
  • 2x
  • 2x
  • 1x
  • 1x
  • 5x
  • 5x
  • 5x
  • 5x
  • 5x
  • 5x
  • 1x
  • 2x
  • 4x
  • 4x
  • 4x
  • 4x
  • 3x
  • 4x
  • 4x
  • 8x
  • 4x
  • 4x
  • 10x
  • 5x
  • 5x
  • 5x
  • 1x
  • 1x
  • -------------------------------------------------------------------------------
  • -- PUMAS Physics metatype
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local lfs = require('lfs')
  • local ffi = require('ffi')
  • local call = require('pumas.call')
  • local clib = require('pumas.clib')
  • local compat = require('pumas.compat')
  • local context = require('pumas.context')
  • local error = require('pumas.error')
  • local os = require('pumas.os')
  • local readonly = require('pumas.readonly')
  • local tabulated = require('pumas.physics.tabulated')
  • local utils = require('pumas.physics.utils')
  • local type_ = {}
  • -------------------------------------------------------------------------------
  • -- Lazy loaders for the Physics
  • -------------------------------------------------------------------------------
  • local function load_composites (self)
  • local tmp = rawget(self, '_composites')
  • if tmp then
  • return tmp
  • end
  • local n = tonumber(clib.pumas_physics_material_length(self._c[0]))
  • local m = tonumber(clib.pumas_physics_composite_length(self._c[0]))
  • local composites = compat.table_new(0, m)
  • for i = n - m, n - 1 do
  • local comp = tabulated.TabulatedMaterial(self, i)
  • composites[comp.name] = comp
  • end
  • composites = readonly.Readonly(composites, 'composites')
  • rawset(self, '_composites', composites)
  • return composites
  • end
  • -- XXX Allow to modify cross-sections?
  • local function load_dcs (self)
  • local tmp = rawget(self, '_dcs')
  • if tmp then
  • return tmp
  • end
  • local dcs = readonly.Readonly({
  • bremsstrahlung = clib.pumas_physics_dcs_get(
  • self._c[0], clib.PUMAS_PROCESS_BREMSSTRAHLUNG),
  • pair_production = clib.pumas_physics_dcs_get(
  • self._c[0], clib.PUMAS_PROCESS_PAIR_PRODUCTION),
  • photonuclear = clib.pumas_physics_dcs_get(
  • self._c[0], clib.PUMAS_PROCESS_PHOTONUCLEAR)
  • }, 'dcs')
  • rawset(self, '_dcs', dcs)
  • return dcs
  • end
  • local function load_elements (self)
  • local tmp = rawget(self, '_elements')
  • if tmp then
  • return tmp
  • end
  • local n = tonumber(clib.pumas_physics_element_length(self._c[0]))
  • local elements = compat.table_new(0, n)
  • local name = ffi.new('const char *[1]')
  • local properties = ffi.new('double [3]')
  • for i = 0, n - 1 do
  • clib.pumas_physics_element_name(self._c[0], i, name)
  • clib.pumas_physics_element_properties(self._c[0], i, properties,
  • properties + 1, properties + 2)
  • local k = ffi.string(name[0])
  • elements[k] = readonly.Readonly(
  • {Z = tonumber(properties[0]), A = tonumber(properties[1]),
  • I = tonumber(properties[2])}, "Element' table '"..k,
  • 'Element')
  • end
  • elements = readonly.Readonly(elements, 'elements')
  • rawset(self, '_elements', elements)
  • return elements
  • end
  • local function load_materials (self)
  • local tmp = rawget(self, '_materials')
  • if tmp then
  • return tmp
  • end
  • local n = tonumber(clib.pumas_physics_material_length(self._c[0]))
  • local m = tonumber(clib.pumas_physics_composite_length(self._c[0]))
  • n = n - m
  • local materials = compat.table_new(0, n)
  • for i = 0, n - 1 do
  • local mat = tabulated.TabulatedMaterial(self, i)
  • materials[mat.name] = mat
  • end
  • materials = readonly.Readonly(materials, 'materials')
  • rawset(self, '_materials', materials)
  • return materials
  • end
  • local function load_particle (self)
  • local tmp = rawget(self, '_particle')
  • if tmp then
  • return tmp
  • end
  • local particle = ffi.new('enum pumas_particle [1]')
  • local lifetime = ffi.new('double [1]')
  • local mass = ffi.new('double [1]')
  • local rc = clib.pumas_physics_particle(self._c[0], particle, lifetime,
  • mass)
  • if rc == 0 then
  • particle = readonly.Readonly({
  • name = utils.particle_string(particle[0]),
  • lifetime = tonumber(lifetime[0]),
  • mass = tonumber(mass[0])
  • }, 'particle')
  • else
  • error.raise{['type'] = 'Physics', bad_member = 'particle'}
  • end
  • rawset(self, '_particle', particle)
  • return particle
  • end
  • -------------------------------------------------------------------------------
  • -- Inner routine for updating composite data if the composition has changed
  • -------------------------------------------------------------------------------
  • local function update_composites (self)
  • if self._update_composites then
  • for _, v in pairs(self._composites) do
  • v:_update()
  • end
  • self._update_composites = false
  • end
  • end
  • -------------------------------------------------------------------------------
  • -- Dump the material tables to a binary file
  • -------------------------------------------------------------------------------
  • local dump_physics
  • do
  • local raise_error = error.ErrorFunction{
  • argnum = 2,
  • fname = 'dump'
  • }
  • function dump_physics (self, path)
  • if path == nil then
  • local nargs = (self ~= nil) and 1 or 0
  • raise_error{argnum = 'bad', expected = 2, got = nargs}
  • end
  • local f = io.open(path, 'wb')
  • if f == nil then
  • raise_error('could not open file '..path)
  • end
  • local errmsg = call.protected(clib.pumas_physics_dump, self._c[0], f)
  • f:close()
  • if errmsg then
  • raise_error{
  • header = 'error when dumping materials',
  • errmsg
  • }
  • end
  • end
  • end
  • -------------------------------------------------------------------------------
  • -- The Physics metatype
  • -------------------------------------------------------------------------------
  • local Physics = {}
  • do
  • function Physics:__index (k)
  • if k == '__metatype' then
  • return 'Physics'
  • elseif k == 'Context' then
  • return context.Context
  • elseif k == 'dump' then
  • return dump_physics
  • elseif k == '_update' then
  • return update_composites
  • elseif k == 'composites' then
  • return load_composites(self)
  • elseif k == 'dcs' then
  • return load_dcs(self)
  • elseif k == 'elements' then
  • return load_elements(self)
  • elseif k == 'materials' then
  • return load_materials(self)
  • elseif k == 'particle' then
  • return load_particle(self)
  • else
  • error.raise{['type'] = 'Physics', bad_member = k}
  • end
  • end
  • end
  • function Physics:__newindex (k, v)
  • local fields = {'__metatype', 'Context', 'composites', 'elements',
  • 'materials', 'particle'}
  • for _, attr in ipairs(fields) do
  • if k == attr then
  • if v == nil then
  • rawset(self, '_'..k, nil) -- It is allowed to unload the data
  • return
  • else
  • error.raise{['type'] = 'Physics', not_mutable = k}
  • end
  • end
  • end
  • error.raise{['type'] = 'Physics', bad_member = k}
  • end
  • -------------------------------------------------------------------------------
  • -- The Physics constructor
  • -------------------------------------------------------------------------------
  • do
  • local physics_version = 0
  • local raise_error = error.ErrorFunction{fname = 'Physics'}
  • local function new (cls, args)
  • local c = ffi.new('struct pumas_physics *[1]')
  • ffi.gc(c, clib.pumas_physics_destroy)
  • if args == nil then
  • error.raise{
  • argnum = 'bad',
  • expected = 1,
  • got = 0
  • }
  • end
  • local tp = type(args)
  • if (tp ~= 'string') and (tp ~= 'table') then
  • raise_error{
  • argnum = 1,
  • expected = 'a string or a table',
  • got = 'a '..type(args)
  • }
  • end
  • -- Load the physics tables
  • if tp == 'table' then
  • local particle = utils.particle_ctype(args.particle, raise_error)
  • call(clib.pumas_physics_create, c, particle, args.mdf, args.dedx)
  • else
  • local path = args
  • local mode, errmsg = lfs.attributes(path, 'mode')
  • if mode == nil then
  • raise_error(errmsg)
  • elseif mode == 'directory' then
  • path = path..os.PATHSEP..'materials.pumas'
  • end
  • local f = io.open(path, 'rb')
  • if f == nil then
  • raise_error('could not open file '..path)
  • end
  • errmsg = call.protected(clib.pumas_physics_load, c, f)
  • f:close()
  • if errmsg then
  • raise_error{
  • header = 'error when loading materials',
  • errmsg
  • }
  • end
  • end
  • local self = setmetatable({_c = c, _version = physics_version,
  • _update_composites = false}, cls)
  • physics_version = physics_version + 1
  • return self
  • end
  • type_.Physics = setmetatable(Physics, {__call = new})
  • end
  • -------------------------------------------------------------------------------
  • -- Return the sub-package
  • -------------------------------------------------------------------------------
  • return type_