def test_component_long(ncount = 1e6): instr = os.path.join(thisdir, "src_mon_instrument.py") outdir = 'out.psd_monitor' ncount = int(ncount) run_script.run(instr, outdir, ncount=ncount, monitor_factory=psd_monitor) if interactive: import histogram.hdf as hh hist = hh.load(os.path.join(outdir, "psd.h5")) from histogram import plot as plotHist plotHist(hist) return
def plotH5File( h5filename, pathinh5file = None, min = None, max = None, output=None, **kwds): if pathinh5file is None: from histogram.hdf.utils import getOnlyEntry pathinh5file = getOnlyEntry( h5filename ) from histogram.hdf import load h = load( h5filename, pathinh5file ) plotHist( h , min = min, max = max, output=output, **kwds) return
def test_compare_mcvine(): """ Tests the acc cpu implementation of a straight guide against mcvine """ num_neutrons = 100000 # Run the mcvine instrument first instr = os.path.join(thisdir, "guide_instrument.py") mcvine_outdir = 'out.debug-mcvine_guide_cpu_instrument' if os.path.exists(mcvine_outdir): shutil.rmtree(mcvine_outdir) run_script.run1(instr, mcvine_outdir, ncount=num_neutrons, buffer_size=num_neutrons, guide_factory="mcvine.components.optics.Guide", overwrite_datafiles=True) # Run our guide implementation outdir = 'out.debug-guide_cpu_instrument' if os.path.exists(outdir): shutil.rmtree(outdir) run_script.run1(instr, outdir, ncount=num_neutrons, buffer_size=num_neutrons, guide_mod="mcvine.acc.components.optics.numpy_guide", overwrite_datafiles=True) # Compare output files mcvine_Ixy = hh.load(os.path.join(mcvine_outdir, "Ixy.h5")) mcvine_Ixdivx = hh.load(os.path.join(mcvine_outdir, "Ixdivx.h5")) Ixy = hh.load(os.path.join(outdir, "Ixy.h5")) Ixdivx = hh.load(os.path.join(outdir, "Ixdivx.h5")) global interactive if interactive: from histogram import plot as plotHist plotHist(mcvine_Ixy) plotHist(mcvine_Ixdivx) plotHist(Ixy) plotHist(Ixdivx) assert mcvine_Ixy.shape() == Ixy.shape() assert mcvine_Ixdivx.shape() == Ixdivx.shape() assert np.allclose(mcvine_Ixy.data().storage(), Ixy.data().storage()) assert np.allclose(mcvine_Ixdivx.data().storage(), Ixdivx.data().storage()) return
def test_compare_mcvine(): """ Tests the acc cpu implementation of a straight guide against mcvine """ num_neutrons = int(1e6) # Run the mcvine instrument first instr = os.path.join(thisdir, "tapered_guide_instrument.py") cpu_outdir = 'out.debug-tapered_guide_cpu_instrument' if os.path.exists(cpu_outdir): shutil.rmtree(cpu_outdir) run_script.run1(instr, cpu_outdir, ncount=num_neutrons, guide_factory="mcvine.components.optics.Guide_tapering", overwrite_datafiles=True) outdir = 'out.debug-tapered_guide_gpu_instrument' if os.path.exists(outdir): shutil.rmtree(outdir) run_script.run1(instr, outdir, ncount=num_neutrons, guide_mod="mcvine.acc.components.optics.guide_tapering", overwrite_datafiles=True) # Compare output files cpu_Ixy = hh.load(os.path.join(cpu_outdir, "Ixy.h5")) cpu_Ixdivx = hh.load(os.path.join(cpu_outdir, "Ixdivx.h5")) Ixy = hh.load(os.path.join(outdir, "Ixy.h5")) Ixdivx = hh.load(os.path.join(outdir, "Ixdivx.h5")) global interactive if interactive: from histogram import plot as plotHist plotHist(cpu_Ixy) plotHist(cpu_Ixdivx) plotHist(Ixy) plotHist(Ixdivx) assert cpu_Ixy.shape() == Ixy.shape() assert cpu_Ixdivx.shape() == Ixdivx.shape() assert np.allclose(cpu_Ixy.I, Ixy.I) assert np.allclose(cpu_Ixdivx.I, Ixdivx.I) return
def plotPklFile( filename, min = None, max = None, output=None, **kwds): from histogram.hpickle import load h = load( filename ) plotHist( h, min = min, max = max, output=output, **kwds) return
def compare_acc_nonacc( className, monitors, tolerances, num_neutrons, debug, workdir=None, instr=None, interactive=False, acc_component_spec = None, nonacc_component_spec = None, ): """ Tests the acc cpu implementation of an instrument against mcvine. Parameters: className (str): the name of the instrument class under test, e.g., "Guide" monitors (list): the names of the monitors to use in testing, e.g., "Ixy" tolerances (dict): tolerance of float comparisons, e.g., "float32": 1e-8 num_neutrons (int): how many neutrons to use in the testing debug (bool): if to save the neutrons that exit the instrument """ if debug: assert num_neutrons < 1001 classname = className.lower() # Run the mcvine instrument first instr = instr or os.path.join(workdir, f"{classname}_instrument.py") mcvine_outdir = f"out.debug-mcvine_{classname}_cpu_instrument" if os.path.exists(mcvine_outdir): shutil.rmtree(mcvine_outdir) # there is inconsistency in the simulation instrument implementations. # for example, tests/components/optics/guide_instrument.py # implements "is_acc", "factory" and "module" kwargs, # but tests/components/optics/slit_instrument.py # only implements "is_acc". # The implementation here works, but we just need to make sure # the {classname}_instrument.py implements "is_acc" kwd correctly. nonacc_component_spec = nonacc_component_spec or dict( factory=f"mcvine.components.optics.{className}", is_acc = False, ) run_script.run1( instr, mcvine_outdir, ncount=num_neutrons, buffer_size=num_neutrons, save_neutrons_after=debug, overwrite_datafiles=True, **nonacc_component_spec ) # Run our accelerated implementation outdir = f"out.debug-{classname}_gpu_instrument" if os.path.exists(outdir): shutil.rmtree(outdir) acc_component_spec = acc_component_spec or dict( module=f"mcvine.acc.components.optics.{classname}", is_acc = True, ) run_script.run1( instr, outdir, ncount=num_neutrons, buffer_size=num_neutrons, save_neutrons_after=debug, overwrite_datafiles=True, **acc_component_spec ) # Compare output files tolerance = tolerances[floattype] for monitor in monitors: mcvine = hh.load(os.path.join(mcvine_outdir, monitor + ".h5")) mcvine_acc = hh.load(os.path.join(outdir, monitor + ".h5")) if interactive: from histogram import plot as plotHist plotHist(mcvine) plotHist(mcvine_acc) plotHist((mcvine_acc - mcvine) / mcvine) assert mcvine.shape() == mcvine_acc.shape() assert np.allclose(mcvine.data().storage(), mcvine_acc.data().storage(), atol=tolerance)