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_NodeMat(self): # Init 1 # Init mesh = Mesh() mesh.element["Triangle3"] = ElementMat(nb_node_per_element=3) mesh.node = NodeMat() mesh.node.add_node(np.array([0, 0])) mesh.node.add_node(np.array([1, 0])) mesh.node.add_node(np.array([1, 2])) mesh.node.add_node(np.array([2, 3])) mesh.node.add_node(np.array([3, 3])) mesh.add_element(np.array([0, 1, 2]), "Triangle3", group=int(3)) mesh.add_element(np.array([1, 2, 3]), "Triangle3", group=int(3)) mesh.add_element(np.array([4, 2, 3]), "Triangle3", group=int(2)) # Method test 1 mesh_grp4 = mesh.set_submesh([3]) # Check results solution = np.array([[0, 1, 2], [1, 2, 3]]) results = mesh_grp4.element["Triangle3"].connectivity testA = np.sum(abs(solution - results)) msg = "Wrong result: returned " + str(results) + ", expected: " + str( solution) DELTA = 1e-10 self.assertAlmostEqual(testA, 0, msg=msg, delta=DELTA) # Method test 2 mesh_grp4 = mesh.set_submesh([3, 2]) # Check results solution = np.array([2, 3]) results = mesh_grp4.element["Segment2"].connectivity testA = np.sum(abs(solution - results)) msg = "Wrong result: returned " + str(results) + ", 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 """ 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) # Create Mesh and Solution dictionaries if (not self.is_sliding_band) or (j_t0 == 0): mesh = Mesh() mesh.element["Triangle3"] = ElementMat( connectivity=listElem, nb_elem=NbElem, group=listElem0[:, 6], nb_node_per_element=3, tag=np.linspace(0, NbElem - 1, NbElem), ) mesh.node = NodeMat( coordinate=listNd[:, 0:2], nb_node=NbNd, tag=np.linspace(0, NbNd - 1, NbNd) ) mesh.group = np.unique(listElem0[:, 6]) else: mesh = None B = results[:, 0:2] H = results[:, 2:4] mu = results[:, 4] Az = results[:, 5] solution = Solution() solution.set_field(field_value=B, field_name="B", field_type="face") solution.set_field(field_value=H, field_name="H", field_type="face") solution.set_field(field_value=mu, field_name="mu", field_type="face") solution.set_field(field_value=Az, field_name="Az", field_type="nodal") return mesh, solution
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