Ejemplo n.º 1
0
def test_grids():

    from dolo.numeric.grids import CartesianGrid, UnstructuredGrid, NonUniformCartesianGrid, SmolyakGrid
    from dolo.numeric.grids import nodes, n_nodes, node

    print("Cartsian Grid")
    grid = CartesianGrid([0.1, 0.3], [9, 0.4], [50, 10])
    print(grid.nodes())
    print(nodes(grid))

    print("UnstructuredGrid")
    ugrid = UnstructuredGrid([[0.1, 0.3], [9, 0.4], [50, 10]])
    print(nodes(ugrid))
    print(node(ugrid, 0))
    print(n_nodes(ugrid))

    print("Non Uniform CartesianGrid")
    ugrid = NonUniformCartesianGrid([[0.1, 0.3], [9, 0.4], [50, 10]])
    print(nodes(ugrid))
    print(node(ugrid, 0))
    print(n_nodes(ugrid))

    print("Smolyak Grid")
    sg = SmolyakGrid([0.1, 0.2], [1.0, 2.0], 2)
    print(nodes(sg))
    print(node(sg, 1))
    print(n_nodes(sg))
Ejemplo n.º 2
0
    def get_grid(model, **dis_opts):
        import copy
        domain = model.get_domain()
        a = np.array([e[0] for e in domain.values()])
        b = np.array([e[1] for e in domain.values()])

        gg = model.symbolic.options.get('grid', {})

        d = copy.deepcopy(gg)
        gtype = dis_opts.get('type')
        if gtype:
            from dolo.compiler.language import minilang
            try:
                cls = [
                    e for e in minilang if e.__name__.lower() == gtype.lower()
                ][0]
            except:
                raise Exception("Unknown grid type {}.".format(gtype))
            d = cls(**d)

    #     return cc
        d.update(**dis_opts)
        if 'a' not in d.keys():
            d['min'] = a
        if 'b' not in d.keys():
            d['max'] = b
        if 'type' in d: d.pop('type')
        grid = d.eval(d=model.calibration.flat)
        # temporary
        from dolo.numeric.grids import CartesianGrid, SmolyakGrid
        if 'Cartesian' in str(grid.__class__):
            return CartesianGrid(grid.a, grid.b, grid.orders)
        if 'Smolyak' in str(grid.__class__):
            return SmolyakGrid(grid.a, grid.b, grid.mu)
Ejemplo n.º 3
0
    def endo_grid(self):

        # determine bounds:
        domain = self.get_domain()
        min = domain.min
        max = domain.max

        options = self.data.get("options", {})

        # determine grid_type
        grid_type = get_type(options.get("grid"))
        if grid_type is None:
            grid_type = get_address(self.data,
                                    ["options:grid:type", "options:grid_type"])
        if grid_type is None:
            raise Exception('Missing grid geometry ("options:grid:type")')

        args = {"min": min, "max": max}
        if grid_type.lower() in ("cartesian", "cartesiangrid"):
            from dolo.numeric.grids import UniformCartesianGrid

            orders = get_address(self.data,
                                 ["options:grid:n", "options:grid:orders"])
            if orders is None:
                orders = [20] * len(min)
            grid = UniformCartesianGrid(min=min, max=max, n=orders)
        elif grid_type.lower() in ("nonuniformcartesian",
                                   "nonuniformcartesiangrid"):
            from dolang.language import eval_data
            from dolo.numeric.grids import NonUniformCartesianGrid

            calibration = self.get_calibration()
            nodes = [
                eval_data(e, calibration) for e in self.data["options"]["grid"]
            ]
            # each element of nodes should be a vector
            return NonUniformCartesianGrid(nodes)
        elif grid_type.lower() in ("smolyak", "smolyakgrid"):
            from dolo.numeric.grids import SmolyakGrid

            mu = get_address(self.data, ["options:grid:mu"])
            if mu is None:
                mu = 2
            grid = SmolyakGrid(min=min, max=max, mu=mu)
        else:
            raise Exception("Unknown grid type.")

        return grid
Ejemplo n.º 4
0
Archivo: model.py Proyecto: xiaohr/dolo
    def get_grid(self):

        states = self.symbols['states']

        # determine bounds:
        domain = self.get_domain()
        min = domain.min
        max = domain.max

        options = self.data.get("options", {})

        # determine grid_type
        grid_type = get_type(options.get("grid"))
        if grid_type is None:
            grid_type = get_address(self.data,
                                    ["options:grid:type", "options:grid_type"])
        if grid_type is None:
            raise Exception('Missing grid geometry ("options:grid:type")')

        args = {'min': min, 'max': max}
        if grid_type.lower() in ('cartesian', 'cartesiangrid'):
            # from dolo.numerid.grids import CartesianGrid
            from dolo.numeric.grids import CartesianGrid
            orders = get_address(self.data,
                                 ["options:grid:n", "options:grid:orders"])
            if orders is None:
                orders = [20] * len(min)
            grid = CartesianGrid(min=min, max=max, n=orders)
        elif grid_type.lower() in ('smolyak', 'smolyakgrid'):
            from dolo.numeric.grids import SmolyakGrid
            mu = get_address(self.data, ["options:grid:mu"])
            if mu is None:
                mu = 2
            grid = SmolyakGrid(min=min, max=max, mu=mu)
        else:
            raise Exception("Unknown grid type.")

        return grid