Commit bf535a33 authored by Blais, Chris's avatar Blais, Chris
Browse files

cleaning up redundant folders

parent e0700bc4
Loading
Loading
Loading
Loading

documenting_failures/__init__.py

deleted100644 → 0
+0 −0

Empty file deleted.

+0 −136
Original line number Diff line number Diff line

import os
import sys
import pickle
import random
import traceback
import yaml
import numpy as np
import copy

# soar_path = os.path.join(r"C:\Users\tjf\Documents\01_gitlab_repos\soar\python")
# if soar_path not in sys.path:
#     sys.path.insert(0, soar_path)
# else:
#     print("soar toolbox already in path")

# add 
module_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if module_path not in sys.path:
    sys.path.insert(0, module_path)
else:
    print("path already in sys.path")

from documenting_failures.setup_example_soar import define_system, get_pareto_front, get_one_result

# for plotting
import matplotlib.pyplot as plt

# compare to soar
from documenting_failures.test_case_nonlinear import constr_ccro_nl, constr_pilot_nl, labelConstraint
from numerical_labelling.labelling import jacsym

def build_test_case_nl(ptype=1, system = "ccro"):
    """
    ptype: problem type
    """
    match system:
        case "ccro":
            constr_nl, varbs_nl, coeff_nl = constr_ccro_nl()
        case "pilot":
            constr_nl, varbs_nl, coeff_nl = constr_pilot_nl()
    # by default, we have all of the columns for the jacobian.
    match ptype:
        # all columns included, all constraints
        case 1:
            # use all columns, all rows.
            varbs_active = {**varbs_nl, **coeff_nl}
            constr_active = constr_nl
        case 2:
            # use all columns
            varbs_active = {**varbs_nl, **coeff_nl}

            constr_active = {}
            cindx = 0
            # exclude any nonlinear constraints
            for iconstr, constr in constr_nl.items():
                if not constr.type.startswith("nl_"):
                    constr_active[cindx] = constr
                    cindx += 1

        case 3: 
            # use all columns
            varbs_active = {**varbs_nl, **coeff_nl}

            constr_active = {}
            cindx = 0
            # exclude any nonlinear constraints
            for iconstr, constr in constr_nl.items():
                if not constr.type == "nl_pressure":
                    constr_active[cindx] = constr
                    cindx += 1
        case 4: 
            # use all columns
            varbs_active = {**varbs_nl, **coeff_nl}

            constr_active = {}
            cindx = 0
            # exclude any nonlinear constraints
            for iconstr, constr in constr_nl.items():
                if not constr.type == "nl_component":
                    constr_active[cindx] = constr
                    cindx += 1
        case 5: 
            # use all columns
            varbs_active = {**varbs_nl, **coeff_nl}

            # use all constraints 
            constr_active = constr_nl

        case _:
            # use all columns, all rows.
            varbs_active = {**varbs_nl, **coeff_nl}
            constr_active = constr_nl

    constr_active_sy = {}
    for iconstr, constr in constr_active.items():
        if isinstance(constr, labelConstraint):
            constr_active_sy[iconstr] = constr.add_expr
        else: 
            constr_active_sy[iconstr] = constr

    jac_nl = jacsym(constr_active_sy.values(), varbs_active.values())

    return constr_nl, varbs_nl, coeff_nl, jac_nl

def make_layout(bl_layout, varbs_nl, ptype=1):
    """
    bl_layout: bilinear layout
    varbs_active: active variables (e.g. mass, pressure, coefficients, etc)
    type: test to be performed
    """
    # get number of pressure variables
    nmass = sum(1 for varb in varbs_nl if varb.lower().startswith('m'))
    nconc = sum(1 for varb in varbs_nl if varb.lower().startswith('x'))
    npress = sum(1 for varb in varbs_nl if varb.lower().startswith('p'))
    ncoeff = len(varbs_nl) - nmass - nconc - npress
    match ptype: 
        case 1:
            layout = bl_layout + [False]*npress + [False]*ncoeff
        case 2: 
            layout = bl_layout + [False]*npress + [False]*ncoeff
        case 3: 
            layout = bl_layout + [False]*npress + [True]*ncoeff
        case 4: 
            layout = bl_layout + [False]*npress + [True]*ncoeff
        case 5: 
            layout = bl_layout + [False]*npress + [True]*ncoeff
        case _:
            layout = bl_layout + [False]*npress + [False]*ncoeff

    return layout
            



+0 −405
Original line number Diff line number Diff line
%% Cell type:code id:c8f1b999 tags:

