def document_model(nw, path='report', filename='report.tex', fmt={}): """Generate LaTeX documentation for a TESPy model. - The documentation is stored at path/filename - Generated figures are stored at path/figures/ Parameters ---------- nw : tespy.networks.network.Network Network instance to document. path : str Folder for the documentation, default :code:`report`. filename : str Desired filename for the LaTeX document, default :code:`report.tex`. fmt : dict Dictionary for formatting the report, for sample see respective section in online documentation. """ # prepare filestructure if path[-1] != '/' and path[-1] != '\\': path += '/' path = hlp.modify_path_os(path) fig_path = hlp.modify_path_os(path + 'figures/') # creat paths, if non existent if not os.path.exists(path): os.makedirs(path) if not os.path.exists(fig_path): os.makedirs(fig_path) rpt = set_defaults(nw) rpt = merge_dicts(rpt, fmt) rpt['path'] = path latex = document_software_info(rpt) latex += document_connections(nw, rpt) latex += document_ude(nw, rpt['path']) latex += document_components(nw, rpt) latex += document_busses(nw, rpt) if rpt['latex_body']: latex += r'\end{document}' with open(path + filename, 'w') as f: f.write(latex) f.close()
def document_model(nw, path='report', filename='report.tex', draft=True): """Generate LaTeX documentation for a TESPy model. - The documentation is stored at path/filename - Generated figures are stored at path/figures/ - Disable draft mode to skip on general info at the beginning of the report Parameters ---------- nw : tespy.networks.network.Network Network instance to document. path : str Folder for the documentation, default :code:`report`. filename : str Desired filename for the LaTeX document, default :code:`report.tex`. draft : boolean Add general usage information at beginning of report, default :code:`True` """ # prepare filestructure if path[-1] != '/' and path[-1] != '\\': path += '/' path = hlp.modify_path_os(path) # creat path, if non existent if not os.path.exists(path): os.makedirs(path) fig_path = path + 'figures' if fig_path[-1] != '/' and fig_path[-1] != '\\': fig_path += '/' fig_path = hlp.modify_path_os(fig_path) # creat path, if non existent if not os.path.exists(fig_path): os.makedirs(fig_path) latex = document_software_info(draft) latex += document_connections(nw) latex += document_ude(nw, path) latex += document_components(nw, path) latex += document_busses(nw, path) with open(path + filename, 'w') as f: f.write(latex) f.close()
def __init__(self, cd): info('GEOSTORAGE Reading input file .geostorage_ctr.json') base_path = os.path.join(cd.working_dir, cd.geostorage_path) path = os.path.join(base_path, cd.scenario + '.geostorage_ctrl.json') # print("PATH: {}".format(path)) self.__specification = dict() with open(path) as file: self.__specification.update(load(file)) self.__specification['simulator_file'] = modify_path_os( self.__specification['simulator_file']) self.__specification['simulation_files'] = modify_path_os( self.__specification['simulation_files']) if self.__specification['simulator_file'][0] == '.': self.__specification[ 'simulator_file'] = base_path + self.__specification[ 'simulator_file'] if self.__specification['simulation_files'][0] == '.': self.__specification[ 'simulation_files'] = base_path + self.__specification[ 'simulation_files'] if self.__specification['simulator_name'] == 'ogs_kb1': info('GEOSTORAGE simulator is OGS_kb1') self.__simulator = OgsKb1({ 'simulator_file': self.__specification['simulator_file'], 'simulation_files': self.__specification['simulation_files'], 'factor': self.__specification['factor'], 'distribution': self.__specification['distribution'], 'density': self.__specification['density'] }) else: error('GEOSTORAGE simulator not supported') self.__simulator = None # print(self.__specification) self.__flag_belowMinimumTemperature = False self.__T_min = float( self.__specification['minimum_discharge_temperature'])
def load_network(path): r""" Load a network from a base path. Parameters ---------- path : str The path to the network data. Returns ------- nw : tespy.networks.networks.network TESPy networks object. Note ---- If you save the network structure of an existing TESPy network, it will be saved to the path you specified. The structure of the saved data in that path is the structure you need to provide in the path for loading the network. The structure of the path must be as follows: - Folder: path (e.g. 'mynetwork') - Subfolder: components ('mynetwork/components') containing - bus.csv* - char.csv* - char_map.csv* - component_class_name.csv (e.g. heat_exchanger.csv) - connections.csv - network.json The imported network has the following additional features: - Connections are accessible by label, e.g. :code:`myimportednetwork.connections['myconnection']`. The default label logic is :code:`source:source_id_target:target_id`, where the source means the label of the component the connection originates from and target means the label of the component, the connections targets on. - Components are accessible by label as well, e.g. for a component 'heat exchanger' :code:`myimportednetwork.components['heat exchanger']`. - Busses are accessible by label, too, e.g. for a bus 'power input' :code:`myimportednetwork.busses['power input']`. Example ------- Create a network and export it. This is followed by loading the network with the network_reader module. All network information stored will be passed to a new network object. Components, connections and busses will be accessible by label. The following example setup is simple gas turbine setup with compressor, combustion chamber and turbine. The fuel is fed from a pipeline and throttled to the required pressure while keeping the temperature at a constant value. >>> import numpy as np >>> from tespy.components import (sink, source, combustion_chamber, ... compressor, turbine, heat_exchanger_simple) >>> from tespy.connections import connection, ref, bus >>> from tespy.networks import load_network, network >>> import shutil >>> fluid_list = ['CH4', 'O2', 'N2', 'CO2', 'H2O', 'Ar'] >>> nw = network(fluids=fluid_list, p_unit='bar', T_unit='C', ... h_unit='kJ / kg', iterinfo=False) >>> air = source('air') >>> f = source('fuel') >>> c = compressor('compressor') >>> comb = combustion_chamber('combustion') >>> t = turbine('turbine') >>> p = heat_exchanger_simple('fuel preheater') >>> si = sink('sink') >>> inc = connection(air, 'out1', c, 'in1', label='ambient air') >>> cc = connection(c, 'out1', comb, 'in1') >>> fp = connection(f, 'out1', p, 'in1') >>> pc = connection(p, 'out1', comb, 'in2') >>> ct = connection(comb, 'out1', t, 'in1') >>> outg = connection(t, 'out1', si, 'in1') >>> nw.add_conns(inc, cc, fp, pc, ct, outg) Specify component and connection properties. The intlet pressure at the compressor and the outlet pressure after the turbine are identical. For the compressor, the pressure ratio and isentropic efficiency are design parameters. A compressor map (efficiency vs. mass flow and pressure rise vs. mass flow) is selected for the compressor. Fuel is Methane. >>> c.set_attr(pr=10, eta_s=0.88, design=['eta_s', 'pr'], ... offdesign=['char_map']) >>> t.set_attr(eta_s=0.9, design=['eta_s'], ... offdesign=['eta_s_char', 'cone']) >>> inc.set_attr(fluid={'N2': 0.7556, 'O2': 0.2315, 'Ar': 0.0129, 'CH4': 0, ... 'H2O': 0}, fluid_balance=True, T=25, p=1) >>> fp.set_attr(fluid={'N2': 0, 'O2': 0, 'Ar': 0, 'CH4': 0.96, 'H2O': 0, ... 'CO2': 0.04}, T=25, p=40) >>> pc.set_attr(T=25) >>> ct.set_attr(T=1100) >>> outg.set_attr(p=ref(inc, 1, 0)) >>> power = bus('total power output') >>> power.add_comps({'comp': c}, {'comp': t}) >>> nw.add_busses(power) For a stable start, we specify the fresh air mass flow. >>> inc.set_attr(m=3) >>> nw.solve('design') The total power output is set to 1 MW, electrical or mechanical efficiencies are not considered in this example. The documentation example in class :func:`tespy.connections.bus` provides more information on efficiencies of generators, for instance. >>> inc.set_attr(m=np.nan) >>> power.set_attr(P=-1e6) >>> nw.solve('design') >>> mass_flow = round(nw.connections['ambient air'].m.val_SI, 1) >>> nw.save('exported_nwk') >>> c.set_attr(igva='var') >>> nw.solve('offdesign', design_path='exported_nwk') >>> round(t.eta_s.val, 1) 0.9 >>> power.set_attr(P=-0.75e6) >>> nw.solve('offdesign', design_path='exported_nwk') >>> eta_s_t = round(t.eta_s.val, 3) >>> igva = round(c.igva.val, 3) >>> eta_s_t 0.898 >>> igva 20.139 The designed network is exported to the path 'exported_nwk'. Now import the network and recalculate. Check if the results match with the previous calculation in design and offdesign case. >>> imported_nwk = load_network('exported_nwk') >>> imported_nwk.set_attr(iterinfo=False) >>> imported_nwk.solve('design', init_path='exported_nwk') >>> round(imported_nwk.connections['ambient air'].m.val_SI, 1) == mass_flow True >>> round(imported_nwk.components['turbine'].eta_s.val, 3) 0.9 >>> imported_nwk.components['compressor'].set_attr(igva='var') >>> imported_nwk.solve('offdesign', design_path='exported_nwk') >>> round(imported_nwk.components['turbine'].eta_s.val, 3) 0.9 >>> imported_nwk.busses['total power output'].set_attr(P=-0.75e6) >>> imported_nwk.solve('offdesign', design_path='exported_nwk') >>> round(imported_nwk.components['turbine'].eta_s.val, 3) == eta_s_t True >>> round(imported_nwk.components['compressor'].igva.val, 3) == igva True >>> shutil.rmtree('./exported_nwk', ignore_errors=True) """ if path[-1] != '/' and path[-1] != '\\': path += '/' path_comps = modify_path_os(path + 'components/') path = modify_path_os(path) msg = 'Reading network data from base path ' + path + '.' logging.info(msg) # load characteristics fn = path_comps + 'char_line.csv' try: char_lines = pd.read_csv(fn, sep=';', decimal='.', converters={ 'x': ast.literal_eval, 'y': ast.literal_eval }) msg = 'Reading characteristic lines data from ' + fn + '.' logging.debug(msg) except FileNotFoundError: char_lines = pd.DataFrame(columns=['id', 'type', 'x', 'y']) # load characteristic maps fn = path_comps + 'char_map.csv' try: msg = 'Reading characteristic maps data from ' + fn + '.' logging.debug(msg) char_maps = pd.read_csv(fn, sep=';', decimal='.', converters={ 'x': ast.literal_eval, 'y': ast.literal_eval, 'z1': ast.literal_eval, 'z2': ast.literal_eval }) except FileNotFoundError: char_maps = pd.DataFrame(columns=['id', 'type', 'x', 'y', 'z1', 'z2']) # load components comps = pd.DataFrame() files = os.listdir(path_comps) for f in files: if f != 'bus.csv' and f != 'char_line.csv' and f != 'char_map.csv': fn = path_comps + f df = pd.read_csv(fn, sep=';', decimal='.', converters={ 'design': ast.literal_eval, 'offdesign': ast.literal_eval, 'busses': ast.literal_eval, 'bus_param': ast.literal_eval, 'bus_P_ref': ast.literal_eval, 'bus_char': ast.literal_eval, 'bus_base': ast.literal_eval }) # create components df['instance'] = df.apply(construct_comps, axis=1, args=(char_lines, char_maps)) cols = [ 'instance', 'label', 'busses', 'bus_param', 'bus_P_ref', 'bus_char' ] if 'bus_base' in df.columns: cols += ['bus_base'] else: msg = ( 'The base value of the bus must be part of the exported ' 'component data for component of type ' + f[:-4] + '. ' 'Please make sure to add the column bus_base to your data ' 'or recreate the network export with the TESPy 0.3.x API. ' 'This warning will be removed in TESPy version 0.3.2.') warnings.warn(msg, FutureWarning, stacklevel=2) comps = pd.concat((comps, df[cols]), axis=0) msg = 'Reading component data (' + f[:-4] + ') from ' + fn + '.' logging.debug(msg) comps = comps.set_index('label') msg = 'Created network components.' logging.info(msg) # create network nw = construct_network(path) # load connections fn = path + 'connections.csv' conns = pd.read_csv(fn, sep=';', decimal='.', converters={ 'design': ast.literal_eval, 'offdesign': ast.literal_eval }) msg = 'Reading connection data from ' + fn + '.' logging.debug(msg) # create connections conns['instance'] = conns.apply(construct_conns, axis=1, args=( comps, nw, )) conns.apply(conns_set_ref, axis=1, args=(conns, )) conns = conns.set_index('id') # add connections to network for c in conns['instance']: nw.add_conns(c) msg = 'Created connections.' logging.info(msg) # load busses try: fn = path_comps + 'bus.csv' busses = pd.read_csv(fn, sep=';', decimal='.') msg = 'Reading bus data from ' + fn + '.' logging.debug(msg) except FileNotFoundError: busses = pd.DataFrame() msg = 'No bus data found!' logging.debug(msg) # create busses if len(busses) > 0: busses['instance'] = busses.apply(construct_busses, axis=1) # add components to busses comps.apply(busses_add_comps, axis=1, args=( busses, char_lines, )) # add busses to network for b in busses['instance']: nw.add_busses(b) msg = 'Created busses.' logging.info(msg) msg = 'Created network.' logging.info(msg) return nw
def load_network(path): r""" Load a network from a base path. Parameters ---------- path : str The path to the network data. Returns ------- nw : tespy.networks.network TESPy networks object. Note ---- If you save the network structure of an existing TESPy network, it will be saved to the path you specified. The structure of the saved data in that path is the structure you need to provide in the path for loading the network. The structure of the path must be as follows: - Folder: path (e. g. 'mynetwork') - Subfolder: comps (e. g. 'mynetwork/comps') containing - bus.csv* - char.csv* - char_map.csv* - component_class_name.csv (e. g. heat_exchanger.csv) - conns.csv - netw.csv The imported network has the following additional features: - Connections are accessible by their target's label and id, e. g. for a connection going to 'condenser' at inlet 'in2' use :code:`myimportednetwork.imp_conns['condenser:in2']`. - Components are accessible by label, e. g. for a component 'heat exchanger' :code:`myimportednetwork.imp_comps['heat exchanger']`. - Busses are accessible by label, e. g. for a bus 'power input' :code:`myimportednetwork.imp_busses['power input']`. Example ------- Create a network and export it. This is followed by loading the network with the network_reader module. All network information stored will be passed to a new network object. Components and busses will be accessible by label, connections by :code:`'source.label:source.id_target.label:target.id'`. The following example setup is simple gas turbine setup with compressor, combustion chamber and turbine. >>> from tespy.components import (sink, source, combustion_chamber, ... compressor, turbine) >>> from tespy.connections import connection, ref, bus >>> from tespy.networks import load_network, network >>> import shutil >>> fluid_list = ['CH4', 'O2', 'N2', 'CO2', 'H2O', 'Ar'] >>> nw = network(fluids=fluid_list, p_unit='bar', T_unit='C', ... h_unit='kJ / kg', T_range=[250, 1300], iterinfo=False) >>> air = source('air') >>> f = source('fuel') >>> c = compressor('compressor') >>> comb = combustion_chamber('combustion') >>> t = turbine('turbine') >>> si = sink('sink') >>> inc = connection(air, 'out1', c, 'in1') >>> cc = connection(c, 'out1', comb, 'in1') >>> fc = connection(f, 'out1', comb, 'in2') >>> ct = connection(comb, 'out1', t, 'in1') >>> outg = connection(t, 'out1', si, 'in1') >>> nw.add_conns(inc, cc, fc, ct, outg) Specify component and connection properties. The intlet pressure at the compressor and the outlet pressure after the turbine are identical. For the compressor, the pressure ratio and isentropic efficiency are design parameters. A compressor map (efficiency vs. mass flow and pressure rise vs. mass flow) is selected for the compressor. Fuel is Methane. >>> c.set_attr(pr=10, eta_s=0.88, design=['eta_s', 'pr'], ... offdesign=['char_map']) >>> t.set_attr(eta_s=0.9, design=['eta_s'], ... offdesign=['eta_s_char', 'cone']) >>> inc.set_attr(fluid={'N2': 0.7556, 'O2': 0.2315, 'Ar': 0.0129, 'CH4': 0, ... 'H2O': 0}, fluid_balance=True, T=25, p=1) >>> fc.set_attr(fluid={'N2': 0, 'O2': 0, 'Ar': 0, 'CH4': 0.96, 'H2O': 0, ... 'CO2': 0.04}, T=25) >>> ct.set_attr(T=1100) >>> outg.set_attr(p=ref(inc, 1, 0)) The total power output is set to 1 MW, electrical or mechanical efficiencies are not considered in this example. The documentation example in class :func:`tespy.connections.bus` provides more information on efficiencies of generators, for instance. >>> power = bus('total power output', P=-1e6) >>> power.add_comps({'c': c}, {'c': t}) >>> nw.add_busses(power) >>> nw.solve('design') >>> nw.save('exported_nwk') >>> c.set_attr(igva='var') >>> nw.solve('offdesign', design_path='exported_nwk', ... init_path='exported_nwk') >>> round(t.eta_s.val, 1) 0.9 >>> power.set_attr(P=-0.75e6) >>> nw.solve('offdesign', design_path='exported_nwk', ... init_path='exported_nwk') >>> eta_s_t = round(t.eta_s.val, 3) >>> igva = round(c.igva.val, 3) >>> eta_s_t 0.898 >>> igva 20.139 The designed network is exported to the path 'exported_nwk'. Now import the network and recalculate. Check if the results match with the previous calculation in design and offdesign case. >>> imported_nwk = load_network('exported_nwk') >>> imported_nwk.set_attr(iterinfo=False) >>> imported_nwk.solve('design') >>> round(imported_nwk.imp_comps['turbine'].eta_s.val, 3) 0.9 >>> imported_nwk.imp_comps['compressor'].set_attr(igva='var') >>> imported_nwk.solve('offdesign', design_path='exported_nwk', ... init_path='exported_nwk') >>> round(imported_nwk.imp_comps['turbine'].eta_s.val, 3) 0.9 >>> imported_nwk.imp_busses['total power output'].set_attr(P=-0.75e6) >>> imported_nwk.solve('offdesign', design_path='exported_nwk', ... init_path='exported_nwk') >>> round(imported_nwk.imp_comps['turbine'].eta_s.val, 3) == eta_s_t True >>> round(imported_nwk.imp_comps['compressor'].igva.val, 3) == igva True >>> shutil.rmtree('./exported_nwk', ignore_errors=True) """ if path[-1] != '/' and path[-1] != '\\': path += '/' path_comps = modify_path_os(path + 'comps/') path = modify_path_os(path) msg = 'Reading network data from base path ' + path + '.' logging.info(msg) # load characteristics fn = path_comps + 'char_line.csv' try: char_lines = pd.read_csv(fn, sep=';', decimal='.', converters={ 'x': ast.literal_eval, 'y': ast.literal_eval }) msg = 'Reading characteristic lines data from ' + fn + '.' logging.debug(msg) except FileNotFoundError: char_lines = pd.DataFrame(columns=['id', 'type', 'x', 'y']) # load characteristic maps fn = path_comps + 'char_map.csv' try: msg = 'Reading characteristic maps data from ' + fn + '.' logging.debug(msg) char_maps = pd.read_csv(fn, sep=';', decimal='.', converters={ 'x': ast.literal_eval, 'y': ast.literal_eval, 'z1': ast.literal_eval, 'z2': ast.literal_eval }) except FileNotFoundError: char_maps = pd.DataFrame(columns=['id', 'type', 'x', 'y', 'z1', 'z2']) # load components comps = pd.DataFrame() files = os.listdir(path_comps) for f in files: if f != 'bus.csv' and f != 'char_line.csv' and f != 'char_map.csv': fn = path_comps + f df = pd.read_csv(fn, sep=';', decimal='.', converters={ 'design': ast.literal_eval, 'offdesign': ast.literal_eval, 'busses': ast.literal_eval, 'bus_param': ast.literal_eval, 'bus_P_ref': ast.literal_eval, 'bus_char': ast.literal_eval }) # create components df['instance'] = df.apply(construct_comps, axis=1, args=( char_lines, char_maps, )) comps = pd.concat((comps, df[[ 'instance', 'label', 'busses', 'bus_param', 'bus_P_ref', 'bus_char' ]]), axis=0) msg = 'Reading component data (' + f[:-4] + ') from ' + fn + '.' logging.debug(msg) comps = comps.set_index('label') msg = 'Created network components.' logging.info(msg) # create network nw = construct_network(path) # make components accessible by labels nw.imp_comps = comps.to_dict()['instance'] # load connections fn = path + 'conn.csv' conns = pd.read_csv(fn, sep=';', decimal='.', converters={ 'design': ast.literal_eval, 'offdesign': ast.literal_eval }) msg = 'Reading connection data from ' + fn + '.' logging.debug(msg) # create connections conns['instance'] = conns.apply(construct_conns, axis=1, args=( comps, nw, )) conns.apply(conns_set_ref, axis=1, args=(conns, )) conns = conns.set_index('id') nw.imp_conns = {} # add connections to network for c in conns['instance']: nw.add_conns(c) nw.imp_conns[c.s.label + ':' + c.s_id + '_' + c.t.label + ':' + c.t_id] = c msg = 'Created connections.' logging.info(msg) # load busses try: fn = path_comps + 'bus.csv' busses = pd.read_csv(fn, sep=';', decimal='.') msg = 'Reading bus data from ' + fn + '.' logging.debug(msg) except FileNotFoundError: busses = pd.DataFrame() msg = 'No bus data found!' logging.debug(msg) # create busses nw.imp_busses = {} if len(busses) > 0: busses['instance'] = busses.apply(construct_busses, axis=1) # add components to busses comps.apply(busses_add_comps, axis=1, args=( busses, char_lines, )) # add busses to network for b in busses['instance']: nw.add_busses(b) nw.imp_busses[b.label] = b msg = 'Created busses.' logging.info(msg) msg = 'Created network.' logging.info(msg) return nw