# Numerical Coupling between a FEM Code and the FVM Code OpenFOAM Using the MED Library
## 3 Coupling Procedure through the MED Library  
This section explains in detail the coupling approach implemented between the two CFD codes, FEMuS and OpenFOAM, that will be used for the numerical demonstrations in the following sections. This procedure can easily be generalized to additional software with minimum modifications, mainly by translating the internal data structures into the MED format. It is essential to point out that this framework scales optimally with the number of computational codes that are connected: adding a new library implies the development of a single wrapper of its data fields in the MED coupling format instead of developing specific procedures to couple the new code to each of the other libraries in the platform. In other words, the coupling strategy follows a hub-and-spoke model instead of a point-to-point approach that would require a significant effort to add new software. A schematic example of the two coupling models is reported in Figure 1.  
The coupling application implements three different classes designed for data transfer and synchronization. The first class acts as the intermediary between OpenFOAM and  
Figure 1: Coupling strategy models: point-to-point on the left and hub-and-spoke on the right.  
the MED library by extracting the numerical field data from the internal data structures into an object compatible with the MED format. This class is the interface OpenFOAM-MED that we named of_interface. Similarly, the second class, femus_interface, is the interface between FEMuS and the MED library, enabling the use of the numerical field data within the FEMuS framework. Finally, the third class, namely, med_class, is responsible for managing the operations within the MED library itself. It includes tasks such as data storage, retrieval, and data manipulation. An additional feature available in this class is the possibility to interpolate a field insisting on a mesh to a different mesh discretization.  
In the following, we generally refer to Code 1 and Code 2: the reader can interchangeably substitute them with FEMuS and OpenFOAM. Figure 2 illustrates the coupling procedure. At the supervisor level, the main function manages the interaction between the two codes and their associated interface structures with specific solver functions. Firstly, it manages the initialization and setup of both Code 1 and Code 2, ensuring they are correctly configured and ready to interact. This involves the problem initialization with the interface structures, including an exchange mesh and its numerical fields. Moreover, the supervisor function manages the synchronization of the simulation time steps between Code 1 and Code 2. This enforces that both codes progress together to maintain consistency in the coupled simulation. At each time step, the supervisor coordinates the exchange of fields, updates solutions, and monitors convergence criteria.  
This framework can be exploited in various simulations involving coupling between volume or boundary fields.  
For simulations requiring volume field transfer, the application allows the exchange of numerical data representing physical quantities distributed in the whole computational domain. On the other hand, simulations involving boundary field transfer focus on the interaction between different physical domains or interfaces within the computational domain. By transferring boundary conditions, forces, or constraints between Code 1 and Code 2, we can simulate complex fluid-structure interactions, conjugate heat transfer processes, and multiphase flow phenomena. For these two types of coupling, the main structure of the algorithm remains consistent. In the following sections, the algorithm implemented is described.