Esempio n. 1
0
def single_faults(mdl, staged=False, track=True):
    """
    Creates and propagates a list of failure scenarios in a model

    Parameters
    ----------
    mdl : model
        The model to inject faults in
    staged : bool, optional
        Whether to inject the fault in a copy of the nominal model at the fault time (True) or instantiate a new model for the fault (False). Setting to True roughly halves execution time. The default is False.
    track : bool, optional
        Whether to track states over time. The default is True.

    Returns
    -------
    endclasses : dict
        A dictionary with the rate, cost, and expected cost of each scenario run with structure {scenname:{expected cost, cost, rate}}
    mdlhists : dict
        A dictionary with the history of all model states for each scenario (including the nominal)
    """

    scenlist = list_init_faults(mdl)
    #run model nominally, get relevant results
    nomscen = construct_nomscen(mdl)
    mdl = mdl.__class__(params=mdl.params)
    if staged:
        nomhist, c_mdl = prop_one_scen(mdl,
                                       nomscen,
                                       track=track,
                                       ctimes=mdl.times)
    else:
        nomhist, c_mdl = prop_one_scen(mdl, nomscen, track=track)
    nomresgraph = mdl.return_stategraph()
    mdl.reset()

    endclasses = {}
    mdlhists = {}
    mdlhists['nominal'] = nomhist
    for i, scen in enumerate(scenlist):
        #run model with fault scenario
        if staged:
            mdl = c_mdl[scen['properties']['time']].copy()
            mdlhists[scen['properties']['name']], _ = prop_one_scen(
                mdl, scen, track=track, staged=True, prevhist=nomhist)
        else:
            mdl = mdl.__class__(params=mdl.params)
            mdlhists[scen['properties']['name']], _ = prop_one_scen(
                mdl, scen, track=track)
        endfaults, endfaultprops = mdl.return_faultmodes()
        resgraph = mdl.return_stategraph()
        endflows = proc.graphflows(resgraph, nomresgraph)
        endclasses[scen['properties']['name']] = mdl.find_classification(
            resgraph, endfaultprops, endflows, scen, {
                'nominal': nomhist,
                'faulty': mdlhists[scen['properties']['name']]
            })

    return endclasses, mdlhists
Esempio n. 2
0
def one_fault(mdl,
              fxnname,
              faultmode,
              time=1,
              track=True,
              staged=False,
              gtype='normal'):
    """
    Runs one fault in the model at a specified time.

    Parameters
    ----------
    mdl : Model
        The model to inject the fault in.
    fxnname : str
        Name of the function with the faultmode
    faultmode : str
        Name of the faultmode
    time : float, optional
        Time to inject fault. Must be in the range of model times (i.e. in range(0, end, mdl.tstep)). The default is 0.
    track : bool, optional
        Whether to track model states over time. The default is True.
    staged : bool, optional
        Whether to inject the fault in a copy of the nominal model at the fault time (True) or instantiate a new model for the fault (False). The default is False.
    gtype : str, optional
        The graph type to return ('bipartite' or 'normal'). The default is 'normal'.

    Returns
    -------
    endresults : dict
        A dictionary summary of results at the end of the simulation with structure {flows:{flow:attribute:value},faults:{function:{faults}}, classification:{rate:val, cost:val, expected cost: val}
    resgraph : networkx.classes.graph.Graph
        A graph object with function faults and degraded flows noted as attributes
    mdlhists : dict
        A dictionary of the states of the model of each fault scenario over time.

    """
    #run model nominally, get relevant results
    mdl = mdl.__class__(params=mdl.params)
    nomscen = construct_nomscen(mdl)
    if staged:
        nommdlhist, mdls = prop_one_scen(mdl,
                                         nomscen,
                                         track=track,
                                         staged=staged,
                                         ctimes=[time])
        nomresgraph = mdl.return_stategraph(gtype)
        mdl.reset()
        mdl = mdls[time]
    else:
        nommdlhist, _ = prop_one_scen(mdl, nomscen, track=track, staged=staged)
        nomresgraph = mdl.return_stategraph(gtype)
        mdl.reset()
        mdl = mdl.__class__(params=mdl.params)
    #run with fault present, get relevant results
    scen = nomscen.copy(
    )  #note: this is a shallow copy, so don't define it earlier
    scen['faults'][fxnname] = faultmode
    scen['properties']['type'] = 'single fault'
    scen['properties']['function'] = fxnname
    scen['properties']['fault'] = faultmode
    if mdl.fxns[fxnname].faultmodes[faultmode]['probtype'] == 'rate':
        scen['properties']['rate'] = mdl.fxns[fxnname].failrate * mdl.fxns[
            fxnname].faultmodes[faultmode]['dist'] * eq_units(
                mdl.fxns[fxnname].faultmodes[faultmode]['units'],
                mdl.units) * (mdl.times[-1] - mdl.times[0]
                              )  # this rate is on a per-simulation basis
    elif mdl.fxns[fxnname].faultmodes[faultmode]['probtype'] == 'prob':
        scen['properties']['rate'] = mdl.fxns[fxnname].failrate * mdl.fxns[
            fxnname].faultmodes[faultmode]['dist']
    scen['properties']['time'] = time

    faultmdlhist, _ = prop_one_scen(mdl,
                                    scen,
                                    track=track,
                                    staged=staged,
                                    prevhist=nommdlhist)
    faultresgraph = mdl.return_stategraph(gtype)

    #process model run
    endfaults, endfaultprops = mdl.return_faultmodes()
    endflows = proc.graphflows(faultresgraph, nomresgraph, gtype)
    mdlhists = {'nominal': nommdlhist, 'faulty': faultmdlhist}
    endclass = mdl.find_classification(faultresgraph, endfaultprops, endflows,
                                       scen, mdlhists)
    resgraph = proc.resultsgraph(faultresgraph, nomresgraph, gtype=gtype)

    endresults = {
        'flows': endflows,
        'faults': endfaults,
        'classification': endclass
    }

    mdl.reset()
    return endresults, resgraph, mdlhists
