pmsco-public/pmsco/calculator.py
matthias muntwiler bbd16d0f94 add files for public distribution
based on internal repository 0a462b6 2017-11-22 14:41:39 +0100
2017-11-22 14:55:20 +01:00

132 lines
4.7 KiB
Python

"""
@package pmsco.calculator
abstract scattering program interface.
this module declares the basic interface to scattering programs.
for each scattering program (EDAC, MSC, SSC, ...) a specific interface must be derived from CalcInterface.
the derived interface must implement the run() method.
the run() method and the scattering code may use only the parameters declared in the interface.
TestCalcInterface is provided for testing the PMSCO code quickly without calling an external program.
@author Matthias Muntwiler
@copyright (c) 2015 by Paul Scherrer Institut @n
Licensed under the Apache License, Version 2.0 (the "License"); @n
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
"""
import time
import numpy as np
import data as md
import cluster as mc
__author__ = 'matthias muntwiler'
class Calculator(object):
"""
Interface class to the calculation program.
"""
def run(self, params, cluster, scan, output_file):
"""
run a calculation with the given parameters and cluster.
the result is returned as the method result and in a file named <code>output_file + '.etpi'</code>,
or <code>output_file + '.etpai'</code> depending on scan mode.
all other intermediate files are deleted unless keep_temp_files is True.
@param params: a msco_project.Params() object with all necessary values except cluster and output files set.
@param cluster: a msco_cluster.Cluster() object with all atom positions set.
@param scan: a msco_project.Scan() object describing the experimental scanning scheme.
@param output_file: base name for all intermediate and output files
@return: result_file, files_cats
@arg result_file is the name of the main ETPI or ETPAI result file to be further processed.
@arg files_cats is a dictionary that lists the names of all created data files with their category.
the dictionary key is the file name,
the value is the file category (cluster, phase, etc.).
"""
return None, None
def check_cluster(self, cluster, output_file):
"""
export the cluster in XYZ format for reference.
along with the complete cluster, the method also saves cuts in the xz (extension .y.xyz) and yz (.x.xyz) plane.
@param cluster: a pmsco.cluster.Cluster() object with all atom positions set.
@param output_file: base name for all intermediate and output files
@return: dictionary listing the names of the created files with their category.
the dictionary key is the file name,
the value is the file category (cluster).
@warning experimental: this method may be moved elsewhere in a future version.
"""
xyz_filename = output_file + ".xyz"
cluster.save_to_file(xyz_filename, fmt=mc.FMT_XYZ)
files = {xyz_filename: 'cluster'}
clucut = mc.Cluster()
clucut.copy_from(cluster)
clucut.trim_slab("x", 0.0, 0.1)
xyz_filename = output_file + ".x.xyz"
clucut.save_to_file(xyz_filename, fmt=mc.FMT_XYZ)
files[xyz_filename] = 'cluster'
clucut.copy_from(cluster)
clucut.trim_slab("y", 0.0, 0.1)
xyz_filename = output_file + ".y.xyz"
clucut.save_to_file(xyz_filename, fmt=mc.FMT_XYZ)
files[xyz_filename] = 'cluster'
return files
class TestCalculator(Calculator):
"""
interface class producing random data for testing the MSCO code without calling an external program.
"""
def run(self, params, cluster, scan, output_file):
"""
produce a random test data set.
the scan scheme is generated from the given parameters.
the intensities are random values.
@return: result_file, files_cats
the result file contains an ETPI or ETPAI array with random intensity data.
"""
# set up scan
params.fixed_cluster = 'a' in scan.mode
# generate file names
base_filename = output_file
clu_filename = base_filename + ".clu"
if params.fixed_cluster:
etpi_filename = base_filename + ".etpai"
else:
etpi_filename = base_filename + ".etpi"
cluster.save_to_file(clu_filename)
# generate data and save in ETPI or ETPAI format
result_etpi = scan.raw_data.copy()
result_etpi['i'] = np.random.random_sample(result_etpi.shape)
# slow down the test for debugging
time.sleep(5)
md.save_data(etpi_filename, result_etpi)
files = {clu_filename: 'cluster', etpi_filename: 'energy'}
return etpi_filename, files