def test_ElementDict_to_ElementDict(self): # Init 1 mesh1 = Mesh() mesh1.element = ElementDict() mesh1.element.connectivity = { "Triangle": np.array([[0, 1, 2], [1, 2, 3]]) } mesh1.element.tag = {"Triangle": np.array([1, 2])} mesh1.element.nb_elem = {"Triangle": 2} mesh1.element.nb_node_per_element = {"Triangle": 3} mesh2 = Mesh() mesh2.element = ElementMat() mesh2.element.connectivity = np.array([[0, 1, 2], [1, 2, 3]]) mesh2.element.nb_elem = 2 mesh2.element.nb_node_per_element = 3 mesh_new = Mesh() mesh_new.element = ElementDict() # Method test 2 mesh_new.element.convert_element(mesh1.element) # Check results 1 solution = np.array([[0, 1, 2], [1, 2, 3] ]) # Warning, elements tags, not line position ! testA = np.sum( abs(solution - mesh_new.element.connectivity["Triangle"])) msg = ("Wrong projection: returned " + str(mesh_new.element.connectivity["Triangle"]) + ", expected: " + str(solution)) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
def test_ElementDict(self): # Init 1 mesh = Mesh() mesh.element = ElementDict() mesh.element.connectivity = {"Triangle": np.array([[0, 1, 2], [1, 2, 3]])} mesh.element.tag = {"Triangle": np.array([1, 2])} mesh.element.nb_elem = {"Triangle": 2} mesh.element.nb_node_per_element = {"Triangle": 3} # Method test 1 elem_tag = mesh.element.get_node2element(1) # Check results solution = np.array([1, 2]) # Warning, elements tags, not line position ! testA = np.sum(abs(solution - elem_tag)) msg = ( "Wrong projection: returned " + str(elem_tag) + ", expected: " + str(solution) ) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA) # Method test 2 elem_tag = mesh.element.get_node2element(3) solution = np.array([2]) testA = np.sum(abs(solution - elem_tag)) msg = ( "Wrong projection: returned " + str(elem_tag) + ", expected: " + str(solution) ) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
def test_ElementDict_NodeMat(self): """unittest with ElementDict and NodeMat objects""" # Init mesh = Mesh() mesh.element = ElementDict() mesh.node = NodeMat() mesh.node.coordinate = np.array([[0, 0], [1, 0], [1, 2], [2, 3]]) mesh.element.connectivity = { "Triangle": np.array([[0, 1, 2], [1, 2, 3]]) } mesh.element.tag = {"Triangle": np.array([1, 2])} mesh.element.nb_elem = {"Triangle": 2} mesh.element.nb_node_per_element = {"Triangle": 3} # Method test 1 node_tags = mesh.element.get_node_tags(2) # Check result solution = np.array([1, 2, 3]) testA = np.sum(abs(solution - node_tags)) msg = ("Wrong projection: returned " + str(node_tags) + ", expected: " + str(solution)) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA) # Method test 2 node_tags = mesh.element.get_node_tags(np.array([1, 2])) # Check result solution = np.array([0, 1, 2, 3]) testA = np.sum(abs(solution - node_tags)) msg = ("Wrong projection: returned " + str(node_tags) + ", expected: " + str(solution)) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA) # Method test 3 node_tags = mesh.element.get_node_tags() # Check result solution = np.array([0, 1, 2, 3]) testA = np.sum(abs(solution - node_tags)) msg = ("Wrong projection: returned " + str(node_tags) + ", expected: " + str(solution)) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
def test_NodeMat_ElementMat(self): """unittest with ElementMat and NodeMat objects""" # Init mesh = Mesh() mesh.element = ElementMat() mesh.node = NodeMat() mesh.node.coordinate = np.array([[0, 0], [1, 0], [1, 2], [2, 3]]) mesh.element.connectivity = np.array([[0, 1, 2], [1, 2, 3]]) mesh.element.nb_elem = 2 mesh.element.nb_node_per_element = 3 node_tags = mesh.element.get_node_tags(1) # Method test 1 nodes_coord = mesh.node.get_coord(node_tags) # Check result solution = np.array([[1, 0], [1, 2], [2, 3]]) testA = np.sum(abs(solution - nodes_coord)) msg = ("Wrong projection: returned " + str(node_tags) + ", expected: " + str(solution)) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
def set_submesh(self, group_number): """Define a mesh object as submesh of parent mesh object Parameters ---------- self : Mesh an Mesh object group_number : int a group number which define the elements which constitute the submesh Returns ------- """ submesh = Mesh() submesh.element = self.element.get_group( group_number ) # Create a new Element object which is restrained to group_number submesh.node = self.node.get_group( element=submesh.element ) # Create a new Node object which corresponds to selection of element
def test_ElementMat(self): # Init 1 mesh = Mesh() mesh.element = ElementMat() mesh.element.connectivity = np.array( [[11, 12, 13], [0, 1, 2], [1, 2, 3], [120, 11, 12]] ) mesh.element.nb_elem = 2 mesh.element.nb_node_per_element = 3 # Method test 1 elem_tag = mesh.element.get_node2element(1) # Check results solution = np.array([1, 2]) # In this case, element tag = line position testA = np.sum(abs(solution - elem_tag)) msg = ( "Wrong projection: returned " + str(elem_tag) + ", expected: " + str(solution) ) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA)
def get_meshsolution(self, is_get_mesh, is_save_FEA, save_path, j_t0): """Load the mesh data and solution data. FEMM must be working and a simulation must have been solved. Parameters ---------- self : MagFEMM a MagFEMM object is_get_mesh : bool 1 to load the mesh and solution into the simulation is_save_FEA : bool 1 to save the mesh and solution into a .json file j_t0 : int Targeted time step Returns ------- res_path: str path to the result folder """ # TODO: Not saving the mesh (only the solution) when the sliding band is activated idworker = "1" # For parallelization TODO path_txt = join(MAIN_DIR, "Functions", "FEMM") + "\\" path_txt_lua = path_txt.replace("\\", "/") path_lua_in = join(path_txt, "get_mesh_data_FEMM.lua") path_lua_out = join(save_path, "get_mesh_data_FEMM" + idworker + ".lua") path_txt_out = save_path + "\\" path_txt_out = path_txt_out.replace("\\", "/") # Create a new LUA script with current paths file_lua = open(path_lua_in, "r") text_lua = file_lua.read() file_lua.close() text_lua = text_lua.replace("my_path_txt", path_txt_out) text_lua = text_lua.replace("my_id_worker", idworker) file_lua_out = open(path_lua_out, "w") file_lua_out.write(text_lua) file_lua_out.close() # Run the LUA externally using FEMM LUA console and store the data in the # temporary text files path_lua_out2 = path_lua_out.replace("\\", "/") callfemm('dofile("' + path_lua_out2 + '")') # Delete the LUA script os.remove(path_lua_out) # Read the nodes and elements files path_node = join(save_path, "nodes" + idworker + ".txt") path_element = join(save_path, "elements" + idworker + ".txt") listNd0 = np.loadtxt(path_node, delimiter=" ") listElem0 = np.loadtxt(path_element, dtype="i", delimiter=" ") NbNd = len(listNd0) NbElem = len(listElem0) # Node list listNd = np.zeros(shape=(NbNd, 3)) listNd[:, 0] = listNd0[:, 0] listNd[:, 1] = listNd0[:, 1] # Element list # listElem = np.zeros(shape=(NbElem, 3)) listElem = listElem0[:, 0:3] - 1 # Delete text files os.remove(path_node) os.remove(path_element) # Read the results file path_results = join(save_path, "results" + idworker + ".txt") results = np.loadtxt(path_results, delimiter=" ") # Delete text files os.remove(path_results) # Save data if is_get_mesh: # Create Mesh and Solution dictionaries mesh = Mesh() mesh.element = ElementMat( connectivity=listElem, nb_elem=NbElem, group=listElem0[:, 6], nb_node_per_element=3, ) mesh.node = NodeMat(coordinate=listNd[:, 0:2], nb_node=NbNd) solution = SolutionFEMM(B=results[:, 0:2], H=results[:, 2:4], mu=results[:, 4]) meshFEMM = MeshSolution(name="FEMM_magnetic_mesh", mesh=mesh, solution=solution) if is_save_FEA: save_path_fea = join(save_path, "meshFEMM" + str(j_t0) + ".json") meshFEMM.save(save_path_fea) return meshFEMM