def test1(self): 'mccomponents.sample.samplecomponent' import mcni neutron = mcni.neutron( r = (0,0,0), v = (0,0,3000), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Ni', 'sampleassemblies/Ni/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) neutrons = mcni.neutron_buffer( 1 ) from mcni.pyre_support.ConsoleNeutronTracer import ConsoleNeutronTracer tracer = ConsoleNeutronTracer() mcni.simulate( instrument, geometer, neutrons, multiple_scattering=True, tracer = tracer ) return
def test1(self): 'mccomponents.sample.samplecomponent' import mcni neutron = mcni.neutron( r = (0,0,0), v = (0,0,4149.48), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'V-constantE', 'sampleassemblies/V-constantE/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) neutrons = mcni.neutron_buffer( 1 ) from mcni.pyre_support.ConsoleNeutronTracer import ConsoleNeutronTracer tracer = ConsoleNeutronTracer() mcni.simulate( instrument, geometer, neutrons, multiple_scattering=True, tracer = tracer ) return
def test2(self): 'shape positioning: rotated sphere' # source from mcni.components.MonochromaticSource import MonochromaticSource import mcni neutron = mcni.neutron(r=(0,0,-1), v=(0,0,1000), prob=1) source = MonochromaticSource('s', neutron, dx=0.10, dy=0.09, dE=0) # sample from mccomponents.sample import samplecomponent scatterer = samplecomponent('sa', 'sphere-rotated-arbitrarily/sampleassembly.xml' ) # neutrons N = 1000 neutrons = mcni.neutron_buffer(N) neutrons = source.process(neutrons) # find neutrons out of target arr = neutrons.to_npyarr() x,y,z = arr[:, :3].T missing = x*x+y*y>0.05*0.05 # print "missed:", missing.sum() # print neutrons scatterer.process(neutrons) # print neutrons arr = neutrons.to_npyarr() x,y,z = arr[:, :3].T assert (z[missing] < -.9).all() hit = arr[np.logical_not(missing), :3] x,y,z = hit.T assert (z > -.1).all() assert np.isclose(np.abs(x*x+y*y+z*z), 0.05*0.05).all() return
def test1(self): 'mccomponents.sample.samplecomponent isotropic kernel, multiple-scattering' import mcni neutron = mcni.neutron(r=(0, 0, 0), v=(0, 0, 3000), time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-isotropickernel/sampleassembly.xml') instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 1), (0, 0, 0)) N0 = 1 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons, multiple_scattering=True) N = len(neutrons) for i in range(N): neutron = neutrons[i] print neutron continue return
def test1(self): 'mccomponents.sample.samplecomponent: IsotropicKernel' import mcni neutron = mcni.neutron( r = (0,0,0), v = (0,0,3000), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-isotropickernel/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons ) N = len(neutrons) for i in range(10): neutron = neutrons[i] print neutron continue # N should be about 2/3 of N0. this is determined by # the mc weights in Al-isotropic-kernel-plate-scatterer.xml self.assert_( N < 0.72*N0 and N > 0.6*N0) return
def test1(self): 'shape positioning: plate perp to beam' # source from mcni.components.MonochromaticSource import MonochromaticSource import mcni neutron = mcni.neutron(r=(0,0,-1), v=(0,0,1000), prob=1) source = MonochromaticSource('s', neutron, dx=0.07, dy=0.09, dE=0) # sample from mccomponents.sample import samplecomponent scatterer = samplecomponent('sa', 'plate/sampleassembly.xml' ) # neutrons N = 1000 neutrons = mcni.neutron_buffer(N) neutrons = source.process(neutrons) # find neutrons out of target arr = neutrons.to_npyarr() x,y,z = arr[:, :3].T missing = (x>0.03) | (x<-0.03) | (y>0.04) | (y<-0.04) # print neutrons scatterer.process(neutrons) # print neutrons arr = neutrons.to_npyarr() x,y,z = arr[:, :3].T assert (z[missing] < -.9).all() hit = arr[np.logical_not(missing), :3] x,y,z = hit.T assert (z > -.1).all() assert (np.isclose(np.abs(x), 0.03) | np.isclose(np.abs(y), 0.04) | np.isclose(np.abs(z), 0.005)).all() return
def test1(self): 'mccomponents.sample.samplecomponent isotropic kernel, multiple-scattering' import mcni neutron = mcni.neutron( r = (0,0,0), v = (0,0,3000), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-isotropickernel/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) N0 = 1 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons, multiple_scattering=True) N = len(neutrons) for i in range(N): neutron = neutrons[i] print neutron continue return
def test2(self): 'shape positioning: cylinder with axis along beam' # source from mcni.components.MonochromaticSource import MonochromaticSource import mcni neutron = mcni.neutron(r=(0,0,-1), v=(0,0,1000), prob=1) source = MonochromaticSource('s', neutron, dx=0.09, dy=0.09, dE=0) # sample from mccomponents.sample import samplecomponent scatterer = samplecomponent('sa', 'cyl-along-beam/sampleassembly.xml' ) # neutrons N = 1000 neutrons = mcni.neutron_buffer(N) neutrons = source.process(neutrons) # find neutrons out of target arr = neutrons.to_npyarr() x,y,z = arr[:, :3].T missing = x*x+y*y > 0.04**2 # print neutrons scatterer.process(neutrons) # print neutrons arr = neutrons.to_npyarr() x,y,z = arr[:, :3].T assert (z[missing] < -.9).all() hit = arr[np.logical_not(missing), :3] x,y,z = hit.T assert (z > -.1).all() assert (np.isclose((x*x + y*y)**.5, 0.04) | np.isclose(np.abs(z), 0.005)).all() return
def test1(self): 'mccomponents.sample.samplecomponent: DGSSXResKernel' import mcni neutron = mcni.neutron( r = (0,0,0), v = (0,0,3000), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-DGSSXResKernel/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,6), (0,0,0) ) N0 = 10 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons ) N = len(neutrons) for i in range(10): neutron = neutrons[i] # print neutron self.assert_(np.allclose(neutron.state.velocity, [3000, 0, 0], atol=20)) continue return
def test2(self): "mccomponents.sample.samplecomponent: E_vQ_Kernel - discontinued dispersion surface" import mcni from mcni.utils import conversion ei = 60 vil = conversion.e2v(ei) vi = (0, 0, vil) neutron = mcni.neutron(r=(0, 0, 0), v=vi, time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource("source", neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent("Al", "sampleassemblies/Al-E_vQ_withdiscontinuity-kernel/sampleassembly.xml") E_Q = "np.where(Qx*Qx+Qy*Qy+Qz*Qz>30, 25, 15)" # in sampleassemblies/Al-E_vQ-kernel/Al-scatterer.xml instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 1), (0, 0, 0)) N0 = 10000 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons) N = len(neutrons) import numpy.linalg as nl import numpy as np from math import sin N_noscatt = 0 for i in range(N): neutron = neutrons[i] vf = np.array(neutron.state.velocity) diffv = vi - vf # no scattering is fine if np.linalg.norm(diffv) < 1e-12: N_noscatt += 1 continue Q = conversion.V2K * diffv Qx, Qy, Qz = Q ef = conversion.v2e(nl.norm(vf)) E = ei - ef # print E, Q, neutron E1 = eval(E_Q) self.assertAlmostEqual(E, E1) continue print "\n* percentage of no scattering (Q happen to be at a singular point):", N_noscatt * 100.0 / N, "%" return
def test1(self): 'mccomponents.sample: ConstantQEKernel' # momentum and energy transfer. defined in the scatterer xml file Q0 = 3 E0 = 30 import mcni from mcni.utils import conversion ei = 60 vi = conversion.e2v(ei) Vi = (0, 0, vi) neutron = mcni.neutron(r=(0, 0, 0), v=Vi, time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-constantqekernel/sampleassembly.xml') instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 1), (0, 0, 0)) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons) N = len(neutrons) import numpy.linalg as nl, numpy as np for i in range(10): neutron = neutrons[i] Vf = np.array(neutron.state.velocity) print Vf ef = conversion.v2e(nl.norm(Vf)) E = ei - ef dV = np.array(Vf) - np.array(Vi) qasv = nl.norm(dV) Q = conversion.v2k(qasv) self.assertAlmostEqual(E, E0, 7) self.assertAlmostEqual(Q, Q0, 7) continue return
def test1(self): 'mccomponents.sample: ConstantvQEKernel' # momentum and energy transfer. defined in the scatterer xml file Q0 = 2,0,2 E0 = 28 import mcni from mcni.utils import conversion ei = 60 vi = conversion.e2v(ei) Vi = (0,0,vi) neutron = mcni.neutron( r = (0,0,0), v = Vi, time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-constantvqekernel/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons ) N = len(neutrons) import numpy.linalg as nl, numpy as np for i in range(10): neutron = neutrons[i] Vf = np.array(neutron.state.velocity) # print Vf ef = conversion.v2e(nl.norm(Vf)) E = ei-ef dV = np.array(Vi) - np.array(Vf) Q = dV * conversion.V2K print E, Q, neutron.probability self.assertAlmostEqual(E, E0, 1) for i in range(3): self.assertAlmostEqual(Q[i], Q0[i], 7) continue return
def test1(self): "mccomponents.sample.samplecomponent: E_vQ_Kernel" import mcni from mcni.utils import conversion ei = 60 vil = conversion.e2v(ei) vi = (0, 0, vil) neutron = mcni.neutron(r=(0, 0, 0), v=vi, time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource("source", neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent("Al", "sampleassemblies/Al-E_vQ-kernel/sampleassembly.xml") E_Q = "20 + 5 * sin(Qx+Qy+Qz)" # in sampleassemblies/Al-E_vQ-kernel/Al-scatterer.xml instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 1), (0, 0, 0)) N0 = 10000 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons) N = len(neutrons) import numpy.linalg as nl import numpy as np from math import sin for i in range(N): neutron = neutrons[i] vf = np.array(neutron.state.velocity) diffv = vi - vf Q = conversion.V2K * diffv Qx, Qy, Qz = Q ef = conversion.v2e(nl.norm(vf)) E = ei - ef # print E, Q, neutron E1 = eval(E_Q) self.assertAlmostEqual(E, E1) continue return
def test1(self): 'mccomponents.sample.samplecomponent: E_vQ_Kernel' import mcni from mcni.utils import conversion ei = 60 vil = conversion.e2v(ei) vi = (0, 0, vil) neutron = mcni.neutron(r=(0, 0, 0), v=vi, time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-E_vQ-kernel/sampleassembly.xml') E_Q = '20 + 5 * sin(Qx+Qy+Qz)' # in sampleassemblies/Al-E_vQ-kernel/Al-scatterer.xml instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 1), (0, 0, 0)) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons) N = len(neutrons) import numpy.linalg as nl import numpy as np from math import sin for i in range(N): neutron = neutrons[i] vf = np.array(neutron.state.velocity) diffv = vi - vf Q = conversion.V2K * diffv Qx, Qy, Qz = Q ef = conversion.v2e(nl.norm(vf)) E = ei - ef # print E, Q, neutron E1 = eval(E_Q) self.assertAlmostEqual(E, E1) continue return
def test1(self): 'mccomponents.sample.samplecomponent: Broadened_E_Q_Kernel' import mcni from mcni.utils import conversion ei = 600 vil = conversion.e2v(ei) vi = (0, 0, vil) neutron = mcni.neutron(r=(0, 0, 0), v=vi, time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-broadened-E_Q-kernel/sampleassembly.xml') E_Q = 'Q*Q/3.5' # in sampleassemblies/Al-broadened-E_Q-kernel/Al-scatterer.xml instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 1), (0, 0, 0)) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons) N = len(neutrons) import numpy.linalg as nl import numpy as np for i in range(10): neutron = neutrons[i] vf = np.array(neutron.state.velocity) diffv = vi - vf Q = conversion.v2k(nl.norm(diffv)) ef = conversion.v2e(nl.norm(vf)) E = ei - ef E1 = eval(E_Q) print E, Q, neutron, E - E1 continue return
def test1(self): 'mccomponents.sample.samplecomponent: Broadened_E_Q_Kernel' import mcni from mcni.utils import conversion ei = 600 vil = conversion.e2v(ei) vi = (0,0,vil) neutron = mcni.neutron( r = (0,0,0), v = vi, time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-broadened-E_Q-kernel/sampleassembly.xml' ) E_Q = 'Q*Q/3.5' # in sampleassemblies/Al-broadened-E_Q-kernel/Al-scatterer.xml instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons ) N = len(neutrons) import numpy.linalg as nl import numpy as np for i in range(10): neutron = neutrons[i] vf = np.array(neutron.state.velocity) diffv = vi - vf Q = conversion.v2k(nl.norm(diffv)) ef = conversion.v2e(nl.norm(vf)) E = ei - ef E1 = eval(E_Q) print E, Q, neutron, E-E1 continue return
def test1(self): 'mccomponents.sample: SimplePowderDiffractionKernel' import mcni from mcni.utils import conversion from mcstas2 import componentfactory as cf f = cf('sources', 'Source_simple') component1 = f( E0=60, dE=20, height=0.01, width=0.01, radius=0, dist=4, xw=0.1, yh=0.1, ) from mccomponents.sample import samplecomponent import mccomponents.sample.diffraction.xml component2 = samplecomponent( 'Al', 'sampleassemblies/Al-simplepowderdiffractionkernel/sampleassembly.xml' ) instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 5), (0, 0, 0)) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons) N = len(neutrons) print N import numpy.linalg as nl, numpy as np for i in range(10): neutron = neutrons[i] Vf = np.array(neutron.state.velocity) print Vf continue return
def test1(self): 'mccomponents.sample.samplecomponent' # energy transfer. defined in the scatterer xml file E0 = 10 import mcni from mcni.utils import conversion ei = 60 vi = conversion.e2v(ei) neutron = mcni.neutron(r=(0, 0, 0), v=(0, 0, vi), time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-constantenergytransfer/sampleassembly.xml') instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 1), (0, 0, 0)) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate(instrument, geometer, neutrons) N = len(neutrons) import numpy.linalg as nl for i in range(10): neutron = neutrons[i] vf = nl.norm(neutron.state.velocity) ef = conversion.v2e(vf) E = ei - ef self.assertAlmostEqual(E, E0, 7) continue return
def test1(self): 'shape positioning: hollow cylinder' # source from mcni.components.MonochromaticSource import MonochromaticSource import mcni neutron = mcni.neutron(r=(0,0,0), v=(0,0,1000), prob=1) source = MonochromaticSource('s', neutron, dx=0.01, dy=0.015, dE=0) # sample from mccomponents.sample import samplecomponent scatterer = samplecomponent('sa', 'sphere-shell/sampleassembly.xml' ) # neutrons N = 1000 neutrons = mcni.neutron_buffer(N) neutrons = source.process(neutrons) # print neutrons scatterer.process(neutrons) # print neutrons arr = neutrons.to_npyarr() x,y,z = arr[:, :3].T assert np.allclose((x*x + y*y + z*z)**.5, 0.10) return
def test1(self): 'mccomponents.sample.samplecomponent' # energy transfer. defined in the scatterer xml file E0 = 10 import mcni from mcni.utils import conversion ei = 60 vi = conversion.e2v(ei) neutron = mcni.neutron( r = (0,0,0), v = (0,0,vi), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Al', 'sampleassemblies/Al-constantenergytransfer/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons ) N = len(neutrons) import numpy.linalg as nl for i in range(10): neutron = neutrons[i] vf = nl.norm(neutron.state.velocity) ef = conversion.v2e(vf) E = ei-ef self.assertAlmostEqual(E, E0, 7) continue return
def test1(self): 'mccomponents.sample: SimplePowderDiffractionKernel' import mcni from mcni.utils import conversion from mcstas2 import componentfactory as cf f = cf('sources', 'Source_simple') component1 = f( E0=60, dE=20, height = 0.01, width = 0.01, radius=0, dist=4, xw = 0.1, yh = 0.1, ) from mccomponents.sample import samplecomponent import mccomponents.sample.diffraction.xml component2 = samplecomponent( 'Al', 'sampleassemblies/Al-simplepowderdiffractionkernel/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,5), (0,0,0) ) N0 = 1000 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons ) N = len(neutrons) print N import numpy.linalg as nl, numpy as np for i in range(10): neutron = neutrons[i] Vf = np.array(neutron.state.velocity) print Vf continue return
def test1(self): 'dgs sx res' import mcni neutron = mcni.neutron( r = (0,0,0), v = (0,0,3000), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent xmlpath = os.path.join( os.path.dirname(__file__), "..", 'sample', 'sampleassemblies/Al-DGSSXResKernel/sampleassembly.xml' ) component2 = samplecomponent( 'Al', xmlpath) from mccomponents.components.DGSSXResPixel import DGSSXResPixel component3 = DGSSXResPixel( "pixel", pressure=10*101325, tof=3e-3, radius=0.0254/2, height=1./128) instrument = mcni.instrument( [component1, component2, component3] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,6), (0,0,0) ) geometer.register( component3, (0,0,6), (0,0,0) ) N0 = 10 neutrons = mcni.neutron_buffer(N0) mcni.simulate( instrument, geometer, neutrons ) N = len(neutrons) for i in range(10): neutron = neutrons[i] print neutron self.assert_(np.allclose(neutron.state.velocity, [3000, 0, 0], atol=20)) continue return
def test1(self): 'kernel orientation' # source from mcni.components.MonochromaticSource import MonochromaticSource import mcni, numpy as np Ei = 100 from mcni.utils import conversion as Conv ki = Conv.e2k(Ei) vi = Conv.e2v(Ei) Qdir = np.array([np.sqrt(3)/2, 0, -1./2]) Q = Qdir * 2 kf = np.array([0,0,ki]) - Q Ef = Conv.k2e(np.linalg.norm(kf)) E = Ei-Ef dv = Qdir * Conv.k2v(Q) vf = np.array([0,0,vi]) - dv # print ki, Q, kf # print Ei, Ef, E neutron = mcni.neutron(r=(0,0,-1), v=(0,0,vi), prob=1) source = MonochromaticSource('s', neutron, dx=0.001, dy=0.001, dE=0) # sample from mccomponents.sample import samplecomponent scatterer = samplecomponent('sa', 'cyl/sampleassembly.xml' ) # incident N = 1000 neutrons = mcni.neutron_buffer(N) neutrons = source.process(neutrons) # print neutrons # scatter scatterer.process(neutrons) # print neutrons self.assertEqual(len(neutrons), N) for neutron in neutrons: np.allclose(neutron.state.velocity, vf) self.assert_(neutron.probability > 0) continue return
def test1(self): 'mccomponents.sample.samplecomponent' import mcni neutron = mcni.neutron( r = (0,0,0), v = (0,0,3000), time = 0, prob = 1 ) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.sample import samplecomponent component2 = samplecomponent( 'Ni', 'sampleassemblies/Ni/sampleassembly.xml' ) instrument = mcni.instrument( [component1, component2] ) geometer = mcni.geometer() geometer.register( component1, (0,0,0), (0,0,0) ) geometer.register( component2, (0,0,1), (0,0,0) ) neutrons = mcni.neutron_buffer( 1 ) mcni.simulate( instrument, geometer, neutrons ) for i in range(len(neutrons)): neutron = neutrons[i] print neutron continue return
def run(xml, **kwds): # import mccomponents.sample.phonon.xml from mccomponents.sample import samplecomponent samplecomponent("s", xml) return
def run(beam_neutrons_path, instrument, samplexmlpath, psi, hkl2Q, pixel, t_m2p, Q, E, hkl_projection, Nbuffer=100000, Nrounds_beam=1): """Run mcvine simulation with using the resolution sample and the resolution pixel, and save the results as numpy arrays - beam_neutrons_path: path to the "neutrons" file of a beam simulation - instrument: instrument object with geometry data such as L1 and L2 - samplexmlpath: path to the sampleassembly xml file - psi: sample rotation angle - hkl2Q: matrix to convert hkl to Q: Q = hkl dot hkl2Q - pixel: pixel object with pixe, height, pressure. and position - t_m2p: exepcted tof from moderator to pixel - Q: expected Q - E: expected E - hkl_projection: the simulated data is projected to this axis and E axis for easy inspection - Nbuffer: neutron buffer size - Nrounds_beam: number of rounds replaying neutrons in the beam """ from mcni.components.NeutronFromStorage import NeutronFromStorage source = NeutronFromStorage('source', path=beam_neutrons_path) from mccomponents.sample import samplecomponent sample = samplecomponent( 'sample', samplexmlpath) # dummy component to save the state of scattered neutrons from mcni.components.Dummy import Dummy sample_location = Dummy('sample_location') # det pixel from mccomponents.components.DGSSXResPixel import DGSSXResPixel pressure = mcvine.units.parse(pixel.pressure)/mcvine.units.parse("kg/m/s/s") r = mcvine.units.parse(pixel.radius)/mcvine.units.meter h = mcvine.units.parse(pixel.height)/mcvine.units.meter pixel_comp = DGSSXResPixel( "pixel", pressure=pressure, tof=t_m2p, radius=r, height=h) # build instrument simulation chain import mcni sim_chain = mcni.instrument( [source, sample, sample_location, pixel_comp] ) # put components into place geometer = mcni.geometer() z_beam = mcvine.units.parse(instrument.offset_sample2beam)/mcvine.units.meter # z along beam geometer.register( source, (0,0,z_beam), (0,0,0) ) geometer.register( sample, (0,0,0), (0,psi*180/np.pi,0) ) geometer.register( sample_location, (0,0,0), (0,0,0) ) geometer.register( pixel_comp, pixel.position, pixel.orientation ) # Q2hkl = np.linalg.inv(hkl2Q) # lengh of hkl_projection squared hkl_proj_len2 = np.dot(hkl_projection, hkl_projection) # neutron buffer from mcni.neutron_storage.idf_usenumpy import count N0 = count(beam_neutrons_path) * Nrounds_beam dxs_all = None; dEs_all = None; probs_all=None; dhkls_all=None start = 0 for i in range(int(np.ceil(N0/Nbuffer))+1): # for i in range(10): end = start + Nbuffer end = min(end, N0) if end<=start: continue sys.stdout.write("%s-%s: " % (start, end-1)); sys.stdout.flush() neutrons = mcni.neutron_buffer(end-start) # simulate tracer = NeutronTracer() mcni.simulate( sim_chain, geometer, neutrons, tracer=tracer) # before_dummy_start, after_dummy_start, \ before_incident, after_incident, \ before_scattered, after_scattered, \ at_sample_location, at_sample_location2, \ before_detected, after_detected, \ before_dummy_end, after_dummy_end = tracer._store incident = after_incident # has to be `at_sample_location` because both sample_location and # beam have no relative rotation. # should not used after_scattered. # it is in the sample's coordinate system, which is rotated by angle psi. # should not use before_detected. It could be rotated in spherical case scattered = at_sample_location detected = after_detected del (before_dummy_start, after_dummy_start, before_incident, after_incident, before_scattered, after_scattered, before_detected, after_detected, before_dummy_end, after_dummy_end) is_scattered = incident.v != scattered.v is_scattered = np.logical_or( is_scattered[:,0], np.logical_or(is_scattered[:,1], is_scattered[:,2]) ) good = np.logical_and(is_scattered, detected.p>np.finfo(float).eps) vi = incident.v[good] vf = scattered.v[good] probs = p = detected.p[good] from mcni.utils import conversion Ei = conversion.VS2E * (vi*vi).sum(axis=-1) Ef = conversion.VS2E * (vf*vf).sum(axis=-1) Es = Ei - Ef vQ = vi-vf Qs = vQ * conversion.V2K Qs = np.array([Qs[:, 2], Qs[:, 0], Qs[:, 1]]).T # print Qs # print Es dQs = Qs - Q dEs = Es - E dhkls = np.dot(dQs, Q2hkl) # print dhkls # print dEs # print p dxs = np.dot( dhkls, np.array(hkl_projection) )/hkl_proj_len2 if dxs_all is None: dxs_all = dxs dEs_all = dEs probs_all = probs dhkls_all = dhkls else: dxs_all = np.concatenate((dxs_all, dxs)) dEs_all = np.concatenate((dEs_all, dEs)) probs_all = np.concatenate((probs_all, probs)) dhkls_all = np.concatenate((dhkls_all, dhkls)) print() start = end continue # reverse x and E # the negative sign here makes the result the PSF dxs_all *= -1 dEs_all *= -1 dhkls_all *= -1 # save results np.save("dhkls.npy", dhkls_all) np.save("dxs.npy", dxs_all) np.save("dEs.npy", dEs_all) np.save("probs.npy", probs_all) h, xedges, yedges = np.histogram2d( dxs_all, dEs_all, bins=100, weights=probs_all) import histogram as H, histogram.hdf as hh xaxis = H.axis('x', boundaries=xedges) Eaxis = H.axis('E', boundaries=yedges) res = H.histogram('res', (xaxis, Eaxis), data=h) hh.dump(res, 'res.h5') sys.stdout.write("Done.\n"); sys.stdout.flush() return