def __init__(self, nodes, elements, edges, mesh_files): """ Test case for a Lagrange mesh. Tries to test an Argyris mesh. """ for mesh_file in mesh_files: self.nodes = nodes # The mesh classes try to flatten nodes if possible. Do it here too. if np.all(self.nodes[:, -1] == self.nodes[0, -1]): self.nodes = self.nodes[:, 0:-1] self.elements = elements self.edges = edges mesh = meshes.mesh_factory(*mesh_file) parsed_mesh = parsers.parser_factory(*mesh_file) npt.assert_equal(self.nodes, mesh.nodes) npt.assert_equal(self.elements, mesh.elements) if parsed_mesh.edges: assert set(self.edges) == reduce( lambda a, b: a + b, mesh.edge_collections.values()) npt.assert_almost_equal(meshtools.project_nodes( lambda x: x[0:2], self.elements, self.nodes), self.nodes[:, 0:2], decimal=10) if parsed_mesh.edges: assert set(map(lambda x : x[0:-1], self.edges)) == \ set(map(lambda x : x[0:-1], meshtools.extract_boundary_edges(self.elements))) # Test Argyris stuff. if self.elements.shape[1] == 6: TestArgyrisCase(mesh_file, parsed_mesh)
def __init__(self, nodes, elements, edges, mesh_files): """ Test case for a Lagrange mesh. Tries to test an Argyris mesh. """ for mesh_file in mesh_files: self.nodes = nodes # The mesh classes try to flatten nodes if possible. Do it here too. if np.all(self.nodes[:, -1] == self.nodes[0, -1]): self.nodes = self.nodes[:,0:-1] self.elements = elements self.edges = edges mesh = meshes.mesh_factory(*mesh_file) parsed_mesh = parsers.parser_factory(*mesh_file) npt.assert_equal(self.nodes, mesh.nodes) npt.assert_equal(self.elements, mesh.elements) if parsed_mesh.edges: assert set(self.edges) == reduce(lambda a, b : a + b, mesh.edge_collections.values()) npt.assert_almost_equal(meshtools.project_nodes(lambda x : x[0:2], self.elements, self.nodes), self.nodes[:,0:2], decimal=10) if parsed_mesh.edges: assert set(map(lambda x : x[0:-1], self.edges)) == \ set(map(lambda x : x[0:-1], meshtools.extract_boundary_edges(self.elements))) # Test Argyris stuff. if self.elements.shape[1] == 6: TestArgyrisCase(mesh_file, parsed_mesh)
def __init__(self, mesh_file, parsed_mesh): argyris_mesh = meshes.mesh_factory(*mesh_file, argyris=True) lagrange_mesh = meshes.mesh_factory(*mesh_file) assert argyris_mesh.elements.shape == (parsed_mesh.elements.shape[0], 21) stacked_nodes = dict() edges_by_midpoint = dict() for element in argyris_mesh.elements: for local_number, global_number in enumerate(element[0:3]): corner_nodes = (element[3 + 2 * local_number], element[3 + 2 * local_number + 1], element[9 + 3 * local_number], element[9 + 3 * local_number + 1], element[9 + 3 * local_number + 2]) if stacked_nodes.has_key(global_number): assert corner_nodes == stacked_nodes[global_number] else: stacked_nodes[global_number] = corner_nodes for node in corner_nodes: npt.assert_almost_equal( argyris_mesh.nodes[global_number - 1, :], argyris_mesh.nodes[node - 1, :]) for midpoint_number, local_edge in enumerate([[0, 1], [0, 2], [1, 2]]): midpoint = element[18 + midpoint_number] if edges_by_midpoint.has_key(midpoint): npt.assert_equal(edges_by_midpoint[midpoint], element[local_edge]) else: edges_by_midpoint[midpoint] = element[local_edge] # Ensure that the Argyris mesh and the Lagrange mesh come up with the # same edges. for name, collection in lagrange_mesh.edge_collections.items(): argyris_collection = \ map(lambda x : x.edge, filter(lambda x : x.name == name, argyris_mesh.node_collections)[0].edges) for edge in collection: argyris_edge = (min(edge[0:2]), max(edge[0:2]), edge[2]) assert argyris_edge in argyris_collection
def __init__(self, mesh_file, parsed_mesh): argyris_mesh = meshes.mesh_factory(*mesh_file, argyris=True) lagrange_mesh = meshes.mesh_factory(*mesh_file) assert argyris_mesh.elements.shape == (parsed_mesh.elements.shape[0],21) stacked_nodes = dict() edges_by_midpoint = dict() for element in argyris_mesh.elements: for local_number, global_number in enumerate(element[0:3]): corner_nodes = (element[3 + 2*local_number], element[3 + 2*local_number + 1], element[9 + 3*local_number], element[9 + 3*local_number + 1], element[9 + 3*local_number + 2]) if stacked_nodes.has_key(global_number): assert corner_nodes == stacked_nodes[global_number] else: stacked_nodes[global_number] = corner_nodes for node in corner_nodes: npt.assert_almost_equal( argyris_mesh.nodes[global_number - 1, :], argyris_mesh.nodes[node - 1, :]) for midpoint_number, local_edge in enumerate([[0,1], [0,2], [1,2]]): midpoint = element[18 + midpoint_number] if edges_by_midpoint.has_key(midpoint): npt.assert_equal(edges_by_midpoint[midpoint], element[local_edge]) else: edges_by_midpoint[midpoint] = element[local_edge] # Ensure that the Argyris mesh and the Lagrange mesh come up with the # same edges. for name, collection in lagrange_mesh.edge_collections.items(): argyris_collection = \ map(lambda x : x.edge, filter(lambda x : x.name == name, argyris_mesh.node_collections)[0].edges) for edge in collection: argyris_edge = (min(edge[0:2]), max(edge[0:2]), edge[2]) assert argyris_edge in argyris_collection
x2, y2 = coordinates[1] x3, y3 = coordinates[2] return np.linalg.inv(np.array([[x1 - x3, x2 - x3], [y1 - y3, y2 - y3]])) if __name__ == '__main__': eps = sys.float_info.epsilon # USER SET PARAMETERS reynolds = 1 eps = 0.01 mesh_file = './files/box.mesh' print "Loading and parsing the mesh..." domain = msh.mesh_factory(mesh_file) elements = domain.elements nodes = np.unique(elements) pnodes = np.unique(elements.T[:3]) interior_nodes = domain.interior_nodes coordinates = domain.nodes n, m = len(nodes), len(pnodes) k = len(interior_nodes) weight = 1 / 6.0 gauss_pts = np.array([[0.5, 0.0], [0.0, 0.5], [0.5, 0.5]]) A = sp.lil_matrix((n, n)) Bup = sp.lil_matrix((n, m))
from cfem import * sys.path.append(os.path.expanduser("~/Documents/Code/ArgyrisPack")) sys.path.append(os.path.expanduser("~/Documents/Code/PODSUPG")) import ap.mesh.meshes as m import supg.core.gmsh as gmsh import supg.core.supg as spg import supg.cdr.Operators2DCDR as operators # Create example data. mesh_file = util.unique_file(suffix=".msh") geo_file = util.unique_file(suffix=".geo") gmsh.write_geo_file(gmsh.unit_square(0.05), geo_file=geo_file) gmsh.call_gmsh(geo_file=geo_file, mesh_file=mesh_file) mesh = m.mesh_factory(mesh_file) # compute the PODSUPG operators too. fe_facade = spg.SUPGFacade(order=2) ops = operators.Operators2DCDR(mesh, fe_facade, stabilization_coeff=3.0) if mesh.interior_nodes.min() == 0: last_boundary_node = max(mesh.boundary_nodes['land']) else: last_boundary_node = max(mesh.boundary_nodes['land']) - 1 first_interior_node = last_boundary_node + 1 mass = get_matrix('mass', mesh, stabilization_coeff=3.0).todense() convection = get_matrix('convection', mesh, stabilization_coeff=3.0).todense() stiffness = get_matrix('stiffness', mesh, stabilization_coeff=3.0).todense() hessian = get_matrix('hessian', mesh, stabilization_coeff=3.0).todense()
from cfem import * sys.path.append(os.path.expanduser("~/Documents/Code/ArgyrisPack")) sys.path.append(os.path.expanduser("~/Documents/Code/PODSUPG")) import ap.mesh.meshes as m import supg.core.gmsh as gmsh import supg.core.supg as spg import supg.cdr.Operators2DCDR as operators # Create example data. mesh_file = util.unique_file(suffix=".msh") geo_file = util.unique_file(suffix=".geo") gmsh.write_geo_file(gmsh.unit_square(0.05), geo_file=geo_file) gmsh.call_gmsh(geo_file=geo_file, mesh_file=mesh_file) mesh = m.mesh_factory(mesh_file) # compute the PODSUPG operators too. fe_facade = spg.SUPGFacade(order=2) ops = operators.Operators2DCDR(mesh, fe_facade, stabilization_coeff=3.0) if mesh.interior_nodes.min() == 0: last_boundary_node = max(mesh.boundary_nodes["land"]) else: last_boundary_node = max(mesh.boundary_nodes["land"]) - 1 first_interior_node = last_boundary_node + 1 mass = get_matrix("mass", mesh, stabilization_coeff=3.0).todense() convection = get_matrix("convection", mesh, stabilization_coeff=3.0).todense() stiffness = get_matrix("stiffness", mesh, stabilization_coeff=3.0).todense() hessian = get_matrix("hessian", mesh, stabilization_coeff=3.0).todense()
def stokes(): eps = sys.float_info.epsilon # USER SET PARAMETERS reynolds = 1e0 eps = 1e-4 / reynolds mesh_file = 'convergence/01.mesh' root_dir = './files/' print "Loading and parsing the mesh..." domain = m.mesh_factory(root_dir + mesh_file) elements = domain.elements nodes = np.unique(elements) pnodes = np.unique(elements.T[:3]) interior_nodes = domain.interior_nodes coordinates = domain.nodes n, m = len(nodes), len(pnodes) weight = 1 / 6.0 gauss_pts = np.array([[0.5, 0.0], [0.0, 0.5], [0.5, 0.5]]) A = sp.lil_matrix((n, n)) B_up = sp.lil_matrix((n, m)) B_vp = sp.lil_matrix((n, m)) F_x = np.zeros(n) F_y = F_x.copy() quad_basis = fn.Quadratic_Basis_Function() lin_basis = fn.Linear_Basis_Function() counter = 1 total = len(elements) print "Constructing system..." for element in elements: #if np.mod(counter, 100) == 1: #print "Element %d of %d..." % (counter, total) # Precalculate some stuff element_coords = get_coordinates(element, domain.nodes) B = calculate_B(element_coords[:3]) detJ = 1 / np.abs(np.linalg.det(B)) weight_scaled = weight * detJ # Allocate local matrices local_A = sp.lil_matrix((6, 6)) #local_T = sp.lil_matrix((3, 3)) local_B_up = sp.lil_matrix((6, 3)) local_B_vp = sp.lil_matrix((6, 3)) local_F_x = np.zeros(n) local_F_y = np.zeros(n) for i in xrange(6): # Assemble load vectors for point in gauss_pts: x_g, y_g = point local_F_x[i] += weight_scaled * f1(x_g, y_g) local_F_y[i] += weight_scaled * f2(x_g, y_g) F_x[element[i] - 1] += local_F_x[i] F_y[element[i] - 1] += local_F_y[i] for j in xrange(6): # Assemble S for point in gauss_pts: x_g, y_g = point local_A[i, j] += (weight_scaled / reynolds) * np.dot( np.dot(quad_basis.grad(j, x_g, y_g), B), np.dot(B.T, quad_basis.grad(i, x_g, y_g))) A[element[i] - 1, element[j] - 1] += local_A[i, j] if j < 3: # Assemble Gx and Gy for point in gauss_pts: x_g, y_g = point local_B_up[i, j] += weight_scaled * ( lin_basis(j, x_g, y_g) * np.dot(quad_basis.grad(i, x_g, y_g), B[0])) local_B_vp[i, j] += weight_scaled * ( lin_basis(j, x_g, y_g) * np.dot(quad_basis.grad(i, x_g, y_g), B[1])) pnode_j = np.where(pnodes == element[j])[0][0] B_up[element[i] - 1, pnode_j] += local_B_up[i, j] B_vp[element[i] - 1, pnode_j] += local_B_vp[i, j] counter += 1 A = A.tocsr() A = A[interior_nodes - 1, :] A = A.tocsc() A = A[:, interior_nodes - 1] B_up = B_up.tocsr() B_up = B_up[interior_nodes - 1, :] B_vp = B_vp.tocsr() B_vp = B_vp[interior_nodes - 1, :] F_x = F_x[interior_nodes - 1, :] F_y = F_y[interior_nodes - 1, :] # Update dimensions since we nuked the Dirchlet BCs, all = 0 k = len(interior_nodes) M = sp.bmat([[A, sp.csc_matrix((k, k))], [sp.csc_matrix((k, k)), A]]) + \ sp.bmat([[B_up.dot(B_up.T), B_up.dot(B_vp.T)], [B_vp.dot(B_up.T), B_vp.dot(B_vp.T)]]) / eps M = M.tocsc() print "Solving the linear systems..." UV = np.zeros(2 * k) UV = spl.gmres(M, np.append(F_x, F_y))[0] P = np.zeros(m) P = -sp.bmat([[B_up.T, B_vp.T]]).dot(UV) / eps U = np.zeros(n) V = np.zeros(n) U[interior_nodes - 1] += UV[:k] V[interior_nodes - 1] += UV[k:2 * k] UVP = np.zeros(2 * n + m) UVP[:n] = U[:] UVP[n:2 * n] = V[:] UVP[2 * n:] = P[:] # POST PROCESSING for system one x, y = coordinates[pnodes - 1].T tri = np.zeros(np.shape(elements[:, :3])) for i, element in enumerate(elements[:, :3]): for j, vert in enumerate(element): tri[i, j] = np.where(pnodes == vert)[0][0] print "Saving some data..." p = UVP[2 * n:] u = UVP[n:2 * n] v = UVP[:n] np.savetxt('./files/UVP.txt', UVP) np.savetxt('./files/tri.txt', tri) np.savetxt('./files/x.txt', x) np.savetxt('./files/y.txt', y) np.savetxt('./files/p.txt', p) np.savetxt('./files/u.txt', u[pnodes - 1]) np.savetxt('./files/v.txt', v[pnodes - 1]) print "All done! Thank you, come again!\n\n"