# Numerical Coupling between a FEM Code and the FVM Code OpenFOAM Using the MED Library
## 3 Coupling Procedure through the MED Library
### Coupling Algorithm
Next, within the med_class class, two functions are employed: fill_med_array(), which sets the field values into the corresponding DataArrayDouble, and update_med_field(), which sets the array into the MED field. At this step, a projection function can be used when a source field from Code 1 has to be interpolated from the source mesh to a target grid suitable for Code 2. The MED library provides a range of functions tailored to this functionality. It is important to highlight that the interpolation functions are available for \(P_{0}\) (e.g., cell-wise) and \(P_{1}\) (e.g., node-wise) fields, both targeting intensive or extensive fields. These interpolation algorithms can combine different field types, e.g., it is possible to interpolate from \(P_{0}\) to \(P_{0}\), from \(P_{0}\) to \(P_{1}\), from \(P_{1}\) to \(P_{0}\), and from \(P_{1}\) to \(P_{1}\). In this application, a \(P_{0}\) to \(P_{0}\) interpolation scheme is employed, requiring that both fields from FEMuS and OpenFOAM be represented as cell-wise fields. Given that FEMuS uses biquadratic fields, it becomes necessary to convert the solution into a cell-wise field. To achieve this conversion, an interpolation algorithm specifically designed for converting a \(P_{2}\) (biquadratic field) to a \(P_{0}\) field is employed after the extraction of the solution from FEMuS. Therefore, the interpolation() function from med_class is called. This function is used to interpolate the MED field from Code 1 to a MED field over a MED mesh of Code 2. The MED field interpolated over the mesh of Code 2 is now available (directly in memory) as a MED object. With an inverse process, it can be stored as the solution of Code 2 using the interface function set_field_to_Code2(). In the scenario where Code 2 is FEMuS, an interpolation algorithm from a \(P_{0}\) to a \(P_{2}\) field must be used before calling the set_field_to_Code2() function. This is necessary to ensure compatibility between the cell-wise field format required by the coupling framework and the biquadratic solution format required by FEMuS. With the solution provided by Code 1, Code 2 can proceed to solve its specific physics. Once the solution of the system of equations in Code 2 is obtained, it provides the field to be exchanged back to Code 1 using a mechanism analogous to the previous one. The interface function get_field_from_Code2() is called to extract the solution from Code 2, while fill_med_array() and update_med_field() are employed to set the solution of Code 2 into the corresponding MED field. Then the interpolation() function is used to interpolate the MED field from Code 2 to the target MED field associated with Code 1. Finally, this interpolated field is written into Code 1 using the set_field_to_Code1() routine. Once Code 1 receives the solution from Code 2, the data exchange between the two codes is completed. With both codes now equipped with the necessary fields, the time loop can proceed to the next time iteration at the supervisor level. This iterative process is repeated at each time step until the end of the simulation.