def test1(self): 'NeutronFromStorage' from mcni.components.NeutronFromStorage import NeutronFromStorage comp = NeutronFromStorage('storage', 'neutron-storage-for-NeutronFromStorage_TestCase') from mcni import neutron_buffer neutrons = neutron_buffer(1) comp.process(neutrons) self.assertEqual(neutrons[0].probability, 9) comp.process(neutrons) self.assertEqual(neutrons[0].probability, 19) return
def test1(self): 'NeutronFromStorage' from mcni.components.NeutronFromStorage import NeutronFromStorage comp = NeutronFromStorage('storage', 'neutron-storage-for-NeutronFromStorage_TestCase') from mcni.utils import mpiutil mpisize = mpiutil.world.size mpirank = mpiutil.rank from mcni import neutron_buffer neutrons = neutron_buffer(1) comp.process(neutrons) self.assertEqual(neutrons[0].probability, mpirank*10+9) comp.process(neutrons) self.assertEqual(neutrons[0].probability, mpisize*10+mpirank*10+9) return
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 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): 'NeutronFromStorage' from mcni.components.NeutronFromStorage import NeutronFromStorage comp = NeutronFromStorage( 'storage', 'neutron-storage-for-NeutronFromStorage_TestCase') from mcni import neutron_buffer neutrons = neutron_buffer(1) comp.process(neutrons) self.assertEqual(neutrons[0].probability, 9) comp.process(neutrons) self.assertEqual(neutrons[0].probability, 19) return
def test1(self): 'NeutronFromStorage' from mcni.components.NeutronFromStorage import NeutronFromStorage comp = NeutronFromStorage( 'storage', 'neutron-storage-for-NeutronFromStorage_TestCase') from mcni.utils import mpiutil mpisize = mpiutil.world.size mpirank = mpiutil.rank from mcni import neutron_buffer neutrons = neutron_buffer(1) comp.process(neutrons) self.assertEqual(neutrons[0].probability, mpirank * 10 + 9) comp.process(neutrons) self.assertEqual(neutrons[0].probability, mpisize * 10 + mpirank * 10 + 9) return
def run(beam_neutrons_path, instrument, samplexmlpath, psi, hkl2Q, pixel, t_m2p, Q, E, hkl_projection, Nbuffer=100000, Nrounds_beam=1): """Run mcvine simulation with using the resolution sample and the resolution pixel, and save the results as numpy arrays - beam_neutrons_path: path to the "neutrons" file of a beam simulation - instrument: instrument object with geometry data such as L1 and L2 - samplexmlpath: path to the sampleassembly xml file - psi: sample rotation angle - hkl2Q: matrix to convert hkl to Q: Q = hkl dot hkl2Q - pixel: pixel object with pixe, height, pressure. and position - t_m2p: exepcted tof from moderator to pixel - Q: expected Q - E: expected E - hkl_projection: the simulated data is projected to this axis and E axis for easy inspection - Nbuffer: neutron buffer size - Nrounds_beam: number of rounds replaying neutrons in the beam """ from mcni.components.NeutronFromStorage import NeutronFromStorage source = NeutronFromStorage('source', path=beam_neutrons_path) from mccomponents.sample import samplecomponent sample = samplecomponent( 'sample', samplexmlpath) # dummy component to save the state of scattered neutrons from mcni.components.Dummy import Dummy sample_location = Dummy('sample_location') # det pixel from mccomponents.components.DGSSXResPixel import DGSSXResPixel pressure = mcvine.units.parse(pixel.pressure)/mcvine.units.parse("kg/m/s/s") r = mcvine.units.parse(pixel.radius)/mcvine.units.meter h = mcvine.units.parse(pixel.height)/mcvine.units.meter pixel_comp = DGSSXResPixel( "pixel", pressure=pressure, tof=t_m2p, radius=r, height=h) # build instrument simulation chain import mcni sim_chain = mcni.instrument( [source, sample, sample_location, pixel_comp] ) # put components into place geometer = mcni.geometer() z_beam = mcvine.units.parse(instrument.offset_sample2beam)/mcvine.units.meter # z along beam geometer.register( source, (0,0,z_beam), (0,0,0) ) geometer.register( sample, (0,0,0), (0,psi*180/np.pi,0) ) geometer.register( sample_location, (0,0,0), (0,0,0) ) geometer.register( pixel_comp, pixel.position, pixel.orientation ) # Q2hkl = np.linalg.inv(hkl2Q) # lengh of hkl_projection squared hkl_proj_len2 = np.dot(hkl_projection, hkl_projection) # neutron buffer from mcni.neutron_storage.idf_usenumpy import count N0 = count(beam_neutrons_path) * Nrounds_beam dxs_all = None; dEs_all = None; probs_all=None; dhkls_all=None start = 0 for i in range(int(np.ceil(N0/Nbuffer))+1): # for i in range(10): end = start + Nbuffer end = min(end, N0) if end<=start: continue sys.stdout.write("%s-%s: " % (start, end-1)); sys.stdout.flush() neutrons = mcni.neutron_buffer(end-start) # simulate tracer = NeutronTracer() mcni.simulate( sim_chain, geometer, neutrons, tracer=tracer) # before_dummy_start, after_dummy_start, \ before_incident, after_incident, \ before_scattered, after_scattered, \ at_sample_location, at_sample_location2, \ before_detected, after_detected, \ before_dummy_end, after_dummy_end = tracer._store incident = after_incident # has to be `at_sample_location` because both sample_location and # beam have no relative rotation. # should not used after_scattered. # it is in the sample's coordinate system, which is rotated by angle psi. # should not use before_detected. It could be rotated in spherical case scattered = at_sample_location detected = after_detected del (before_dummy_start, after_dummy_start, before_incident, after_incident, before_scattered, after_scattered, before_detected, after_detected, before_dummy_end, after_dummy_end) is_scattered = incident.v != scattered.v is_scattered = np.logical_or( is_scattered[:,0], np.logical_or(is_scattered[:,1], is_scattered[:,2]) ) good = np.logical_and(is_scattered, detected.p>np.finfo(float).eps) vi = incident.v[good] vf = scattered.v[good] probs = p = detected.p[good] from mcni.utils import conversion Ei = conversion.VS2E * (vi*vi).sum(axis=-1) Ef = conversion.VS2E * (vf*vf).sum(axis=-1) Es = Ei - Ef vQ = vi-vf Qs = vQ * conversion.V2K Qs = np.array([Qs[:, 2], Qs[:, 0], Qs[:, 1]]).T # print Qs # print Es dQs = Qs - Q dEs = Es - E dhkls = np.dot(dQs, Q2hkl) # print dhkls # print dEs # print p dxs = np.dot( dhkls, np.array(hkl_projection) )/hkl_proj_len2 if dxs_all is None: dxs_all = dxs dEs_all = dEs probs_all = probs dhkls_all = dhkls else: dxs_all = np.concatenate((dxs_all, dxs)) dEs_all = np.concatenate((dEs_all, dEs)) probs_all = np.concatenate((probs_all, probs)) dhkls_all = np.concatenate((dhkls_all, dhkls)) print() start = end continue # reverse x and E # the negative sign here makes the result the PSF dxs_all *= -1 dEs_all *= -1 dhkls_all *= -1 # save results np.save("dhkls.npy", dhkls_all) np.save("dxs.npy", dxs_all) np.save("dEs.npy", dEs_all) np.save("probs.npy", probs_all) h, xedges, yedges = np.histogram2d( dxs_all, dEs_all, bins=100, weights=probs_all) import histogram as H, histogram.hdf as hh xaxis = H.axis('x', boundaries=xedges) Eaxis = H.axis('E', boundaries=yedges) res = H.histogram('res', (xaxis, Eaxis), data=h) hh.dump(res, 'res.h5') sys.stdout.write("Done.\n"); sys.stdout.flush() return