# bw2calc.monte_carlo#

## Module Contents#

### Classes#

 ComparativeMonteCarlo First draft approach at comparative LCA IterativeMonteCarlo Base class to use iterative techniques instead of LU factorization in Monte Carlo. MonteCarloLCA Normal LCA class now supports Monte Carlo and iterative use. You normally want to use it instead. MultiMonteCarlo This is a class for the efficient calculation of many demand vectors from ParallelMonteCarlo Split a Monte Carlo calculation into parallel jobs

### Functions#

 multi_worker(args) Calculate a single Monte Carlo iteration for many demands. single_worker(args)
class bw2calc.monte_carlo.ComparativeMonteCarlo(demands, *args, **kwargs)[source]#

First draft approach at comparative LCA

Create a new LCA calculation.

Parameters
• demand (*) β The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {7: 2.5}.

• method (*) β LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

class bw2calc.monte_carlo.IterativeMonteCarlo(*args, iter_solver=cgs, **kwargs)[source]#

Bases: MonteCarloLCA

Base class to use iterative techniques instead of LU factorization in Monte Carlo.

Create a new LCA calculation.

Parameters
• demand (*) β The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {7: 2.5}.

• method (*) β LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

solve_linear_system()[source]#

Master solution function for linear system $$Ax=B$$.

To most numerical analysts, matrix inversion is a sin.

βNicolas Higham, Accuracy and Stability of Numerical Algorithms, Society for Industrial and Applied Mathematics, Philadelphia, PA, USA, 2002, p. 260.

We use UMFpack, which is a very fast solver for sparse matrices.

If the technosphere matrix has already been factorized, then the decomposed technosphere (self.solver) is reused. Otherwise the calculation is redone completely.

class bw2calc.monte_carlo.MonteCarloLCA(*args, **kwargs)[source]#

Normal LCA class now supports Monte Carlo and iterative use. You normally want to use it instead.

Create a new LCA calculation.

Parameters
• demand (*) β The demand or functional unit. Needs to be a dictionary to indicate amounts, e.g. {7: 2.5}.

• method (*) β LCIA Method tuple, e.g. ("My", "great", "LCIA", "method"). Can be omitted if only interested in calculating the life cycle inventory.

Returns

A new LCA object

class bw2calc.monte_carlo.MultiMonteCarlo(demands, method=None, data_objs=None, iterations=100, cpus=None)[source]#

This is a class for the efficient calculation of many demand vectors from each Monte Carlo iteration.

Parameters
• dictionaries (* args is a list of demand) β

• method (* method is a LCIA) β

• desired (* iterations is the number of Monte Carlo iterations) β

• the (* cpus is) β

The input list can have complex demands, so [{('foo', 'bar'): 1, ('foo', 'baz'): 1}, {('foo', 'another'): 1}] is OK.

Call .calculate() to generate results.

calculate(worker=multi_worker)[source]#

Calculate Monte Carlo results for many demand vectors.

Returns a list of results with the format:

[(demand dictionary, [lca scores])]


There is no guarantee that the results are returned in the same order as the demand input variable.

merge_results(objs)[source]#

Merge the results from each multi_worker worker.

[('a', [0,1]), ('a', [2,3])] becomes [('a', [0,1,2,3)].

class bw2calc.monte_carlo.ParallelMonteCarlo(demand, method=None, data_objs=None, iterations=1000, chunk_size=None, cpus=None, log_config=None)[source]#

Split a Monte Carlo calculation into parallel jobs

calculate(worker=single_worker)[source]#
bw2calc.monte_carlo.direct_solving_worker(args)[source]#
bw2calc.monte_carlo.multi_worker(args)[source]#

Calculate a single Monte Carlo iteration for many demands.

args are in order:
• project: Name of project

• demands: List of demand dictionaries

• method: LCIA method

Returns a list of results: [(demand dictionary, result)]

bw2calc.monte_carlo.single_worker(args)[source]#