def test(self): "Source_simple --> E_monitor" from mcstas2 import componentfactory ssimplefac = componentfactory( 'sources', 'Source_simple' ) ssimple = ssimplefac( 'ssimple', radius=0.1, dist=2, xw=0.1, yh=0.1, E0=55, dE=2) from mcstas2.wrappers import wrap wrap( 'E_monitor.comp', 'monitors' ) emonfac = componentfactory( 'monitors', 'E_monitor' ) emon = emonfac( 'emon', nchan=20, filename="e.dat", xmin=-0.2, xmax=0.2, ymin=-0.2, ymax=0.2, Emin=50, Emax=60) import mcni instrument = mcni.instrument( [ssimple, emon] ) geometer = mcni.geometer() geometer.register( ssimple, (0,0,0), (0,0,0) ) geometer.register( emon, (0,0,1), (0,0,0) ) neutrons = mcni.neutron_buffer( 100 ) mcni.simulate( instrument, geometer, neutrons ) return
def test(self): "wrap Fermi_chop2" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory('component', len=0.10, w=0.06, nu=600, delta=0.0, tc=0.0, ymin=-0.03, ymax=0.03, nchan=32, bw=0.00035, blader=0.5801) import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test(self): "wrap SNS_source_r1" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory( "component", S_filename="source_sct521_bu_17_1.dat", width=0.1, height=0.12, dist=2.5, xw=0.1, yh=0.12, Emin=50, Emax=70, ) import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test(self): "wrap Channeled_guide" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory('component', w1=-0.1, h1=0.12, w2=0.02, h2=0.02, l=2.0, R0=0.99, Qcx=0.021, Qcy=0.021, alphax=6.07, alphay=6.07, W=0.003, k=1, d=0.0005, mx=1, my=1) import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test(self): "wrap TOF_monitor2" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory( 'component', nchan=100, xmin=-0.1, xmax=0.1, ymin=-0.1, ymax=0.1, tmin=0.0, tmax=0.00005, filename="tof.dat", ) import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test(self): "wrap Single_crystal" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory('component', xwidth=0.01, yheight=0.01, zthick=0.01, delta_d_d=1e-4, mosaic=5, ax=3.8186, ay=0, az=0, bx=0, by=3.8843, bz=0, cx=0, cy=0, cz=11.6777, reflections="YBaCuO.lau") import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test(self): "wrap SNS_source_r1" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory( 'component', S_filename="source_sct521_bu_17_1.dat", width=0.1, height=0.12, dist=2.5, xw=0.1, yh=0.12, Emin=50, Emax=70, ) import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test(self): "wrap IQE_monitor" from mcstas2 import componentfactory category = 'monitors' componentname = 'IQE_monitor' factory = componentfactory(category, componentname) Qmin = 0 Qmax = 13. nQ = 130 Emin = -50 Emax = 50. nE = 100 component = factory( 'component', Ei=Ei, Qmin=Qmin, Qmax=Qmax, nQ=nQ, Emin=Emin, Emax=Emax, nE=nE, max_angle_out_of_plane=30, min_angle_out_of_plane=-30, max_angle_in_plane=120, min_angle_in_plane=-30, ) scatterer = makeScatterer() import mcni N = 10000 neutrons = mcni.neutron_buffer(N) for i in range(N): neutron = mcni.neutron(r=(0, 0, 0), v=(0, 0, vi), time=0, prob=1) scatterer.scatter(neutron) neutrons[i] = neutron #print neutrons[i] continue component.process(neutrons) hist = get_histogram(component) import os f = os.path.basename(__file__) filename = 'IQE-%s.h5' % f if os.path.exists(filename): os.remove(filename) import histogram.hdf as hh hh.dump(hist, filename, '/', 'c') if self.interactive: from histogram.plotter import defaultPlotter defaultPlotter.plot(hist) return
def test(self): "Source_simple --> E_monitor" from mcstas2 import componentfactory ssimplefac = componentfactory('sources', 'Source_simple') ssimple = ssimplefac('ssimple', radius=0.1, dist=2, xw=0.1, yh=0.1, E0=55, dE=2) from mcstas2.wrappers import wrap wrap('E_monitor.comp', 'monitors') emonfac = componentfactory('monitors', 'E_monitor') emon = emonfac('emon', nchan=20, filename="e.dat", xmin=-0.2, xmax=0.2, ymin=-0.2, ymax=0.2, Emin=50, Emax=60) import mcni instrument = mcni.instrument([ssimple, emon]) geometer = mcni.geometer() geometer.register(ssimple, (0, 0, 0), (0, 0, 0)) geometer.register(emon, (0, 0, 1), (0, 0, 0)) neutrons = mcni.neutron_buffer(100) mcni.simulate(instrument, geometer, neutrons) return
def test2(self): "SNS_source_r1: angling" from mcstas2 import componentfactory factory = componentfactory(category, componentname) Emin = 59.99 Emax = 60.01 component = factory( "component", S_filename="source_sct521_bu_17_1.dat", width=0.001, height=0.001, dist=2.5, xw=0.001, yh=0.001, Emin=Emin, Emax=Emax, angling=30, ) import mcni N = 100 neutrons = mcni.neutron_buffer(N) for i in range(N): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) from mcni.utils import conversion as Conv expected_vlen = Conv.e2v((Emin + Emax) / 2) import numpy as np for i in range(N): neutron = neutrons[i] state = neutron.state r = state.position assert abs(r[0]) < 0.001 assert abs(r[1]) < 0.001 assert abs(r[2]) < 0.001 v = np.array(state.velocity) vlen = np.linalg.norm(v) # print v, expected_vlen, vlen assert abs(vlen - expected_vlen) / expected_vlen < 0.01 assert abs(-v[0] - expected_vlen / 2.0) / expected_vlen < 0.01 assert abs(v[2] - expected_vlen * np.sqrt(3) / 2.0) / expected_vlen < 0.01 return
def test(self): "wrap IQE_monitor" from mcstas2 import componentfactory factory = componentfactory(category, componentname) Qmin = 0 Qmax = 13. nQ = 130 Emin = -50 Emax = 50. nE = 100 component = factory( 'component', Ei=Ei, Qmin=Qmin, Qmax=Qmax, nQ=nQ, Emin=Emin, Emax=Emax, nE=nE, max_angle_out_of_plane=30, min_angle_out_of_plane=-30, max_angle_in_plane=120, min_angle_in_plane=-30, ) kernel = makeKernel() import mcni N = 10000 neutrons = mcni.neutron_buffer(N) for i in range(N): neutron = mcni.neutron(r=(0, 0, 0), v=(0, 0, vi), time=0, prob=1) kernel.scatter(neutron) neutrons[i] = neutron #print neutrons[i] continue component.process(neutrons) from mcstas2.pyre_support._component_interfaces.monitors.IQE_monitor import get_histogram hist = get_histogram(component) if interactive: from histogram.plotter import defaultPlotter defaultPlotter.plot(hist) return
def test(self): "wrap V_sample" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', xwidth=0.05, yheight=0.10, zthick=0.002, ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap IQE_monitor" from mcstas2 import componentfactory category = 'monitors' componentname = 'IQE_monitor' factory = componentfactory( category, componentname ) Qmin=0; Qmax=13.; nQ=130 Emin=-50; Emax=50.; nE=100 component = factory( 'component', Ei=Ei, Qmin=Qmin, Qmax=Qmax, nQ=nQ, Emin=Emin, Emax=Emax, nE=nE, max_angle_out_of_plane=30, min_angle_out_of_plane=-30, max_angle_in_plane=120, min_angle_in_plane=-30, ) scatterer = makeScatterer() import mcni N = 10000 neutrons = mcni.neutron_buffer( N ) for i in range(N): neutron = mcni.neutron(r=(0,0,0), v=(0,0,vi), time=0, prob=1) scatterer.scatter(neutron) neutrons[i] = neutron #print neutrons[i] continue component.process( neutrons ) hist = get_histogram(component) import os f = os.path.basename(__file__) filename = 'IQE-%s.h5' % f if os.path.exists(filename): os.remove(filename) import histogram.hdf as hh hh.dump(hist, filename, '/', 'c') if self.interactive: from histogram.plotter import defaultPlotter defaultPlotter.plot(hist) return
def test(self): "wrap Arm" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap Guide" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', w1=0.1, h1=0.1, w2=0.1, h2=0.1, l=2.0, R0=0.99, Qc=0.021, alpha=6.07, m=2, W=0.003, ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap Moderator" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', radius = 0.0707, dist = 9.035, xw = 0.021, yh = 0.021, E0 = 10, E1 = 15, Ec = 9.0, t0 = 37.15, gam = 39.1, ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap IQE_monitor" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) Ei = 70 Qmin=0; Qmax=13.; nQ=130 Emin=-50; Emax=50.; nE=100 component = factory( 'component', Ei=Ei, Qmin=Qmin, Qmax=Qmax, nQ=nQ, Emin=Emin, Emax=Emax, nE=nE, max_angle_out_of_plane=30, min_angle_out_of_plane=-30, max_angle_in_plane=120, min_angle_in_plane=-30, ) import mcni from mcni.utils import conversion as C neutrons = mcni.neutron_buffer( nQ*nE ) import numpy as N count = 0 for Q in N.arange(Qmin, Qmax, (Qmax-Qmin)/nQ): for E in N.arange(Emin,Emax,(Emax-Emin)/nE): Ef = Ei-E cosphi = (Ei+Ef-C.k2e(Q))/(2*N.sqrt(Ei)*N.sqrt(Ef)) vf = C.e2v(Ef) vfz = vf*cosphi sinphi = N.sqrt(1-cosphi*cosphi) vfx = vf*sinphi neutrons[count] = mcni.neutron(r=(0,0,0), v=(vfx,0,vfz), time = 0, prob = 1) count += 1 continue component.process( neutrons ) from mcstas2.pyre_support._component_interfaces.monitors.IQE_monitor import get_histogram hist = get_histogram(component) if interactive: from histogram.plotter import defaultPlotter defaultPlotter.plot(hist) return
def test2(self): "wrap Slit: ctor using radius and cut" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', radius=0.01, cut=1e-10) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap Arm" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory('component', ) import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test(self): "wrap Fermi_chop2" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', len=0.10, w=0.06, nu=600, delta=0.0, tc=0.0, ymin=-0.03, ymax=0.03, nchan=32, bw=0.00035, blader=0.5801 ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "fccNi kernel constructed by hand" from mcstas2 import componentfactory category = 'monitors' componentname = 'IQE_monitor' factory = componentfactory( category, componentname ) Qmin=0; Qmax=13.; nQ=130 Emin=-50; Emax=50.; nE=100 component = factory( 'component', Ei=Ei, Qmin=Qmin, Qmax=Qmax, nQ=nQ, Emin=Emin, Emax=Emax, nE=nE, max_angle_out_of_plane=30, min_angle_out_of_plane=-30, max_angle_in_plane=120, min_angle_in_plane=-30, ) kernel = makeKernel() import mcni # N = 500000 # needs about 20 minutes on a Intel Core2 Duo 2.53GHz virtual machine, ubuntu 10.04LTS on a 2.53GHz Intel Core 2 Duo Macbook N = 10000 neutrons = mcni.neutron_buffer( N ) for i in range(N): neutron = mcni.neutron(r=(0,0,0), v=(0,0,vi), time=0, prob=1) kernel.scatter(neutron) neutrons[i] = neutron #print neutrons[i] continue component.process( neutrons ) hist = get_histogram(component) if self.interactive: from histogram.plotter import defaultPlotter defaultPlotter.plot(hist) return
def test(self): "wrap Channeled_guide" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', w1=-0.1, h1=0.12, w2=0.02, h2=0.02, l=2.0, R0=0.99, Qcx=0.021, Qcy=0.021, alphax=6.07, alphay=6.07, W=0.003, k=1, d=0.0005, mx=1, my=1 ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap Single_crystal" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', xwidth=0.01, yheight=0.01, zthick=0.01, delta_d_d=1e-4, mosaic = 5, ax = 3.8186, ay = 0, az = 0, bx = 0, by = 3.8843, bz = 0, cx = 0, cy = 0, cz = 11.6777, reflections="YBaCuO.lau" ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap IQE_monitor" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) Qmin=0; Qmax=13.; nQ=130 Emin=-50; Emax=50.; nE=100 component = factory( 'component', Ei=Ei, Qmin=Qmin, Qmax=Qmax, nQ=nQ, Emin=Emin, Emax=Emax, nE=nE, max_angle_out_of_plane=30, min_angle_out_of_plane=-30, max_angle_in_plane=120, min_angle_in_plane=-30, ) kernel = makeKernel() import mcni N = 10000 neutrons = mcni.neutron_buffer( N ) for i in range(N): neutron = mcni.neutron(r=(0,0,0), v=(0,0,vi), time=0, prob=1) kernel.scatter(neutron) neutrons[i] = neutron #print neutrons[i] continue component.process( neutrons ) from mcstas2.pyre_support._component_interfaces.monitors.IQE_monitor import get_histogram hist = get_histogram(component) if interactive: from histogram.plotter import defaultPlotter defaultPlotter.plot(hist) return
def test(self): "wrap TOF_monitor2" from mcstas2 import componentfactory factory = componentfactory( category, componentname ) component = factory( 'component', nchan=100, xmin=-0.1, xmax=0.1, ymin=-0.1, ymax=0.1, tmin=0.0, tmax=0.00005, filename="tof.dat", ) import mcni neutrons = mcni.neutron_buffer( 5 ) for i in range(5): neutrons[i] = mcni.neutron(r=(0,0,-1), v=(0,0,3000), time = 0, prob = 1) continue component.process( neutrons ) print neutrons return
def test(self): "wrap V_sample" from mcstas2 import componentfactory factory = componentfactory(category, componentname) component = factory( 'component', xwidth=0.05, yheight=0.10, zthick=0.002, ) import mcni neutrons = mcni.neutron_buffer(5) for i in range(5): neutrons[i] = mcni.neutron(r=(0, 0, -1), v=(0, 0, 3000), time=0, prob=1) continue component.process(neutrons) print neutrons return
def test1(): from mcstas2 import componentfactory cf = componentfactory(type='IQE_monitor', category='monitors') monitor = cf('monitor') getNormalization(monitor, N=1000000) return
def getNormalization(monitor, N=None, epsilon=1e-7): # randomly shoot neutrons to monitor in 4pi solid angle print "* start computing normalizer..." core = monitor.core() if N is None: N = core.nQ * core.nE * 10000 import mcni, random, mcni.utils.conversion as conversion, math, os import numpy as np # incident velocity vi = conversion.e2v(core.Ei) # 1. create neutrons def make_neutrons(N): neutrons = mcni.neutron_buffer(N) # # randomly select E, the energy transfer E = core.Emin + np.random.random(N) * (core.Emax - core.Emin) # the final energy Ef = core.Ei - E # the final velocity vf = conversion.e2v(Ef) # choose cos(theta) between -1 and 1 cos_t = np.random.random(N) * 2 - 1 # theta theta = np.arccos(cos_t) # sin(theta) sin_t = np.sin(theta) # phi: 0 - 2pi phi = np.random.random(N) * 2 * np.pi # compute final velocity vector vx, vy, vz = vf * sin_t * np.cos(phi), vf * sin_t * np.sin( phi), vf * cos_t # neutron position, spin, tof are set to zero x = y = z = sx = sy = t = np.zeros(N, dtype="float64") # probability prob = np.ones(N, dtype="float64") * (vf / vi) # XXX: this assumes a specific data layout of neutron struct n_arr = np.array([x, y, z, vx, vy, vz, sx, sy, t, prob]).T.copy() neutrons.from_npyarr(n_arr) return neutrons # 2. create a copy of the original monitor from mcstas2 import componentfactory cf = componentfactory(type='IQE_monitor', category='monitors') props = [ 'Ei', 'Emin', 'Emax', 'nE', 'Qmin', 'Qmax', 'nQ', 'max_angle_out_of_plane', 'min_angle_out_of_plane', 'max_angle_in_plane', 'min_angle_in_plane', ] kwds = {} for p in props: kwds[p] = getattr(core, p) import tempfile tmpdir = tempfile.mkdtemp() outfilename = os.path.join(tmpdir, 'mon.dat') kwds['filename'] = outfilename monitorcopy = cf('monitor', **kwds) # 3. send neutrons to monitor copy N1 = 0 dN = int(1e6) print " - total neutrons needed :", N while N1 < N: n = min(N - N1, dN) neutrons = make_neutrons(n) monitorcopy.process(neutrons) N1 += n print " - processed %s" % N1 continue h = get_histogram(monitorcopy) # for debug # import histogram.hdf as hh # hh.dump(h, 'tmp.h5', '/', 'c') h.I[h.I < epsilon] = 1 # print " - done computing normalizer" return h
def getNormalization(monitor, N=None, epsilon=1e-7): # randomly shoot neutrons to monitor in 4pi solid angle print "* start computing normalizer..." core = monitor.core() if N is None: N = core.nQ * core.nE * 10000 import mcni, random, mcni.utils.conversion as conversion, math, os import numpy as np # incident velocity vi = conversion.e2v(core.Ei) # 1. create neutrons def make_neutrons(N): neutrons = mcni.neutron_buffer(N) # # randomly select E, the energy transfer E = core.Emin + np.random.random(N) * (core.Emax-core.Emin) # the final energy Ef = core.Ei - E # the final velocity vf = conversion.e2v(Ef) # choose cos(theta) between -1 and 1 cos_t = np.random.random(N) * 2 - 1 # theta theta = np.arccos(cos_t) # sin(theta) sin_t = np.sin(theta) # phi: 0 - 2pi phi = np.random.random(N) * 2 * np.pi # compute final velocity vector vx,vy,vz = vf*sin_t*np.cos(phi), vf*sin_t*np.sin(phi), vf*cos_t # neutron position, spin, tof are set to zero x = y = z = sx = sy = t = np.zeros(N, dtype="float64") # probability prob = np.ones(N, dtype="float64") * (vf/vi) # XXX: this assumes a specific data layout of neutron struct n_arr = np.array([x,y,z,vx,vy,vz, sx,sy, t, prob]).T.copy() neutrons.from_npyarr(n_arr) return neutrons # 2. create a copy of the original monitor from mcstas2 import componentfactory cf = componentfactory(type='IQE_monitor', category='monitors') props = [ 'Ei', 'Emin', 'Emax', 'nE', 'Qmin', 'Qmax', 'nQ', 'max_angle_out_of_plane', 'min_angle_out_of_plane', 'max_angle_in_plane', 'min_angle_in_plane', ] kwds = {} for p in props: kwds[p] = getattr(core, p) import tempfile tmpdir = tempfile.mkdtemp() outfilename = os.path.join(tmpdir, 'mon.dat') kwds['filename'] = outfilename monitorcopy = cf('monitor', **kwds) # 3. send neutrons to monitor copy N1 = 0; dN = int(1e6) print " - total neutrons needed :", N while N1 < N: n = min(N-N1, dN) neutrons = make_neutrons(n) monitorcopy.process(neutrons) N1 += n print " - processed %s" % N1 continue h = get_histogram(monitorcopy) # for debug # import histogram.hdf as hh # hh.dump(h, 'tmp.h5', '/', 'c') h.I[h.I<epsilon] = 1 # print " - done computing normalizer" return h