Example #1
0
def plotGrid3dSlices(grid):
    gs = grid.getStorage()
    p = DataVector(3)

    d = {}

    for i in range(gs.getSize()):
        gp = gs.getPoint(i)
        gs.getCoordinates(gp, p)
        if p[2] in d:
            d[p[2]].append([p[0], p[1],
                            gp.getLevel(0), gp.getLevel(1),
                            gp.getIndex(0), gp.getIndex(1)])
        else:
            d[p[2]] = [[p[0], p[1],
                        gp.getLevel(0), gp.getLevel(1),
                        gp.getIndex(0), gp.getIndex(1)]]

    print(sum([len(dd) for dd in list(d.values())]))

    for z, items in list(d.items()):
        fig = plt.figure()
        plt.xlim(0, 1)
        plt.ylim(0, 1)
        plt.title('z = %g %s, len=%i' % (z, "(border)" if hasBorder(grid) else "",
                                         len(items)))
        for x, y, l0, l1, i0, i1 in items:
            plt.plot(x, y, marker='o', color='blue')
            plt.text(x, y, "(%i, %i), (%i, %i)" % (l0, l1, i0, i1),
                     horizontalalignment='center')
        fig.show()
Example #2
0
 def fromGrid(self, grid):
     """
     Indicates that all grid points in grid should also be in the
     new grid
     @param grid:
     """
     self.__grid = grid
     self.__dim = grid.getStorage().dim()
     self.__deg = getDegree(grid)
     self.__border = hasBorder(grid)
     if self.__border:
         self.level = 0
     return self
Example #3
0
    def fromGrid(self, grid):
        """
        Indicates that all grid points in grid should also be in the
        new grid
        @param grid:
        """
        self.__grid = grid
        self.__dim = grid.getStorage().getDimension()
        self.__deg = getDegree(grid)
        self.__gridType = grid.getType()
        if hasBorder(grid.getType()):
            self.__boundaryLevel = 1
            self.level = 0

        return self
Example #4
0
    def buildSetting(self,
                     params,
                     f=None,
                     level=None,
                     gridType=None,
                     grid=None,
                     deg=1,
                     maxGridSize=1000,
                     isFull=False,
                     boundaryLevel=1,
                     balancing=True,
                     epsilon=1e-15,
                     adaptive=None,
                     refinementTechnique="refinement",
                     adaptPoints=3,
                     saveAfterN=-1,
                     adaptRate=None,
                     uqSetting=None,
                     uqSettingRef=None,
                     knowledgeFilename=None,
                     knowledgeTypes=[
                         KnowledgeTypes.SIMPLE, KnowledgeTypes.SQUARED,
                         KnowledgeTypes.EXPECTATIONVALUE
                     ],
                     qoi=None,
                     toi=None):
        builder = ASGCUQManagerBuilder()

        builder.withParameters(params)\
               .withTypesOfKnowledge(knowledgeTypes)\
               .useInterpolation()

        if qoi is not None:
            builder.withQoI(qoi)
        if toi is not None:
            builder.withTimeStepsOfInterest(toi)
        if uqSetting is not None:
            builder.useUQSetting(uqSetting)
        else:
            builder.defineUQSetting().withSimulation(f).saveAfterEachRun(
                saveAfterN)

        if uqSettingRef is not None and len(uqSettingRef) > 0:
            builder.withTestSet(uqSettingRef)\
                   .learnWithTest()

        if knowledgeFilename is not None:
            builder.withKnowledge(knowledgeFilename)

        samplerSpec = builder.defineSampler()
        gridSpec = samplerSpec.withGrid()
        if grid is not None:
            gridSpec.fromGrid(grid)
        else:
            assert level is not None
            assert gridType is not None
            gridSpec.withLevel(level).hasType(gridType)
            if deg > 1:
                gridSpec.withDegree(deg)
            if isFull:
                gridSpec.isFull()
            if hasBorder(gridType) and boundaryLevel is not None:
                gridSpec.withBoundaryLevel(boundaryLevel)

        if adaptive is not None:
            # specify the refinement
            refinement = samplerSpec.withRefinement()

            refinement.withAdaptThreshold(epsilon)\
                      .withAdaptPoints(adaptPoints)

            if balancing:
                refinement.withBalancing()

            if adaptRate is not None:
                refinement.withAdaptRate(adaptRate)

            if refinementTechnique == "refinement":
                refineNodes = refinement.refineMostPromisingNodes()
                refineNodes.createAllChildrenOnRefinement()
                if adaptive == "simple":
                    refineNodes.withSurplusRanking()
                elif adaptive == "weighted":
                    refineNodes.withWeightedSurplusRanking()
                elif adaptive == "l2":
                    refineNodes.withWeightedL2OptimizationRanking()
                elif adaptive == "exp":
                    refineNodes.withExpectationValueOptimizationRanking()
                elif adaptive == "var":
                    refineNodes.withVarianceOptimizationRanking()
                elif adaptive == "mean_squared":
                    refineNodes.withMeanSquaredOptRanking()
                elif adaptive == "squared":
                    refineNodes.withSquaredSurplusRanking()
                elif adaptive == "anchored_exp":
                    refineNodes.withAnchoredExpectationValueOptimizationRanking(
                    )
                elif adaptive == "anchored_var":
                    refineNodes.withAnchoredVarianceOptimizationRanking()
                elif adaptive == "anchored_l2":
                    refineNodes.withAnchoredWeightedL2OptimizationRanking()
                elif adaptive == "anchored_mean_squared":
                    refineNodes.withAnchoredMeanSquaredOptRanking()
                elif adaptive == "l2_mc_pm1d":
                    refineNodes.withPM1D_MC_Ranking(deg, gridType, 'l2')
                elif adaptive == "exp_mc_pm1d":
                    refineNodes.withPM1D_MC_Ranking(deg, gridType, 'exp')
                elif adaptive == "var_mc_pm1d":
                    refineNodes.withPM1D_MC_Ranking(deg, gridType, 'var')
                else:
                    raise AttributeError(
                        "unknown ranking method: refinement, %s" % adaptive)
            else:
                addNodes = refinement.addMostPromisingChildren()
                if adaptive == "weighted":
                    addNodes.withWeightedSurplusOptimizationRanking()
                elif adaptive == "l2":
                    addNodes.withWeightedL2OptimizationRanking()
                else:
                    raise AttributeError(
                        "unknown ranking method: predictive, %s" % adaptive)

            if toi is not None:
                refinement.withAverageWeightening()

            samplerSpec.withStopPolicy().withGridSizeLimit(maxGridSize)

        uqManager = builder.andGetResult()

        # update the stats, which are not stored with the knowledge
        if knowledgeFilename is not None:
            uqManager.recomputeStats()

        return uqManager