def test(self):
        E_Q = "Q*Q/3."
        S_Q = "1"
        Qmin = 0; Qmax = 10
        absorption_coefficient = scattering_coefficient = 1.
        kernel = mccomponentsbp.create_E_Q_Kernel(
            E_Q, S_Q,
            Qmin, Qmax,
            absorption_coefficient,
            scattering_coefficient,
            )

        ei = 500 # meV
        from mcni.utils import conversion
        vil = conversion.e2v(ei)
        vi = (0,0,vil)

        import numpy.linalg as nl
        import numpy as np
        for i in range(10):
            event = mcni.neutron( 
                r = (0,0,0), v = vi, 
                prob = 1, time = 0 )
            kernel.scatter( event );
            vf = np.array(event.state.velocity)
            diffv = vi - vf
            Q = conversion.v2k(nl.norm(diffv))
            ef = conversion.v2e(nl.norm(vf))
            E = ei - ef
            # print E, Q, event
            E1 = eval(E_Q)
            self.assertAlmostEqual(E, E1)
            continue

        return
Example #2
0
 def test1(self):
     "conversion: e2v"
     e = 100
     v = conversion.e2v(e)
     self.assertAlmostEqual(v, 4373.9331, 3)
     
     e1 = conversion.v2e(v)
     self.assertAlmostEqual(e1, e, 3)
     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: 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: 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 test(self):
        E=10 #meV
        kernel = mccomponentsbp.ConstantEnergyTransferKernel(E, 1,1)

        ei = 100 # meV
        from mcni.utils import conversion
        vi = conversion.e2v(ei)

        import numpy.linalg as nl
        for i in range(10):
            event = mcni.neutron( r = (0,0,0), v = (0,0,vi), prob = 1, time = 0 )
            kernel.scatter( event );
            vf = event.state.velocity
            vf = nl.norm(vf)
            ef = conversion.v2e(vf)
            # print ef
            self.assertAlmostEqual(ei-ef, E, 5)
            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
nevents = 100000
npixels = 100
detradius = 0.0125
detlength = 1
pressure = 10.

L = 5. #distance from source to detector
vi =3000. # velocity of neutrons

tofparams = tmin, tmax, tstep = 0, 10e-3, 1e-4

# absorption probability
from mccomponents.detector import he3_transmission_percentage
from mcni.utils import conversion
absorption_probability = 1-he3_transmission_percentage(conversion.v2e(vi), pressure, 2*detradius*100)


# monte carlo weights
from mccomponents.detector import default_mc_weights_for_detector_scatterer
absorption_weight, scattering_weight, transmission_weight = default_mc_weights_for_detector_scatterer
assert absorption_weight+scattering_weight+transmission_weight==1.

class detector_TestCase(unittest.TestCase):

    def test1(self):
        'simple. one detector'
        ndets = 1
        
        cylinder = primitives.cylinder( detradius, detlength )
        he3tube = md.he3tube(