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 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):
        '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 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 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
示例#8
0
    def main(self, *args, **kwds):
        if self.inventory.dumpregistry:
            self._dumpRegsitry()
            return
        
        import mcni
        instrument = self._createInstrument()
        
        geometer = self.geometer

        context = self._makeSimContext()
        
        if self.ncount < self.buffer_size:
            self.buffer_size = int(self.ncount)
        n = int(self.ncount / self.buffer_size)
        
        from mcni import journal
        logger = journal.logger(
            'info', 'instrument', header='', footer='', format='-> %s')
        for i in range(n):
            logger("mpi node %s at loop %s" % (self.mpi.rank, i))
            neutrons = mcni.neutron_buffer( self.buffer_size )
            context.iteration_no = i
            mcni.simulate( instrument, geometer, neutrons, context=context)
            continue
        
        remain = int(self.ncount % self.buffer_size)
        if remain:
            logger("mpi node %s at last loop" % (self.mpi.rank,))
            neutrons = mcni.neutron_buffer(remain)
            context.iteration_no = n
            mcni.simulate( instrument, geometer, neutrons, context=context)
            
        print os.times()
        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):
        "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 test(self):
        component1 = Component('comp')
        instrument = mcni.instrument( [component1] )
        
        geometer = mcni.geometer()
        geometer.register( component1, (0,0,0), (0,0,0) )

        neutrons = mcni.neutron_buffer( 2 )

        mcni.simulate( instrument, geometer, neutrons, multiple_scattering=1)
        return
    def test(self):
        component1 = Component("comp")
        instrument = mcni.instrument([component1])

        geometer = mcni.geometer()
        geometer.register(component1, (0, 0, 0), (0, 0, 0))

        neutrons = mcni.neutron_buffer(2)

        mcni.simulate(instrument, geometer, neutrons, multiple_scattering=1)
        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.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: 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
示例#16
0
    def test2(self):
        'storage --> verifier'
        from mcni.components.NeutronFromStorage import NeutronFromStorage
        component1 = NeutronFromStorage('storage', '%s-saved' % neutron_storage_path)
        component2 = Verifier( 'verifier', self)
        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( packetsize*(npackets+1) )

        mcni.simulate( instrument, geometer, neutrons )
        return
    def test(self):
        component1 = Source('source')
        import mcni.mcnibp as c
        component2 = c.DummyComponent('dummy2')
        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,90) )

        neutrons = mcni.neutron_buffer( 1 )

        mcni.simulate( instrument, geometer, neutrons )

        return
示例#18
0
    def test1(self):

        from mcni.components.NeutronFromStorage import NeutronFromStorage
        component1 = NeutronFromStorage('storage', neutron_storage_path)
        component2 = Verifier('verifier', self)
        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(1)

        mcni.simulate(instrument, geometer, neutrons)
        return
示例#19
0
    def test1(self):
        
        from mcni.components.NeutronFromStorage import NeutronFromStorage
        component1 = NeutronFromStorage('storage', neutron_storage_path)
        component2 = Verifier( 'verifier', self)
        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( 1 )

        mcni.simulate( instrument, geometer, neutrons )
        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 test(self):
        if self.nompi: return
        
        component1 = Source('source')
        component2 = Printer('printer')
        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( 2 )

        mcni.simulate( instrument, geometer, neutrons )

        return
    def test(self):
        if self.nompi: return

        component1 = Source('source')
        component2 = Printer('printer')
        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(2)

        mcni.simulate(instrument, geometer, neutrons)

        return
    def test2(self):
        'storage --> verifier'
        from mcni.components.NeutronFromStorage import NeutronFromStorage
        component1 = NeutronFromStorage('storage',
                                        '%s-saved' % neutron_storage_path)
        component2 = Verifier('verifier', self)
        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(packetsize * (npackets + 1))

        mcni.simulate(instrument, geometer, neutrons)
        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
