# Coupling matlab with openfoam

**URL:** <https://precice.discourse.group/t/coupling-matlab-with-openfoam/2570>\
**Category:** Official adapters and tutorials\
**Tags:** openfoam, data-mapping, matlab\
**Created:** [July 21, 2025, 10:03am UTC](https://precice.discourse.group/t/coupling-matlab-with-openfoam/2570 "2025-07-21T10:03:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jian1](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jian1](https://precice.discourse.group/u/Jian1)\
**Post date:** [July 21, 2025, 10:03am UTC](https://precice.discourse.group/t/coupling-matlab-with-openfoam/2570/1 "2025-07-21T10:03:25Z")

</div>

Hello everyone, I’m trying to couple `solid-matlab` with `fluid-openfoam`. Taking the official tutorial `perpendicular-flap`（ [tutorials/perpendicular-flap at develop · precice/tutorials](https://github.com/precice/tutorials/tree/develop/perpendicular-flap)） as an example, I modified the `solverdummy.m` file from `matlab-bindings/examples/solverdummy`（ [matlab-bindings/examples/solverdummy at master · precice/matlab-bindings](https://github.com/precice/matlab-bindings/tree/master/examples/solverdummy)）, and replaced the `solid-calculix` folder with a new `solid-matlab` folder. The `fluid-openfoam` folder and the `precice-config.xml` file remained unchanged.The folder structures are as follows:

 ![02cf977ad5ce2235bc5c137a13826e2](https://global.discourse-cdn.com/free1/uploads/precice/original/2X/c/c459dcfcdb3397787e08f87403dbabb0d3ab04a6.png)  
In `interface.txt`, I provide the interface node indices, and in `NODE.txt` the full solid mesh node coordinates. In MATLAB, I use these to construct the `vertices` matrix, where the first and second rows store the x and y coordinates of all interface nodes respectively.

To simplify the case, I just set the solid deformation to zero, i.e., the displacement values written via `interface.writeData(...)` are all zero.

When I run both the solid and fluid solvers, they both display communication to coupling partner/s, indicating that communication was established:

 ![6c36b71b6218113b5ef411edfe40c0e](https://global.discourse-cdn.com/free1/uploads/precice/original/2X/2/2693f42672a63755b0fb1f41650897de179457d1.jpeg)

However, the fluid solver (OpenFOAM) crashes with an error that seems related to failed data transfer from the solid side due to some issues of the ‘interface’. MATLAB also confirmed this error. This is confusing to me because I reused the original interface node IDs from the `solid-calculix` case, and didn’t change any of the node positions or interface definitions.

 ![7349da29aff7190299cd04782913a63](https://global.discourse-cdn.com/free1/uploads/precice/original/2X/1/1cafccf3c31a961cc81394fe7d6141f117142de1.jpeg)

Why does the OpenFOAM solver crash due to interface data mismatch, even though I used the same interface definitions and node IDs as in the original case?

I have a hypothesis about the cause of the error: In OpenFOAM, this is defined as a 2D case using `frontAndBack` patches, but the fluid mesh is actually generated as a 3D mesh by extrusion. So, do I need to store xyz coordinates in `vertices` instead of just xy? And should the line `<mesh name="Solid-Mesh" dimensions="2">` in `precice-config.xml` be set to 3 instead?

Any help is appreciated!

The code of the solid solver `solverdummy1_copy.m` is as follows:

```
participantName='Solid';
configFileName='../precice-config.xml';
NODE=importdata("NODE.txt");
interfa=importdata("interfa.txt");
NODE_interfa=zeros(size(interfa,1),3);
for i=1:size(NODE_interfa,1)
    NODE_interfa(i,:)=[NODE(interfa(i),2), NODE(interfa(i),3), NODE(interfa(i),4)];
end
    writeDataName = 'Displacement';
    readDataName = 'Force';
    meshName = 'Solid-Mesh';

    numVertices = size(NODE_interfa,1);

% disp('DUMMY: Starting MATLAB solverdummy with the following parameters:')
% disp(['DUMMY: configFileName: ', configFileName])
% disp(['DUMMY: participantName: ', participantName])

    ProcessIndex = 0;
    ProcessSize = 1;

    interface = precice.Participant(participantName, configFileName, ProcessIndex, ProcessSize);

    dims = interface.getMeshDimensions(meshName);

    vertices(dims, numVertices) = 0;
    readData(dims, numVertices) = 0;
    writeData(dims, numVertices) = 0;

    for x = 1 : numVertices
        for y = 1 : dims
            vertices(y, x) = NODE_interfa(x,y);
% readData(y, x) = x;
% writeData(y, x) = x;
        end
    end

    vertexIDs = interface.setMeshVertices(meshName, vertices);

   if interface.requiresInitialData()
       disp("DUMMY: Writing initial data.")
   end

    interface.initialize();

    while(interface.isCouplingOngoing())
       if interface.requiresWritingCheckpoint()
           disp('DUMMY: Writing iteration checkpoint.')
       end
        
        dt = interface.getMaxTimeStepSize();

        readData = interface.readData(meshName, readDataName, vertexIDs, dt);

        writeData = zeros(dims,size(interfa,1));
        %disp(writeData)
        interface.writeData(meshName, writeDataName, vertexIDs, writeData);

        interface.advance(dt);

       if interface.requiresReadingCheckpoint()
           disp('DUMMY: Reading iteration checkpoint.')
       else
           disp('DUMMY: Advancing in time.')
       end
   end
    
    interface.finalize();

```

---

<div class="post-metadata">

**Author:** ![IshaanDesai](https://yyz2.discourse-cdn.com/free1/user_avatar/precice.discourse.group/ishaandesai/32/1048_2.png) [@IshaanDesai](https://precice.discourse.group/u/IshaanDesai)\
**Post date:** [August 29, 2025, 5:33am UTC](https://precice.discourse.group/t/coupling-matlab-with-openfoam/2570/2 "2025-08-29T05:33:32Z")

</div>

Hi @Jian1

Can you print out the values of the `vertices` data structure on the MATLAB side? I suspect that some vertex coordinates are incorrect, which is causing the error you see.

---

<div class="post-metadata">

**Author:** ![Jian1](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jian1](https://precice.discourse.group/u/Jian1)\
**Post date:** [August 31, 2025, 9:10am UTC](https://precice.discourse.group/t/coupling-matlab-with-openfoam/2570/3 "2025-08-31T09:10:31Z")

</div>

Hi Mr. Desai,@IshaanDesai

Thank you for your kind reply, I have provided the required vertex files on the MATLAB side, namely “ **interfa.txt** ” and “ **NODE.txt** ”, as attachments. For clarity and to facilitate further discussion, I have also included the complete calculation folder “ **perpendicular-flap.zip​** ” of the perpendicular-flap example.

​In addition, I have attached two illustrative figures:

(1) **NODE\_all.png** – a schematic of all solid mesh nodes,

(2) **NODE\_all\_with\_interface.png** – a schematic highlighting the interface nodes.

In the second figure, the interface nodes are marked in red and overlap with the corresponding solid mesh nodes originally shown in black.

Once the issue has been resolved, I will share an update on the preCICE forum, so that other researchers and users facing similar challenges may benefit from the solution.

I sincerely appreciate the time and effort you have devoted to this issue.  
Best regards,  
Jian

[attachments.zip](https://precice.discourse.group/uploads/short-url/v8RL1q1Gavb5y6sTf4k2nOLNTIk.zip) (285.2 KB)
