• 88.84% Rate
  • 199 Hits
  • 25 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
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 11x
  • 11x
  • 22x
  • 11x
  • 11x
  • 11x
  • 35x
  • 24x
  • 24x
  • 11x
  • 5x
  • 5x
  • 10x
  • 5x
  • 5x
  • 5x
  • 10x
  • 5x
  • 5x
  • 1x
  • 1x
  • 1x
  • 60x
  • 31x
  • 29x
  • 29x
  • 40x
  • 8x
  • 16x
  • 8x
  • 32x
  • 64x
  • 32x
  • 80x
  • 80x
  • 200x
  • 480x
  • 160x
  • 160x
  • 40x
  • 8x
  • 32x
  • 5x
  • 1x
  • 2x
  • 4x
  • 4x
  • 15x
  • 10x
  • 4x
  • 8x
  • 40x
  • 30x
  • 60x
  • 10x
  • 5x
  • 10x
  • 10x
  • 5x
  • 10x
  • 10x
  • 10x
  • 16x
  • 5x
  • 12x
  • 4x
  • 4x
  • 105x
  • 105x
  • 47x
  • 4x
  • 2x
  • 6x
  • 4x
  • 4x
  • 2x
  • 4x
  • 2x
  • 2x
  • 2x
  • 2x
  • 12x
  • 6x
  • 2x
  • 2x
  • 2x
  • 2x
  • 1x
  • 2x
  • 1x
  • 3x
  • 6x
  • 3x
  • 3x
  • 8x
  • 24x
  • 16x
  • 8x
  • 6x
  • 18x
  • 12x
  • 6x
  • 3x
  • 6x
  • 6x
  • 3x
  • 6x
  • 18x
  • 12x
  • 6x
  • 3x
  • 6x
  • 6x
  • 3x
  • 1x
  • 335x
  • 6x
  • 329x
  • 12x
  • 317x
  • 221x
  • 96x
  • 4x
  • 4x
  • 4x
  • 92x
  • 92x
  • 76x
  • 92x
  • 92x
  • 92x
  • 40x
  • 40x
  • 52x
  • 11x
  • 41x
  • 1x
  • 18x
  • 1x
  • 1x
  • 18x
  • 9x
  • 9x
  • 9x
  • 9x
  • 9x
  • 18x
  • 9x
  • 9x
  • 9x
  • 9x
  • 9x
  • 6x
  • 6x
  • 6x
  • 6x
  • 12x
  • 6x
  • 6x
  • 12x
  • 6x
  • 12x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 6x
  • 3x
  • 3x
  • 3x
  • 3x
  • 3x
  • 3x
  • 6x
  • 3x
  • 9x
  • 9x
  • 9x
  • 2x
  • 2x
  • 2x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Tabulation wrapper for PUMAS Physics
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local ffi = require('ffi')
  • local call = require('pumas.call')
  • local compat = require('pumas.compat')
  • local clib = require('pumas.clib')
  • local error = require('pumas.error')
  • local material_ = require('pumas.material')
  • local metatype = require('pumas.metatype')
  • local composite = require('pumas.physics.composite')
  • local readonly = require('pumas.readonly')
  • local tabulated = {}
  • -------------------------------------------------------------------------------
  • -- Utility functions providing providing some material properties
  • -------------------------------------------------------------------------------
  • local function parse_composition (index, n_elements, c)
  • local indices = ffi.new('int [?]', n_elements)
  • local fractions = ffi.new('double [?]', n_elements)
  • clib.pumas_physics_material_properties(c, index,
  • nil, nil, nil, nil, indices, fractions)
  • local name_ = ffi.new('const char *[1]')
  • local composition = compat.table_new(0, n_elements)
  • for i = 1, n_elements do
  • clib.pumas_physics_element_name(c, indices[i - 1], name_)
  • composition[ffi.string(name_[0])] = tonumber(fractions[i - 1])
  • end
  • return readonly.Readonly(composition, 'elements')
  • end
  • local function parse_composite (physics_, index)
  • local n_elements= ffi.new('int [1]')
  • local density = ffi.new('double [1]')
  • clib.pumas_physics_material_properties(physics_._c[0], index,
  • n_elements, density, nil, nil, nil, nil)
  • n_elements = tonumber(n_elements[0])
  • local composition = parse_composition(index, n_elements, physics_._c[0])
  • local ZoA, I = material_.compute_ZoA_and_I(
  • composition, physics_.elements)
  • return tonumber(density[0]), composition, ZoA, I
  • end
  • -------------------------------------------------------------------------------
  • -- The TabulateMaterial metatype
  • -------------------------------------------------------------------------------
  • local TabulatedMaterial = {}
  • do
  • local value = ffi.new('double [1]')
  • local toindex
  • do
  • function toindex (mode, fname)
  • if (mode == nil) or (mode == 'csda') then
  • return clib.PUMAS_MODE_CSDA
  • elseif (mode == 'hybrid') or (mode == 'detailed') then
  • return clib.PUMAS_MODE_HYBRID
  • else
  • error.raise{fname = fname, argnum = 3,
  • description = "invalid mode '"..mode.."'"}
  • end
  • end
  • end
  • local function get_or_update_table (self, property, mode, ref)
  • local n, is_update, t
  • if ref then
  • is_update = true
  • t = readonly.rawget(ref)
  • n = #t
  • else
  • is_update = false
  • n = tonumber(clib.pumas_physics_table_length(self.physics._c[0]))
  • t = compat.table_new(n, 0)
  • end
  • local c_property = clib['PUMAS_PROPERTY_'..property:upper()]
  • mode = toindex(mode)
  • for i = 0, n - 1 do
  • clib.pumas_physics_table_value(self.physics._c[0], c_property, mode,
  • self._index, i, value)
  • t[i + 1] = tonumber(value[0])
  • end
  • if is_update then
  • return ref
  • else
  • return readonly.Readonly(t, property, 'PhysicsTable')
  • end
  • end
  • local function get_or_update_all_tables (self, t)
  • local is_update
  • if t then
  • is_update = true
  • t = readonly.rawget(t)
  • else
  • is_update = false
  • t = {}
  • end
  • for _, mode in pairs{'csda', 'hybrid'} do
  • local v
  • if is_update then
  • v = readonly.rawget(t[mode])
  • else
  • v = {}
  • end
  • for _, k in pairs{'energy_loss', 'grammage', 'proper_time'} do
  • local ref = is_update and v[k] or nil
  • v[k] = get_or_update_table(self, k, mode, ref)
  • end
  • if mode == 'csda' then
  • local ref = is_update and v['kinetic_energy'] or nil
  • v['kinetic_energy'] = get_or_update_table(
  • self, 'kinetic_energy', mode, ref)
  • else
  • local ref = is_update and v['cross_section'] or nil
  • v['cross_section'] = get_or_update_table(
  • self, 'cross_section', mode, ref)
  • end
  • if not is_update then
  • t[mode] = readonly.Readonly(v, mode)
  • end
  • end
  • if not is_update then
  • readonly.rawget(t.hybrid).kinetic_energy = t.csda.kinetic_energy
  • t.detailed = t.hybrid
  • return readonly.Readonly(t)
  • end
  • end
  • local function update (self, fname)
  • if not self then
  • fname = fname or 'TabulatedMaterial'
  • error.raise{fname = fname, argnum = 1,
  • expected = 'a TabulatedMaterial table', got = metatype.a(self)}
  • end
  • if self._properties.composite and
  • self._properties.materials._needs_update then
  • local fractions = ffi.new('double [?]',
  • self._properties.materials._n)
  • for i, k in ipairs(self._properties.materials._names) do
  • fractions[i - 1] = self._properties.materials._fractions[k]
  • end
  • clib.pumas_physics_composite_update(self._physics._c[0],
  • self._index, fractions)
  • local density, composition, ZoA, I = parse_composite(
  • self._physics, self._index)
  • self._properties.density = density
  • if self._properties.elements then
  • local t = readonly.rawget(self._properties.elements)
  • for k, v in composition:pairs() do
  • t[k] = v
  • end
  • else
  • self._properties.elements = composition
  • end
  • self._properties.ZoA = ZoA
  • self._properties.I = I
  • local tables = rawget(self, 'table')
  • if tables then
  • get_or_update_all_tables(self, tables)
  • end
  • self._properties.materials._needs_update = false
  • end
  • end
  • local index = {
  • cross_section = function (self, energy)
  • update(self, 'cross_section')
  • call(clib.pumas_physics_property_cross_section, self._physics._c[0],
  • self._index, energy, value)
  • return tonumber(value[0])
  • end,
  • energy_loss = function (self, energy, mode)
  • update(self, 'energy_loss')
  • call(clib.pumas_physics_property_energy_loss, self.physics._c[0],
  • toindex(mode, 'energy_loss'), self._index, energy, value)
  • return tonumber(value[0])
  • end,
  • grammage = function (self, energy, mode)
  • update(self, 'grammage')
  • call(clib.pumas_physics_property_grammage, self.physics._c[0],
  • toindex(mode, 'grammage'), self._index, energy, value)
  • return tonumber(value[0])
  • end,
  • kinetic_energy = function (self, grammage, mode)
  • update(self, 'kinetic_energy')
  • call(clib.pumas_physics_property_kinetic_energy, self.physics._c[0],
  • toindex(mode, 'kinetic_energy'), self._index, grammage, value)
  • return tonumber(value[0])
  • end,
  • magnetic_rotation = function (self, energy)
  • update(self, 'magnetic_rotation')
  • call(clib.pumas_physics_property_magnetic_rotation,
  • self.physics._c[0], self._index, energy, value)
  • return tonumber(value[0])
  • end,
  • proper_time = function (self, energy, mode)
  • update(self, 'proper_time')
  • call(clib.pumas_physics_property_proper_time, self.physics._c[0],
  • toindex(mode, 'proper_time'), self._index, energy, value)
  • return tonumber(value[0])
  • end,
  • scattering_length = function (self, energy)
  • update(self, 'scattering_length')
  • call(clib.pumas_physics_property_scattering_length,
  • self.physics._c[0], self._index, energy, value)
  • return tonumber(value[0])
  • end
  • }
  • function TabulatedMaterial:__index (k)
  • if k == '__metatype' then
  • return 'TabulatedMaterial'
  • elseif k == 'name' then
  • return self._name
  • elseif k == 'physics' then
  • return self._physics
  • elseif k == 'table' then
  • local t = get_or_update_all_tables(self)
  • rawset(self, 'table', t)
  • return t
  • elseif k == '_update' then
  • return update
  • else
  • if (k ~= 'materials') and (k ~= 'composite') then
  • update(self)
  • end
  • local prop = rawget(self, '_properties')
  • prop = prop and prop[k]
  • if prop ~= nil then return prop end
  • local func = index[k]
  • if func then return func end
  • for _, attr in ipairs{'materials', 'state', 'a', 'k', 'x0', 'x1',
  • 'Cbar', 'delta0'} do
  • if k == attr then return nil end
  • end
  • error.raise{['type'] = 'TabulatedMaterial', bad_member = k}
  • end
  • end
  • end
  • function TabulatedMaterial.__newindex (_, k, _)
  • error.raise{['type'] = 'TabulatedMaterial', not_mutable = k}
  • end
  • function TabulatedMaterial:__tostring ()
  • return self._name
  • end
  • -------------------------------------------------------------------------------
  • -- The TabulateMaterial constructor
  • -------------------------------------------------------------------------------
  • do
  • local raise_error = error.ErrorFunction{fname = 'TabulatedMaterial'}
  • local function new (cls, physics_, arg)
  • if metatype(physics_) ~= 'Physics' then
  • raise_error{argnum = 1, expected = 'a Physics table',
  • got = metatype.a(physics_)}
  • end
  • local n = clib.pumas_physics_material_length(physics_._c[0])
  • local name, index
  • local tp = type(arg)
  • if tp == 'number' then
  • if (arg < 0) or (arg >= n) then
  • raise_error{argnum = 1, expected = 'a value between '..
  • '0 and '..(n - 1), got = arg}
  • end
  • local c_name = ffi.new('const char *[1]')
  • call(clib.pumas_physics_material_name, physics_._c[0], arg,
  • c_name)
  • index = arg
  • name = ffi.string(c_name[0])
  • elseif tp == 'string' then
  • local c_index = ffi.new('int [1]')
  • call(clib.pumas_physics_material_index, physics_._c[0], arg,
  • c_index)
  • index = tonumber(c_index[0])
  • name = arg
  • else
  • raise_error{argnum = 1, expected = 'a number or a string',
  • got = metatype.a(arg)}
  • end
  • local m = clib.pumas_physics_composite_length(physics_._c[0])
  • local properties
  • if index < n - m then
  • -- This is a base material. Let us fetch its properties
  • local n_elements= ffi.new('int [1]')
  • local density = ffi.new('double [1]')
  • local I = ffi.new('double [1]')
  • local density_effect =
  • ffi.new('struct pumas_physics_density_effect[1]')
  • clib.pumas_physics_material_properties(physics_._c[0], index,
  • n_elements, density, I, density_effect, nil, nil)
  • n_elements = tonumber(n_elements[0])
  • local composition = parse_composition(
  • index, n_elements, physics_._c[0])
  • local ZoA = material_.compute_ZoA(composition, physics_.elements)
  • properties = {
  • composite = false,
  • density = tonumber(density[0]),
  • I = tonumber(I[0]),
  • a = tonumber(density_effect[0].a),
  • k = tonumber(density_effect[0].k),
  • x0 = tonumber(density_effect[0].x0),
  • x1 = tonumber(density_effect[0].x1),
  • Cbar = tonumber(density_effect[0].Cbar),
  • delta0 = tonumber(density_effect[0].delta0),
  • elements = composition,
  • ZoA = ZoA}
  • else
  • -- This is a composite material. Let us fetch its properties
  • local density, composition, ZoA, I = parse_composite(
  • physics_, index)
  • properties = {
  • composite = true,
  • density = density,
  • I = I,
  • elements = composition,
  • ZoA = ZoA,
  • materials = composite.CompositeMaterials(physics_, index)
  • }
  • end
  • local self = {_physics = physics_, _index = index, _name = name,
  • _properties = properties}
  • return setmetatable(self, cls)
  • end
  • tabulated.TabulatedMaterial = setmetatable(
  • TabulatedMaterial, {__call = new})
  • error.register('physics.TabulatedMaterial', TabulatedMaterial)
  • end
  • -------------------------------------------------------------------------------
  • -- Return the sub-package
  • -------------------------------------------------------------------------------
  • return tabulated