From 2fef21dcb4d08513dc03fcf171e4d1b753c63989 Mon Sep 17 00:00:00 2001 From: Mark Driver <mdd31@alumni.cam.ac.uk> Date: Fri, 14 Feb 2020 16:44:29 +0000 Subject: [PATCH] update to add CLI. --- phasecalculator/__main__.py | 26 ++++++ phasecalculator/phasecalculatorcli.py | 109 +++++++++++++++++++++++--- 2 files changed, 125 insertions(+), 10 deletions(-) diff --git a/phasecalculator/__main__.py b/phasecalculator/__main__.py index e69de29..0558d70 100644 --- a/phasecalculator/__main__.py +++ b/phasecalculator/__main__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# phasecalculator calculates FGIPs, solvent similarity and VLE with SSIMPLE. +# Copyright (C) 2019 Mark D. Driver +# +# phasecalculator is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +""" +Main script to be called for module level operation. + +@author: Mark +""" + +from phasecalculator.phasecalculatorcli import main + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/phasecalculator/phasecalculatorcli.py b/phasecalculator/phasecalculatorcli.py index 0e0ef74..63065f0 100755 --- a/phasecalculator/phasecalculatorcli.py +++ b/phasecalculator/phasecalculatorcli.py @@ -22,7 +22,12 @@ Script with CLI for phasecalculator. import logging import argparse - +import phasecalculator.runners.phasecalculatorrunner as phasecrun +import phasecalculator.io.systemcollectionprocessor as sysproc +import phasecalculator.io.phasecsvconverter as csvconv +from phasecalculator.classes.runtimeinformation import RuntimeInformation +from phasecalculator.classes.outputinformation import OutputInformation +from phasecalculator.classes.system import System, SystemCollection logging.basicConfig() LOGGER = logging.getLogger(__name__) @@ -30,22 +35,106 @@ LOGGER.setLevel(logging.WARN) def main(): - # Create parser - parser = create_argparser() + # Create parser + parser = create_phasecalculator_argparser() # parse args args = parser.parse_args() return process_args(args) -def process_args(): +def process_args(args): + return args.func(args) + +def process_inputgen(args): + output_filename = args.filename + system_collection = create_system_collection(args) + sysproc.write_system_collection_file(system_collection, output_filename) + +def process_phasecalculator(args): + xml_filename = args.file + system_collection = read_calculator_xml(xml_filename) + if args.memreq is not None: + run_system_collection(system_collection, memory_req=args.memreq) + else: + run_system_collection(system_collection) + +def create_system_collection(args): + phases = read_phasecsv(args.phases) + jar_path = args.jar + scratch_dir = args.scratch + output_dir = args.out_dir + run_inf = create_runtime_inf(jar_path, scratch_dir, output_dir) + fgip_output = args.fgip + similarity_output = args.sim + similarity_output_type = args.sim_type if similarity_output else None + vle_output = args.vle + out_inf = create_output_inf(fgip_output, similarity_output, similarity_output_type, vle_output) + system = System(phases, run_inf, out_inf) + return SystemCollection([system]) + +def create_output_inf(fgip_output, similarity_output, similarity_output_type, vle_output): + return OutputInformation(fgip_output, similarity_output, similarity_output_type, vle_output) + +def create_runtime_inf(jar_path, scratch_dir, output_dir): + return RuntimeInformation(jar_path, scratch_dir, output_dir) -def create_calculator_xml(): +def read_calculator_xml(xml_filename): + return sysproc.read_system_collection_file(xml_filename) + +def read_phasecsv(csv_filename): + return csvconv.convert_csv_file_to_phases(csv_filename) + +def run_system_collection(system_collection, **kwargs): + for system_info in system_collection.system_list: + phasecrun.run_all_analysis(system_info, **kwargs) + +def create_phasecalculator_argparser(): + description = """Phase Calculator provides methods to perform FGIP, +similarity and VLE analysis for solvents.""" + epilog = """Example usage""" + phase_argparser= argparse.ArgumentParser(description=description, + epilog=epilog, + conflict_handler='resolve', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + subparsers = phase_argparser.add_subparsers(title="commands", dest="command") -def read_calculator_xml(): -def run_system_collection(): + calc_description = """Calculation runner based on input System XML.""" + calc_epilog = """""" -def create_phasecalculator_argparser(): + calc_argparser = subparsers.add_parser("calculate", description=calc_description, + epilog=calc_epilog, + help="calculate phase properties.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + calc_argparser.add_argument("-i", "-f","--file", type=str, required=True, + help="") + calc_argparser.add_argument("--memreq", type=str, default=None, + help="") + calc_argparser.set_defaults(func=process_phasecalculator) + + inpgen_description = """Input SystemCollection XML generation.""" + inpgen_epilog = """""" -def create_calculate_argparser(): + inpgen_argparser = subparsers.add_parser("inpgen", description=inpgen_description, + epilog=inpgen_epilog, + help="create system input xml.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + inpgen_argparser.add_argument("-p", "--phases", type=str, + help="") + inpgen_argparser.add_argument("-j", "--jar", type=str, required=True, + help="Phasetransfer jar path.") + inpgen_argparser.add_argument("-x", "--scratch", type=str, default="scratch", + help="scratch directory path.") + inpgen_argparser.add_argument("-o", "--out_dir", type=str, default="output", + help="output directory for calculation results") -def create_inputgen_argparser(): \ No newline at end of file + inpgen_argparser.add_argument("-f", "--fgip", action='store_true', + help="Calculate FGIPs for input solvents") + inpgen_argparser.add_argument("-s","--sim", action='store_true', + help="Calculate similarity for input solvents") + inpgen_argparser.add_argument("--sim_type", type=str, default="all", + help="similarity output type.") + inpgen_argparser.add_argument("-v","--vle", action='store_true', + help="Calculate VLE for input solvents") + inpgen_argparser.add_argument("--filename", type=str, default="systemcollection.xml") + inpgen_argparser.set_defaults(func=process_inputgen) + return phase_argparser -- GitLab