Exemplo n.º 1
0
def poission_model():
    module = Module("poission process")
    module.addConstant(Constant('r', None))

    # variable definition
    v = Variable('n', 0, None, int, False)  # n: int init 0;
    module.addVariable(v)
    # command definition

    comm = Command('', lambda vs, cs: True,
                   lambda vs, cs: vs['n'].setValue(vs['n'].getValue() + 1),
                   module, module.getConstant("r"), CommandKind.NONE, None)
    module.addCommand(comm)

    # 增加label表示n>=4这个ap
    labels = dict()

    def nge4(vs, cs):
        return vs['n'] >= 4

    labels['nge4'] = nge4

    model = ModulesFile.ModulesFile(ModelType.CTMC,
                                    modules=[module],
                                    labels=labels)
    return model
Exemplo n.º 2
0
    def _s3rmodule(self):
        config = self.config
        module = Module('S3R')
        module.addConstant(config.getParam('S3R_K'))
        module.addConstant(config.getParam('S3R_A_MU'))
        module.addConstant(config.getParam('S3R_A_SIGMA'))
        module.addConstant(config.getParam('S3R_B'))
        module.addConstant(config.getParam('S3R_DELTAV_THRESHOLD'))

        module.addVariable(Variable('s3r_status', 1, range(2), int))

        def guard(vs, cs):
            # the sb_status and s3r_status must both be 1 for this transition
            # to happen
            return vs['timer_turn'] == False and vs['s3r_status'] == 1 and vs[
                'sb_status'] == 1 and vs['bcr_status'] == 1 and vs[
                    'bdr_status'] == 1

        def faction(vs, cs):
            vs['s3r_status'].setValue(0)
            vs['timer_turn'].setValue(True)

        def naction(vs, cs):
            vs['timer_turn'].setValue(True)

        def f(day_var):
            def inner():
                day = day_var.getValue()
                dose = module.getConstant('S3R_K') / config.getParam(
                    'SCREEN_THICKNESS') * (day / 365.0)
                x = config.getParam('S3R_DELTAV_THRESHOLD') / (
                    config.getParam('S3R_B') *
                    pow(e,
                        config.getParam('S3R_B') * dose))
                std_x = (-config.getParam('S3R_A_MU') + x) / \
                    config.getParam('S3R_A_SIGMA').getValue()
                p = 1 - pcf(std_x)
                logger.info('day:{0}, s3r failure prob:{1}'.format(day, p))
                return p

            return inner

        def n(day_var):
            def inner():
                ff = f(day_var)
                return 1 - ff()

            return inner

        if not hasattr(self, 'timer'):
            self.timer = self._timermodule()

        module.addCommand(
            Command('s3r_fail_cmd', guard, faction, module,
                    f(self.timer.getVariable('day'))))

        module.addCommand(
            Command('s3r_nrl_cmd', guard, naction, module,
                    n(self.timer.getVariable('day'))))
        return module
Exemplo n.º 3
0
    def _sbmodule(self):
        config = self.config
        module = Module('SB')
        module.addConstant(config.getParam('SB_K'))
        module.addConstant(config.getParam('SB_A_MU'))
        module.addConstant(config.getParam('SB_A_SIGMA'))
        module.addConstant(config.getParam('SB_B'))
        module.addConstant(config.getParam('SB_P_THRESHOLD'))

        module.addVariable(Variable('sb_status', 1, range(2), int))

        def guard(vs, cs):
            # sb_status and s3r_status must both be 1, e.g. the system has not
            # failed.
            return vs['timer_turn'] == False and vs['sb_status'] == 1 and vs[
                's3r_status'] == 1 and vs['bcr_status'] == 1 and vs[
                    'bdr_status'] == 1

        # failure action
        def faction(vs, cs):
            vs['sb_status'].setValue(0)
            vs['timer_turn'].setValue(True)

        # normal action
        def naction(vs, cs):
            vs['timer_turn'].setValue(True)

        def f(day_var):
            def inner():
                day = day_var.getValue()
                dose = module.getConstant('SB_K') * config.getParam(
                    'SCREEN_THICKNESS') * day / 365.0
                x = (-module.getConstant('SB_P_THRESHOLD') + 1) / \
                    (log(module.getConstant('SB_B') * dose + 1))
                std_x = 1.0 / (module.getConstant('SB_A_SIGMA') /
                               (-module.getConstant('SB_A_MU') + x))
                return 1 - pcf(std_x)

            return inner

        def n(day_var):
            def inner():
                ff = f(day_var)
                return 1 - ff()

            return inner

        if not hasattr(self, 'timer'):
            self.timer = self._timermodule()

        module.addCommand(
            Command('sb_fail_cmd', guard, faction, module,
                    f(self.timer.getVariable('day'))))

        module.addCommand(
            Command('sb_nrl_cmd', guard, naction, module,
                    n(self.timer.getVariable('day'))))
        return module
