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 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): '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 test3(self): 'MonochromaticSource - energy spread' # source component from mcni.components.MonochromaticSource import MonochromaticSource from mcni import neutron_buffer, neutron neutron0 = neutron(v=(0, 0, 3000), r=(0.3, 0.4, 1.5)) dx = 0.1 dy = 0.8 dE = 5 s = MonochromaticSource("name", neutron0, dx=0, dy=0, dE=dE) # neutron buffer N = 100000 b = neutron_buffer(N) # process s.process(b) # E0 = neutron0.energy() # n_in_onesigma = 0 n_in_twosigma = 1 for n in b: E = n.energy() # print n # print E, E0 # not always true but usually true self.assertNotEqual(E, E0) # if abs(E - E0) < dE: n_in_onesigma += 1 if abs(E - E0) < 2 * dE: n_in_twosigma += 1 self.assertEqual( tuple(n.state.position), tuple(neutron0.state.position), ) self.assertEqual( n.time, neutron0.time, ) self.assertEqual( n.probability, neutron0.probability, ) continue self.assert_(abs(n_in_onesigma * 1. / N - 0.68) < 0.01) self.assert_(abs(n_in_twosigma * 1. / N - 0.95) < 0.01) return
def test3(self): 'MonochromaticSource - energy spread' # source component from mcni.components.MonochromaticSource import MonochromaticSource from mcni import neutron_buffer, neutron neutron0 = neutron(v=(0,0,3000), r=(0.3, 0.4, 1.5)) dx=0.1; dy=0.8; dE = 5 s = MonochromaticSource("name", neutron0, dx=0, dy=0, dE=dE) # neutron buffer N = 100000 b = neutron_buffer(N) # process s.process(b) # E0 = neutron0.energy() # n_in_onesigma = 0 n_in_twosigma = 1 for n in b: E = n.energy() # print n # print E, E0 # not always true but usually true self.assertNotEqual(E, E0) # if abs(E-E0) < dE: n_in_onesigma += 1 if abs(E-E0) < 2*dE: n_in_twosigma += 1 self.assertEqual( tuple(n.state.position), tuple(neutron0.state.position), ) self.assertEqual( n.time, neutron0.time, ) self.assertEqual( n.probability, neutron0.probability, ) continue self.assert_( abs(n_in_onesigma*1./N-0.68) < 0.01) self.assert_( abs(n_in_twosigma*1./N-0.95) < 0.01) return
def test2(self): 'MonochromaticSource - x-y spread' # source component from mcni.components.MonochromaticSource import MonochromaticSource from mcni import neutron_buffer, neutron neutron0 = neutron(v=(0, 0, 1000), r=(0.3, 0.4, 1.5)) dx = 0.1 dy = 0.8 s = MonochromaticSource("name", neutron0, dx=dx, dy=dy) # neutron buffer N = 10 b = neutron_buffer(N) # process s.process(b) # for n in b: # print n r = n.state.position # not always true but usually true r0 = neutron0.state.position self.assert_(r[0] != r0[0]) self.assert_(r[1] != r0[1]) # always true self.assert_(abs(r[0] - r0[0]) <= dx / 2) self.assert_(abs(r[1] - r0[1]) <= dy / 2) self.assert_(abs(r[2]) == r0[2]) self.assertEqual( tuple(n.state.velocity), tuple(neutron0.state.velocity), ) self.assertEqual( tuple(n.state.velocity), tuple(neutron0.state.velocity), ) self.assertEqual( n.time, neutron0.time, ) self.assertEqual( n.probability, neutron0.probability, ) return
def test2(self): 'MonochromaticSource - x-y spread' # source component from mcni.components.MonochromaticSource import MonochromaticSource from mcni import neutron_buffer, neutron neutron0 = neutron(v=(0,0,1000), r=(0.3, 0.4, 1.5)) dx=0.1; dy=0.8 s = MonochromaticSource("name", neutron0, dx=dx, dy=dy) # neutron buffer N = 10 b = neutron_buffer(N) # process s.process(b) # for n in b: # print n r = n.state.position # not always true but usually true r0 = neutron0.state.position self.assert_(r[0]!=r0[0]) self.assert_(r[1]!=r0[1]) # always true self.assert_(abs(r[0]-r0[0])<=dx/2) self.assert_(abs(r[1]-r0[1])<=dy/2) self.assert_(abs(r[2])==r0[2]) self.assertEqual( tuple(n.state.velocity), tuple(neutron0.state.velocity), ) self.assertEqual( tuple(n.state.velocity), tuple(neutron0.state.velocity), ) self.assertEqual( n.time, neutron0.time, ) self.assertEqual( n.probability, neutron0.probability, ) 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): '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): '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: 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): '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): 'MonochromaticSource' # source component from mcni.components.MonochromaticSource import MonochromaticSource from mcni import neutron_buffer, neutron neutron0 = neutron(v=(0, 0, 1000)) s = MonochromaticSource("name", neutron0) # neutron buffer N = 100 b = neutron_buffer(N) # process s.process(b) # for n in b: self.assertEqual( tuple(n.state.position), tuple(neutron0.state.position), ) self.assertEqual( tuple(n.state.velocity), tuple(neutron0.state.velocity), ) self.assertEqual( n.time, neutron0.time, ) self.assertEqual( n.probability, neutron0.probability, ) return
def test1(self): 'MonochromaticSource' # source component from mcni.components.MonochromaticSource import MonochromaticSource from mcni import neutron_buffer, neutron neutron0 = neutron(v=(0,0,1000)) s = MonochromaticSource("name", neutron0) # neutron buffer N = 100 b = neutron_buffer(N) # process s.process(b) # for n in b: self.assertEqual( tuple(n.state.position), tuple(neutron0.state.position), ) self.assertEqual( tuple(n.state.velocity), tuple(neutron0.state.velocity), ) self.assertEqual( n.time, neutron0.time, ) self.assertEqual( n.probability, neutron0.probability, ) 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' # 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): 'detector component' import mcni neutron = mcni.neutron(r=(0, 0, 0), v=(1500, 0, 2000), time=0, prob=1) from mcni.components.MonochromaticSource import MonochromaticSource component1 = MonochromaticSource('source', neutron) from mccomponents.detector import detectorcomponent component2 = detectorcomponent('detectorsystem', instrumentxml, coordinate_system, tofparams, outfilename) instrument = mcni.instrument([component1, component2]) geometer = mcni.geometer() geometer.register(component1, (0, 0, 0), (0, 0, 0)) geometer.register(component2, (0, 0, 0), (0, 0, 0)) neutrons = mcni.neutron_buffer(nevents) mcni.simulate(instrument, geometer, neutrons) return