• 77.78% Rate
  • 126 Hits
  • 36 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
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 1x
  • 12x
  • 6x
  • 6x
  • 1x
  • 5x
  • 6x
  • 3x
  • 4x
  • 2x
  • 2x
  • 2x
  • 6x
  • 2x
  • 1x
  • 52x
  • 6x
  • 46x
  • 13x
  • 12x
  • 1x
  • 33x
  • 3x
  • 30x
  • 3x
  • 27x
  • 3x
  • 2x
  • 1x
  • 24x
  • 3x
  • 21x
  • 15x
  • 6x
  • 1x
  • 10x
  • 3x
  • 1x
  • 1x
  • 4x
  • 2x
  • 2x
  • 2x
  • 7x
  • 1x
  • 6x
  • 1x
  • 5x
  • 2x
  • 4x
  • 2x
  • 1x
  • 1x
  • 1x
  • 3x
  • 1x
  • 2x
  • 2x
  • 2x
  • 1x
  • 1x
  • 1x
  • 7x
  • 7x
  • 7x
  • 7x
  • 7x
  • 7x
  • 7x
  • 5x
  • 5x
  • 5x
  • 19x
  • 14x
  • 41x
  • 41x
  • 14x
  • 14x
  • 14x
  • 5x
  • 2x
  • 2x
  • 2x
  • 2x
  • 3x
  • 5x
  • 2x
  • 3x
  • 3x
  • 5x
  • 4x
  • 4x
  • 4x
  • 2x
  • 5x
  • 4x
  • 2x
  • 5x
  • 5x
  • 5x
  • 2x
  • 2x
  • 2x
  • 5x
  • 10x
  • 5x
  • 5x
  • 2x
  • 2x
  • 6x
  • 3x
  • 3x
  • 5x
  • 1x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Uniform medium for PUMAS
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local ffi = require('ffi')
  • local clib = require('pumas.clib')
  • local coordinates = require('pumas.coordinates')
  • local error = require('pumas.error')
  • local material_ = require('pumas.material')
  • local base = require('pumas.medium.base')
  • local metatype = require('pumas.metatype')
  • local gradient = {}
  • -------------------------------------------------------------------------------
  • -- The gradient medium metatype
  • -------------------------------------------------------------------------------
  • local GradientMedium = {}
  • local strtype = 'GradientMedium'
  • local gradient_density
  • do
  • local state = ffi.new('struct pumas_state_extended')
  • function gradient_density (self, arg)
  • if metatype(self) ~= 'Medium' then
  • error.raise{fname = 'density', argnum = 1,
  • expected = 'a GradientMedium table', got = metatype.a(self)}
  • end
  • local mt, s = metatype(arg)
  • if mt == 'State' then
  • s = ffi.cast('struct pumas_state_extended *', arg._c)
  • elseif mt == 'Coordinates' then
  • state.base.position = arg:get()
  • s = state
  • else
  • local ok, msg = pcall(function ()
  • state.base.position = arg
  • end)
  • if ok then
  • s = state
  • else
  • error.raise{fname = 'density', argnum = 2, description = msg}
  • end
  • end
  • return tonumber(clib.pumas_medium_gradient_density(self._c, s))
  • end
  • error.register('GradientMedium.__index.density', gradient_density)
  • end
  • function GradientMedium:__index (k)
  • if k == 'density' then
  • return gradient_density
  • elseif k == 'axis' then
  • if self._c.gradient.project == nil then
  • return self._c.gradient.axis
  • else
  • return 'vertical'
  • end
  • elseif k == 'lambda' then
  • return tonumber(self._c.gradient.lambda)
  • elseif k == 'rho0' then
  • return tonumber(self._c.gradient.rho0)
  • elseif k == 'type' then
  • if self._c.gradient.type == clib.PUMAS_MEDIUM_GRADIENT_LINEAR then
  • return 'linear'
  • else
  • return 'exponential'
  • end
  • elseif k == 'z0' then
  • return tonumber(self._c.gradient.z0)
  • elseif k == 'magnet' then
  • return self._c.magnet
  • else
  • return base.BaseMedium.__index(self, k, strtype)
  • end
  • end
  • function GradientMedium:__newindex (k, v)
  • if k == 'axis' then
  • if v == 'vertical' then
  • self._c.gradient.project =
  • clib.pumas_medium_gradient_project_altitude
  • else
  • local ok, description = coordinates.set_double3(
  • self._c.gradient, 'axis', v)
  • if not ok then
  • error.raise{fname = 'GradientMedium', argname = 'axis',
  • description = description}
  • end
  • self._c.gradient.project = nil
  • end
  • elseif k == 'lambda' then
  • self._c.gradient.lambda = v
  • elseif k == 'rho0' then
  • self._c.gradient.rho0 = v
  • elseif k == 'type' then
  • if type(v) == 'string' then
  • v = v:lower()
  • else
  • return error.raise{fname = 'GradientMedium', argname = 'type',
  • expected = 'a string', got = metatype.a(v)}
  • end
  • if v == 'linear' then
  • self._c.gradient.type = clib.PUMAS_MEDIUM_GRADIENT_LINEAR
  • elseif v == 'exponential' then
  • self._c.gradient.type = clib.PUMAS_MEDIUM_GRADIENT_EXPONENTIAL
  • else
  • return error.raise{fname = 'GradientMedium', argname = 'type',
  • description = "unknown type '"..v.."'"}
  • end
  • elseif k == 'z0' then
  • self._c.gradient.z0 = v
  • elseif k == 'magnet' then
  • local ok, description = coordinates.set_double3(self._c, 'magnet', v)
  • if not ok then
  • error.raise{fname = 'GradientMedium', argname = 'magnet',
  • description = description}
  • end
  • else
  • base.BaseMedium.__newindex(self, k, v, strtype)
  • end
  • end
  • -------------------------------------------------------------------------------
  • -- The gradient medium constructor
  • -------------------------------------------------------------------------------
  • do
  • local ctype = ffi.typeof('struct pumas_medium_gradient')
  • local ctype_ptr = ffi.typeof('struct pumas_medium_gradient *')
  • local raise_error = error.ErrorFunction{fname = 'GradientMedium'}
  • -- Protected conversion to a number
  • local function ptonumber (args, argname)
  • local v, ok = args[argname]
  • local tp = type(v)
  • if tp == 'ctype' then
  • ok, v = pcall(ffi.tonumber, v)
  • elseif tp == 'number' then
  • ok = true
  • end
  • if ok then
  • return v
  • else
  • raise_error{
  • argname = argname, expected = 'a number',
  • got = metatype.a(v)}
  • end
  • end
  • local function new (cls, material, args)
  • -- Check the number and type of arguments
  • if args == nil then
  • local nargs = (material ~= nil) and 1 or 0
  • raise_error{
  • argnum = 'bad', expected = 2, got = nargs}
  • end
  • if type(args) ~= 'table' then
  • raise_error{
  • argnum = 2, expected = 'table', got = metatype.a(args)}
  • end
  • -- Check the named arguments
  • local parameters = {'axis', 'lambda', 'rho0', 'type', 'z0', 'magnet'}
  • for k, _ in pairs(args) do
  • local ok = false
  • for _, parameter in ipairs(parameters) do
  • if k == parameter then
  • ok = true
  • break
  • end
  • end
  • if not ok then
  • raise_error{
  • argname = k, description = 'unknown'}
  • end
  • end
  • -- Parse the gradient type
  • local type_
  • if args.type then
  • if type(args.type) ~= 'string' then
  • raise_error{
  • argname = 'type', expected = 'a string',
  • got = metatype.a(args.type)}
  • end
  • local type_tag = args.type:lower()
  • if type_tag == 'linear' then
  • type_ = clib.PUMAS_MEDIUM_GRADIENT_LINEAR
  • elseif type_tag == 'exponential' then
  • type_ = clib.PUMAS_MEDIUM_GRADIENT_EXPONENTIAL
  • else
  • raise_error{
  • argname = 'type', expected = "'linear' or 'exponential'",
  • got = args.type}
  • end
  • else
  • type_ = clib.PUMAS_MEDIUM_GRADIENT_EXPONENTIAL
  • end
  • -- Parse the gradient axis
  • local axis
  • if args.axis == nil then
  • axis = 'vertical'
  • else
  • if type(args.axis) == 'string' then
  • axis = args.axis:lower()
  • if axis ~= 'vertical' then
  • raise_error{
  • argname = 'axis',
  • description = "bad value '"..args.axis.."'"
  • }
  • end
  • else
  • axis = args.axis
  • end
  • end
  • -- Parse the gradient reference density
  • local rho0
  • if args.rho0 == nil then
  • local m = material_.materials[material]
  • if m then
  • rho0 = m.density
  • else
  • raise_error{
  • argname = 'rho0', expected = 'a number', got = 'nil'}
  • end
  • else
  • rho0 = ptonumber(args, 'rho0')
  • end
  • -- Parse the gradient reference altitude
  • local z0
  • if args.z0 == nil then
  • z0 = 0
  • else
  • z0 = ptonumber(args, 'z0')
  • end
  • local lambda = ptonumber(args, 'lambda')
  • local magnet = args.magnet
  • if magnet then
  • local ok, tmp = coordinates.to_double3(magnet)
  • if ok then
  • magnet = tmp
  • else
  • raise_error{argname = 'magnet', description = tmp}
  • end
  • end
  • local self = base.BaseMedium.new(ctype, ctype_ptr, material)
  • clib.pumas_medium_gradient_initialise(self._c, -1, type_, lambda,
  • z0, rho0, magnet)
  • if axis == 'vertical' then
  • self._c.gradient.project =
  • clib.pumas_medium_gradient_project_altitude
  • else
  • local ok, description = coordinates.set_double3(
  • self._c.gradient, 'axis', axis)
  • if not ok then
  • raise_error{argname = 'axis', description = description}
  • end
  • end
  • return setmetatable(self, cls)
  • end
  • gradient.GradientMedium = setmetatable(GradientMedium, {__call = new})
  • end
  • -------------------------------------------------------------------------------
  • -- Return the package
  • -------------------------------------------------------------------------------
  • return gradient