Esempio n. 1
0
def test3():
    sample = loadSampleYml(os.path.join(here, 'KVO-rotated.yml'))
    al_can = loadSampleYml(os.path.join(here, 'al-can.yml'))
    createSampleAssembly("_tmp.sampleassembly3", sample, al_can)
    o = os.system('diff -r _tmp.sampleassembly3 ' + os.path.join(here, 'expected-sampleassembly3'))
    assert not o
    return
Esempio n. 2
0
def test3():
    V_plate = sample.loadSampleYml(os.path.join(here, '..', 'data', 'V-plate.yml'))
    assert V_plate.chemical_formula == 'V2'
    assert np.allclose(V_plate.lattice.basis_vectors, np.eye(3)*3.024)
    for atom in V_plate.atomic_structure:
        print atom
    return
Esempio n. 3
0
def slice(
        sample, 
        ei, psi_axis,
        hkl0, hkl_dir, x_axis, 
        instrument, erange,
        out):
    """dynamic range of slice"""
    from mcvine.workflow.sample import loadSampleYml
    sample = loadSampleYml(sample)
    code = "from mcvine.workflow.DGS import %s as mod" % instrument
    d = {}; exec(code, d); mod = d['mod']
    psi_angles = np.arange(*tuple(psi_axis))
    x_axis = np.arange(*tuple(x_axis))
    from matplotlib import pyplot as plt
    plt.figure()
    from ...singlextal import dynrange
    dynrange.plotDynRangeOfSlice(
        sample, psi_angles, ei, hkl0, hkl_dir, x_axis,
        mod.scattering_angle_constraints,
        Erange=erange)
    if out:
        plt.savefig(out)
    else:
        plt.show()
    return
Esempio n. 4
0
def test_plotDynRangeOfSlice2():
    sample = loadSampleYml(sample_yml_path)
    psilist = np.arange(-5, 90., 0.5)
    Ei = 100.
    hkl0 = [0,0,0]
    hkl_dir = [1., 0., 0.]
    xaxis = np.arange(-12, 6, .1)
    plt.figure()
    dynrange.plotDynRangeOfSlice(
        sample, psilist, Ei, hkl0, hkl_dir, xaxis,
        ARCS.scattering_angle_constraints,
        Erange=(-5, 80))
    plt.savefig(os.path.join(outdir, "dynrange.slice_100_from000.png"))
    return
Esempio n. 5
0
 def validate(self):
     if not super(Step1_Sample_selector, self).validate(): return False
     path = self.getSelectedFile()
     if not os.path.exists(path):
         self.updateStatusBar("%s: does not exist" % path)
         return False
     from mcvine.workflow.sample import loadSampleYml
     try:
         sample = loadSampleYml(path)
     except Exception as exc:
         self.updateStatusBar("Failed to load sample from %r: \n%s" %
                              (path, exc))
         return False
     self.context.sample_yaml = path
     return True
Esempio n. 6
0
def test_iterPointsInSlice():
    sample = loadSampleYml(sample_yml_path)
    psilist = np.arange(-5, 90., 0.5)
    Ei = 100.
    hkl0 = [-16/3., -8/3., 8/3.]
    hkl_dir = [-1., 1., -1.]
    xaxis = np.arange(-6, 6, .1)
    plt.figure()
    for psi, xs, Es in dynrange.iterPointsInSlice(
            sample, psilist, Ei, hkl0, hkl_dir, xaxis,
            ARCS.scattering_angle_constraints):
        plt.plot(xs, Es, label=str(psi))
        continue
    plt.savefig(os.path.join(outdir, "dynrange.slice_111.png"))
    return
Esempio n. 7
0
def test_plotDynRangeOfSlices2():
    sample = loadSampleYml(sample_yml_path)
    psilist = np.arange(-5, 90., 0.5)
    Ei = 100.
    hkl0s = [[h,0,l] for h in range(-3, 3)
             for l in range(-3,3)]
    hkl_dir = [0., 1., 0]
    xaxis = np.arange(-6, 6, .1)
    for hkl0 in hkl0s:
        plt.figure()
        dynrange.plotDynRangeOfSlice(
            sample, psilist, Ei, hkl0, hkl_dir, xaxis,
            ARCS.scattering_angle_constraints,
            Erange=(-5, 80))
        fn = "dynrange.slice_010_from%s,%s,%s.png" % tuple(hkl0)
        plt.savefig(os.path.join(outdir, fn))
    return
