Example #1
0
    def testSettings(self):
        # set distributions of the input parameters
        builder = ParameterBuilder()
        up = builder.defineUncertainParameters()
        up.new().isCalled('x').withUniformDistribution(0, 1)
        up.new().isCalled('y').withUniformDistribution(0, 1)
        params = builder.andGetResult()

        def postprocessor(x, *args, **kws):
            return {'x': np.array([x[0], -x[0]]), 'time': list(range(len(x)))}

        f = lambda x, **kws: [x[0], 2 * x[0], 4 * x[0], 8 * x[0]]
        toi = [0, 1]

        # set up uq setting
        builder = ASGCUQManagerBuilder().withParameters(params)\
                                        .withTypesOfKnowledge([KnowledgeTypes.SIMPLE,
                                                               KnowledgeTypes.SQUARED])\
                                        .withQoI("x")\
                                        .withTimeStepsOfInterest(toi)\
                                        .useInterpolation()

        builder.defineUQSetting().withSimulation(f)\
                                 .withPostprocessor(postprocessor)

        builder.defineSampler().withGrid().withLevel(3)\
                                          .hasType(GridType_Poly)\
                                          .withDegree(2)\
                                          .withBoundaryLevel(1)

        uqManager = builder.andGetResult()
        uqManager.runNextSamples()

        # define analysis
        uqAnalysis = ASGCAnalysisBuilder().withUQManager(uqManager)\
                                          .withAnalyticEstimationStrategy()\
                                          .andGetResult()

        # plot result
        ans = {}
        for t in uqManager.getTimeStepsOfInterest():
            grid, alpha = uqManager.getKnowledge().getSparseGridFunction(
                t=t, qoi="x")
            fig = plt.figure()
            ans[t] = plotSG2d(grid,
                              alpha,
                              show_grid_points=True,
                              show_numbers=True)
            fig.show()

        assert all(ans[0] == -ans[1])
        plt.show()
Example #2
0
builder.withParameters(params)\
       .withTypesOfKnowledge([KnowledgeTypes.SIMPLE,
                              KnowledgeTypes.SQUARED])\
       .useInterpolation()


# define model function
def g(x, **kws):
    return np.arctan(
        50 *
        (x[0] - .35)) + np.pi / 2 + 4 * x[1]**3 + 10 * np.exp(x[0] * x[1] - 1)


builder.defineUQSetting().withSimulation(g)

samplerSpec = builder.defineSampler()
samplerSpec.withGrid().withLevel(4)\
                      .withPolynomialBase(2)\
                      .withBorder(BorderTypes.TRAPEZOIDBOUNDARY)
samplerSpec.withRefinement()\
           .withAdaptThreshold(1e-10)\
           .withAdaptPoints(5)\
           .withBalancing()\
           .refineMostPromisingNodes().withSquaredSurplusRanking()\
                                      .createAllChildrenOnRefinement()\
                                      .refineInnerNodes()
#         ref.withBalancing()\
#            .addMostPromisingChildren().withLinearSurplusEstimationRanking()

samplerSpec.withStopPolicy().withAdaptiveIterationLimit(0)