def set_port_endogenous_values(self, pc_coeffs, germ, portid): """ Sample polynomial chaos expansion for endogenous values at germ Assign those values to the nodes on the exodus mesh """ endo_map = self.get_endogenous_data() with Suppressor(): e = exodus.exodus(self.outfile, mode='a') ss_ids = e.get_side_set_ids() ss_names = e.get_side_set_names() dictionary = dict(zip(ss_names, ss_ids)) # Get list of nodes ssname = self.endogenous_output_ports[portid].ssname varname = self.endogenous_output_ports[portid].varname side_set_node_ids = e.get_side_set_node_list(dictionary[ssname])[1] for timestep, node_map in enumerate(endo_map): nodal_values = e.get_node_variable_values(varname, timestep + 1) ssid = e.get_side_set_node_list(dictionary[ssname])[1] side_set_unique_node_ids = set(ssid) for nid in side_set_unique_node_ids: index = timestep * self.num_endogenous_nodes + node_map.keys( ).index(nid) endo_val = self.evaluate_pce(pc_coeffs[index, ...], germ) nodal_values[nid - 1] = endo_val e.put_node_variable_values(varname, timestep + 1, nodal_values) with Suppressor(): e.close()
def save_nodal_fields_from_structured(meshfilename, outputfilename, fieldnames, fielddata): if os.path.isfile(outputfilename): os.remove(outputfilename) with Suppressor(): e = exodus.exodus(meshfilename, array_type='numpy', mode='r') x, y, z = e.get_coords() e.close() ux = np.unique(x.round(decimals=6)) uy = np.unique(y.round(decimals=6)) dx = ux[1] - ux[0] dy = uy[1] - uy[0] i = np.rint((x - min(x)) / dx) j = np.rint((y - min(y)) / dy) with Suppressor(): e = exodus.copy_mesh(meshfilename, outputfilename) e.close() e = exodus.exodus(outputfilename, mode='a', array_type="numpy") exodus.add_variables(e, nodal_vars=fieldnames) for vidx, name in enumerate(fieldnames): for ts in range(fielddata.shape[0]): e.put_node_variable_values( name, ts + 1, fielddata[ts, vidx, i.astype(int), j.astype(int)]) e.put_time(ts + 1, ts) e.close()
def set_all_endogenous_values(self, pc_coeffs, germ): """ Sample polynomial chaos expansion for endogenous values at germ Assign those values to the nodes on the exodus mesh """ endo_map = self.get_endogenous_data() with Suppressor(): e = exodus.exodus(self.outfile, mode='a') ss_ids = e.get_side_set_ids() ss_names = e.get_side_set_names() dictionary = dict(zip(ss_names, ss_ids)) index = 0 for timestep in range(self.num_timesteps): for portid, port in enumerate(self.endogenous_output_ports): node_map = endo_map[timestep * len(self.endogenous_output_ports) + portid] nodal_values = e.get_node_variable_values( port.varname, timestep + 1) ssid = e.get_side_set_node_list(dictionary[port.ssname])[1] side_set_unique_node_ids = set(ssid) for nid in side_set_unique_node_ids: idx = index + node_map.keys().index(nid) endo_val = self.evaluate_pce(pc_coeffs[idx, ...], germ) nodal_values[nid - 1] = endo_val index += len(side_set_unique_node_ids) e.put_node_variable_values(port.varname, timestep + 1, nodal_values) with Suppressor(): e.close()
def get_QoI_data(self): with Suppressor(): e = exodus.exodus(self.outfile, mode='r') QoI_vals = {} for QoI in self.QoIs: QoI_vals[QoI] = e.get_global_variable_values(QoI) with Suppressor(): e.close() return QoI_vals
def set_times(filename, times): with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='a') assert len(times) == e.num_times() for i, t in enumerate(times): e.put_time(i + 1, t) with Suppressor(): e.close()
def get_coords(filename): """ Returns the spatial coordinates of nodes in all blocks """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') x, y, z = e.get_coords() e.close() return x, y, z
def isin_sideset(filename, ssname): with Suppressor(): e = exodus.exodus(filename, mode='r') ss_ids = e.get_side_set_ids() ss_names = e.get_side_set_names() dictionary = dict(zip(ss_names, ss_ids)) vals = np.zeros(e.num_nodes()) ssid = e.get_side_set_node_list(dictionary[ssname])[1] side_set_unique_node_ids = set(ssid) for nid in side_set_unique_node_ids: vals[nid - 1] = 1 with Suppressor(): e.close() return vals
def get_num_nodes(filename): """ Returns the total number of nodes in all blocks """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') n = e.num_nodes() e.close() return n
def get_nodal_variable_names(filename): """ Returns list of nodal variables present in exodus file """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') names = e.get_node_variable_names() e.close() return names
def get_times(filename): """ Returns list of times corresponding to time planes in the exodus file """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') t = e.get_times() e.close() return t
def get_nodal_variable_values(filename, varname, step=1): """ Extracts nodal field data from exodus file and returns a numpy array of nodal values """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') vals = e.get_node_variable_values(varname, step) e.close() return vals
def get_num_globals(filename): """ Returns number of global variables in the exodus file """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') n = e.get_global_variable_number() e.close() return n
def get_node_id_map(filename): """ Returns mapping between node index and node id from exodus file """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') nid = e.get_node_id_map() e.close() return nid
def get_combined_timeline(filenames): times = [] for filename in filenames: with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') times.append(e.get_times()) e.close() times = [item for sublist in times for item in sublist] return sorted(set(times))
def add_QoI(self, varname): """ Specify global variables from exodus output to be treated at QoIs """ with Suppressor(): e = exodus.exodus(self.outfile, mode='r') gnames = e.get_global_variable_names() e.close() assert varname in gnames, "%s not a global variable in %s." % ( varname, self.outfile) self.QoIs.append(varname)
def get_solution_times(self): if self.output_times: print("using self.output_times") times = self.output_times else: print("no output times found, reading from exodus") with Suppressor(): e = exodus.exodus(self.outfile, mode='r') times = e.get_times() e.close() return times
def save_nodal_fields(meshfilename, outputfilename, fieldnames, fielddata): if os.path.isfile(outputfilename): os.remove(outputfilename) with Suppressor(): e = exodus.copy_mesh(meshfilename, outputfilename) e.close() e = exodus.exodus(outputfilename, mode='a', array_type="numpy") exodus.add_variables(e, nodal_vars=fieldnames) for i, name in enumerate(fieldnames): e.put_node_variable_values(name, 1, fielddata[i]) e.close()
def add_endogenous_port(self, ssname, varname): """ Add an endogenous port between two aria_component instances Port is specified on the *sending* component """ with Suppressor(): e = exodus.exodus(self.outfile, mode='r') ssnames = e.get_side_set_names() e.close() assert ssname in ssnames, "%s not a sideset in %s." % (ssname, self.outfile) my_port = endogenous_port(ssname, varname) self.endogenous_output_ports.append(my_port)
def add_global_variable(filename, name, vals): """ Adds global variable and fills with values """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='a') num_gvars = e.get_global_variable_number() e.set_global_variable_number(num_gvars + 1) e.put_global_variable_name(name, num_gvars + 1) for i, val in enumerate(vals): e.put_global_variable_value(name, i + 1, val) e.close()
def get_endogenous_data(self): """ Retreive my output data at endogenous nodes """ if self.output_times: sync_times.interpolate_to_timeline(self.outfile, self.outfile + ".tmp", self.output_times) os.rename(self.outfile + ".tmp", self.outfile) with Suppressor(): e = exodus.exodus(self.outfile, mode='r') ss_ids = e.get_side_set_ids() ss_names = e.get_side_set_names() dictionary = dict(zip(ss_names, ss_ids)) # Get list of time steps for which to provide data times = e.get_times() self.num_timesteps = len(times) vals = [] for timestep in range(self.num_timesteps): self.num_endogenous_nodes = 0 for port in self.endogenous_output_ports: endogenous_vals = {} ssid = e.get_side_set_node_list(dictionary[port.ssname])[1] nodal_values = e.get_node_variable_values( port.varname, timestep + 1) side_set_unique_node_ids = set(ssid) for nid in side_set_unique_node_ids: endogenous_vals[nid] = nodal_values[nid - 1] vals.append(endogenous_vals) self.num_endogenous_nodes += len(endogenous_vals) with Suppressor(): e.close() return vals
def save_nodal_fields_transient(meshfilename, outputfilename, fieldnames, fielddata): # assert len(fieldnames) == fielddata.shape[1] # assert get_num_nodes(meshfilename) == fielddata.shape[2] if os.path.isfile(outputfilename): os.remove(outputfilename) with Suppressor(): e = exodus.copy_mesh(meshfilename, outputfilename) e.close() e = exodus.exodus(outputfilename, mode='a', array_type="numpy") exodus.add_variables(e, nodal_vars=fieldnames) for i, name in enumerate(fieldnames): for ts in range(fielddata.shape[0]): e.put_node_variable_values(name, ts + 1, fielddata[ts, i, :]) e.put_time(ts + 1, ts) e.close()
def get_all_global_variable_values(filename): """ Returns global variable values for all times """ with Suppressor(): e = exodus.exodus(filename, array_type='numpy', mode='r') if e.get_global_variable_number() > 0: global_data = np.zeros( (e.num_times(), e.get_global_variable_number())) for timestep in range(e.num_times()): global_data[timestep, :] = e.get_all_global_variable_values( timestep + 1) return global_data else: return None
def append_exodus(filenamelist, outputfilename="joined-output.e", skip_first=0, skip_last=0): if os.path.isfile(outputfilename): os.remove(outputfilename) with Suppressor(): # Copy Mesh e = exodus.copy_mesh(filenamelist[0], outputfilename) e.close() e = exodus.exodus(outputfilename, mode='a', array_type="numpy") # Add Variable Names var_names = [] gvar_names = [] for f in filenamelist: exo = exodus.exodus(f, mode='r', array_type="numpy") var_names.extend(exo.get_node_variable_names()) gvar_names.extend(exo.get_global_variable_names()) exo.close() var_names = list(set(var_names)) gvar_names = list(set(gvar_names)) exodus.add_variables(e, nodal_vars=var_names) e.set_global_variable_number(len(gvar_names)) for i, gvar in enumerate(gvar_names): e.put_global_variable_name(gvar, i + 1) # Add Variable Data ts = 1 for f in filenamelist: exo = exodus.exodus(f, mode='r', array_type="numpy") for step in range(skip_first, exo.num_times() - skip_last): for var in exo.get_node_variable_names(): e.put_node_variable_values( var, ts, exo.get_node_variable_values(var, step + 1)) if len(gvar_names) > 0: gvar_vals = [] for gvar in exo.get_global_variable_names(): gvar_vals.append( exo.get_global_variable_values(gvar)[step]) e.put_all_global_variable_values(ts, gvar_vals) e.put_time(ts, ts - 1) ts += 1 exo.close() e.close()
def get_all_data(self, varname, filename=None): """ Retreive my output data at all nodes """ if filename == None: filename = self.outfile with Suppressor(): e = exodus.exodus(filename, mode='r') # Get list of time steps for which to provide data times = e.get_times() self.num_timesteps = len(times) vals = [] for timestep in range(self.num_timesteps): nodal_values = e.get_node_variable_values( varname, timestep + 1) vals.append(nodal_values) e.close() return vals
def interpolate_to_timeline(input_filename, output_filename, output_times): with Suppressor(): e = exodus.copy_mesh(input_filename, output_filename) e.close() exo_in = exodus.exodus(input_filename, mode='r', array_type="numpy") exo_out = exodus.exodus(output_filename, mode='a', array_type="numpy") input_times = exo_in.get_times() #if list(input_times) == output_times: # print("skip") # return #print("interpolating %d input times to %d output times..." % (len(input_times), len(output_times))) node_varnames = exo_in.get_node_variable_names() global_varnames = exo_in.get_global_variable_names() exodus.add_variables(exo_out, nodal_vars=node_varnames, global_vars=global_varnames) for step, time in enumerate(output_times): idx = np.searchsorted(input_times, time) if idx == 0: left_idx = idx else: left_idx = idx - 1 if idx == len(input_times): right_idx = idx - 1 else: right_idx = idx for varname in node_varnames: left_data = exo_in.get_node_variable_values(varname, left_idx + 1) if left_idx == right_idx: exo_out.put_node_variable_values(varname, step + 1, left_data) else: right_data = exo_in.get_node_variable_values( varname, right_idx + 1) alpha = (time - input_times[left_idx]) / ( input_times[right_idx] - input_times[left_idx]) interp_data = left_data + alpha * (right_data - left_data) exo_out.put_node_variable_values(varname, step + 1, interp_data) for varname in global_varnames: left_data = exo_in.get_global_variable_value(varname, left_idx + 1) if left_idx == right_idx: exo_out.put_global_variable_value(varname, step + 1, left_data) else: right_data = exo_in.get_global_variable_value( varname, right_idx + 1) alpha = (time - input_times[left_idx]) / ( input_times[right_idx] - input_times[left_idx]) interp_data = left_data + alpha * (right_data - left_data) exo_out.put_global_variable_value(varname, step + 1, interp_data) exo_out.put_time(step + 1, time) with Suppressor(): exo_in.close() exo_out.close()
def append_exodus_ss(filenamelist, outputfilename="joined-output.e", labels=None): if os.path.isfile(outputfilename): os.remove(outputfilename) with Suppressor(): # Copy Mesh e = exodus.copy_mesh(filenamelist[0], outputfilename) e.close() e = exodus.exodus(outputfilename, mode='a', array_type="numpy") # Add Variable Names nvar_names = [] gvar_names = [] evar_names = [] for f in filenamelist: exo = exodus.exodus(f, mode='r', array_type="numpy") nvar_names.extend(exo.get_node_variable_names()) gvar_names.extend(exo.get_global_variable_names()) evar_names.extend(exo.get_element_variable_names()) exo.close() if labels: gvar_names.extend(labels.keys()) nvar_names = list(set(nvar_names)) gvar_names = list(set(gvar_names)) evar_names = list(set(evar_names)) exodus.add_variables(e, nodal_vars=nvar_names, element_vars=evar_names) e.set_global_variable_number(len(gvar_names)) for i, gvar in enumerate(gvar_names): e.put_global_variable_name(gvar, i + 1) gvar_vals = {} for gvar in gvar_names: gvar_vals[gvar] = [] ts = 1 for f in filenamelist: exo = exodus.exodus(f, mode='r', array_type="numpy") step = exo.num_times() e.put_time(ts, ts) for var in exo.get_node_variable_names(): e.put_node_variable_values( var, ts, exo.get_node_variable_values(var, step)) for evar in exo.get_element_variable_names(): # TODO: only works for 1 block e.put_element_variable_values( 1, evar, ts, exo.get_element_variable_values(1, evar, step)) for gvar in exo.get_global_variable_names(): val = exo.get_global_variable_value(gvar, step) gvar_vals[gvar].append(val) if labels: for key in labels: val = labels[key][ts - 1] gvar_vals[key].append(val) ts += 1 exo.close() for ts in range(1, e.num_times() + 1): vals = [] for gvar in e.get_global_variable_names(): vals.append(gvar_vals[gvar][ts - 1]) e.put_all_global_variable_values(ts, vals) e.close()