def writeGEO(grid, mesh, filename): """Example: grid = np.array([[0,0],[0.5,0.5],[0,0.5]]), mesh = np.array([1,4,3])""" # with pygmsh.geo.Geometry() as geom: # for e in range(mesh.shape[0] // 3): # p1 = grid[mesh[3*e]] # p2 = grid[mesh[3*e+1]] # p3 = grid[mesh[3*e+2]] # # geom.add_polygon( # [ # list(p1),list(p2),list(p3) # ], # ) # mesh = geom.generate_mesh() # # mesh.write(filename) mesh_geo = Mesh() pt_list = [] for i in range(grid.shape[0]): p = Entity.Point(grid[i, :]) pt_list.append(p) mesh_geo.addEntity(p) for e in range(mesh.shape[0] // 3): p1 = pt_list[mesh[3 * e]] p2 = pt_list[mesh[3 * e + 1]] p3 = pt_list[mesh[3 * e + 2]] l1 = Entity.Curve([p1, p2]) l2 = Entity.Curve([p2, p3]) l3 = Entity.Curve([p3, p1]) # entities can also be added in a batch mesh_geo.addEntities([l1, l2, l3]) mesh_geo.writeGeo(filename)
def geometry_to_gmsh(domain): import py2gmsh from py2gmsh.Mesh import * from py2gmsh.Entity import * from py2gmsh.Fields import * self = domain lines_dict = {} mesh = Mesh() if self.boundaryTags: for i, tag in enumerate(self.boundaryTags): phys = PhysicalGroup(nb=i, name=tag) mesh.addGroup(phys) for i, v in enumerate(self.vertices): if domain.nd == 2: p = Point([v[0], v[1], 0.]) else: p = Point(v) mesh.addEntity(p) g = mesh.groups.get(self.vertexFlags[i]) if g: g.addEntity(p) nb_points = i + 1 for i in range(nb_points): lines_dict[i] = {} for i, s in enumerate(self.segments): lines_dict[s[0]][s[1]] = i l = Line([mesh.points[s[0] + 1], mesh.points[s[1] + 1]]) mesh.addEntity(l) g = mesh.groups.get(self.segmentFlags[i]) if g: g.addEntity(l) for i, f in enumerate(self.facets): if self.nd == 3 or (self.nd == 2 and i not in self.holes_ind): lineloops = [] for j, subf in enumerate(f): lineloop = [] # vertices in facet for k, ver in enumerate(subf): if ver in lines_dict[subf[k - 1]].keys(): lineloop += [lines_dict[subf[k - 1]][ver] + 1] elif subf[k - 1] in lines_dict[ver].keys(): # reversed lineloop += [(lines_dict[ver][subf[k - 1]] + 1)] else: l = Line([ mesh.points[subf[k - 1] + 1], mesh.points[ver + 1] ]) mesh.addEntity(l) lineloop += [l.nb] ll = LineLoop(mesh.getLinesFromIndex(lineloop)) mesh.addEntity(ll) lineloops += [ll.nb] s = PlaneSurface([mesh.lineloops[loop] for loop in lineloops]) mesh.addEntity(s) g = mesh.groups.get(self.facetFlags[i]) if g: g.addEntity(s) for i, V in enumerate(self.volumes): surface_loops = [] if i not in self.holes_ind: for j, sV in enumerate(V): sl = SurfaceLoop((np.array(sV) + 1).tolist()) mesh.addEntity(sl) surface_loops += [sl.nb] vol = Volume(surface_loops) mesh.addEntity(vol) g = mesh.groups.get(self.regionFlags[i]) if g: g.addEntity(vol) return mesh
def objective(x, sign=-1.0): my_mesh = Mesh() d = x[0] a = x[1] L = x[2] if volume(x) > 10 * V_sk: d1 = 0.05 elif volume(x) > 2 * V_sk: d1 = 0.005 else: d1 = 0.0005 filename = 'my_mesh' # create points p1 = Entity.Point([0., 0., 0., d1]) #fyrsti punktur neðri vinstri # add point to mesh my_mesh.addEntity(p1) #create more points p2 = Entity.Point([0., a / 2, 0., d1]) #2. punktur efri vinstri my_mesh.addEntity(p2) p3 = Entity.Point([t_veggur, a / 2, 0., d1]) #3. punktur efri hægri my_mesh.addEntity(p3) p4 = Entity.Point([t_veggur, (a - d) / 2 + d - a / 2, 0., d1]) #4. punktur niður frá efri hægri my_mesh.addEntity(p4) p5 = Entity.Point([t_veggur + L, (a - d) / 2 + d - a / 2, 0., d1]) #5.punktur endi á ribbu efri my_mesh.addEntity(p5) p6 = Entity.Point([t_veggur + L, 0., 0., d1]) #6. punktur endi á ribbu neðri my_mesh.addEntity(p6) # create curves l1 = Entity.Curve([p1, p2]) #innri bein lína upp l2 = Entity.Curve([p2, p3]) # efri hlið einangrun l3 = Entity.Curve([p3, p4]) # ytri bein lína upp l4 = Entity.Curve([p4, p5]) #ribba bein lína upp l5 = Entity.Curve([p5, p6]) #ribba endi l6 = Entity.Curve([p6, p1]) #ribba bein lína niðri my_mesh.addEntities([l1, l2, l3, l4, l5, l6]) ll1 = Entity.CurveLoop([l1, l2, l3, l4, l5, l6], mesh=my_mesh) s1 = Entity.PlaneSurface([ll1], mesh=my_mesh) g1 = Entity.PhysicalGroup(name='innri') g2 = Entity.PhysicalGroup(name='ytri') g3 = Entity.PhysicalGroup(name='ribba') g4 = Entity.PhysicalGroup(name='einangrun') my_mesh.addEntities([g1, g2, g3, g4]) g1.addEntities([l1]) g2.addEntities([l3, l4, l5, l6]) g4.addEntities([l2]) g3.addEntities([s1]) # set max element size #my_mesh.Options.Mesh.CharacteristicLengthMax = 0.1 # adding Coherence option my_mesh.Coherence = True # write the geofile #os.system('rm .geo') try: my_mesh.writeGeo('{}.geo'.format(filename)) os.system('gmsh {}.geo -2 -o {}.msh'.format(filename, filename)) except: return -0.1 #os.system('gmsh my_mesh.geo') try: xu, y, tri, T, V, q = axifem.axiHeatCond('{}.msh'.format(filename), \ {'ribba':k}, {'ytri':(h_utan,-h_utan*T_inf_utan),'innri':(h_innan,-h_innan*T_inf_innan),'einangrun':(0,0)}) print(sign * q['ytri'][1]) except: return -0.1 return sign * q['ytri'][1]
#sol = differential_evolution(objective,bounds,constraints = (nlc1,nlc3,nlc4)) sol = minimize(objective, x0, method='COBYLA', constraints=cons) a = sol['x'][1] print(a) #sol = minimize(objective,x0,method='SLSQP',options={'gtol': 1e-6, 'disp': True}, constraints = cons) print(sol) print('initial volume: {}'.format(V_sk)) x, y, tri, T, V, q = axifem.axiHeatCond('my_mesh.msh', \ {'ribba':k}, {'ytri':(h_utan,-h_utan*T_inf_utan),'innri':(h_innan,-h_innan*T_inf_innan),'einangrun':(0,0)}) my_mesh = Mesh() # create points p1 = Entity.Point([0., 0., 0., d1]) #fyrsti punktur neðri vinstri # add point to mesh my_mesh.addEntity(p1) #create more points p2 = Entity.Point([0., a / 2, 0., d1]) #2. punktur efri vinstri my_mesh.addEntity(p2) p3 = Entity.Point([t_veggur, a / 2, 0., d1]) #3. punktur efri hægri my_mesh.addEntity(p3) p4 = Entity.Point([t_veggur, 0., 0., d1]) #4. punktur niður frá efri hægri my_mesh.addEntity(p4) # create curves l1 = Entity.Curve([p1, p2]) #innri bein lína upp l2 = Entity.Curve([p2, p3]) # efri hlið einangrun l3 = Entity.Curve([p3, p4]) # ytri bein lína upp l4 = Entity.Curve([p4, p1]) #neðri lína
def _mesh_geometry( self, x_camber: np.ndarray, upper_curve: np.ndarray, lower_curve: np.ndarray, ) -> Mesh: """ Function computing a triangulation of the airfoil domain. :params: x_camber: x-coeficients of points used in the discretization. upper_curve: y-coefficients of the upper profile of the sampled airfoil. The discretization is evaluated at x_camber points. lower_curve: y-coefficients of the lower profile of the sampled airfoil. The discretization is evaluated at x_camber points. :returns: mash: airfoil's triangulation computed using GMSH package. """ mesh = Mesh() top_left = Entity.Point([*self.top_left_corner, 0]) top_right = Entity.Point( [self.bottom_right_corner[0], self.top_left_corner[1], 0]) bottom_left = Entity.Point( [self.top_left_corner[0], self.bottom_right_corner[1], 0]) bottom_right = Entity.Point([*self.bottom_right_corner, 0]) # Add domain's corners to the geometry mesh.addEntity(top_left) mesh.addEntity(top_right) mesh.addEntity(bottom_left) mesh.addEntity(bottom_right) # Define the outer boundary top_boundary = Entity.Curve([top_left, top_right]) right_boundary = Entity.Curve([top_right, bottom_right]) bottom_boundary = Entity.Curve([bottom_right, bottom_left]) left_boundary = Entity.Curve([bottom_left, top_left]) domain_outer_boundary = [ top_boundary, right_boundary, bottom_boundary, left_boundary, ] mesh.addEntities(domain_outer_boundary) # Add airfoil points airfoil_points = [] for x, y in zip(x_camber, upper_curve): point = Entity.Point([x, y, 0]) airfoil_points.append(point) mesh.addEntity(point) for x, y in zip(x_camber[::-1][1:-1], lower_curve[::-1][1:-1]): point = Entity.Point([x, y, 0]) airfoil_points.append(point) mesh.addEntity(point) # Discretize the airfoil profile intervals = [] for i in range(len(airfoil_points) - 1): point1 = airfoil_points[i] point2 = airfoil_points[i + 1] interval = Entity.Curve([point1, point2]) intervals.append(interval) # Close the profile using endpoints point1 = airfoil_points[-1] point2 = airfoil_points[0] interval = Entity.Curve([point1, point2]) intervals.append(interval) mesh.addEntities(intervals) field_boundary_layer = Field.BoundaryLayer(mesh=mesh) # Setting boundary layer parameters field_boundary_layer.EdgesList = intervals field_boundary_layer.AnisoMax = 1.0 field_boundary_layer.hfar = 0.2 field_boundary_layer.hwall_n = 0.001 field_boundary_layer.thickness = 0.05 field_boundary_layer.ratio = 1.1 field_boundary_layer.Quads = 1 field_boundary_layer.IntersectMetrics = 0 mesh.BoundaryLayerField = field_boundary_layer # set max element size mesh.Options.Mesh.CharacteristicLengthMax = 0.3 return mesh
a = x0[1] L = x0[2] d1 = 0.0002 d2 = 0.0005 V_sk = 1.8849555921538741e-06 tolerance = 6e-013 #tolerance for volume V_sk = np.pi * (a / 2)**2 * t_veggur + np.pi * (d / 2)**2 * L print(V_sk * 2) # create Mesh class instance my_mesh = Mesh() # create points p1 = Entity.Point([0., -a / 2, 0., d1]) #fyrsti punktur neðri vinstri # add point to mesh my_mesh.addEntity(p1) #create more points p2 = Entity.Point([0., a - a / 2, 0., d1]) #2. punktur efri vinstri my_mesh.addEntity(p2) p3 = Entity.Point([t_veggur, a - a / 2, 0., d1]) #3. punktur efri hægri my_mesh.addEntity(p3) p4 = Entity.Point([t_veggur, (a - d) / 2 + d - a / 2, 0., d1]) #4. punktur niður frá efri hægri my_mesh.addEntity(p4) p5 = Entity.Point([t_veggur + L, (a - d) / 2 + d - a / 2, 0., d1]) #5.punktur endi á ribbu efri my_mesh.addEntity(p5) p6 = Entity.Point([t_veggur + L, (a - d) / 2 - a / 2, 0., d1]) #6. punktur endi á ribbu neðri