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))
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