Coupling matlab with openfoam

Hello everyone, I’m trying to couple solid-matlab with fluid-openfoam. Taking the official tutorial perpendicular-flaptutorials/perpendicular-flap at develop · precice/tutorials) as an example, I modified the solverdummy.m file from matlab-bindings/examples/solverdummymatlab-bindings/examples/solverdummy at master · precice/matlab-bindings), 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:


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:

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.

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();

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.

1 Like

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 (285.2 KB)