def test_variable_allocation(function_name): from gna.env import env ns = env.globalns(function_name) ndata = 10 allocator = R.arrayviewAllocatorSimple(context.current_precision())(ndata) with context.allocator(allocator): ns.defparameter('float1', central=1, fixed=True, label='Float variable 1') ns.defparameter('float2', central=2, fixed=True, label='Float variable 2') ns.defparameter('angle', central=3, label='Angle parameter', type='uniformangle') ns.defparameter('discrete', default='a', label='Discrete parameter', type='discrete', variants=OrderedDict([('a', 10.0), ('b', 20.0), ('c', 30.0)])) ns.printparameters(labels=True) print('Data (filled):', allocator.view()) print('Data (all):', allocator.viewall())
def test_vararray_preallocated_v01(function_name): ns = env.globalns(function_name) names = ['zero', 'one', 'two', 'three', 'four', 'five'] values = N.arange(len(names), dtype=context.current_precision_short()) variables = R.vector('variable<%s>' % context.current_precision())() with context.allocator(100) as allocator: for name, value in zip(names, values): par = ns.defparameter(name, central=value, relsigma=0.1) variables.push_back(par.getVariable()) with ns: vsum = C.VarSum(names, 'sum', ns=ns) vsum_var = ns['sum'].get() variables.push_back(vsum_var.getVariable()) vprod = C.VarProduct(names, 'product', ns=ns) vprod_var = ns['product'].get() variables.push_back(vprod_var.getVariable()) va = C.VarArrayPreallocated(variables) pool = allocator.view() res = va.vararray.points.data() values_all = N.zeros(shape=values.size + 2, dtype=values.dtype) values_all[:-2] = values values_all[-2] = values_all[:-2].sum() values_all[-1] = values_all[:-2].prod() print('Python array:', values_all) print('VarArray (preallocated):', res) print('Pool:', pool) assert (values_all == res).all() assert (values_all == pool).all() assert (res == pool).all() for i, (val, name) in enumerate(enumerate(names, 2)): ns[name].set(val) values_all[i] = val values_all[-2] = values_all[:-2].sum() values_all[-1] = values_all[:-2].prod() res = va.vararray.points.data() print('Iteration', i) print(' Python array:', values_all) print(' VarArray (preallocated):', res) assert (values_all == res).all() assert (values_all == pool).all() assert (res == pool).all()
def test_variable_allocation_complex(): from gna.env import env gns = env.globalns('test_variable_allocation_complex') ndata = 200 allocator = R.arrayviewAllocatorSimple('double')(ndata) with context.allocator(allocator): ns = gns('namespace1') ns.defparameter('float1', central=1, fixed=True, label='Float variable 1') ns.defparameter('float2', central=2, fixed=True, label='Float variable 2') ns = gns('namespace2') ns.defparameter('angle', central=3, label='Angle parameter', type='uniformangle') ns.defparameter('discrete', default='a', label='Discrete parameter', type='discrete', variants=OrderedDict([('a', 10.0), ('b', 20.0), ('c', 30.0)])) ns = gns('namespace3') from gna.parameters.oscillation import reqparameters reqparameters(ns) with ns: ns.materializeexpressions() ns['Delta'].set(N.pi * 1.5) gns.printparameters(labels=True) allocdata = allocator.view() print('Data (filled):', allocdata) print('Data (all):', allocator.viewall()) for name, par in gns.walknames(): av = par.getVariable().values() offset = av.offset() size = av.size() avview = av.view() assert (allocdata[offset:offset + size] == avview).all()
def test_variable_allocation2(function_name): from gna.env import env gns = env.globalns(function_name) ndata = 10 with context.allocator(ndata) as allocator: ns = gns('namespace1') ns.defparameter('float1', central=1, fixed=True, label='Float variable 1') ns.defparameter('float2', central=2, fixed=True, label='Float variable 2') ns = gns('namespace2') ns.defparameter('angle', central=3, label='Angle parameter', type='uniformangle') ns.defparameter('discrete', default='a', label='Discrete parameter', type='discrete', variants=OrderedDict([('a', 10.0), ('b', 20.0), ('c', 30.0)])) ns = gns('namespace3') ns.defparameter('float3', central=100, fixed=True, label='Float variable 3 (independent)') ns.defparameter('float4', central=200, fixed=True, label='Float variable 4 (independent)') gns.printparameters(labels=True) print('Data (filled):', allocator.view()) print('Data (all):', allocator.viewall())