def __init__(self, test_file): self.file = test_file self.file_name = os.path.basename(self.file.name) self.full_folder = os.path.dirname(self.file.name) self.folder_name = os.path.basename(self.full_folder) self.current_line = None self.states = StateSpace() self.initial_state = None self.goal_state = None self.grid = Grid() self.load_attributes() self.update_coordinates()
def example(): """ This function demonstrates the usage of the poisson module and some of its classes """ # # Make the grid on which to solve the Poisson equation # domain = Domain(center=(0, 0, 0), edges=(1, 1, 1)) grid = Grid(domain, shape=(33, 33, 33)) # # Prepare the boundary conditions # bc = {} for index in grid.boundary: x, y, z = grid.loc(index) bc[index] = g(x, y, z) # # Prepare the field with the right-hand-side of the Poisson equation. # rhs = grid.field_from_function(f) # rhs is of type Field, which has two properties: grid and values, the # latter being a numpy ndarray holding the field values at the grid # points # # Now solve the Poisson equation \Delta u = rhs # solver = MultiGridSolver(rhs, bc, atol=1.0E-6) try: solver.solve() u = solver.solution() # u is of type Field # print solution for index in u.grid.indices(): (x, y, z), u_val = u[index] print("{:10.2f}{:10.2f}{:10.2f}{:12.6f}".format(x, y, z, u_val)) except Exception as e: print("No convergence")
def _create_grid(self, grid_dimension, spatial_extents): """ create the grid objects and grid data that are required to execute the workflow """ if grid_dimension is not 2 and grid_dimension is not 3: raise PyStochWorkflowError( "Grid dimension must be 2 or 3: Recieved '%s'" % grid_dimension) if self._grid_type is Grid.FIXED: grid = Grid.create_fixed( extents=spatial_extents, grid_spacing=self._grid_spacing[:grid_dimension]) elif self._grid_type is Grid.FLOATING: grid = Grid.create_floating( extents=spatial_extents, grid_dimensions=self._grid_dimensions[:grid_dimension]) elif self._grid_type is Grid.FILE: grid = Grid.create_from_file(self._grid_file) return grid
def is_nc_grid_good(nc_grid_file): if not os.path.exists(nc_grid_file): return False if os.path.getsize(nc_grid_file) == 0: return False nc_handle = netCDF4.Dataset(nc_grid_file, "r") nc_keys = nc_handle.variables.keys() grid_keys = Grid().get_allowed_keys() for key in grid_keys: if key not in nc_keys: return False return True
def _prepare_gaussian_example(self): domain = Domain(center=(0, 0, 0), edges=(1, 1, 1)) grid = Grid(domain, shape=(17, 17, 17)) # Prepare exact solution def exact_solution(x, y, z): return math.exp(-(x**2 + y**2 + z**2)) exact = grid.field_from_function(exact_solution) # Prepare problem to solve def the_rhs(x, y, z): r2 = x**2 + y**2 + z**2 return (4 * r2 - 6) * math.exp(-r2) rhs = grid.field_from_function(the_rhs) bc = {} for ind in grid.boundary: x, y, z = grid.loc(ind) bc[ind] = exact_solution(x, y, z) return grid, rhs, bc, exact
class Test(): """ Description ----------- Class that defines a Test. It is used as an input to the solver algorithms. Parameters ---------- test_file: file \\ -- The file conataining the test info. Attributes ---------- file: file \\ -- The file conataining the test info. file_name: str \\ -- The name of the file. full_folder: str \\ -- Full path to the file, except for the actual name of the file. folder_name: str \\ -- Name of the folder where the test file is stored. current_line: str \\ -- The current line that was read from the file. states: StateSpace() \\ -- A StateSpace() instance that has the states read from the file. initial_state: State() \\ -- The initial state read from the file. goal_state: State() \\ -- The goal state read from the file. grid: Grid() \\ -- A Grid() instance that stores the grid read form the file. """ def __init__(self, test_file): self.file = test_file self.file_name = os.path.basename(self.file.name) self.full_folder = os.path.dirname(self.file.name) self.folder_name = os.path.basename(self.full_folder) self.current_line = None self.states = StateSpace() self.initial_state = None self.goal_state = None self.grid = Grid() self.load_attributes() self.update_coordinates() def __repr__(self): return f'Test({self.file_name})' def __str__(self): return f''' File: {os.path.join(self.folder_name, self.file_name)} Init: {self.initial_state} Goal: {self.goal_state} Grid: {self.grid}''' def read_file_line(self): """ Description ----------- Function used to update the `current_line` attribute. """ self.current_line = self.file.readline().strip() def load_attributes(self): """ Description ----------- Function used to update most of the attributes. It follows the standard found on all of the test files. """ while self.current_line != 'states': self.read_file_line() self.load_states() # Will run for all directions # North, South, East and West for i in range(0, 4): while not self.current_line.startswith('action'): self.read_file_line() # line should be 'action move-direction' move_direction = str.split(self.current_line)[1] self.load_actions(move_direction) while self.current_line != 'cost': self.read_file_line() self.load_costs() while self.current_line != 'initialstate': self.read_file_line() self.read_file_line() self.initial_state = self.states.get_state(self.current_line) while self.current_line != 'goalstate': self.read_file_line() self.read_file_line() self.goal_state = self.states.get_state(self.current_line) while self.current_line != 'Grid:': self.read_file_line() self.load_grid() def load_states(self): """ Description ----------- Function used to load all the states from the line containing them. It then stores them into a list and updates the `states` attribute. """ self.read_file_line() states_list = self.current_line.split(', ') self.states.load_states_list(states_list) def load_actions(self, move_direction): """ Description ----------- Function used to load all the actions for a specific direction. It reads all of the lines using their pattern, discarding the last item, which won't be used, and getting the respective states and updating the action for the initial state. """ self.read_file_line() while self.current_line != 'endaction': init_state, end_state, prob, discard = self.current_line.split() del discard init_state = self.states.get_state(init_state) end_state = self.states.get_state(end_state) init_state.update_action(move_direction, end_state, prob) self.read_file_line() def load_costs(self): """ Description ----------- Function used to load all the costs for all the actions and updating the actions with their respective cost. """ self.read_file_line() while self.current_line != 'endcost': cost_list = self.current_line.split() state = self.states.get_state(cost_list[0]) state.update_action_cost(cost_list[1], cost_list[2]) self.read_file_line() def load_grid(self): """ Description ----------- Function used to load the grid from the file, updating the `grid` attribute. """ self.read_file_line() while self.current_line != '': self.grid.add_row(self.current_line.split()) self.read_file_line() def update_coordinates(self): """ Description ----------- Function used to update the `shape` attribute from the `grid` attribute and using that info to update the coordinates for each state. """ self.grid.update_shape() self.states.update_coordinates(self.grid)