def input_generator(cstate_subdomain, cboundary_subdomain): sub_cstate = numpy_utils.mobius_extract(cstate, cstate_subdomain, has_batch=True, padding_type=self.padding_type, return_padding=True) sub_cboundary = numpy_utils.mobius_extract(cboundary, cboundary_subdomain, has_batch=True, padding_type=self.padding_type, return_padding=True) return [sub_cstate, sub_cboundary]
def mapping(self, mapping, shape_converter, input_generator, output_shape, run_output_shape): nr_subdomains = [int(math.ceil(x/float(y))) for x, y in zip(output_shape, run_output_shape)] output = [] for ijk in itertools.product(xrange(nr_subdomains[0]), xrange(nr_subdomains[1])): # make input and output subdomains if not(type(shape_converter) is list): shape_converter = [shape_converter] input_subdomain = [] output_subdomain = [] for converter in shape_converter: pos = [x * y for x, y in zip(ijk, run_output_shape)] subdomain = SubDomain(pos, run_output_shape) input_subdomain.append(converter.out_in_subdomain(copy(subdomain))) output_subdomain.append(converter.in_out_subdomain(copy(input_subdomain[-1]))) output_subdomain = output_subdomain[0] output_subdomain.zero_pos() # generate input with input generator sub_input = input_generator(*input_subdomain) # perform mapping function and extract out if needed if not (type(sub_input) is list): sub_input = [sub_input] sub_output = mapping(*sub_input) if not (type(sub_output) is list): sub_output = [sub_output] for i in xrange(len(sub_output)): sub_output[i] = numpy_utils.mobius_extract(sub_output[i], output_subdomain, has_batch=True) # append to list of sub outputs output.append(sub_output) # make total output shape total_subdomain = SubDomain(len(output_shape)*[0], output_shape) ctotal_subdomain = shape_converter[0].out_in_subdomain(copy(total_subdomain)) total_subdomain = shape_converter[0].in_out_subdomain(copy(ctotal_subdomain)) total_subdomain = SubDomain([-x for x in total_subdomain.pos], output_shape) # stack back together to form one output output = llist2list(output) for i in xrange(len(output)): output[i] = numpy_utils.stack_grid(output[i], nr_subdomains, has_batch=True) output[i] = numpy_utils.mobius_extract(output[i], total_subdomain, has_batch=True) if len(output) == 1: output = output[0] return output
def input_generator(subdomain): if self.start_boundary is not None: input_geometry, pad_input_geometry = numpy_utils.mobius_extract(self.start_boundary, subdomain, has_batch=False, padding_type=self.padding_type, return_padding=True) else: input_geometry = self.input_boundary(subdomain) pad_input_geometry = np.zeros(subdomain.size + [1]) input_geometry = np.expand_dims(input_geometry, axis=0) pad_input_geometry = np.expand_dims(pad_input_geometry, axis=0) return (input_geometry, pad_input_geometry)
def input_generator(subdomain): if self.start_state is not None: start_state, pad_start_state = numpy_utils.mobius_extract(self.start_state, subdomain, has_batch=False, padding_type=self.padding_type, return_padding=True) else: vel = self._domain.velocity_initial_conditions(0,0,None) feq = self.DxQy.vel_to_feq(vel).reshape([1] + self.DxQy.dims*[1] + [self.DxQy.Q]) start_state = np.zeros(subdomain.size + [self.DxQy.Q]) + feq[0] pad_start_state = np.zeros(subdomain.size + [1]) start_state = np.expand_dims(start_state, axis=0) pad_start_state = np.expand_dims(pad_start_state, axis=0) return (start_state, pad_start_state)
def read_boundary(self, subdomain=None, add_batch=False): boundary_file = self.boundary_file() boundary = None if os.path.isfile(boundary_file): boundary = np.load(boundary_file) boundary = boundary.astype(np.float32) boundary = boundary[1:-1, 1:-1] if subdomain is not None: boundary, pad_boundary = numpy_utils.mobius_extract( boundary, subdomain, padding_type=self.padding_type, return_padding=True) if add_batch: boundary = np.expand_dims(boundary, axis=0) pad_boundary = np.expand_dims(pad_boundary, axis=0) if subdomain is not None: return (boundary, pad_boundary) else: return boundary
def read_state(self, iteration, subdomain=None, add_batch=False): # load flow file state_file = self.iter_to_cpoint(iteration) state = np.load(state_file) state = state.f.dist0a[:, 1:-1, 1:self.sim_shape[1] + 1] state = state.astype(np.float32) state = np.swapaxes(state, 0, 1) state = np.swapaxes(state, 1, 2) state = self.DxQy.subtract_lattice(state) if subdomain is not None: state, pad_state = numpy_utils.mobius_extract( state, subdomain, padding_type=self.padding_type, return_padding=True) if add_batch: state = np.expand_dims(state, axis=0) if subdomain is not None: pad_state = np.expand_dims(pad_state, axis=0) if subdomain is not None: return (state, pad_state) else: return state