def integration_process(folder_path,preProcData):
	file_names=File_names()
	results_folder=file_names.get_results_folder_name()
	results_path=os.path.join(folder_path,results_folder)
	
#%% Instances and geral definitions
	aux_RNM=AuxMethodsRNM()
	global_variables=GlobalVariables()
	error=Errors()
	operations=Operations()
	materials_lib=get_materials_lib()
	vacuum=Vacuum()
	get_gauss_points_class=GaussPoints()
	shape_functions=ShapeFuncions()
	operations=Operations()
	
	str_noflux_face=global_variables.str_noflux_face
	open_circuit_reluctance=global_variables.magnetic_open_circuit_reluctance
	results_folder=file_names.get_results_folder_name()
	
	face=Face()
	#------------------------------------------------------------------------------
	# Setup
	regions_material=preProcData.RegionMaterial
	regions_excitation=preProcData.RegionExcitation
	boundary=preProcData.BC
	external_reluctances=preProcData.ExternalReluctances
	coupling=preProcData.CoupNet
	#------------------------------------------------------------------------------
	# Mesh
	mesh_data=preProcData.MeshData
	all_elem_nodes_3D= mesh_data.ElemNodes
	nodes_coordenates=mesh_data.NodesCoordenates
	elem_tags=mesh_data.ElemTags
	elem_type=mesh_data.ElemType
	
	#%%Get source field

	#Run Biot-Savart
	run_biot_savart=False
#	for eachregion in regions_excitation:
#		 if eachregion.Value != 0.0:
#			 field_solution=Biot_Savart.run_Biot_Savart(setup_file_name,mesh_file_name,folder_path)
	#		 run_biot_savart=True
	#		 break
	
	# Run Permanent Magnets
	run_permanent_magnets=False
	for each_region in regions_material:
		for each in materials_lib[each_region.MaterialName].Hc:
			if each !=0.0:
				field_solution=permanent_magnets.run_permanent_magnets(preProcData,folder_path)
				run_permanent_magnets=True
				break
	
	#Read the file
	if run_biot_savart or run_permanent_magnets:
		(H_field,H_field_elem_nodes_3D)=get_Field_solution(os.path.join(results_path,file_names.get_H_results_file_name()))
	
	run_external_circuit=False
	if len(coupling)>0:
		run_external_circuit=True
	
	
	#%% Get 2D and 3D elements, with their materials name
	elem_2D_ID=list()
	elem_type_3D=list()
	elem_nodes_3D=list()
	elem_tags_3D=list()
	region_ID_list_3D=list()
	number_elements_2D=0
	
	for counter,each_elem in enumerate(all_elem_nodes_3D):
		this_elem_type=elem_type[counter]
		if this_elem_type<4:
			elem_2D_ID.append(counter)
			number_elements_2D+=1
		else:
			elem_nodes_3D.append(each_elem)
			elem_type_3D.append(elem_type[counter])
			elem_tags_3D.append(elem_tags[counter])
			
			find=False
			for each_region in regions_material:
				if each_region.RegionNumber==elem_tags[counter][0]:
					region_ID_list_3D.append(each_region.MaterialName)
					find=True
			if not find:
			   error.physical_surface_not_defined(str(each_region.RegionNumber),elem_tags[counter][0])
	
	number_elements=len(elem_nodes_3D)
	number_nodes=len(nodes_coordenates)
	faces_ID=list()
	faces_list=list()
	
#%%Creates the face list
	print_message("Creating faces list")
	for elem_counter in range(0,number_elements):
		number_local_faces=shape_functions.get_number_faces(this_elem_type)
		this_elem_type=elem_type_3D[elem_counter]
		this_element_nodes=elem_nodes_3D[elem_counter]
		faces_nodes_ID=shape_functions.get_nodes_ID_2_face(this_elem_type)
		number_faces,nodes_per_face=faces_nodes_ID.shape
		local_faces_ID=list()								   
		for local_face_counter in range(0,number_faces):
			nodes_list=list()
			for node_counter in range(nodes_per_face):
				node_ID=faces_nodes_ID[local_face_counter,node_counter]
				nodes_list.append(this_element_nodes[node_ID])
			local_faces_ID.append(face.add_to_list(nodes_list,elem_counter,faces_list))
		faces_ID.append(local_faces_ID)
	print_message("Creating faces list - Done")


