• 98.85% Rate
  • 86 Hits
  • 1 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
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 3x
  • 1x
  • 4x
  • 2x
  • 2x
  • 1x
  • 1x
  • 7x
  • 2x
  • 2x
  • 10x
  • 2x
  • 2x
  • 8x
  • 2x
  • 2x
  • 3x
  • 3x
  • 3x
  • 1x
  • 4x
  • 2x
  • 2x
  • 1x
  • 2x
  • 1x
  • 1x
  • 1x
  • 1x
  • 218x
  • 33x
  • 185x
  • 3x
  • 182x
  • 3x
  • 179x
  • 7x
  • 172x
  • 86x
  • 86x
  • 8x
  • 78x
  • 1x
  • 80x
  • 21x
  • 21x
  • 24x
  • 59x
  • 35x
  • 52x
  • 12x
  • 12x
  • 7x
  • 7x
  • 2x
  • 2x
  • 73x
  • 2x
  • 34x
  • 34x
  • 34x
  • 34x
  • 10x
  • 10x
  • 3x
  • 9x
  • 8x
  • 8x
  • 8x
  • 8x
  • 56x
  • 48x
  • 2x
  • 2x
  • 33x
  • 2x
  • 1x
  • 1x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Monte Carlo state for PUMAS
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local ffi = require('ffi')
  • local error = require('pumas.error')
  • local metatype = require('pumas.metatype')
  • local state = {}
  • -------------------------------------------------------------------------------
  • -- The Monte Carlo state metatype
  • -------------------------------------------------------------------------------
  • -- XXX Add generators for configuring an initial state
  • local State = {}
  • local ctype = ffi.typeof('struct pumas_state_extended')
  • local pumas_state_ptr = ffi.typeof('struct pumas_state *')
  • local function clear (self)
  • if self == nil then
  • error.raise{fname = 'clear', argnum = 'bad', expected = 1, got = 0}
  • elseif metatype(self) ~= 'State' then
  • error.raise{fname = 'clear', argnum = 1, expected = 'a State table',
  • got = metatype.a(self)}
  • end
  • ffi.fill(self._c, ffi.sizeof(ctype))
  • return self
  • end
  • local function set (self, other)
  • if other == nil then
  • local nargs = (self ~= nil) and 1 or 0
  • error.raise{fname = 'set', argnum = 'bad', expected = 2, got = nargs}
  • elseif metatype(self) ~= 'State' then
  • error.raise{fname = 'set', argnum = 1, expected = 'a State table',
  • got = metatype.a(self)}
  • elseif metatype(other) ~= 'State' then
  • error.raise{fname = 'set', argnum = 2, expected = 'a State table',
  • got = metatype.a(other)}
  • end
  • ffi.copy(self._c, other._c, ffi.sizeof(ctype))
  • return self
  • end
  • local function clone (self)
  • if self == nil then
  • error.raise{fname = 'clone', argnum = 'bad', expected = 1, got = 0}
  • elseif metatype(self) ~= 'State' then
  • error.raise{fname = 'clone', argnum = 1, expected = 'a State table',
  • got = metatype.a(self)}
  • end
  • local other = state.State()
  • return other:set(self)
  • end
  • error.register('State.__index.clear', clear)
  • error.register('State.__index.clone', clone)
  • error.register('State.__index.set', set)
  • function State:__index (k)
  • if k == '__metatype' then
  • return 'State'
  • elseif k == 'clear' then
  • return clear
  • elseif k == 'clone' then
  • return clone
  • elseif k == 'set' then
  • return set
  • elseif (k == 'position') or (k == 'direction') then
  • return self._c[k]
  • elseif k == 'decayed' then
  • return (self._c.decayed ~= 0)
  • else
  • return tonumber(self._c[k])
  • end
  • end
  • function State:__newindex (k, v)
  • if (k == 'position') or (k == 'direction') then
  • local mt = metatype(v)
  • if mt == 'Coordinates' then
  • -- Get the Monte Carlo representation of the position or direction
  • v = v:get()
  • end
  • elseif (k == 'charge') or (k == 'energy') or (k == 'distance') or
  • (k == 'grammage') or (k == 'time') or (k == 'weight') then
  • if type(v) ~= 'number' then
  • error.raise{['type'] = 'State', argname = k, expected = 'a number',
  • got = metatype.a(v)}
  • end
  • elseif k == 'decayed' then
  • if type(v) ~= 'boolean' then
  • error.raise{['type'] = 'State', argname = k,
  • expected = 'a boolean', got = metatype.a(v)}
  • end
  • else
  • error.raise{['type'] = 'State', bad_attribute = k}
  • end
  • self._c[k] = v
  • end
  • -------------------------------------------------------------------------------
  • -- The Monte Carlo state constructor
  • -------------------------------------------------------------------------------
  • state.State = setmetatable(State, {
  • __call = function (cls, args)
  • local c = ffi.cast(pumas_state_ptr, ffi.C.calloc(1, ffi.sizeof(ctype)))
  • ffi.gc(c, ffi.C.free)
  • local self = setmetatable({_c = c}, cls)
  • if args ~= nil then
  • local mt = metatype(args)
  • if mt == 'State' then
  • self:set(args)
  • elseif mt == 'table' then
  • c.charge = -1
  • c.energy = 1
  • c.weight = 1
  • c.direction[2] = 1
  • for k, v in pairs(args) do
  • self[k] = v
  • end
  • else
  • error.raise{fname = 'State', argnum = 1,
  • expected = 'a (State) table', got = metatype.a(args)}
  • end
  • end
  • return self
  • end})
  • -------------------------------------------------------------------------------
  • -- Register the subpackage
  • -------------------------------------------------------------------------------
  • function state.register_to (t)
  • t.State = state.State
  • end
  • -------------------------------------------------------------------------------
  • -- Return the package
  • -------------------------------------------------------------------------------
  • return state