1212*/
1313
1414#include < gismo.h>
15+ #include < fstream>
16+ #include < iterator>
1517#include < gsPreCICE/gsPreCICE.h>
1618#include < gsPreCICE/gsPreCICEFunction.h>
1719
@@ -37,6 +39,19 @@ int main(int argc, char *argv[])
3739 cmd.addInt (" s" ," side" , " Patchside of interface: 0 (Dirichlet), 1 (Neumann)" , side);
3840 try { cmd.getValues (argc,argv); } catch (int rv) { return rv; }
3941
42+ auto configContains = [](const std::string & fileName, const std::string & needle) -> bool
43+ {
44+ std::ifstream config (fileName.c_str ());
45+ GISMO_ASSERT (config.good (), " No precice config file has been defined" );
46+ const std::string contents ((std::istreambuf_iterator<char >(config)),
47+ std::istreambuf_iterator<char >());
48+ return contents.find (needle) != std::string::npos;
49+ };
50+
51+ const bool useDirectMeshAccess =
52+ configContains (precice_config, " direct-access=\" true\" " ) ||
53+ configContains (precice_config, " api-access=\" true\" " );
54+
4055 // ! [Read input file]
4156
4257 gsMultiPatch<> patches;
@@ -151,9 +166,9 @@ int main(int argc, char *argv[])
151166 // Register local mesh coordinates
152167 interface.addMesh (localMeshName, xy);
153168
154- // Define access region for the received mesh in serial mode
155- // Use a generous bounding box pattern seen in other examples
169+ if (useDirectMeshAccess)
156170 {
171+ // Restrict mesh access to the remote interface only when the config enables API access.
157172 gsMatrix<> bbox (rows, 2 );
158173 bbox.col (0 ).setConstant (-1e300 );
159174 bbox.col (1 ).setConstant ( 1e300 );
@@ -166,19 +181,22 @@ int main(int argc, char *argv[])
166181
167182 real_t precice_dt = interface.initialize ();
168183
169- // Direct-access: fetch partner mesh vertex IDs and coordinates.
170- // Evaluate quantities at partner coordinates (no reordering needed).
171184 gsVector<index_t > remoteIDs;
172185 gsMatrix<> remoteCoords;
173- interface.getMeshVertexIDsAndCoordinates (remoteMeshName, remoteIDs, remoteCoords);
174- // Invert partner physical coords to param coords on our coupling patch
175186 gsMatrix<> uvRemote;
176- patches.patch (couplingInterface.patch ).invertPoints (remoteCoords, uvRemote, 1e-10 );
177- // Ensure boundary parameter is exact for evalBdr
178- const int bdir = couplingInterface.side ().direction ();
179- const real_t bpar = couplingInterface.side ().parameter ();
180- for (index_t i = 0 ; i != uvRemote.cols (); ++i)
181- uvRemote (bdir, i) = bpar;
187+ if (useDirectMeshAccess)
188+ {
189+ // Direct-access: fetch partner mesh vertex IDs and coordinates.
190+ // Evaluate quantities at partner coordinates (no reordering needed).
191+ interface.getMeshVertexIDsAndCoordinates (remoteMeshName, remoteIDs, remoteCoords);
192+ patches.patch (couplingInterface.patch ).invertPoints (remoteCoords, uvRemote, 1e-10 );
193+
194+ // Ensure boundary parameter is exact for evalBdr.
195+ const int bdir = couplingInterface.side ().direction ();
196+ const real_t bpar = couplingInterface.side ().parameter ();
197+ for (index_t i = 0 ; i != uvRemote.cols (); ++i)
198+ uvRemote (bdir, i) = bpar;
199+ }
182200
183201// ----------------------------------------------------------------------------------------------
184202
@@ -260,23 +278,27 @@ int main(int argc, char *argv[])
260278 result (0 ,k) = tmp2.at (0 );
261279 }
262280 }
263- // Build values at partner coordinates (order matches remoteIDs)
264- result.resize (1 , uvRemote.cols ());
265- for (index_t k=0 ; k!=uvRemote.cols (); ++k)
281+ if (useDirectMeshAccess)
266282 {
267- if (side==0 ) // Dirichlet writes flux
268- {
269- tmp2 = ev.evalBdr ( - k_temp * (igrad (u_sol, G) * nv (G).normalized ()), uvRemote.col (k), couplingInterface);
270- result (0 ,k) = tmp2.at (0 );
271- }
272- else // Neumann writes temperature
283+ // Build values at partner coordinates (order matches remoteIDs).
284+ result.resize (1 , uvRemote.cols ());
285+ for (index_t k=0 ; k!=uvRemote.cols (); ++k)
273286 {
274- tmp2 = ev.evalBdr (u_sol, uvRemote.col (k), couplingInterface);
275- result (0 ,k) = tmp2.at (0 );
287+ if (side==0 ) // Dirichlet writes flux
288+ {
289+ tmp2 = ev.evalBdr ( - k_temp * (igrad (u_sol, G) * nv (G).normalized ()), uvRemote.col (k), couplingInterface);
290+ result (0 ,k) = tmp2.at (0 );
291+ }
292+ else // Neumann writes temperature
293+ {
294+ tmp2 = ev.evalBdr (u_sol, uvRemote.col (k), couplingInterface);
295+ result (0 ,k) = tmp2.at (0 );
296+ }
276297 }
298+ interface.writeData (remoteMeshName, (side==0 ? fluxName : tempName), remoteIDs, result);
277299 }
278- // Write on partner mesh using its vertex IDs (direct-access config)
279- interface.writeData (remoteMeshName , (side==0 ? fluxName : tempName), remoteIDs , result);
300+ else
301+ interface.writeData (localMeshName , (side==0 ? fluxName : tempName), xy , result);
280302 }
281303
282304 // Initialize the RHS for assembly
@@ -372,23 +394,27 @@ int main(int argc, char *argv[])
372394 result (0 ,k) = tmp.at (0 );
373395 }
374396 }
375- // Build values at partner coordinates (order matches remoteIDs)
376- result.resize (1 , uvRemote.cols ());
377- for (index_t k=0 ; k!=uvRemote.cols (); ++k)
397+ if (useDirectMeshAccess)
378398 {
379- if (side==0 ) // Dirichlet writes flux
380- {
381- tmp = ev.evalBdr ( - k_temp * (igrad (u_sol, G) * nv (G).normalized ()), uvRemote.col (k), couplingInterface);
382- result (0 ,k) = tmp.at (0 );
383- }
384- else // Neumann writes temperature
399+ // Build values at partner coordinates (order matches remoteIDs).
400+ result.resize (1 , uvRemote.cols ());
401+ for (index_t k=0 ; k!=uvRemote.cols (); ++k)
385402 {
386- tmp = ev.evalBdr (u_sol, uvRemote.col (k), couplingInterface);
387- result (0 ,k) = tmp.at (0 );
403+ if (side==0 ) // Dirichlet writes flux
404+ {
405+ tmp = ev.evalBdr ( - k_temp * (igrad (u_sol, G) * nv (G).normalized ()), uvRemote.col (k), couplingInterface);
406+ result (0 ,k) = tmp.at (0 );
407+ }
408+ else // Neumann writes temperature
409+ {
410+ tmp = ev.evalBdr (u_sol, uvRemote.col (k), couplingInterface);
411+ result (0 ,k) = tmp.at (0 );
412+ }
388413 }
414+ interface.writeData (remoteMeshName, (side==0 ? fluxName : tempName), remoteIDs, result);
389415 }
390- // Write on partner mesh using its vertex IDs (direct-access config)
391- interface.writeData (remoteMeshName , (side==0 ? fluxName : tempName), remoteIDs , result);
416+ else
417+ interface.writeData (localMeshName , (side==0 ? fluxName : tempName), xy , result);
392418
393419 // do the coupling
394420 precice_dt = interface.advance (dt);
0 commit comments