## tools.DemandAggregationTool


The demand Aggregation tool from MIKE+ water distribution.


Usage

``` python
tools.DemandAggregationTool(database)
```


Aggregates demand allocations (`mw_DemAlloc`) to junction demands (`mw_MDemand`) or to pipe demand coefficients (`mw_Pipe.Coeff1` to `Coeff4`). Rows the tool writes to `mw_MDemand` have `GenerateTypeNo` 3 (allocation); `reset_existing` deletes only those rows, never demands entered any other way.

Every method takes an optional list of demand allocation MUIDs to aggregate. Leave it out to aggregate all demand allocations; an empty list does nothing, not even `reset_existing`.


## Examples

``` python
>>> from mikeplus import Database
>>> from mikeplus.tools import DemandAggregationTool
>>> db = Database("path/to/model.sqlite")
>>> tool = DemandAggregationTool(db)
>>> tool.aggregate_to_node_demands(reset_existing=True)
>>> tool.aggregate_to_pipe_coefficients(["Alloc_1", "Alloc_2"], coefficient=2)
>>> db.close()
```


## Methods

| Name | Description |
|----|----|
| [__init__()](#__init__) | Initialize the DemandAggregationTool with the given Database. |
| [aggregate_to_node_demands()](#aggregate_to_node_demands) | Sum the demand allocations of each junction into junction demands. |
| [aggregate_to_pipe_coefficients()](#aggregate_to_pipe_coefficients) | Sum the demand allocations of each pipe into a pipe demand coefficient. |
| [assign_to_multiple_demands()](#assign_to_multiple_demands) | Copy each demand allocation to its junction as a separate demand. |

------------------------------------------------------------------------


#### \_\_init\_\_()


Initialize the DemandAggregationTool with the given Database.


Usage

``` python
__init__(database)
```


##### Parameters


`database: Database`  
A Database object for the MIKE+ model.


------------------------------------------------------------------------


#### aggregate_to_node_demands()


Sum the demand allocations of each junction into junction demands.


Usage

``` python
aggregate_to_node_demands(
    demand_allocations=None,
    *,
    reset_existing=False,
    keep_categories=True,
    category=None,
    pattern=None
)
```


As in MIKE+, a sum with any allocation whose `Demand` is NULL is NULL.


##### Parameters


`demand_allocations: Sequence[str] = None`  
MUIDs of the demand allocations to aggregate. By default all; an empty list does nothing. Allocations without a junction are skipped.

`reset_existing: bool = ``False`  
If true, first delete the junction demands that earlier aggregations wrote. By default False.

`keep_categories: bool = ``True`  
If true, write one demand per junction for each combination of demand category and pattern found in its allocations. If false, write a single demand per junction with `category` and `pattern`. By default True.

`category: str = None`  
Demand category for the aggregated demands. Only allowed when `keep_categories` is false.

`pattern: str = None`  
Demand pattern for the aggregated demands. Only allowed when `keep_categories` is false.


##### Raises


`TypeError`  
If `demand_allocations` is a str or contains anything but str.

`ValueError`  
If `category` or `pattern` is given while `keep_categories` is true, or if `demand_allocations` contains MUIDs not in `mw_DemAlloc`.


------------------------------------------------------------------------


#### aggregate_to_pipe_coefficients()


Sum the demand allocations of each pipe into a pipe demand coefficient.


Usage

``` python
aggregate_to_pipe_coefficients(
    demand_allocations=None, *, coefficient=1, reset_existing=False
)
```


As in MIKE+, a sum with any allocation whose `Demand` is NULL is NULL.


##### Parameters


`demand_allocations: Sequence[str] = None`  
MUIDs of the demand allocations to aggregate. By default all; an empty list does nothing. Allocations without a pipe are skipped.

`coefficient: int = ``1`  
Which pipe demand coefficient to write, 1 to 4 for `Coeff1` to `Coeff4`. By default 1.

`reset_existing: bool = ``False`  
If true, set the coefficient to 0 on pipes with no aggregated allocation. If false, leave those pipes unchanged. By default False.


##### Raises


`TypeError`  
If `demand_allocations` is a str or contains anything but str.

`ValueError`  
If `coefficient` is not 1, 2, 3 or 4, or if `demand_allocations` contains MUIDs not in `mw_DemAlloc`.


------------------------------------------------------------------------


#### assign_to_multiple_demands()


Copy each demand allocation to its junction as a separate demand.


Usage

``` python
assign_to_multiple_demands(demand_allocations=None, *, reset_existing=False)
```


Unlike [aggregate_to_node_demands](tools.DemandAggregationTool.md#mikeplus.tools.DemandAggregationTool.aggregate_to_node_demands), nothing is summed: every allocation becomes one junction demand, keeping its demand, category, pattern, and meter.


##### Parameters


`demand_allocations: Sequence[str] = None`  
MUIDs of the demand allocations to assign. By default all; an empty list does nothing. Allocations without a junction are skipped.

`reset_existing: bool = ``False`  
If true, first delete the junction demands that earlier aggregations wrote. By default False.


##### Raises


`TypeError`  
If `demand_allocations` is a str or contains anything but str.

`ValueError`  
If `demand_allocations` contains MUIDs not in `mw_DemAlloc`.
