# Numerical Coupling between a FEM Code and the FVM Code OpenFOAM Using the MED Library
## 3 Coupling Procedure through the MED Library
### Coupling Algorithm  
As shown in Algorithm 1, in the initial step of the coupling process, both Code 1 and Code 2 are required to generate a mesh copy in MED format corresponding to the computational domain (or a portion of it). Both interface classes of Code 1 and Code 2 feature a function named init_interface() used for extracting essential information to recreate the mesh in MED format. This function assigns the interface name for reference at the supervisor level and retrieves the mesh connectivity, coordinates, and mapping data necessary to link the data structure of the code mesh with that of the MED mesh. Since the FEMuS code employs the finite element method (FEM) and handles biquadratic fields, its  
Figure 2: Coupling procedure scheme. mesh is biquadratic. However, for coupling with the OpenFOAM problem, which operates with linear meshes, the interface to the MED coupling implements a linear mesh. Therefore, holding the information from the original biquadratic mesh, we extract the data to create a corresponding linear mesh for the FEMuS coupling interface. Once the interface classes have stored the necessary information, the MED library can generate a copy of the mesh in MED format. The function responsible for managing the creation of the mesh is called create_mesh(), and it belongs to the med_class class. ```
1:proceduremain()
2:Initialization of Code 1 and Code 2 structures. ```
3:Initialization of interfaces
4:functioninit_interface()
5:Set interface name for reference at the supervisor level. 6:\(comm\) = get_mesh_connectivity() \(\triangleright\) Get interface mesh connectivity
7:\(coords\) = get_mesh_coordinates() \(\triangleright\) Get coordinates of mesh nodes
8:set_map_CodeFromToMED() \(\triangleright\) Map mesh nodes \(\rightleftarrows\) MED mesh nodes
9:endfunctioncreate_mesh()
10:insert cells with \(conn\) information into the MED mesh structure. 11:setup \(coords\) information into the MED mesh structure. 12:creation of MED mesh copy from the mesh of Code 1 and Code 2
13:endfunction
14:functioninit_med_field_on_nodes/cells()
15:assigns the MED field to the corresponding interface MED mesh
16:allocate_med_array() \(\triangleright\) MED array memory allocation
17:init_med_field() \(\triangleright\) set MED field values to zero
18:endfunction ```  
**Algorithm 1** Coupling Algorithm  
At this stage, both codes have their respective copies of the mesh in MED format. Following the creation of meshes, both codes initialize the fields to be exchanged. We implemented two distinct functions within the med_class, which are init_med_field_on_nodes() and init_med_field_on_cells(). The first creates and initializes a MED format field of the type MEDCoupling::MEDCouplingFieldDouble on mesh nodes, setting it to zero. In contrast, the other function performs a similar operation but targets mesh cells instead of nodes. In these functions, an array of the type MEDCeoupling::DataArrayDouble is generated for each field that is required by the specific coupling procedure. This format enables the MED library to effectively set the values of the MED field based on the stored data. Both codes have now completed the initialization at the supervisor level through their dedicated functions within their respective classes. Moreover, the interfaces for the data transfer have been configured with their corresponding MED mesh copies and MED fields. At the supervisor level, the process can start the time loop. The time loop begins with the execution of the solver functions within Code 1, which are responsible for solving the system of governing equations of the specific physics being modeled. Once Code 1 has completed its computations and obtained a solution, the internal fields are transferred to the corresponding MED fields. This transfer process involves a sequence of functions. Firstly, the interface class of Code 1 uses the function get_field_from_Code1() to extract the solution of the field from Code 1.