Esempio n. 8
0
def plotDynRange(hkl0, hkl_projection, qaxis, Erange, config):
    from mcvine.workflow.singlextal import dynrange
    from mcvine.workflow.sample import loadSampleYml

    sample = loadSampleYml(config.sample_yaml)
    psi_scan = config.psi_scan
    psilist = np.arange(psi_scan.min, psi_scan.max, psi_scan.step)

    dynrange.plotDynRangeOfSlice(
        sample,
        psi_scan.ticks(),
        config.Ei,
        hkl0,
        hkl_projection,
        qaxis,
        config.instrument.scattering_angle_constraints,
        Erange=Erange)
    return
Esempio n. 9
0
def create_project(
        beam_dir='beam',
        sample_yaml='sample.yaml',
        work_dir='work',
        ncount=int(1e8),
        buffer_size=int(1e6),
        nodes=10,
        multiple_scattering=False,
        detector_vessel_angle=None,
        # Qaxis=[0.0, 15.0, 0.1],
        instrument_name='ARCS'):
    type = 'DGS'
    work = os.path.abspath(work_dir)
    import shutil
    if os.path.exists(work):
        shutil.rmtree(work)
    cmd = 'mcvine workflow powder --type {type} --instrument {instrument_name} '
    cmd += '--sample=V --workdir {work} --ncount {ncount} --buffer_size {buffer_size} '
    cmd += '--multiple_scattering {multiple_scattering} --nodes {nodes} '  # --qaxis "{Qaxis[0]} {Qaxis[1]} {Qaxis[2]}"'
    cmd += '--detector-vessel-angle {detector_vessel_angle}'
    cmd = cmd.format(**locals())
    if os.system(cmd):
        raise RuntimeError("%s failed" % cmd)
    # beam
    shutil.rmtree(os.path.join(work, 'beam'))
    os.symlink(beam_dir, os.path.join(work, 'beam'))
    Ei = _getEi(beam_dir)
    # sample
    sa_dir = os.path.join(work, 'sampleassembly')
    shutil.rmtree(sa_dir)
    from mcvine.workflow.sample import loadSampleYml, dgs_setEi
    sample = loadSampleYml(sample_yaml)
    dgs_setEi(sample, Ei)
    from mcvine.workflow.sampleassembly.scaffolding import createSampleAssembly
    createSampleAssembly(sa_dir, sample)
    return