示例#26
0
    def main(self, *args, **kwds):
        if self.inventory.dumpregistry:
            self._dumpRegsitry()
            return

        import mcni
        instrument = self._createInstrument()

        geometer = self.geometer

        from mcni.SimulationContext import SimulationContext
        context = SimulationContext()
        context.multiple_scattering = self.inventory.multiple_scattering
        context.tracer = self.tracer
        context.mpiRank = self.mpiRank
        context.mpiSize = self.mpiSize
        context.outputdir = self.outputdir
        context.overwrite_datafiles = self.overwrite_datafiles

        n = int(self.ncount / self.buffer_size)
        assert n > 0, 'ncount should be larger than buffer_size: ncount=%s, buffer_size=%s' % (
            self.ncount, self.buffer_size)

        from mcni import journal
        logger = journal.logger('info',
                                'instrument',
                                header='',
                                footer='',
                                format='-> %s')
        for i in range(n):
            logger("mpi node %s at loop %s" % (self.mpiRank, i))
            neutrons = mcni.neutron_buffer(self.buffer_size)
            context.iteration_no = i
            mcni.simulate(instrument, geometer, neutrons, context=context)
            continue

        remain = int(self.ncount % self.buffer_size)
        if remain:
            logger("mpi node %s at last loop" % (self.mpiRank, ))
            neutrons = mcni.neutron_buffer(remain)
            context.iteration_no = n
            mcni.simulate(instrument, geometer, neutrons, context=context)

        import os
        print os.times()
        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
示例#28
0
    def test1(self):
        'neutron --> storage'
        #from mcni.components.MonochromaticSource import MonochromaticSource
        #component1 = MonochromaticSource('source', neutron)
        component1 = Source( 'source' )
        
        from mcni.components.NeutronToStorage import NeutronToStorage
        component2 = NeutronToStorage( 'storage', neutron_storage_path)
        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,90) )

        neutrons = mcni.neutron_buffer( ntotneutrons )

        mcni.simulate( instrument, geometer, neutrons )
        return
    def test1(self):
        'neutron --> storage'
        #from mcni.components.MonochromaticSource import MonochromaticSource
        #component1 = MonochromaticSource('source', neutron)
        component1 = Source('source')

        from mcni.components.NeutronToStorage import NeutronToStorage
        component2 = NeutronToStorage('storage', neutron_storage_path)
        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, 90))

        neutrons = mcni.neutron_buffer(ntotneutrons)

        mcni.simulate(instrument, geometer, neutrons)
        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
示例#31
0
    def main(self, *args, **kwds):
        if self.inventory.dumpregistry:
            self._dumpRegsitry()
            return
        
        import mcni
        instrument = self._createInstrument()
        
        geometer = self.geometer

        from mcni.SimulationContext import SimulationContext
        context = SimulationContext()
        context.multiple_scattering = self.inventory.multiple_scattering
        context.tracer = self.tracer
        context.mpiRank = self.mpiRank
        context.mpiSize = self.mpiSize
        context.outputdir = self.outputdir
        context.overwrite_datafiles = self.overwrite_datafiles
        
        n = int(self.ncount / self.buffer_size)
        assert n>0, 'ncount should be larger than buffer_size: ncount=%s, buffer_size=%s' % (self.ncount, self.buffer_size)
        
        from mcni import journal
        logger = journal.logger(
            'info', 'instrument', header='', footer='', format='-> %s')
        for i in range(n):
            logger("mpi node %s at loop %s" % (self.mpiRank, i))
            neutrons = mcni.neutron_buffer( self.buffer_size )
            context.iteration_no = i
            mcni.simulate( instrument, geometer, neutrons, context=context)
            continue
        
        remain = int(self.ncount % self.buffer_size)
        if remain:
            logger("mpi node %s at last loop" % (self.mpiRank,))
            neutrons = mcni.neutron_buffer(remain)
            context.iteration_no = n
            mcni.simulate( instrument, geometer, neutrons, context=context)
            
        import os
        print os.times()
        return
示例#32
0
    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
示例#33
0
    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
    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
示例#35
0
    def test(self):
        component1 = Source('source')
        component2 = Verifier('dummy2', self)
        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,90) )

        neutrons = mcni.neutron_buffer( 1 )

        mcni.simulate( instrument, geometer, neutrons )

        for i in range(len(neutrons)):
            neutron = neutrons[i]
            r = list( neutron.state.position )
            v = list( neutron.state.velocity )
            self.assertVectorAlmostEqual( r, (1,2,3) )
            self.assertVectorAlmostEqual( v, (1,2,3) )
            continue
        
        return
示例#36
0
    def test(self):
        component1 = Source("source")
        component2 = Verifier("dummy2", self)
        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, 90))

        neutrons = mcni.neutron_buffer(1)

        mcni.simulate(instrument, geometer, neutrons)

        for i in range(len(neutrons)):
            neutron = neutrons[i]
            r = list(neutron.state.position)
            v = list(neutron.state.velocity)
            self.assertVectorAlmostEqual(r, (1, 2, 3))
            self.assertVectorAlmostEqual(v, (1, 2, 3))
            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
示例#38
0
    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 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 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
示例#41
0
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