Commit 3a0193df authored by Islam, Fahima's avatar Islam, Fahima
Browse files

Upload New File

parent 11a8608b
Loading
Loading
Loading
Loading
+115 −0
Original line number Diff line number Diff line
import os

template = """<?xml version="1.0"?>

<!DOCTYPE SampleAssembly[
   {shape_file_entries}
]>
<SampleAssembly name="{sample_assembly_name}"
   max_multiplescattering_loops_among_scatterers="{max_scatterering}"
   max_multiplescattering_loops_interactM_path1="{max_interactM_scattering}"
   min_neutron_probability="{min_neutron_probability}"
 >
    {sample_blocks}

    <LocalGeometer registry-coordinate-system="InstrumentScientist">
        {geom_regs}
    </LocalGeometer>

    <Environment temperature="300*K"/>

</SampleAssembly>
"""
def _shape_file_entry(shape_name, shape_fileName):
  """
    creating the template for shape entry
  Parameters
  ----------
  shape_name: string
            name of the shape
  shape_fileName : string
            name of the geometry file

  Returns
  -------
  template: string
    template for shape file

  """
  return """ <!ENTITY {shape_name} SYSTEM "{shape_fileName}.xml">
""".format(shape_name=shape_name, shape_fileName=shape_fileName)


def _sample_block(name, shape_name, formula, structureFile, strutureFiletype):
  """
    creating the template for sample block
  Parameters
  ----------
  name: string
    name of the scatterer
  shape_name: string
    name of the shape
  formula: string
    chemical formula of the scatterer
  structureFile: string
    structure file path of the scatterer
  strutureFiletype: string
     extension of the structure file (cif or xyz)
  Returns
  -------
  template: string
    template for sample block
  """
  return """  <PowderSample name="{name}" type="sample">
    <Shape>
      &{shape_name};
    </Shape>
    <Phase type="crystal">
      <ChemicalFormula>{formula}</ChemicalFormula>
      <{strutureFiletype}file>{structureFile}</{strutureFiletype}file>
    </Phase>
  </PowderSample>
""".format(name=name, shape_name=shape_name, formula=formula, structureFile=structureFile, strutureFiletype=strutureFiletype)

scatterers = {
     ('sample', 'sampleshape', 'sample', 'H', 'H.xyz', 'xyz'),   # (name, shape_name, geometry file name, formula, structure file, structurefile type)

}

def makeSAXML(sampleassembly_fileName, pathTosave, max_scatterering, max_interactM_scattering,
              min_neutron_probability, scatterers=scatterers):
   """
    creating the sample assembly file on which mcvine simulation will run
   Parameters
   ----------
   sampleassembly_fileName: string
        file name of the sampleassembly
   pathTosave: string
        the path where the sample assembly file will be saved
   scatterers:set
        the set of scatterer information : scatterer name, shape name, shape file name, chemical formula,
        structure file path, structure file extension


   """

   shape_file_entries = [_shape_file_entry(shape_name, shape_fileName) for name, shape_name, shape_fileName, formula,  structureFile,strutureFiletype  in scatterers]
   shape_file_entries='\n'.join(shape_file_entries)
   sample_blocks = [_sample_block(name, shape_name, formula,structureFile,strutureFiletype) for name, shape_name, shape_fileName, formula, structureFile,strutureFiletype  in scatterers]
   sample_blocks = '\n'.join(sample_blocks)
   lines = ['<Register name="{}" position="(0,0,0)" orientation="(0,0,0)"/>' .format(name) for name, shape_name, shape_fileName, formula, structureFile,strutureFiletype in scatterers]
   geom_regs = '\n '.join(lines)
   text = template.format(shape_file_entries=shape_file_entries, max_scatterering=max_scatterering, 
                          max_interactM_scattering= max_interactM_scattering,
                          sample_assembly_name=sampleassembly_fileName,
                          min_neutron_probability=min_neutron_probability,
                          sample_blocks=sample_blocks, geom_regs=geom_regs)
   with open(os.path.join('{}', 'sampleassembly_{}.xml').format(pathTosave, sampleassembly_fileName), "w") as sam_new:
       sam_new.write(text)