``` python
import sympy as sy
import pandas as pd
import os
import scipy as sp
import numpy as np
import math
import itertools
import sys
module_path = os.path.join(os.path.dirname(os.path.abspath('')))
if module_path not in sys.path:
    sys.path.insert(0, module_path)
else:
    print("path already in sys.path")

import matplotlib.pyplot as plt
from numerical_labelling.labelling import *
```

%% Output

    path already in sys.path

%% Cell type:markdown id:8e4f0a26 tags:

## Linear case
<img src="../fig/ccro_layout.png" width="50%">

%% Cell type:code id:a87b952f tags:

``` python
# generate constraints
def constr_mem_lin():
    """
    constraint equations for membrane example
    """

    m1, m2, m3, m4, m5, m6, m7, m8 = sy.symbols('m1 m2 m3 m4 m5 m6 m7 m8') # overall mass flow rates
    varbs = [
        m1, m2, m3, m4, m5, m6, m7, m8
        ]
    var_labels = [
        'M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8'
        ]

    # Create a dictionary to store all variables and their values
    var_dict = dict(zip(var_labels, varbs))

    constr = {}
    # mass balance
    constr[0] = m1 - m2
    constr[1] = m2 - m3 + m8
    constr[2] = m3 - m4 - m5
    constr[3] = m5 - m6 - m7
    constr[4] = m7 - m8

    return constr, var_dict
```

%% Cell type:code id:92ba160f tags:

``` python
constr_lin, varbs_lin = constr_mem_lin()
```

%% Cell type:code id:8f31dfa5 tags:

``` python
jac_lin = jacsym(constr_lin.values(), varbs_lin.values())
```

%% Cell type:code id:307d5feb tags:

``` python
jac_lin
```

%% Output

    $\displaystyle \left[\begin{matrix}1 & -1 & 0 & 0 & 0 & 0 & 0 & 0\\0 & 1 & -1 & 0 & 0 & 0 & 0 & 1\\0 & 0 & 1 & -1 & -1 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 1 & -1 & -1 & 0\\0 & 0 & 0 & 0 & 0 & 0 & 1 & -1\end{matrix}\right]$
    Matrix([
    [1, -1,  0,  0,  0,  0,  0,  0],
    [0,  1, -1,  0,  0,  0,  0,  1],
    [0,  0,  1, -1, -1,  0,  0,  0],
    [0,  0,  0,  0,  1, -1, -1,  0],
    [0,  0,  0,  0,  0,  0,  1, -1]])

%% Cell type:code id:7c92dbab tags:

``` python
layout = [True, True, True, False, False, False, False, False]
layout_inv = [not(i) for i in layout]
layout_inv
```

%% Output

    [False, False, False, True, True, True, True, True]

%% Cell type:code id:035143cd tags:

``` python
ju = jac_lin[:,layout_inv]
jm = jac_lin[:, layout]
```

%% Cell type:code id:50a64f26 tags:

``` python
ju.rref()[0]
```

%% Output

    $\displaystyle \left[\begin{matrix}1 & 0 & 1 & 0 & 0\\0 & 1 & -1 & 0 & 0\\0 & 0 & 0 & 1 & 0\\0 & 0 & 0 & 0 & 1\\0 & 0 & 0 & 0 & 0\end{matrix}\right]$
    Matrix([
    [1, 0,  1, 0, 0],
    [0, 1, -1, 0, 0],
    [0, 0,  0, 1, 0],
    [0, 0,  0, 0, 1],
    [0, 0,  0, 0, 0]])

%% Cell type:code id:1fad065b tags:

``` python
# transfer one measured over:
layout = [False, True, True, False, False, False, False, False]
layout_inv = [not(i) for i in layout]
ju = jac_lin[:,layout_inv]
jm = jac_lin[:, layout]
ju.rref()[0]
```

%% Output

    $\displaystyle \left[\begin{matrix}1 & 0 & 0 & 0 & 0 & 0\\0 & 1 & 0 & 1 & 0 & 0\\0 & 0 & 1 & -1 & 0 & 0\\0 & 0 & 0 & 0 & 1 & 0\\0 & 0 & 0 & 0 & 0 & 1\end{matrix}\right]$
    Matrix([
    [1, 0, 0,  0, 0, 0],
    [0, 1, 0,  1, 0, 0],
    [0, 0, 1, -1, 0, 0],
    [0, 0, 0,  0, 1, 0],
    [0, 0, 0,  0, 0, 1]])

%% Cell type:markdown id:21ed7f7f tags:

