def quad_mesh(self, curves, *args, **kwargs): if isinstance(curves[0], Curve): curve_list = ' '.join(str(x.mentat_id) for x in curves) else: curve_list = ' '.join(str(x) for x in curves) n1 = pm.py_get_int('max_element_id()') pm.py_send('*af_planar_quadmesh %s #' % curve_list) self.options = {} for key, value in kwargs.items(): self.options[key] = value try: element_set = self.options['membership'] #end = ' #' n2 = pm.py_get_int('max_element_id()') #elements = list(range(n1+1,n2+1)) #elem = ' '.join(str(x) for x in elements) elem = str(n1) + ' to ' + str(n2) mentat_command = ' '.join(['*store_elements', element_set, elem]) pm.py_send(mentat_command) except: pass
def quad_tri_mesh(self, curves, max_quad_distortion=0.9, *args, **kwargs): if isinstance(curves[0], Curve): curve_list = ' '.join(str(x.mentat_id) for x in curves) else: curve_list = ' '.join(str(x) for x in curves) n1 = pm.py_get_int('max_element_id()') pm.py_send('*af_set_max_quad_distortion', str(max_quad_distortion)) pm.py_send('*af_planar_quad_trimesh', curve_list) pm.py_send('#') self.options = {} for key, value in kwargs.items(): self.options[key] = value try: element_set = self.options['membership'] end = ' #' n2 = pm.py_get_int('max_element_id()') elements = list(range(n1 + 1, n2 + 1)) elem = ' '.join(str(x) for x in elements) mentat_command = ' '.join( ['*store_elements', element_set, elem, end]) pm.py_send(mentat_command) except: pass
def _get_nset_entries(self): pm.py_send('*remove_sets ntemp') pm.py_send('*store_nodes ntemp all_selected') n = pm.py_get_int('nsets()') k = pm.py_get_int('nset_entries(%d)' % n) #pm.py_send('*remove_set_entries ntemp all_existing') return k
def tomentat(self): if isinstance(self.connectivity[0], Node): conns = ' '.join(str(x.mentat_id) for x in self.connectivity) else: conns = ' '.join(str(x) for x in self.connectivity) cdict = {'Hex (8)': 'hex8', 'Quad (4)': 'quad4', 'Tria (3)': 'tria3'} pm.py_send('*set_element_class', cdict[self.category]) pm.py_send('*add_elements', conns) mentat_id = pm.py_get_int('max_element_id()') try: element_set = self.options['membership'] end = ' #' mentat_command = ' '.join( ['*store_elements', element_set, str(mentat_id), end]) pm.py_send(mentat_command) except: pass self.mentat_id = mentat_id
def find_n_corn(unit_p) -> list: """Find the corner node IDs of a grid Parameters ---------- unit_p : unit_p The unit parameters Returns ------- list The list of node IDs """ # Initialisations n_corn = [] # Calculate the element IDs of the corner elements e_corn = [unit_p.template.n_e + 1] e_corn.append(unit_p.template.n_e * 5 - len(unit_p.rem) * 4 + unit_p.template.x_e) e_corn.append(unit_p.template.n_e * 25 - len(unit_p.rem) * 24) e_corn.append(unit_p.template.n_e * 21 - len(unit_p.rem) * 20 - unit_p.template.x_e + 1) # Loop through the list of corner elements for i in range(1, len(e_corn) + 1): # Determine the node ID of the corner node n_corn.append( py_get_int("element_node_id({}, {})".format(e_corn[i - 1], i))) return n_corn
def tomentat(self): conns_flat = self.connectivity.flatten() if isinstance(conns_flat[0],Point): if 'Arc' in self.category or 'Circle' in self.category: # point coordinates conns = ' '.join(' '.join(str(y) for y in x.coordinates) for x in conns_flat) else: # point numbers conns = ' '.join(str(x.mentat_id) for x in conns_flat) else: conns = ' '.join(str(x) for x in conns_flat) cdict = {'Line': 'line', 'Bezier': 'bezier', 'Cubic Spline':'cubic_spline', 'NURBS': 'nurb', 'Polyline':'polyline', 'Composite':'composite', 'Circle Cen/Pnt':'circle_cp', 'Arc Cen/Pnt/Pnt':'arc_cpp'} pm.py_send('*set_curve_type', cdict[self.category]) pm.py_send('*add_curves', conns) mentat_id = pm.py_get_int('max_curve_id()') self.mentat_id = mentat_id try: element_set = self.options['membership'] mentat_command = ' '.join(['*store_curves', element_set, str(mentat_id), ' #']) pm.py_send(mentat_command) except: pass mentat_command = ' '.join(['*apply_curve_divisions', str(self.mentat_id), ' #']) try: curve_div = self.options['Divisions'] pm.py_send('*set_curve_div_type_fix_ndiv') pm.py_send('*set_curve_div_num', str(curve_div)) pm.py_send(mentat_command) except: pass try: target_length = self.options['Length'] pm.py_send('*set_curve_div_type_fix_avgl') pm.py_send('*set_curve_div_avgl', str(target_length)) pm.py_send(mentat_command) except: pass try: l2l1 = self.options['Ratio'] pm.py_send('*set_curve_div_type_variable_l1l2') pm.py_send('*set_curve_div_opt_ndiv_l2l1_ratio') pm.py_send('*set_curve_div_l2l1_ratio', str(l2l1)) pm.py_send(mentat_command) except: pass
def submit(self): self.joblist = [] njobs = pm.py_get_int('njobs()') for j in range(njobs): joblabel = pm.py_get_string('job_name_index(%d)' % j) self.joblist.append(joblabel) self.mentat_id = self.joblist.index(self.label) + 1 pm.py_send('*submit_job %d' % self.mentat_id) # % *monitor_job
def post_processing(): py_mentat.py_send('*post_open_default') py_mentat.py_send('*post_value Equivalent Von Mises Stress') py_mentat.py_send('*post_contour_bands') n_id = py_mentat.py_get_int("scalar_max_node()") value = py_mentat.py_get_float('scalar_1({})'.format(n_id)) if value: print('the result is ', value) else: print('no value')
def _get_set_entry_list(self, centroid, nnodes, extend, axes=[0, 1, 2], tol=1e-12): nodes = np.zeros(0, dtype=int) #coord = np.zeros((0,3)) dist = np.zeros(0) n = pm.py_get_int('nsets()') k = pm.py_get_int('nset_entries(%d)' % n) #print('nsets, entries', n, k) for k in range(1, k + 1): node = pm.py_get_int('set_entry(%d,%d)' % (n, k)) nodes = np.append(nodes, node) x = pm.py_get_float('node_x(%d)' % node) y = pm.py_get_float('node_y(%d)' % node) z = pm.py_get_float('node_z(%d)' % node) xi = np.array([x, y, z]) dist = np.append( dist, np.linalg.norm(centroid.take(axes) - xi.take(axes))) #coord = np.vstack((coord,[x,y,z])) sorted_nodes = nodes[np.argsort(dist)] sorted_dist = np.sort(dist) j = 0 if extend: while (sorted_dist[nnodes + j] - sorted_dist[nnodes - 1] )**2 < tol and nnodes + j < len(dist): j = j + 1 if j > 0: print( 'WARNING: Extended number of NextNodes from %d to %d due to equal distances.' % (nnodes, nnodes + j)) return sorted_nodes[:nnodes + j]
def tomentat(self): x,y,z = self.coordinates pm.py_send("*add_nodes %f %f %f" % (x,y,z)) mentat_id = pm.py_get_int('max_node_id()') try: node_set = self.options['membership'] end = ' #' mentat_command = ' '.join(['*store_nodes', node_set, str(mentat_id), end]) pm.py_send(mentat_command) except: pass self.mentat_id = mentat_id
def tomentat(self): conns = ' '.join(str(x) for x in self.connectivity) pm.py_send('*set_element_class', self.category) pm.py_send('*add_elements', conns) mentat_id = pm.py_get_int('max_element_id()') try: element_set = self.options['membership'] end = ' #' mentat_command = ' '.join( ['*store_elements', element_set, str(mentat_id), end]) pm.py_send(mentat_command) except: pass self.mentat_id = mentat_id
def sweep_nodes( a: str, c: int, ) -> None: """Sweep a specific set of nodes Parameters ---------- a : str The axis of the nodes c : int The coordinate of the nodes """ # Initialisations n_l = [] # Fetch the total number of nodes n_n = py_get_int("max_node_id()") # Loop through the number of nodes for i in range(1, n_n + 1): # Fetch the relevant coordinate of the current node c_n = py_get_float("node_{}({})".format(a, i)) # Check if the selected coordinate matches the desirec coordinate if c_n == c: # Add the node to the list n_l.append(i) # Sweep the nodes py_send("*sweep_nodes ") # Loop through the selected nodes for i in n_l: py_send("{} ".format(i)) py_send("#") return
def submit(self, monitor=False, verbose=0): self.joblist = [] njobs = pm.py_get_int('njobs()') for j in range(njobs): joblabel = pm.py_get_string('job_name_index(%d)' % j) self.joblist.append(joblabel) self.mentat_id = self.joblist.index(self.label) + 1 pm.py_send('*submit_job %d' % self.mentat_id) stsfile = '.'.join( self.filename.split('.')[:-1]) + '_' + self.label + '.sts' if monitor: pm.py_send('*monitor_job') else: # pause script while job is running while True: # check every 5 seconds if job has finished by evaluating # model_job##.sts file time.sleep(5) with open(stsfile, 'r') as f1: lines = f1.readlines() try: # catch error if less than 3 lines are present exit_message = lines[-3] except: exit_message = '' # check if job has finished if '3004' in exit_message: if verbose > 0: print(exit_message) break else: # print current loadcase and increment try: # catch error if the last line does not contain the progress lc = int(lines[-1][:7]) inc = int(lines[-1][7:15]) progress = 'current loadcase %d / inc. %d' % (lc, inc) if verbose > 0: print(progress) except: pass
def add_bc_fd_edge( label: str, a: str, d: str, e: int, m: float, tab_nam: str = None, ) -> None: """[summary] Parameters ---------- label : str The label of the boundary condition a : str The axis of the applied boundary condition "x" or "y" d : str The direction of the applied boundary condition "x" or "y" e : int The edge coordinate of the boundary condition m : float The magnitude of the applied displacement tab_nam : str, optional The name of the table defining the displacement function, by default None """ # Initialisation n_l = [] # Fetch the total number of nodes n_n = py_get_int("max_node_id()") # Apply the fixed boundary condition py_send("*new_apply") py_send("*apply_type fixed_displacement") py_send("*apply_name bc_fd_{}".format(label)) py_send("*apply_dof {}".format(a)) py_send("*apply_dof_value {} {}".format(a, m)) # Apply the displacement function if applicable if tab_nam is not None: py_send("*apply_dof_table {} {}".format(a, tab_nam)) # Loop through the number of nodes for i in range(1, n_n + 1): # Fetch the relevant coordinate of the current node c_n = py_get_float("node_{}({})".format(d, i)) # Check if the selected coordinate matches the desired edge if c_n == e: # Add the node to the list n_l.append(i) # Apply the boundary condition to the selected nodes py_send("*add_apply_nodes ") # Loop through the selected nodes for i in n_l: py_send("{} ".format(i)) py_send("#") return
parser.error('file can not be specified when port is given.') if filenames == []: filenames = [None] if remote: import py_mentat damask.util.report(scriptName, 'waiting to connect...') filenames = [ os.path.join(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + '.mfd') ] try: py_mentat.py_connect('', options.port) py_mentat.py_send('*set_save_formatted on') py_mentat.py_send('*save_as_model "{}" yes'.format(filenames[0])) py_mentat.py_get_int("nnodes()") except py_mentat.InputError as err: damask.util.croak( '{}. Try Tools/Python/"Run as Separate Process" & "Initiate".'. format(err)) sys.exit(-1) damask.util.croak('connected...') for name in filenames: while remote and not os.path.exists(name): time.sleep(0.5) with open(name, 'r') if name is not None else sys.stdin as fileIn: damask.util.report(scriptName, name) mfd = parseMFD(fileIn) add_servoLinks(mfd, [options.x, options.y, options.z])
try: import py_mentat except: damask.util.croak('no valid Mentat release found.') sys.exit(-1) damask.util.report(scriptName, 'waiting to connect...') filenames = [ os.path.join(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()) + '.mfd') ] try: py_mentat.py_connect('', options.port) py_mentat.py_send('*set_save_formatted on') py_mentat.py_send('*save_as_model "{}" yes'.format(filenames[0])) py_mentat.py_get_int( "nnodes()") # hopefully blocks until file is written except: damask.util.croak( 'failed. try setting Tools/Python/"Run as Separate Process" & "Initiate".' ) sys.exit() damask.util.croak('connected...') for name in filenames: while remote and not os.path.exists(name): time.sleep(0.5) # wait for Mentat to write MFD file with open(name, 'r') if name is not None else sys.stdin as fileIn: damask.util.report(scriptName, name) mfd = parseMFD(fileIn) add_servoLinks(mfd, [options.x, options.y, options.z])