Esempio n. 3
0
def approach(mdl, app, staged=False, track=True):
    """
    Injects and propagates faults in the model defined by a given sample approach

    Parameters
    ----------
    mdl : model
        The model to inject faults in.
    app : sampleapproach
        SampleApproach used to define the list of faults and sample time for the model.
    staged : bool, optional
        Whether to inject the fault in a copy of the nominal model at the fault time (True) or instantiate a new model for the fault (False). Setting to True roughly halves execution time. The default is False.
    track : bool, optional
        Whether to track states over time. The default is True.

    Returns
    -------
    endclasses : dict
        A dictionary with the rate, cost, and expected cost of each scenario run with structure {scenname:{expected cost, cost, rate}}
    mdlhists : dict
        A dictionary with the history of all model states for each scenario (including the nominal)
    """
    mdl = mdl.__class__(params=mdl.params)
    if staged:
        nomhist, c_mdl = prop_one_scen(mdl,
                                       app.create_nomscen(mdl),
                                       track=track,
                                       ctimes=app.times)
    else:
        nomhist, c_mdl = prop_one_scen(mdl,
                                       app.create_nomscen(mdl),
                                       track=track)
    nomresgraph = mdl.return_stategraph()
    mdl.reset()

    endclasses = {}
    mdlhists = {}
    mdlhists['nominal'] = nomhist
    for i, scen in enumerate(app.scenlist):
        #run model with fault scenario
        if staged:
            mdl = c_mdl[scen['properties']['time']].copy()
            mdlhists[scen['properties']['name']], _ = prop_one_scen(
                mdl, scen, track=track, staged=True, prevhist=nomhist)
        else:
            mdl = mdl.__class__(params=mdl.params)
            mdlhists[scen['properties']['name']], _ = prop_one_scen(
                mdl, scen, track=track)
        endfaults, endfaultprops = mdl.return_faultmodes()
        resgraph = mdl.return_stategraph()

        endflows = proc.graphflows(
            resgraph,
            nomresgraph)  #TODO: supercede this with something in faultprop?
        endclasses[scen['properties']['name']] = mdl.find_classification(
            resgraph, endfaultprops, endflows, scen, {
                'nominal': nomhist,
                'faulty': mdlhists[scen['properties']['name']]
            })

    return endclasses, mdlhists
Esempio n. 4
0
def mult_fault(mdl, faultseq, track=True, rate=np.NaN, gtype='normal'):
    """
    Runs one fault in the model at a specified time.

    Parameters
    ----------
    mdl : Model
        The model to inject the fault in.
    faultseq : dict
        Dict of times and modes defining the fault scenario {time:{fxns: [modes]},}
    track : bool, optional
        Whether to track model states over time. The default is True.
    rate : float, optional
        Input rate for the sequence (must be calculated elsewhere)
    gtype : str, optional
        The graph type to return ('bipartite' or 'normal'). The default is 'normal'.

    Returns
    -------
    endresults : dict
        A dictionary summary of results at the end of the simulation with structure {flows:{flow:attribute:value},faults:{function:{faults}}, classification:{rate:val, cost:val, expected cost: val}
    resgraph : networkx.classes.graph.Graph
        A graph object with function faults and degraded flows noted as attributes
    mdlhists : dict
        A dictionary of the states of the model of each fault scenario over time.

    """
    #run model nominally, get relevant results
    mdl = mdl.__class__(params=mdl.params)
    nomscen = construct_nomscen(mdl)

    nommdlhist, _ = prop_one_scen(mdl, nomscen, track=track, staged=False)
    nomresgraph = mdl.return_stategraph(gtype)
    mdl.reset()

    mdl = mdl.__class__(params=mdl.params)
    #run with fault present, get relevant results
    scen = nomscen.copy(
    )  #note: this is a shallow copy, so don't define it earlier
    scen['faults'] = list(faultseq.values())
    scen['properties']['type'] = 'sequence'
    scen['properties']['sequence'] = faultseq
    scen['properties']['rate'] = rate  # this rate is on a per-simulation basis
    scen['properties']['time'] = list(faultseq.keys())

    faultmdlhist, _ = prop_one_scen(mdl,
                                    scen,
                                    track=track,
                                    staged=False,
                                    prevhist=nommdlhist)
    faultresgraph = mdl.return_stategraph(gtype)

    #process model run
    endfaults, endfaultprops = mdl.return_faultmodes()
    endflows = proc.graphflows(faultresgraph, nomresgraph, gtype)
    mdlhists = {'nominal': nommdlhist, 'faulty': faultmdlhist}
    endclass = mdl.find_classification(faultresgraph, endfaultprops, endflows,
                                       scen, mdlhists)
    resgraph = proc.resultsgraph(faultresgraph, nomresgraph, gtype=gtype)

    endresults = {
        'flows': endflows,
        'faults': endfaults,
        'classification': endclass
    }

    mdl.reset()
    return endresults, resgraph, mdlhists