# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['blackblox']

package_data = \
{'': ['*']}

install_requires = \
['graphviz>=0.16,<0.17',
 'matplotlib>=3.4,<4.0',
 'molmass>=2020.6.10,<2021.0.0',
 'numpy>=1.20,<2.0',
 'openpyxl>=3.0.7,<4.0.0',
 'pandas>=1.2,<2.0',
 'seaborn>=0.11.1,<0.12.0',
 'xlrd>=2.0.1,<3.0.0']

setup_kwargs = {
    'name': 'blackblox',
    'version': '0.2.2',
    'description': 'Simple simulator for black box process models',
    'long_description': '# BlackBlox\nBlackBlox is a calculator for "black box" systems, ranging from single unit processes to factories with branching chains of processes.\n\n## Installation and development instructions\n\n### Installing and using BlackBlox\n\n1. (Optional) Install the only non-Python dependency: *Graphviz*\n   + By following the instructions on the [official website](https://graphviz.org/download/)\n   + Important is that the `dot` executable should be in your `PATH`\n2. Do the normal installation of the library via `pip`:\n   + `pip install blackblox`\n\n#### Usage and examples\n\n1. Look around the configuration options, see what they mean, and whether the defaults seems acceptable to you\n   + In `blackblox.dataconfig_format` you find what are the names and meanings of the options\n   + In `blackblox.dataconfig_default` you see what are the default values\n2. Look at the demonstration scenario under `scenarios-examples/demo/demo.py` for inspiration\n   + Here you can learn how to change configuration options\n   + As well as how to create and balance unit processes, chains, factories, etc.\n\n### Contributing to BlackBlox development and releasing to PyPI\n\n1. This project uses `poetry` as its dependency management, virtualenv management and release (build) tool\n   + Install following the steps described in https://python-poetry.org/docs/#installation\n\n2. The API docs and explanations about all data needed to run scenarios are in the `docs` directory\n   + We use `Sphinx` for building docs. Poetry also ensures that dev dependencies (Sphinx etc.) are installed\n   + Run the make command from Sphinx through `poetry run`, so that Sphinx is found from the project\'s virtualenv\n     (managed by Poetry)\n     - `cd docs; poetry run make html`\n   + The generated pages will be under `docs/_build/html`\n\n3. Building a (new) release and publishing it to PyPI:\n   1. Make an account on `https://pypi.org`. Ask (optional) for invitation to become project contributor on PyPI.\n   2. Add API token on the "account settings" page of PyPI (global scope for now)\n   3. Register the API token to be the one used by Poetry: `poetry config pypi-token.pypi "<your_api_token>"`\n   4. Do the actual contribution to the project 🙂\n   5. Run `poetry update` to get possible dependecy updates, and commit the updated `poetry.lock` file\n   6. Increment the package\'s version number in `pyproject.toml`\n   7. Build the package (wheel and source): `poetry build`. The built artifacts will be placed in the `dist` folder\n   8. Publish to PyPI: `poetry publish`\n\n\n# What is blackblox.py?\nBlackblox is an open source python3 library for constructing and solving "black box" process models. Its particular strength is allowing for rapid comparison of different scenarios of parameters. Input data can be stored in Excel workbooks or tab or comma delimited text files. Results are returned as dictionaries for use within python and written to Excel workbooks. Results can also be displayed on the terminal console. Blackblox\'s basic functionality can be run from the terminal without needing to write any Python script.\n\nThe basic "block" of blackblox is the **UnitProcess**, which contains the relationship between the inflows and outflows of a process or subprocess under evaluation. This block can be connected with others to form a linear **ProcessChain**. Units and chains can be further connected in a linear or branching fashion to form a **Factory** model.\n\nFor each level of model, two main functions are available:\n\n- `balance()`: which evaluates the model based on one scenario, given a quantity and a specific flow to balance on.\n- `run_scenarios()`: which evaluates the model on multiple scenarios, returning comparative results in a single table.\n\nEach of these return the results as dictionaries or Pandas DataFrames, which can be used for further process within Python. Results can be written to the console or saved to an Excel file. \n\nAdditionally, the **Factory**-level model has support for single-variable sensitivity analysis.\n\n# Unit Processes\nUnit processes are the smallest "block" in blackblox. Each unit process has a set of inflows and outflows and a set of specified fixed-ratio (linear) relationships between the process flows. Then, given a quantity for one inflow or outflow, the quantities of the remaining inflows and outflows can be calculated. \n\n![Example Unit Process](docs/source/doc_assets/unit.drawio.svg)\n\nA unit process is defined by two tables, a **calculations table** that specifies relatioships between flows, and a **variables table** that specifies the numerical values for the variable parameters used in the calculations. Extra data about unit process flows can be stored in **lookup tables**. All unit processes need to be listed in a **unit library** table, which tells blackblox where to find them.\n\nFigure: Representation of a unit process\n\n## Calculations Tables\nThe calculation tables specifies the relationships between flows in a unit process. Each row in the table:\n\n- names two flows that have a relationship\n- designates whether each flow is an inflow, outflow, or a flow internal to the process\n- specifies the type of calculation that would generate the quantity of the second substance if the quantity of the first substance is known\n- specifies the variable parameter (if any) used in that calculation that is the same name as a column in the variables table.\n\nTable 1 provides an example of a calculation table for a simplified cement kiln. \n- **KnownQty** (k) and **UnknownQty** (v) contain the two substances with a known relationship.\n- **k_QtyFrom** and **u_QtyTo** specify the location of each flow.\n  - **inflow** or **i**: inflow of unit process. Each inflow must be uniquely named and represent a unique calculation.  \n  - **outflow** or **o**: outflow of unit process. Each outflow must be uniquely named\n  - **tmp** or **t**: a process-internal dictionary. This can be used for intermediate calculations and does not appear in inflows or outflows.\n  - **emission** or **e**: outflow of unit process, which does not have to be unique. E.g. In our example table, both the calcination of limestone and the combustion of fuel will generate CO~2~. By specifying CO~2~ as the *u_QtyTo* location, the CO~2~ from both calculations can be added together. Emission flows cannot be used to balance the unit process.\n  - **coinflow** or **c**: inflow of unit process, which does not have to be unique. Coinflows cannot be used to balance the unit process.\n- **Calculation** specifies the type of calculation performed between the two flows. The calculation types must be those available in the program\'s calculator library, some of which are described in Table 2.  \n- **Variable** (v) species the column in the Variables Table where to find the value of the variable parameter used in the calculation. It is also possible to define substance names that are "lookup variables" that allow the substance to be defined in the variables table and also have properties defined elsewhere. By default in blackblox, the flow name "fuel" is designated as a lookup variable, corresponding to the "fuelType" column in the Variables Table.\n\nBy default, flow names that begin or end with "energy", "heat", or "electricity" are assumed to be energy flows. All other flows are assumed to be mass flows.\n\nTable: Example of blackblox calculation table for a clinker kiln\n\n| KnownQty   | k_QtyFrom | UnknownQty  | u_QtyTo | Calculation  | Variable    |\n|------------|-----------|-------------|---------|--------------|-------------|\n| clinker    | outflow    | CaO         | tmp     | Ratio        | CaO_in_Clinker |\n| CaO        | tmp       | CaCO3       | inflow     | MolMassRatio | 1        |\n| CaCO3      | inflow       | clay        | inflow   | Remainder        | CaCO3_in_Meal  |\n| CaCO3      | tmp       | CO2       | emission  | MolMassRatio |         |\n| clinker    | outflow    | fuel  | tmp     | inflow        | fuelDemand  |\n| fuelDemand | inflow       | energy_from_fuel     | outflow   | Combustion   | combustEff  |\n| clinker    | outflow    | electricity | inflow   | Ratio        | elecDemand  |\n\nTable: Calculation Types. (k = KnownQty, U = UnknownQty, V = variable)\n\n| Calculation Name | Variable Parameter Type (v) | Relationship (k:u) | Description |\n|------------------|-----------------------|--|-------------|\n| Ratio | float | k * var = u | Multiples the known flow quantity by the variable parameter |\n| Remainder | float | k * (1 - var) = u | Multiples the known flow quantity by (1 - the variable parameter). Only works if the variable parameter is less than one. |\n| Returnvalue | float | k = u | Returns the known flow quantity. |\n| MolMassRatio | float or none | (Molar Mass of U / Molar Mass of K) * v = U | Multiples the known flow quantity by the molar mass ratio of the unknown flow to the known flow. Requires both flows names to be valid chemical formulas. If no value for the variable is provided, it defaults to 1. | \n| Combustion | float | k (mass) * (MJ/kg of K) * v = u (energy) OR k (energy) / MJ / (1-v) = u (mass) | Using data from a separate "fuels" table, generates the energy provided by combusting a specified amount of fuel, or the fuel required to generate a specified amount of energy, as well as any specified emissions from the combustion. The variable parameter is the combustion efficiency, with the waste heat retuned as a separate flow. |\n|  |  | e.g. [k OR u] (energy) * (1-v) = wast heat (as emission) | Combustion also adds the heat remaining after the combustion efficiency is applied as "waste heat" to the dictionary  |\n|  |  | e.g. [k OR u] (mass) * (kg CO~2~/kg fuel) = CO~2~ (as emission) | Combustion can also look up emission factors from a separate "fuels" table (here shown for CO2, but any emission desired can be added) |\n| Addition | none | k + k2 = u | Adds two known flow quantities together. Requires two columns to be added to the calculations table: `2nd Known Substance`, `2Qty Origin`  |\n| Subtraction | none | k - k2 = u | Adds two known flow quantities together. Requires two columns to be added to the calculations table: `2nd Known Substance`, `2Qty Origin`  |\n\n\n## Variables Tables \nThe **variables table** provides the values of the variable parameters specified in the calculations table. There is one column for each of the variables named in the **calculation table**. The column name must be exactly the same as the specified variable name. Each row is a set of variable parameter values, identified by a **scenario** name.\n\nSeparating the values into their own table allows for the same unit process to be easily evaluated for multiple scenarios of parameter values (e.g. different production efficiencies or fuel types). (note: rows whose scenarios begin with "meta" are ignored by blackblax and can be used for notes. This is also true for columns whose name begins with "meta".)\n\nTable: Example of a blackblox variables table for a hypothetical clinker kiln.\n\n| scenario   | fuelDemand      | fuelType | CaO_in_Clinker | CaCO3_in_Meal | combustEff | elecDemand     |\n|------------|-----------------|----------|-------------|------------|------------|----------------|\n| meta-units | (mj /t clinker) | name     | (t/t)       | (t/t)      | (%)        | (mj/t clinker) |\n| default    | 3             | coal     | 0.65       | 0.8        | 1          | 0.1            |\n| EU-old | 3.6            | coal     | 0.75        | 0.8        | 1          | 0.2            |\n| EU-bat_bio | 3          | charcoal | 0.65        | 0.8        | 1          | 0.1            |\n| EU-typical | 3.2        | coal     | 0.67        | 0.8        | 1          | 0.1            |\n\n\n## Lookup tables\n\nBlackblox.py has the ability to get data about flows stored in other spreadsheets. By default, a "fuels" table is a lookup table that is recognized by the combustion calculation to get information about energy content and emission factors.\n\nTable: Example Fuels Table\n\n| fuel type | LHV | CO2__fossil | CO2__bio | meta-source |\n|---|---|---|---|---|\n| meta-units | (GJ/dry tonne) | (t/t combusted) | (t/t combusted) |  |\n| heavy fuel oil | 40.4 | 3.127 | 0 | IPCC emission factor database |\n| coal | 25.8 | 2.4794 | 0 | IPCC emission factor database |\n| natural gas | 48 | 2.6928 | 0 | IPCC emission factor database |\n| charcoal | 29.5 | 0 | 3.304 | IPCC emission factor database |\n| coke | 28.2 | 3.0174 | 0 | IPCC emission factor database |\n\n## Unit  Library\nThe unit library is a table that lets blackblox know what unit processes exist and how to identify them. It requires the columns:\n\n- **id**: a unique identifier for the unit\n- **display name**: what the unit is called in the resulting output\n- **product**: the default product of the unit. This is the flow the unit will be balanced on if no other is specified\n- **productType**: ("inflow" or "outflow"). Whether the default product is an inflow or outflow of the unit process.\n\nTable: Example Unit Library\n\n| id | display name | product | productType |\n|---|---|---|---|\n| clinker_kiln | kiln | clinker | outflow |\n| cement_blender | blender | cement | outflow |\n| meal_mixer | mixer | meal | outflow |\n| electricity | electricity generation | electricity | outflow |\n| gas_scrubber | SCR flue gas cleaning | flue gas | inflow |\n\n## Storing Data\n\nBy default, blacblox looks for data in a `data/` subfolder of the working directory. Data can be in tab or comma delimited text files (.txt, .tsv, .csv) or in the sheets of an Excel workbook (.xls, .xlsx). If Excel workbooks are used, variable tables and calculation tables must be in separate workbooks.\n\nThe data folder needs to contain the unit process library, `unitlibrary.xlsx` (or .csv, .tsv, .txt) and any lookup tables, e.g. `fuels.csv`.\n\nBlackblox will look for unit process data in the `data\\` folder and its immediate subfolder. It identifies files that contain variable or calculation tables by the use of a filename prefix. By default, unit variable data files need to begin with `var_` (e.g. `var_cement.xls) and calculation data files need to begin with `calc_` (e.g. `calc_cement.txt`). \n\nIf using delimited text files to store unit process data, the file name after the prefix should be the same as the unit id listed in the Unit Library. For example,  a process with the id `clinker_kiln` would require the files `var_clinker_kiln.csv` and `calc_clinker_kiln.csv`.\n\nIf using excel files to store unit process data, the excel file must begin with the file identifiers (e.g. `var_units.xlsx` and `calc_units.xlsx`) and the sheet names of each file must align with the unit ids from the unit library (e.g. `clinker_kiln` should appear as a sheet in each `var_units.xlsx` and `calc_units.xlsx` with the relevant variable table and calculation table, respectively).\n\nSo a valid file structure for blackblox could look something like this:\n\n```\nproject/\n├─ config.yaml\n├─ data/\n│  ├─ unitlibrary.csv\n│  ├─ fuels.csv\n│  ├─ units/\n│  │  ├─ var_units.xlsx\n│  │  ├─ calc_units.xlsx\n│  │  ├─ var_unitA.csv\n│  │  ├─ calc_unitA.csv\n```\n\n\n## Creating and balancing a unit processes  \n\nBalancing a unit process calculates the quantity of all inflows and outflows of that unit process for a given set of variable parameters. To balance a unit process, the following information is needed: \n\n- the quantity of one inflow or outflow.  (if not specified, defaults to 1.0)\n- the name of that inflow or outflow substance (if not specified, defaults to the product listed in the unit library)\n- whether the substance is an inflow or outflow (if not specified, defaults to the productType listed in the unit library)\n- the name of the scenario to use from the variables table (if not specified, defaults to `default`.)\n  \nAfter calculating all flows based on the user input, blackblox then checks for whether the mass and energy flows are balanced, and either raises an exception or, by default, creates an \'UKNOWN\' balancer flow where needed. When processing combustion-type calculations, blackblox writes an "energy in combusted `fuelType`" flow to the inflows dictionary, to balance the energy of combustion. A similar balancer flow is provided for the combustion emissions. \n\n\n## Balancing a unit process in python\n\nUnit Processes need to be created and then can be balanced. In python, the unit process can be created via: \n\n``blackblox.unitprocess.UnitProcess(unit_id)``\n\nSo, to create and balance the clinker kiln of our example, you could create it using\n\n``kiln = blackblox.unitprocess.UnitProcess(clinker_kiln)``\n\nand then balance it with \n\n``kiln.balance(scenario=\'EU-typical\', qty=100)``\n\nThe results are returned as a dictionary and can be displayed in the console using `write_to_console=True` or written to an excel sheet using `write_to_xls=True`.\n\nTo compare multiple scenarios, the function `run_scenarios` can be used. By default, the results are written to an Excel file.\n\n``kiln.run_scenarios(scenario_list=[\'EU-typical\', \'EU-old\', \'EU-bat_bio\'])``\n\n# Process Chain\nA process chain is a linear set of connected unit processes, where an outflow of a preceding unit process is an inflow of the following unit process.   \n\n![Example of a process chain](docs/source/doc_assets/chain_pfd.png)\n\n\n## Defining a chain\nA process chain is defined by a table with a list of unit processes with an inflow and outflow to each, where the outflow of a unit process must be the inflow into the next unit process, as shown in the below table. \n\nTable:  Example chain table \n\n| Inflow  | Process_ID | Outflow |\n|---------|---------|---------|\n| CaCO3   | mixer   | meal    |\n| meal    | kiln    | clinker |\n| clinker | blender | cement  |\n\nWhen the process chain is first used, an initialisation process creates each of the unit processes, if they do not already exist, and verifies that the inflows and outflows specified in the chain table exist for the corresponding unit processes. \n\nA process chain can be defined by specifying the location of the chain table either in an Excel workbook or delimited text file using\n\n```\nchainName = processchain.ProcessChain(chain_data=\'path/to/chaindata.xlsx, \n                                       xls_sheet=\'sheet name)\n```\n\n(`xls_sheet` is only required if using an Excel file with multiple sheets)\n\n## Balancing a chain \n\nBalancing a chain calculates the quantity of all inflows and outflows of each unit process in the chain, either from first inflow to last outflow or from last outflow to first inflow. To balance a chain, the following arguments  must be provided: \n  * `qty`: the quantity of one inflow or outflow somewhere in the chain\n  * `product`: the name of that inflow or outflow substance. (Attempts to default to product of last unit process, if an outflow. If not, will attempt to default to the product of the first unit process, if an inflow. )\n  * `i_o`: whether the product is an inflow or outflow (defaults to the default product location)\n  * `scenario` the name of the configuration scenario to use from the variables table. (defaults to "default")\n  * the name of the unit process in the chain, if the specified flow is not an inflow into the first process or an outflow of the last process.\n  \n All arguments besides the quantity can be optional, if a default product can be determined.\n \n Balancing a chain returns a dictionary with both the calculated inflows and outflows for each unit process, as well as the the overall inflows and outflows. The results can also be written to the console or an excel file.\n\n## Generating a chain diagram \nAfter a chain has been defined, a process flow diagram of the chain can be generated automatically using \n\n`processName.diagram()`\n\nFf Graphviz has been correctly installed to your computer\'s path. If not, this will fail with a hopefully useful error message.\n\n![This ProcessChain diagram was autogenerated](docs/source/doc_assets/chain_pfd.png)\n\n# Factory\nA factory is a collection of one or more connected process chains, where the inflow of outflow of any unit process within any chain can be the inflow or outflow of any other chain. A factory has a single main chain, and zero or more auxiliary chains. By specifying an input or output quantity to the main chain, it is possible to calculate the inflows and outflows of all processes within the chain.\n\n## Defining a factory\nA factory is defined by two tables: one that lists the chains (and their file locations) and another that lists the connections between the chains. Factory data can be in separate tab delimited text files or in a single excel workbook.\n\n### Factory chains table\nThis table specifies the location of the process chain data used in the factory, as well as the  primary product and whether that product is an inflow or outflow of the chain.\n\nThe first chain of the chain list is assumed to be the main product chain, but otherwise it doesn\'t matter.\nThe chains can either be in the same file, if an excel workbook, or in a separate file.\n\nTable: Example of a Factory Chains Table\n\n| ChainName  | ChainProduct | Product_IO | ChainFile | ChainSheet   |\n|------------|--------------|------------|-----------|--------------|\n| cement     | cement       | outflow    | here      | Cement Chain |\n| CO2capture | CO2          | inflow     | here      | CO2 Capture  |\n| power      | electricity  | outflow    | here      | Power Chain  |\n \n### Factory connections table\nThe factory connections table specifies how the chains connect, including the origin process chain and unit process, the destination chain, the connecting product, and whether the product is an inflow or outflow of the origin and destination.\nThe connections table is order dependent, and the first chain should always be the main chain. \n\nTable: Example of a Factory Connections Table\n\n| OriginChain | OriginProcess | Product     | Product_IO_of_Origin | Product_IO_of_Destination | DestinationChain |\n|-------------|---------------|-------------|----------------------|---------------------------|------------------|\n| cement      | kiln          | CO2         | outflow              | inflow                    | CO2capture       |\n| cement      | all           | electricity | inflow               | outflow                   | power            |\n| CO2capture  | all           | electricity | inflow               | outflow                   | power            |\n\n\nIf the destination is not the start or end of a chain, an optional `destination process` column can be used. This column is also used when specifying recycling connections. Currently two types of recycling are usable:\n\n- *1-to-1 replacement*: allows for a recycle flow to replace a flow in another unit process, given that is a 1-to-1 correspondent and does not affect other flows in unit process.\n- *energy replacing fuel*: allows for recycled energy to replace energy generated from a fuel in a unit process that has a combustion calculation. The emission outflows (e.g. CO~2~) and inflow of oxygen are also recalculated.\n\nIf there is a greater quantity of the recycled flow than is used in the destination process, the remainder is treated as a normal output.  A single flow can be recycled into multiple units, by specifying each connection as a new line in the table.  If there is insufficient recycling flow to fully replace the existing flow, it is only partially replaced.\n\n## Balancing a factory\nBy default, the factory is balanced on a quantity of the product of the main ProductChain, and uses a single scenario of variables for the whole factory. `factory.balance()` takes the same parameters as `productchain.balance()`. Additionally, the factory can be balanced in other products from the main chain by passing `product`, `product_unit`, `product_io` parameters. A factory cannot be balanced on auxiliary chains.\n\nBalancing a factory returns a nested dictionary with the inflows and outflows of the factory, every chain, and every unit process. By default, the results are saved to an Excel workbook with sheets for:\n\n  - Total factory inflows and outflows\n  - A matrix for all inflow from every unit process\n  - A matrix for all outflows from every unit process\n  - A list of all internal flows/connections, both within chains and\n  - Inflows for each chain unit process and chain totals (per chain)\n  - Outflows for each chain unit process and chain totals (per chain)\n\nBy default, the data is divided into mass and energy flows, if energy flow name signifiers are provided, with totals for both mass and energy flows.\n\n## Generating a factory diagram \nAfter a factory has been defined, a process flow diagram of the factory can be generated. However, due to the limitations of the diagram rendering software, they are unlikely to be pretty. Graphviz must be properly installed for this to work.\n  \n![Example of an auto-generated factory digram](docs/source/doc_assets/factory_pfd.png)\n\n## Sensitivity Analyses\nA single-variable sensitivity analysis can also be run on a Factory model.  The `factory.run_sensitivity()` function takes all the same parameters as `factory.balance()` as well as:\n\n- `unit_name`: (str) the identifier of the **UnitProcess** containing the variable of interest\n- `chain_name`: (str) the **ProcessChain** containing the unit of interest\n- `variable`: (str) the variable of interest, which should correspond to a column in the unit\'s **Variable Table**\n- `variable_options`: a list of the variables options to be calculated.\n\nThe sensitivity analysis returns an Excel workbook with results for each variable option in columns.\n\n# Where to Find blackblox.py\nBlackblox.py is an free and open source library released under the GNU General Public License v3 (GPLv3). Blackblox is under active development. Currently available features and documentation may be different than what is provided in this Appendix.\n\n- Full documentation for blackblox can be found at the project homepage: https://concoctions.org/blackblox/\n- Source code and data are available on GitHub: https://github.com/concoctions/BlackBlox\n- Blackblox can be installed using `pip install blackblox`. Information about the package can be found at PyPi: https://pypi.org/project/blackblox/\n',
    'author': 'S.E. Tanzer',
    'author_email': 'S.E.Tanzer@tudelft.nl',
    'maintainer': None,
    'maintainer_email': None,
    'url': 'https://concoctions.org/blackblox/',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
    'python_requires': '>=3.8,<4.0',
}


setup(**setup_kwargs)