Exemplo n.º 4
0
def sps_model_dtmc(duration):
    # duration: timer.day的最大值:天数
    # timer module of the system
    timer = Module('timer_module')
    timer.addConstant(Constant('TIME_LIMIT', duration))
    day = BoundedVariable(
        'day', 1, range(timer.getConstant('TIME_LIMIT').get_value() + 1), int,
        True)
    timer_turn = BoundedVariable('timer_turn', True, set([True, False]), bool,
                                 True)
    timer.add_variable(day)
    timer.add_variable(timer_turn)

    def incdayaction(vs, cs):
        vs['day'].set_value(vs['day'].get_value() + 1)
        vs['timer_turn'].set_value(False)

    inc_day = Command(
        'inc day', lambda vs, cs: vs['timer_turn'] == True and vs['day'] <
        timer.getConstant('TIME_LIMIT').get_value(), incdayaction, timer, 1.0)
    timer.addCommand(inc_day)

    # solar battery module of the system
    sb = Module("solar battery module")

    # set initial value None to denote uncertainty
    screenthick = Constant('SCREEN_THICKNESS', None)
    sb.addConstant(screenthick)
    # the dose of one year: dose = K_SB * thickness
    sb.addConstant(Constant('SB_K', 0.0039))
    sb.addConstant(Constant('SB_B', 12))
    sb.addConstant(Constant('SB_P_THRESHOLD', 0.7))
    sb.addConstant(Constant('SB_A_MU', 0.1754))
    sb.addConstant(Constant('SB_A_SIGMA', 0.02319029 * 21))

    # variables
    sb_status = BoundedVariable('sb_status', 1, range(2), int, True)
    sb.add_variable(sb_status)

    def failaction(vs, cs):
        vs['sb_status'].set_value(0)
        vs['timer_turn'].set_value(True)

    # use closure to delay the computation of fail rate
    def f1(day_var):
        def failprobsb():
            # return the failure probability of solar battery after day-dose
            niel_dose = sb.getConstant('SB_K').get_value() * sb.getConstant(
                'SCREEN_THICKNESS').get_value() * (day_var.get_value() / 365.0)
            x = (1 - sb.getConstant('SB_P_THRESHOLD').get_value()) / \
                log(1 + niel_dose * sb.getConstant('SB_B').get_value())
            std_x = (x - sb.getConstant('SB_A_MU').get_value() /
                     sb.getConstant('SB_A_SIGMA').get_value())
            temp = pcf(std_x)
            i = id(day_var)
            return 1 - temp

        return failprobsb

    def f1n(day_var):
        def normalprobsb():
            niel_dose = sb.getConstant('SB_K').get_value() * sb.getConstant(
                'SCREEN_THICKNESS').get_value() * (day_var.get_value() / 365.0)
            x = (1 - sb.getConstant('SB_P_THRESHOLD').get_value()) / \
                log(1 + niel_dose * sb.getConstant('SB_B').get_value())
            std_x = (x - sb.getConstant('SB_A_MU').get_value() /
                     sb.getConstant('SB_A_SIGMA').get_value())
            return pcf(std_x)

        return normalprobsb

    comm_fail = Command(
        'solar battery failure command',
        lambda vs, cs: vs['sb_status'] == 1 and vs['timer_turn'] == False,
        failaction,
        sb,
        f1(timer.getVariable('day')),
        kind=CommandKind.FAILURE)
    sb.addCommand(comm_fail)

    comm_normal = Command(
        'solar battery stay-normal command',
        lambda vs, cs: vs['sb_status'] == 1 and vs['timer_turn'] == False,
        lambda vs, cs: vs['timer_turn'].set_value(True), sb,
        f1n(timer.getVariable('day')))
    sb.addCommand(comm_normal)

    s3r = Module('S3R模块')
    s3r.addConstant(screenthick)
    s3r.addConstant(Constant('S3R_K', 200.5 / 3.0))  # s3r, bdr, bcr三个模块均摊电离能损
    s3r.addConstant(Constant('S3R_DELTAV_THRESHOLD', 450))
    s3r.addConstant(Constant('S3R_A_MU', 570.8 * 18))
    s3r.addConstant(Constant('S3R_A_SIGMA', 6.7471 * 120))
    s3r.addConstant(Constant('S3R_B', 0.01731))

    s3r_status = BoundedVariable('s3r_status', 1, range(2), int, True)
    s3r.add_variable(s3r_status)

    def s3rfailaction(vs, cs):
        vs['s3r_status'].set_value(0)
        vs['timer_turn'].set_value(True)

    def f2(day_var):
        def failprobs3r():
            iel_dose = day_var.get_value() / 365.0 * (s3r.getConstant('S3R_K').get_value() / \
                                                      s3r.getConstant('SCREEN_THICKNESS').get_value())
            x = s3r.getConstant('S3R_DELTAV_THRESHOLD').get_value() / (
                s3r.getConstant('S3R_B').get_value() *
                pow(e,
                    s3r.getConstant('S3R_B').get_value() * iel_dose))
            std_x = (x - s3r.getConstant('S3R_A_MU').get_value()) / \
                    s3r.getConstant('S3R_A_SIGMA').get_value()
            return 1 - pcf(std_x)

        return failprobs3r

    def f2n(day_var):
        def normalprobs3r():
            iel_dose = day_var.get_value() / 365.0 * (s3r.getConstant('S3R_K').get_value() / \
                                                      s3r.getConstant('SCREEN_THICKNESS').get_value())
            x = s3r.getConstant('S3R_DELTAV_THRESHOLD').get_value() / (
                s3r.getConstant('S3R_B').get_value() *
                pow(e,
                    s3r.getConstant('S3R_B').get_value() * iel_dose))
            std_x = (x - s3r.getConstant('S3R_A_MU').get_value()) / \
                    s3r.getConstant('S3R_A_SIGMA').get_value()
            return pcf(std_x)

        return normalprobs3r

    s3r_comm_fail = Command(
        's3r failure command',
        lambda vs, cs: vs['s3r_status'] == 1 and vs['timer_turn'] == False,
        s3rfailaction, s3r, f2(timer.getVariable('day')), CommandKind.FAILURE)
    s3r.addCommand(s3r_comm_fail)
    s3r_comm_norm = Command(
        's3r normal command',
        lambda vs, cs: vs['timer_turn'] == False and vs['s3r_status'] == 1,
        lambda vs, cs: vs['timer_turn'].set_value(True), s3r,
        f2n(timer.getVariable('day')))
    s3r.addCommand(s3r_comm_norm)

    def failureif(vs, cs):
        return vs['s3r_status'] == 0 or vs['sb_status'] == 0

    labels = dict()
    labels['up'] = lambda vs, cs: vs['s3r_status'] == 1 and vs['sb_status'
                                                               ] == 1
    labels['failure'] = lambda vs, cs: vs['s3r_status'] == 0 or vs['sb_status'
                                                                   ] == 0

    i = id(timer.getVariable('day'))

    model = ModulesFile.ModulesFile(ModelType.DTMC,
                                    modules=[timer, sb, s3r],
                                    failureCondition=failureif,
                                    labels=labels,
                                    stopCondition=failureif)

    ii = id(model.localVars['day'])
    assert i == ii
    return model