## bilinear case

%% Cell type:code id:e1d43176 tags:

``` python
import sympy as sy
import pandas as pd
import os
import scipy as sp
import numpy as np
import math
import itertools
import sys
module_path = os.path.join(os.path.dirname(os.path.abspath('')))
if module_path not in sys.path:
    sys.path.insert(0, module_path)
else:
    print("path already in sys.path")

import matplotlib.pyplot as plt
from numerical_labelling.labelling import *
from numerical_labelling.plot_layout import make_pretty_labels
```

%% Output

    path already in sys.path

%% Cell type:code id:a6ff637e tags:

``` python
def constr_spl():
    constr = {}
    m1, m2, m3 = sy.symbols("m1 m2 m3")
    x1, x2, x3 = sy.symbols("x1 x2 x3")
    varbs_sy = [m1, m2, m3, x1, x2, x3]

    constr[0] = m1 - m2 - m3
    constr[1] = x1 - x2
    constr[2] = x1 - x3

    varb_labels = [
        "m1", "m2", "m3",
        "x1", "x2", "x3",
        ]

    varbs = dict(zip(varb_labels,varbs_sy))

    return constr, varbs

def constr_spl_sup(simple_constr = False):
    "include superfluous constraint"
    constr = {}
    m1, m2, m3 = sy.symbols("m1 m2 m3")
    x1, x2, x3 = sy.symbols("x1 x2 x3")
    varbs_sy = [m1, m2, m3, x1, x2, x3]

    constr[0] = m1 - m2 - m3
    constr[1] = x1 - x2
    constr[2] = x1 - x3
    if simple_constr:
        constr[3] = m1 - m2 - m3
    else:
        constr[3] = m1*x1 - m2*x2 - m3*x3

    varb_labels = [
        "m1", "m2", "m3",
        "x1", "x2", "x3",
        ]

    varbs = dict(zip(varb_labels,varbs_sy))

    return constr, varbs
```

%% Cell type:code id:9a97895b tags:

``` python
constr_req, varbs_req = constr_spl()
constr_sup, varbs_sup = constr_spl_sup()
constr_sup_mb, varbs_sup_mb = constr_spl_sup(simple_constr=True)
jac_req = jacsym(constr_req.values(), varbs_req.values())
jac_sup = jacsym(constr_sup.values(), varbs_sup.values())
jac_sup_mb = jacsym(constr_sup_mb.values(), varbs_sup_mb.values())
```

%% Cell type:code id:2701d8e2 tags:

``` python
layout = [
    True, False, False,
    False, True, False
]

bool_measured = np.where(np.array(layout))[0]
bool_unmeasured = np.where(~np.array(layout))[0]
vb_all, vb_m, vb_um = make_pretty_labels(varbs_req, layout)
```

%% Cell type:code id:f55e7793 tags:

``` python
r1, r2, r3, r4 = sy.symbols("r1 r2 r3 r4")
rhs_syms = sy.Matrix([r1, r2, r3, r4])
```

%% Cell type:code id:4c3f3568 tags:

``` python
Ju_req = jac_req[:, list(bool_unmeasured)]
Ju_sup = jac_sup[:, list(bool_unmeasured)]
Ju_sup_mb = jac_sup_mb[:, list(bool_unmeasured)]
rref_req, rhs_req = Ju_req.rref_rhs(rhs_syms[:Ju_req.shape[0],:])
rref_sup, rhs_sup = Ju_sup.rref_rhs(rhs_syms[:Ju_sup.shape[0],:])
rref_sup_mb, rhs_sup_mb = Ju_sup_mb.rref_rhs(rhs_syms[:Ju_sup_mb.shape[0], :])
```

%% Cell type:code id:d4b906cd tags:

``` python
sy.Matrix.vstack(vb_um, rref_req)
```

%% Output

    $\displaystyle \left[\begin{matrix}m_{2} & m_{3} & x_{1} & x_{3}\\- & - & - & -\\1 & 1 & 0 & 0\\0 & 0 & 1 & 0\\0 & 0 & 0 & 1\end{matrix}\right]$
    Matrix([
    [m2, m3, x1, x3],
    [ -,  -,  -,  -],
    [ 1,  1,  0,  0],
    [ 0,  0,  1,  0],
    [ 0,  0,  0,  1]])

%% Cell type:code id:aad07395 tags:

``` python
sy.Matrix.vstack(vb_um, rref_sup)
```