#%%Integration
	print_message("Integration process")

		
	num_meshed_rel=len(faces_list)
	num_non_meshed_rel=len(external_reluctances)
	num_total_rel=num_meshed_rel+num_non_meshed_rel
	
#sparse matrix
	cols_rel_sparse=list()
	rows_rel_sparse=list()

#reluctance matrix	
	diagonal=list()
	for each_element_faces in faces_ID:
		numbre_faces_this_element=len(each_element_faces)
		for face_counter_1 in xrange(numbre_faces_this_element):
			pos_1=each_element_faces[face_counter_1]
			for face_counter_2 in xrange(numbre_faces_this_element):
				pos_2=each_element_faces[face_counter_2]
				if faces_list[pos_1].elem_2!=str_noflux_face and faces_list[pos_1].elem_2!=str_noflux_face:
					if pos_1==pos_2:
						if pos_1 not in diagonal:
							rows_rel_sparse.append(pos_1)
							cols_rel_sparse.append(pos_2)
							diagonal.append(pos_1)
					else:
						rows_rel_sparse.append(pos_1)
						cols_rel_sparse.append(pos_2)

	data_rel_sparse=np.zeros(len(rows_rel_sparse))
	faces_rel_spare=csr_matrix((data_rel_sparse, (rows_rel_sparse, cols_rel_sparse)), shape=(num_meshed_rel, num_meshed_rel))
#fmm matrix		
	cols_fmm_sparse=np.zeros(num_meshed_rel)
	rows_fmm_sparse=xrange(0,num_meshed_rel)
	data_fmm_sparse=np.zeros(num_meshed_rel)
	fmm_sparse=csr_matrix((data_fmm_sparse, (rows_fmm_sparse, cols_fmm_sparse)), shape=(num_meshed_rel,1))
	
	
	
	faces_rel_matrix=np.zeros((num_meshed_rel,num_meshed_rel))
	faces_fmm_matrix=np.zeros((num_meshed_rel,1))
	xy_integ_points_list=list()
	W_integ_points_list=list()
	
	this_elem_type=""
	for elem_counter in xrange(number_elements):
		this_elem_type_changed=elem_type_3D[elem_counter]
		if this_elem_type!=this_elem_type_changed:
			this_elem_type=this_elem_type_changed
			gauss_points=get_gauss_points_class.get_gauss_points(this_elem_type)
			number_local_faces=shape_functions.get_number_faces(this_elem_type)
			number_integ_points=len(get_gauss_points_class.get_gauss_points(this_elem_type))
			wtri=get_gauss_points_class.get_integration_weight(this_elem_type)
	
		faces_ID_Elem=faces_ID[elem_counter]
		this_element_nodes=elem_nodes_3D[elem_counter]
		mu_elem=vacuum.mu0*materials_lib[region_ID_list_3D[elem_counter]].Permeability
	
	
# Get W at reference element 
		w_local_this_element=list() #[gauss point][face]
		for each_integ_point in xrange(number_integ_points):
	
#u,v,w coordinates
			u=gauss_points[each_integ_point,0]
			v=gauss_points[each_integ_point,1]
			w=gauss_points[each_integ_point,2]
			
# Shape functions @ reference element
			w_local_this_point=shape_functions.get_facet_shape_function(this_elem_type,u,v,w)
			w_local_this_element.append(w_local_this_point)
	
# Shape functions @ real element for each face
		w_real_all_points=list() # w_real_lista[0] contains  the shape function of all points for face 0

#   Face 0...Fn
#P0  W
#Pn	
		
		for face_counter in xrange(number_local_faces):
			w_real_this_point=list()
			for point_counter in  range(0,number_integ_points):
				w_local=w_local_this_element[point_counter][face_counter]
				w_real=operations.convert_local_real_Piola(this_elem_type,w_local[0,0],w_local[0,1],w_local[0,2],this_element_nodes,nodes_coordenates)
				w_real_this_point.append(w_real)
			w_real_all_points.append(w_real_this_point)

# Source fields	 
		if run_biot_savart or run_permanent_magnets:
			H_points=H_field_elem_nodes_3D[elem_counter]
	
		for local_face_counter in xrange(number_local_faces):
			
			w_1=copy.deepcopy(w_real_all_points[local_face_counter])
			face_ID_1=faces_ID_Elem[local_face_counter]
			
			if faces_list[face_ID_1].elem_2==elem_counter:
				for each in w_1:
					each=aux_RNM.invert_w(each)

