|
| 1 | +######################################################################## |
| 2 | +# Copyright 2021, UChicago Argonne, LLC |
| 3 | +# |
| 4 | +# Licensed under the BSD-3 License (the "License"); you may not use |
| 5 | +# this file except in compliance with the License. You may obtain a |
| 6 | +# copy of the License at |
| 7 | +# |
| 8 | +# https://opensource.org/licenses/BSD-3-Clause |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | +# implied. See the License for the specific language governing |
| 14 | +# permissions and limitations under the License. |
| 15 | +######################################################################## |
| 16 | +""" |
| 17 | +date: 2020-11-20 |
| 18 | +author: matz |
| 19 | +Main DASSH calculation procedure |
| 20 | +""" |
| 21 | +######################################################################## |
| 22 | +import os |
| 23 | +# import numpy as np |
| 24 | +import sys |
| 25 | +import dassh |
| 26 | +import argparse |
| 27 | +import cProfile |
| 28 | +_log_info = 20 # logging levels must be int |
| 29 | + |
| 30 | + |
| 31 | +def main(): |
| 32 | + """Perform temperature sweep in DASSH""" |
| 33 | + # Parse command line arguments to DASSH |
| 34 | + parser = argparse.ArgumentParser(description='Process DASSH cmd') |
| 35 | + parser.add_argument('inputfile', |
| 36 | + metavar='inputfile', |
| 37 | + help='The input file to run with DASSH') |
| 38 | + parser.add_argument('--verbose', |
| 39 | + action='store_true', |
| 40 | + help='Verbose; print summary with each axial step') |
| 41 | + parser.add_argument('--save_reactor', |
| 42 | + action='store_true', |
| 43 | + help='Save DASSH Reactor object after sweep') |
| 44 | + parser.add_argument('--profile', |
| 45 | + action='store_true', |
| 46 | + help='Profile the execution of DASSH') |
| 47 | + parser.add_argument('--no_power_calc', |
| 48 | + action='store_false', |
| 49 | + help='Skip VARPOW calculation if done previously') |
| 50 | + args = parser.parse_args() |
| 51 | + # dassh_path = os.path.dirname(os.path.abspath(__file__)) |
| 52 | + |
| 53 | + # Enable the profiler, if desired |
| 54 | + if args.profile: |
| 55 | + pr = cProfile.Profile() |
| 56 | + pr.enable() |
| 57 | + |
| 58 | + # Initiate logger |
| 59 | + print(dassh._ascii._ascii_title) |
| 60 | + in_path = os.path.split(args.inputfile)[0] |
| 61 | + dassh_logger = dassh.logged_class.init_root_logger(in_path, 'dassh') |
| 62 | + |
| 63 | + # Pre-processing: read input file and set up DASSH objects |
| 64 | + dassh_logger.log(_log_info, f'Reading input: {args.inputfile}') |
| 65 | + dassh_input = dassh.DASSH_Input(args.inputfile) |
| 66 | + reactor = dassh.Reactor(dassh_input, |
| 67 | + calc_power=args.no_power_calc, |
| 68 | + write_output=True) |
| 69 | + |
| 70 | + # Perform the sweep |
| 71 | + dassh_logger.log(_log_info, 'Performing temperature sweep...') |
| 72 | + reactor.temperature_sweep(verbose=args.verbose) |
| 73 | + |
| 74 | + # Post-processing: write output, save reactor if desired |
| 75 | + dassh_logger.log(_log_info, 'Temperature sweep complete') |
| 76 | + if args.save_reactor and sys.version_info >= (3, 7): |
| 77 | + reactor.save() |
| 78 | + elif dassh_input.data['Setup']['Plot']: |
| 79 | + reactor.save() # just in case figure generation fails |
| 80 | + else: |
| 81 | + pass |
| 82 | + dassh_logger.log(_log_info, 'Output written') |
| 83 | + |
| 84 | + # Post-processing: generate figures, if desired |
| 85 | + if ('Plot' in dassh_input.data.keys() |
| 86 | + and len(dassh_input.data['Plot']) > 0): |
| 87 | + dassh_logger.log(_log_info, 'Generating figures') |
| 88 | + dassh.plot.plot_all(dassh_input, reactor) |
| 89 | + |
| 90 | + # if dassh_input.orificing: |
| 91 | + # dassh_logger.log(_log_info, 'Entering orificing optimization') |
| 92 | + # while not converged: |
| 93 | + # predict groups and mass flow rates: |
| 94 | + # reactor.orificing() ?? |
| 95 | + # dassh_logger.log(_log_info, 'Performing temperature sweep...') |
| 96 | + # reactor.temperature_sweep(verbose=args.verbose) |
| 97 | + # dassh_logger.log(_log_info, 'Temperature sweep complete') |
| 98 | + # check_convergence |
| 99 | + |
| 100 | + # Finish the calculation |
| 101 | + dassh_logger.log(_log_info, 'DASSH execution complete') |
| 102 | + # Print/dump profiler results |
| 103 | + if args.profile: |
| 104 | + pr.disable() |
| 105 | + # pr.print_stats() |
| 106 | + pr.dump_stats('dassh_profile.out') |
| 107 | + |
| 108 | + |
| 109 | +def plot(): |
| 110 | + """Command-line interface to postprocess DASSH data to make |
| 111 | + matplotlib figures""" |
| 112 | + # Get input file from command line arguments |
| 113 | + parser = argparse.ArgumentParser(description='Process DASSH cmd') |
| 114 | + parser.add_argument('inputfile', |
| 115 | + metavar='inputfile', |
| 116 | + help='The input file to run with DASSH') |
| 117 | + args = parser.parse_args() |
| 118 | + |
| 119 | + # Initiate logger |
| 120 | + print(dassh._ascii._ascii_title) |
| 121 | + in_path = os.path.split(args.inputfile)[0] |
| 122 | + dassh_logger = dassh.logged_class.init_root_logger(in_path, |
| 123 | + 'dassh_plot') |
| 124 | + |
| 125 | + # Check whether Reactor object exists; if so, process with |
| 126 | + # DASSHPlot_Input and get remaining info from Reactor object |
| 127 | + rpath = os.path.join(os.path.abspath(in_path), 'dassh_reactor.pkl') |
| 128 | + if os.path.exists(rpath): |
| 129 | + dassh_logger.log(_log_info, f'Loading DASSH Reactor: {rpath}') |
| 130 | + r = dassh.reactor.load(rpath) |
| 131 | + dassh_logger.log(_log_info, f'Reading input: {args.inputfile}') |
| 132 | + inp = dassh.DASSHPlot_Input(args.inputfile, r) |
| 133 | + |
| 134 | + # Otherwise, build Reactor object from complete DASSH input |
| 135 | + else: |
| 136 | + dassh_logger.log(_log_info, f'Reading input: {args.inputfile}') |
| 137 | + inp = dassh.DASSH_Input(args.inputfile) |
| 138 | + dassh_logger.log(_log_info, f'Building DASSH Reactor from input') |
| 139 | + r = dassh.Reactor(inp, calc_power=False) |
| 140 | + |
| 141 | + # Generate figures |
| 142 | + dassh_logger.log(_log_info, 'Generating figures') |
| 143 | + dassh.plot.plot_all(inp, r) |
| 144 | + dassh_logger.log(_log_info, 'DASSH_PLOT execution complete') |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == '__main__': |
| 148 | + main() |
0 commit comments