%% Output

    $\displaystyle \left[\begin{matrix}m_{2} & m_{3} & x_{1} & x_{3}\\- & - & - & -\\1 & 0 & 0 & 0\\0 & 1 & 0 & 0\\0 & 0 & 1 & 0\\0 & 0 & 0 & 1\end{matrix}\right]$
    Matrix([
    [m2, m3, x1, x3],
    [ -,  -,  -,  -],
    [ 1,  0,  0,  0],
    [ 0,  1,  0,  0],
    [ 0,  0,  1,  0],
    [ 0,  0,  0,  1]])

%% Cell type:code id:07ba3528 tags:

``` python
sy.Matrix.vstack(vb_um, rref_sup_mb)
```

%% Output

    $\displaystyle \left[\begin{matrix}m_{2} & m_{3} & x_{1} & x_{3}\\- & - & - & -\\1 & 1 & 0 & 0\\0 & 0 & 1 & 0\\0 & 0 & 0 & 1\\0 & 0 & 0 & 0\end{matrix}\right]$
    Matrix([
    [m2, m3, x1, x3],
    [ -,  -,  -,  -],
    [ 1,  1,  0,  0],
    [ 0,  0,  1,  0],
    [ 0,  0,  0,  1],
    [ 0,  0,  0,  0]])

%% Cell type:code id:20d2fb5a tags:

``` python
rhs_req
```

%% Output

    $\displaystyle \left[\begin{matrix}- r_{1}\\r_{2}\\r_{2} - r_{3}\end{matrix}\right]$
    Matrix([
    [    -r1],
    [     r2],
    [r2 - r3]])

%% Cell type:code id:3257c8d7 tags:

``` python
rhs_sup
```

%% Output

    $\displaystyle \left[\begin{matrix}\frac{m_{1} r_{2} - m_{3} r_{2} + m_{3} r_{3} + r_{1} x_{3} - r_{4}}{x_{2} - x_{3}}\\\frac{- m_{1} r_{2} + m_{3} r_{2} - m_{3} r_{3} - r_{1} x_{2} + r_{4}}{x_{2} - x_{3}}\\r_{2}\\r_{2} - r_{3}\end{matrix}\right]$
    Matrix([
    [ (m1*r2 - m3*r2 + m3*r3 + r1*x3 - r4)/(x2 - x3)],
    [(-m1*r2 + m3*r2 - m3*r3 - r1*x2 + r4)/(x2 - x3)],
    [                                             r2],
    [                                        r2 - r3]])

%% Cell type:code id:56de1575 tags:

``` python
rhs_sup_mb
```

%% Output

    $\displaystyle \left[\begin{matrix}- r_{4}\\r_{2}\\r_{2} - r_{3}\\r_{1} - r_{4}\end{matrix}\right]$
    Matrix([
    [    -r4],
    [     r2],
    [r2 - r3],
    [r1 - r4]])

%% Cell type:code id:e4b4c4d8 tags:

``` python
rref_subout, rhs_subout = Ju_sup.subs({'x2':varbs_req['x3']}).rref_rhs(rhs_syms[:4, :])
rhs_subout
```

%% Output

    $\displaystyle \left[\begin{matrix}\frac{m_{1} r_{2} - m_{3} r_{2} + m_{3} r_{3} - r_{4}}{x_{3}}\\r_{2}\\r_{2} - r_{3}\\\frac{m_{1} r_{2} - m_{3} r_{2} + m_{3} r_{3} + r_{1} x_{3} - r_{4}}{x_{3}}\end{matrix}\right]$
    Matrix([
    [        (m1*r2 - m3*r2 + m3*r3 - r4)/x3],
    [                                     r2],
    [                                r2 - r3],
    [(m1*r2 - m3*r2 + m3*r3 + r1*x3 - r4)/x3]])

%% Cell type:code id:224f51bb tags:

``` python
Ju_req
```

%% Output

    $\displaystyle \left[\begin{matrix}-1 & -1 & 0 & 0\\0 & 0 & 1 & 0\\0 & 0 & 1 & -1\end{matrix}\right]$
    Matrix([
    [-1, -1, 0,  0],
    [ 0,  0, 1,  0],
    [ 0,  0, 1, -1]])

%% Cell type:code id:f0f6e228 tags:

``` python
varbs_req['x2']
```

%% Output

    $\displaystyle x_{2}$
    x2

%% Cell type:markdown id:58024a89 tags:

## Bilinear failure: operating point selection

%% Cell type:markdown id:e4747f6d tags:
+0 −530

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −328

File deleted.

Preview size limit exceeded, changes collapsed.

Loading