# mmf source integration process
			source=0
			for each_integ_point in xrange(number_integ_points):
				w_1_this_point=w_1[each_integ_point]
				
				u=gauss_points[each_integ_point,0]
				v=gauss_points[each_integ_point,1]
				p=gauss_points[each_integ_point,2]
				w=w_1_this_point
				
	
				xy_coord=operations.convert_local_real(this_elem_type,u,v,p,this_element_nodes,nodes_coordenates)
				xy_integ_points_list.append(xy_coord)
				W_integ_points_list.append(w)
				
	
			
				if run_biot_savart or run_permanent_magnets:
					 H_point=H_points[each_integ_point]
					 Hx=H_field[H_point][0]
					 Hy=H_field[H_point][1]
					 Hz=H_field[H_point][2]
					 
					 if (Hx!=0) or (Hy!=0) or (Hz!=0) :
						 Hxy=np.array([[Hx],
								[Hy],
								[Hz]])
	
# Jacobian
						 jac=operations.get_jacobian(this_elem_type,this_element_nodes,nodes_coordenates,u,v)
						 det_jac=np.linalg.det(jac)
						 abs_det_jac=np.abs(det_jac)
	
						 source=source+wtri*abs_det_jac*aux_RNM.dot_product(w,Hxy)
	
	
			aux_RNM.fmm_matrix_control(face_ID_1,faces_fmm_matrix,source,False,faces_list,str_noflux_face)
			aux_RNM.fmm_matrix_control(face_ID_1,fmm_sparse,source,False,faces_list,str_noflux_face)
			fmm_sparse
	
# Relutances integration process
			for local_face_counter_2 in xrange(number_local_faces):
				w_2=copy.deepcopy(w_real_all_points[local_face_counter_2])
				face_ID_2=faces_ID_Elem[local_face_counter_2]
				 
				if faces_list[face_ID_2].elem_2==elem_counter:
					 for each in w_2:
						 each=aux_RNM.invert_w(each)			 
				 
				wtot=0.0
				for each_integ_point in range(0,number_integ_points):
					 u=gauss_points[each_integ_point,0]
					 v=gauss_points[each_integ_point,1]
					 p=gauss_points[each_integ_point,2]
					 
# Jacobian
					 jac=operations.get_jacobian(this_elem_type,this_element_nodes,nodes_coordenates,u,v,p)
					 det_jac=np.linalg.det(jac)
					 abs_det_jac=np.abs(det_jac)
	
# Reluctance
					 w_1_this_point=w_1[each_integ_point]
					 w_2_this_point=w_2[each_integ_point]
					 wtot=wtot+wtri*abs_det_jac*aux_RNM.dot_product(w_1_this_point,w_2_this_point)/mu_elem
					 
				aux_RNM.reluctance_matrix_control(face_ID_1,face_ID_2,faces_rel_matrix,wtot,False,faces_list,str_noflux_face)
				aux_RNM.reluctance_matrix_control(face_ID_1,face_ID_2,faces_rel_spare,wtot,False,faces_list,str_noflux_face)
	print_message("Integration process - Done")
	
# Connection between the physical line with the circuit node
	print_message("External faces") 
	magnetic_short_circuit_reluctance=global_variables.magnetic_short_circuit_reluctance
	
	for counter,each_face in enumerate(faces_list):
		if each_face.elem_2==str_noflux_face:
			for each_face_con in elem_2D_ID:
				nodes_face_shared=all_elem_nodes_3D[each_face_con]
				phys_line=elem_tags[each_face_con][0]
	
				for each_coupling in coupling:
					if each_coupling.PhysLine==phys_line:
	
						bool_connect=True
						for each_node_this_face in each_face.nodes_list:
							if each_node_this_face not in nodes_face_shared:
								bool_connect=False
			
						if bool_connect:

# Redefine the face
							   new_face=Face(each_face.nodes_list,each_face.elem_1,each_coupling.Node+number_elements)				
							   faces_list[counter]=new_face
							   each_coupling.Face_ID_List.append(counter)
							   
#Set the reluctance as a magnetic short circuit
#							   aux_RNM.reluctance_matrix_control(counter,counter,faces_rel_spare,magnetic_short_circuit_reluctance,False)


	
#%% Delete the faces without external connections
	faces_ID_deleted_list=list()
	faces_deleted_list=list()

	counter=0