Esempio n. 10
0
def setup(outdir, sampleyml, beam, E, hkl, hkl_projection, psi_axis, instrument, pixel, log=None):
    """setup the simulation directory and scripts and input files

    - outdir: output dir
    - sampleyml: path. should contain
      * name
      * chemical_formula
      * lattice
      * orientation
      * shape
    - beam: mcvine beam simulation path
    - E: energy transfer
    - hkl: momentum transfer
    - hkl_projection: hkl projection axis for slice
    - psi_axis: psi scan
    - instrument: instrument object with geometry data such as L1 and L2
    - pixel: pixel object with pixe, height, pressure. and position
    - log: logger object
    """
    if log is None:
        import sys
        log = sys.stdout
    # load beam
    from ._beam import computeEi_and_t0
    Ei, t0 = computeEi_and_t0(beam, instrument)
    log.write( "Ei=%s, t0=%s\n" % (Ei, t0) )
    # load sample
    from mcvine.workflow.sample import loadSampleYml
    sample = loadSampleYml(sampleyml)
    # the sample kernel need information of E and hkl
    Q, hkl2Qmat, psi = calcQ(sampleyml, Ei, E, hkl, psi_axis, Npsisegments=10)
    log.write( "Computed:\n" )
    log.write( "* psi=%s degree\n" % (psi/np.pi*180,) )
    log.write( "* Q=%s\n" % (Q,) )
    log.write( "* hkl2Qmat=%s\n" % (hkl2Qmat,) )
    kfv, Ef = computeKf(Ei, E, Q, log)
    log.write( "* Ef=%s\n" % (Ef,))
    pixel_position, pixel_orientation = computePixelPositionOrientation(kfv, instrument, log)
    # at this point the coordinates have convention of z vertical up
    # ** coordinate system for calculated position: z is vertical **
    # this pixel_position is in the instrument coordinate system.
    # we need, however, the pixel_position in the instrument
    # coordinate system rorated psi angle.
    from numpy import sin, cos
    x,y,z=pixel_position
    x2,y2,z2 = x*cos(psi)+y*sin(psi), -x*sin(psi)+y*cos(psi), z
    # convert to z along beam
    pixel_position3 = y2, z2, x2
    # compute nominal tof from mod to sample
    vi = Conv.e2v(Ei)
    L_m2s = mcvine.units.parse(instrument.L_m2s)/mcvine.units.meter
    t_m2s = L_m2s/vi + t0*1e-6
    # nominal tof from mod to pixel
    vf = Conv.e2v(Ef)
    t_s2p = np.linalg.norm(pixel_position)/vf
    t_m2p = t_m2s + t_s2p
    log.write( "t_m2s=%s, t_s2p=%s, t_m2p=%s\n" % (t_m2s, t_s2p, t_m2p))
    # tof passing through the pixel
    r = mcvine.units.parse(pixel.radius)/mcvine.units.meter
    h = mcvine.units.parse(pixel.height)/mcvine.units.meter
    dtof = np.sqrt(4*r*r+h*h)*1.1/vf
    # decorate the sample kernel
    kernel = sample.excitations[0]
    kernel.target_position = "%s*meter,%s*meter,%s*meter" % pixel_position3
    kernel.target_radius = "%s*meter" % (np.sqrt(r*r+h*h/4)*1.1,)
    kernel.tof_at_target = "%s*microsecond" % (t_m2p*1e6)
    kernel.dtof = "%s*microsecond" % (dtof*1e6,)
    # create sample assembly
    from mcvine.workflow.singlextal.scaffolding import createSampleAssembly
    sampledir = os.path.abspath(os.path.join(outdir, 'sample'))
    createSampleAssembly(sampledir, sample, add_elastic_line=False)
    # save instrument object
    ofstream = tpf.NamedTemporaryFile(
        dir=outdir, prefix='instrument', suffix='.pkl', delete=False)
    pkl.dump(instrument, ofstream)
    instr_fn = os.path.abspath(ofstream.name)
    ofstream.close()
    # save pixel object
    pixel.position = pixel_position
    pixel.orientation = pixel_orientation
    ofstream = tpf.NamedTemporaryFile(
        dir=outdir, prefix='pixel', suffix='.pkl', delete=False)
    pkl.dump(pixel, ofstream)
    pixel_fn = os.path.abspath(ofstream.name)
    ofstream.close()
    # create sim script
    params = dict(
        beam_neutrons_path = os.path.join(beam, 'out', 'neutrons'),
        instr_fn = instr_fn, pixel_fn = pixel_fn,
        samplexmlpath = os.path.join(sampledir, "sampleassembly.xml"),
        psi = psi,
        hkl2Q = hkl2Qmat,
        t_m2p = t_m2p,
        Q = Q,
        E = E,
        hkl_projection = hkl_projection,
        )
    script = sim_script_template % params
    open(os.path.join(outdir, 'run.py'), 'wt').write(script)
    return
Esempio n. 11
0
def test4():
    sample = loadSampleYml(os.path.join(here, '..', '..', '..', 'data', 'V-plate.yml'))
    createSampleAssembly("_tmp.sampleassembly4", sample)
    o = os.system('diff -r _tmp.sampleassembly4 ' + os.path.join(here, 'expected-sampleassembly4'))
    assert not o
    return
Esempio n. 12
0
def test2():
    sample = loadSampleYml(os.path.join(here, 'KVO-rotated.yml'))
    createSampleAssembly("_tmp.sampleassembly2", sample)
    o = os.system('diff -r _tmp.sampleassembly2 ' + os.path.join(here, 'expected-sampleassembly2'))
    assert not o
    return
Esempio n. 13
0
def test2():
    sample.loadSampleYml(os.path.join(here, '..', 'data', 'KVO.yml'))
    return