Ejemplo n.º 1
0
class TestCase(unittest.TestCase):
    def setUp(self):
        self.config = SPSConfig()
        self.modulefactory = ModuleFactory(self.config)

    def tests3rfail(self):
        results = []
        s3r = self.modulefactory.s3rmodule()
        logger.info("===============built===============")
        for t in range(4, 5):
            self.config.setParam("SCREEN_THICKNESS", t)
            ps = []
            xs = []
            std_xs = []
            for v in [BoundedVariable("day", i) for i in interval(1, 365 * YEAR, 1)]:
                dose = v.get_value() / 365.0 * (s3r.getConstant("S3R_K").get_value() /
                                                self.config.getParam("SCREEN_THICKNESS").get_value())
                # 阈值电压漂移
                xs.append(dose)
                x = s3r.getConstant("S3R_DELTAV_THRESHOLD").get_value() / (
                    s3r.getConstant("S3R_B").get_value() * exp(s3r.getConstant("S3R_B").get_value() * dose))
                std_x = (x - s3r.getConstant("S3R_A_MU").get_value()) / s3r.getConstant("S3R_A_SIGMA").get_value()
                p = 1 - pcf(std_x)
                ps.append(p)
                std_xs.append(std_x)
            logger.info("thickness={}, p_max={}".format(t, ps[-1]))
            results.append(ps)

        return results

    def get_parsed(self):
        constructor = ModelConstructor()
        model = constructor._parse("../../prism_model/smalltest.prism")
        return model

    # 测试同样的数据for parsed model
    def testparsing(self):
        parsed = self.get_parsed()
        days = range(1, YEAR * 365 + 1)
        s3r_mdl = parsed.get_module("S3R")
        results = []
        logger.info("===============parsed===============")
        for thickness in range(4, 5):
            probs = []
            parsed.setConstant("SCREEN_THICKNESS", thickness)
            for d in days:
                parsed.setVariable("day", d)
                fail_prob = s3r_mdl.commands["s3r_fail_cmd"].prob()
                probs.append(fail_prob)
            results.append(probs)
            logger.info("thickness={}, p_max={}".format(thickness, probs[-1]))
        return results

    def test_final(self):
        results_built = self.tests3rfail()[0]
        results_parsed = self.testparsing()[0]
        assert len(results_built) == len(results_parsed), "results array length not equal. "
        precision = 1e-6
        for index, (v1, v2) in enumerate(zip(results_parsed, results_built)):
            assert fabs(v1 - v2) <= precision, "day={}, v1={}, v2={}".format(index+1, v1, v2)
Ejemplo n.º 2
0
inc_day = Command(
    'inc day', lambda vs, cs: vs['timer_turn'] == True and vs['day'] <= timer.
    getConstant('TIME_LIMIT').getValue(), incdayaction, timer, 1.0)
timer.addCommand(inc_day)

config = SPSConfig()

# 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(config.getParam("SB_K"))
sb.addConstant(config.getParam("SB_B"))
sb.addConstant(config.getParam("SB_P_THRESHOLD"))
sb.addConstant(config.getParam("SB_A_MU"))
sb.addConstant(config.getParam("SB_A_SIGMA"))

# variables
sb_status = Variable('sb_status', 1, range(2), int, True)
sb.addVariable(sb_status)


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