def test(neutron_fn=DEFAULT_NEUTRON_FILE, niter=1): # load file and store copy to avoid reloading for multiple iterations from mcni.neutron_storage import load, neutrons_from_npyarr, \ neutrons_as_npyarr, ndblsperneutron neutrons_orig = load(neutron_fn) neutrons_orig = neutrons_as_npyarr(neutrons_orig) neutrons_orig.shape = -1, ndblsperneutron from mcvine.acc.components.optics import guide_tapering g = guide_tapering.Guide( name='guide', option="file={}".format(guide11_dat), l=guide11_len, mx=guide11_mx, my=guide11_my, ) times = [] for i in range(niter): neutrons = neutrons_from_npyarr(neutrons_orig) t1 = time.time_ns() g.process(neutrons) delta = time.time_ns() - t1 times.append(delta) print("GPU processing time: {} ms ({} s)".format( 1e-6 * delta, 1e-9 * delta)) if niter > 1: # report average time over niter times = np.asarray(times) avg = times.sum() / len(times) print("--------") print("Average GPU time ({} iters): {} ms ({} s)".format( niter, 1e-6 * avg, 1e-9 * avg)) print("--------") neutrons = neutrons_from_npyarr(neutrons_orig) import mcvine.components as mc g = mc.optics.Guide_tapering( name='guide', option="file={}".format(guide11_dat), l=guide11_len, mx=guide11_mx, my=guide11_my, ) t1 = time.time() g.process(neutrons) print("CPU processing time:", time.time() - t1) return
def mcstas2mcvine(inpath, outpath): arr = np.loadtxt(inpath) N = len(arr) newarr = np.zeros((N, 10), dtype=float) newarr[:, list(range(10))] = arr[:, [1, 2, 3, 4, 5, 6, 8, 9, 7, 0]] neutrons = ns.neutrons_from_npyarr(newarr) ns.dump(neutrons, outpath) return
def mcstas2mcvine(inpath, outpath): """ converting mcstas neutron events file to mcvine neutron events file Parameters ---------- inpath: string path where mcstas event file exist outpath:string path where mcvine event file will be saved Returns ------- """ arr = np.loadtxt(inpath) N = len(arr) newarr = np.zeros((N, 10), dtype=float) newarr[:, list(range(10))] = arr[:, [1,2,3, 4,5,6, 8,9, 7, 0]] neutrons = ns.neutrons_from_npyarr(newarr) ns.dump(neutrons, outpath)
def process(self, neutrons): # number of neutrons n = len(neutrons) # mpi if self.parallel: self._setCursor(self.mpiSize, n) # read as numpy array npyarr = self._storage.read(n, asnpyarr=True) if len(npyarr): debug.log(npyarr[0]) else: debug.log("no neutrons") # convert to neutron buffer from mcni.neutron_storage import neutrons_from_npyarr neutrons = neutrons_from_npyarr( npyarr, neutrons ) if len(neutrons): debug.log(neutrons[0]) return neutrons
def process(self, neutrons): # number of neutrons n = len(neutrons) # mpi if self.parallel: self._setCursor(self.mpiSize, n) # read as numpy array npyarr = self._storage.read(n, asnpyarr=True) if len(npyarr): debug.log(npyarr[0]) else: debug.log("no neutrons") # convert to neutron buffer from mcni.neutron_storage import neutrons_from_npyarr neutrons = neutrons_from_npyarr(npyarr, neutrons) if len(neutrons): debug.log(neutrons[0]) return neutrons
#!/usr/bin/env python import numpy arr = numpy.arange(1000.) arr.shape = -1, 10 from mcni.neutron_storage import neutrons_from_npyarr, storage neutrons = neutrons_from_npyarr(arr) s = storage('neutron-storage-for-NeutronFromStorage_TestCase', 'w') s.write(neutrons)
def read(self, n=None, asnpyarr=False, wrap=True): """read (n) neutrons from the current position. n: optional. number of neutrons. if None, read all neutrons wrap: if true, will wrap around to the start when reach the end of file. so you can read forever asnpyarr: if true, returns numpy array instead of neutron array """ path = self.path if not self._readonly: raise RuntimeError, "Neutron storage %r was opened for write" % path position = self._position debug.log('my current position: %s' % position) # n defaults to packetsize n = n or self._packetsize debug.log('number of neutrons to read: %s' % n) # total number of neutrons in the file ntotal = self._ntotal debug.log('total number of neutrons in the storage: %s' % ntotal) # no default, read all is left if n is None: n = ntotal - position debug.log('n was given as None, now set to %s' % n) # nothing to read if n == 0: return # next position of cursor nextpostion = position + n debug.log('next cursor position: %s' % nextpostion) if nextpostion < ntotal: # if it is not beyond the end of file, just read debug.log('no wrapping needed because next-cursor-position<ntotal') npyarr = idfio.read(stream=self.stream, start=position, n=n) debug.log('read %s neutrons start from %s' % (n, position)) self._position = nextpostion debug.log('set my position to %s' % self._position) else: # if out of bound, read to the end of file n1 = ntotal - position npyarr = idfio.read(stream=self.stream, start=position, n=n1) # if wrap, restart from the beginning if wrap: n2 = n - n1 # n2 may be still larger than ntotal # we may need to read the whole file several times if n2 >= ntotal: ntimes = n2 / ntotal allneutrons = idfio.read(stream=self.stream) for i in range(ntimes): npyarr = numpy.concatenate((npyarr, allneutrons)) continue # now n2 is maller than ntotal n2 = n2 - ntimes * ntotal assert n2 < ntotal if n2 > 0: npyarr2 = idfio.read(stream=self.stream, start=0, n=n2) npyarr = numpy.concatenate((npyarr, npyarr2)) self._position = n2 else: self._position = ntotal # npy array? if asnpyarr: return npyarr from mcni.neutron_storage import neutrons_from_npyarr neutrons = neutrons_from_npyarr(npyarr) return neutrons
def read(self, n=None, asnpyarr=False, wrap=True): """read (n) neutrons from the current position. n: optional. number of neutrons. if None, read all neutrons wrap: if true, will wrap around to the start when reach the end of file. so you can read forever asnpyarr: if true, returns numpy array instead of neutron array """ path = self.path if not self._readonly: raise RuntimeError, "Neutron storage %r was opened for write" % path position = self._position debug.log("my current position: %s" % position) # n defaults to packetsize n = n or self._packetsize debug.log("number of neutrons to read: %s" % n) # total number of neutrons in the file ntotal = self._ntotal debug.log("total number of neutrons in the storage: %s" % ntotal) # no default, read all is left if n is None: n = ntotal - position debug.log("n was given as None, now set to %s" % n) # nothing to read if n == 0: return # next position of cursor nextpostion = position + n debug.log("next cursor position: %s" % nextpostion) if nextpostion < ntotal: # if it is not beyond the end of file, just read debug.log("no wrapping needed because next-cursor-position<ntotal") npyarr = idfio.read(stream=self.stream, start=position, n=n) debug.log("read %s neutrons start from %s" % (n, position)) self._position = nextpostion debug.log("set my position to %s" % self._position) else: # if out of bound, read to the end of file n1 = ntotal - position npyarr = idfio.read(stream=self.stream, start=position, n=n1) # if wrap, restart from the beginning if wrap: n2 = n - n1 # n2 may be still larger than ntotal # we may need to read the whole file several times if n2 >= ntotal: ntimes = n2 / ntotal allneutrons = idfio.read(stream=self.stream) for i in range(ntimes): npyarr = numpy.concatenate((npyarr, allneutrons)) continue # now n2 is maller than ntotal n2 = n2 - ntimes * ntotal assert n2 < ntotal if n2 > 0: npyarr2 = idfio.read(stream=self.stream, start=0, n=n2) npyarr = numpy.concatenate((npyarr, npyarr2)) self._position = n2 else: self._position = ntotal # npy array? if asnpyarr: return npyarr from mcni.neutron_storage import neutrons_from_npyarr neutrons = neutrons_from_npyarr(npyarr) return neutrons