#Delete from reluctances and fmm matrix
	for face_counter in  xrange(len(faces_list)):
		if faces_list[face_counter].elem_2==str_noflux_face:
			faces_ID_deleted_list.append(face_counter)
	
	faces_rel_matrix=np.delete(faces_rel_matrix, faces_ID_deleted_list, axis=0)  
	faces_rel_matrix=np.delete(faces_rel_matrix, faces_ID_deleted_list, axis=1)
	faces_fmm_matrix=np.delete(faces_fmm_matrix, faces_ID_deleted_list, axis=0)

##Delete from faces_list
	counter=0
	for each in faces_ID_deleted_list:
		face_ID=each+counter
		this_face=faces_list[face_ID]
		faces_list.remove(this_face)
		faces_deleted_list.append(this_face)
		counter-=1
	
#%% Add the external circuit reluctances
	external_nodes_list=list()
	rows,cols=faces_rel_matrix.shape
	for each in external_reluctances:
	
		mu_elem=vacuum.mu0*materials_lib[each.Material].Permeability
	
#Get the list of external nodes
		if each.node_from not in external_nodes_list:
			external_nodes_list.append(each.node_from)
		if each.node_to not in external_nodes_list:
			external_nodes_list.append(each.node_to)
	   
#add the reluctance in the matrix system
		row_matrix=np.zeros((1,cols))
		faces_rel_matrix=np.vstack((faces_rel_matrix,row_matrix))
		rows+=1
		col_matrix=np.zeros((rows,1))
		faces_rel_matrix=np.hstack((faces_rel_matrix,col_matrix))
		cols+=1
		faces_fmm_matrix=np.vstack((faces_fmm_matrix,each.fmm))
		
#reluctance value
		reluctance_value=each.LS/mu_elem
		faces_rel_matrix[rows-1,cols-1]=reluctance_value
	
#create a new face in the faces_list
		new_face=Face([],each.node_from+number_elements,each.node_to+number_elements)
		faces_list.append(new_face)
		
#Get the list of external nodes
	for each in coupling:
		if each.Node not in external_nodes_list:
			external_nodes_list.append(each.Node )
	
	print_message("External faces - Done")
#%% Incidence matrix
	print_message("Incidence matrix")
	external_nodes=0
	if run_external_circuit:
		external_nodes+=len(external_nodes_list)
	else:
		external_nodes+=0
	
	total_nodes=number_elements+external_nodes
	incidence_matrix=np.zeros((total_nodes,len(faces_list)))
	
	for counter,each_face in enumerate(faces_list):
		if each_face.elem_2!=str_noflux_face:
			incidence_matrix[each_face.elem_1,counter]=1
			incidence_matrix[each_face.elem_2,counter]=-1
		else:
			incidence_matrix[each_face.elem_1,counter]=1
			incidence_matrix[total_nodes-1,counter]=-1
	
	print_message("Incidence matrix - Done")
	
	return faces_ID,results_path,faces_rel_matrix,incidence_matrix,faces_fmm_matrix,faces_ID_deleted_list,faces_list,faces_deleted_list
Example #2
0
                 Hx=H_field[H_point][0]
                 Hy=H_field[H_point][1]
                 
                 if (Hx!=0) or (Hy!=0):
                     Hxy=np.array([[Hx],
                                   [Hy]])
                                   
    # Jacobian
                     jac=operations.get_jacobian(this_elem_type,this_element_nodes,nodes_coordenates,u,v)
                     det_jac=np.linalg.det(jac)
                     abs_det_jac=np.abs(det_jac)

                     source=source+wtri*abs_det_jac*aux_RNM.dot_product(w,Hxy)


        aux_RNM.fmm_matrix_control(face_ID_1,faces_fmm_matrix,source,False)

# Relutances integration process
        for local_face_counter_2 in range(0,number_local_faces):
             w_2=copy.deepcopy(w_real_all_points[local_face_counter_2])
             face_ID_2=faces_ID_Elem[local_face_counter_2]
             
             
             if faces_list[face_ID_2].elem_2==elem_counter:
                 for each in w_2:
                     each=aux_RNM.invert_w(each)             
             
             wtot=0.0
             for each_integ_point in range(0,number_integ_points):
                 u=gauss_points[each_integ_point,0]
                 v=gauss_points[each_integ_point,1]