# How to access time-window-size in a C++ based adapter?

**URL:** <https://precice.discourse.group/t/how-to-access-time-window-size-in-a-c-based-adapter/791>\
**Category:** Using preCICE\
**Tags:** adapters\
**Created:** [November 5, 2021, 10:05am UTC](https://precice.discourse.group/t/how-to-access-time-window-size-in-a-c-based-adapter/791 "2021-11-05T10:05:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alphaoo1](https://yyz2.discourse-cdn.com/free1/user_avatar/precice.discourse.group/alphaoo1/32/497_2.png) [@Alphaoo1](https://precice.discourse.group/u/Alphaoo1)\
**Post date:** [November 5, 2021, 10:05am UTC](https://precice.discourse.group/t/how-to-access-time-window-size-in-a-c-based-adapter/791/1 "2021-11-05T10:05:05Z")

</div>

Hello,

This question relates to the usage of preCICE C++ API in an adapter (the in-house implemented for XDEM). Please be patient with the thorough post, as this is not an official adapter I am giving as much info as possible.

### Scenario:

I am doing some calculations on the XDEM side, which need to know the coupling time step value.  
As these things are to be integrated over the coupling time step.  
Hence, I am trying to access `time-window-size` in XDEM adapter.

My plan was to pass the precise coupling time step i.e `time-window-size` to the Coupling object I have.

Then proceed with Calculations and exchange of data.

### General adapter outline

This is similar to the guidelines posted in the [step-by-step](https://precice.org/couple-your-code-preparing-your-solver.html) guide.

```c++
// MAIN >>>
// Initialize XDEM

// initialize preCICE
const std::string solverName("XDEM");
precice::SolverInterface precice(solverName, configFileName, commRank, commSize);
const int precice_dimension = precice.getDimensions();

***Modifications - new place I initialize preCICE***
// Get preCICE dt
double precice_dt = precice.initialize();
std::cout << "Debug: precice_coupling_dt: = " << precice_dt << std::endl;

// Save list of coupled objects = precice.initialize();

// Get XDEM Meshes (for coupling with CFD or FEM)
// Here we create & initialize the coupled object i.e coupled mesh, by passing various variables needed by the object 
***here I want to pass the preCICE coupling dt***

// initialize preCICE
//double precice_dt = precice.initialize(); //ususal place to initialize preCICE

//time loop
 while (!xdem.is_over())
 {
     // read and apply data
     // run XDEM
     // compute and write data

     // Advance preCICE time
    precice_dt = precice.advance( precice_dt );
}

// Finalize preCICE
  precice.finalize();
// Finalize XDEM
xdem.finalize();

```

### Issue.

If I want to pass preCICE coupling time step (`time-window-size`) to my coupled object, I have to do `precice.initialize();` before I do the `// Get XDEM Meshes` step.  
I then run the adapter, get the expected output,

```
preCICE: This is preCICE version 2.3.0
preCICE: Revision info: v2.3.0
preCICE: Configuration: Debug
preCICE: Configuring preCICE with configuration "./precice-config.xml"
preCICE: I am participant "XDEM"
preCICE: Setting up master communication to coupling partner/s
preCICE: Masters are connected
preCICE: Setting up preliminary slaves communication to coupling partner/s
preCICE: Prepare partition for mesh Domain
preCICE: Receive global mesh Fluid-Mesh-PV
preCICE: Setting up slaves communication to coupling partner/s
preCICE: Slaves are connected
preCICE: iteration: 1, time-window: 1, time: 0 of 500, time-window-size: 0.2, max-timestep-length: 0.2, ongoing: yes, time-window-complete: no, 
Debug: precice_coupling_dt: = 0.2
Deformable Objects in XDEM:
Fluid Volumes in XDEM:
    - Domain: Used in preCICE!
Inherited Momentum
Inherited HT
Inherited MT
[XDEM-Adapter] Created all the possible fields list
[XDEM-Adapter] Reading all the available fields for coupling

```

the **error**

```
preCICE:ERROR: This participant attempted to modify the Mesh "Domain" while locked. Mesh modification is only allowed before calling initialize().

```

The weird thing is, this works if I run in debug mode in QT.  
Does not work when I run normally in the terminal.  
I think this is just a C++ thing that I am probably messing up.

But maybe there is an alternate solution to using `precice.initialize()` for getting `time-window-size`.

---

<div class="post-metadata">

**Author:** ![Makis](https://yyz2.discourse-cdn.com/free1/user_avatar/precice.discourse.group/makis/32/25_2.png) [@Makis](https://precice.discourse.group/u/Makis)\
**Post date:** [November 5, 2021, 2:27pm UTC](https://precice.discourse.group/t/how-to-access-time-window-size-in-a-c-based-adapter/791/2 "2021-11-05T14:27:15Z")

</div>

There is currently no such method to get the configured coupling time window size in the API: [preCICE: precice::SolverInterface Class Reference](https://precice.org/doxygen/develop/classprecice_1_1SolverInterface.html)

While it would be possible to add such a method, there always needs to be a good reason / a clear use case. What is special in XDEM than in other solvers that would require this?

As a workaround, why not select a default timestep size in XDEM and give it to preCICE?

---

<div class="post-metadata">

**Author:** ![Alphaoo1](https://yyz2.discourse-cdn.com/free1/user_avatar/precice.discourse.group/alphaoo1/32/497_2.png) [@Alphaoo1](https://precice.discourse.group/u/Alphaoo1)\
**Post date:** [November 5, 2021, 5:22pm UTC](https://precice.discourse.group/t/how-to-access-time-window-size-in-a-c-based-adapter/791/3 "2021-11-05T17:22:07Z")

</div>

> [@Makis](#):
>
> While it would be possible to add such a method, there always needs to be a good reason / a clear use case. What is special in XDEM than in other solvers that would require this?

I wouldn’t say anything special. 😅  
I am calculating Heat and mass sources. Which are to be accumulated (integrated) over the coupling time step, as that’s when I exchange the heat and mass source.  
Anyways good to know that currently, it’s not possible. 🙂  
This solves my doubt. Thank you @Makis

Usually in direct coupling in XDEM, we have XDEM dt = OpenFOAM dt = Coupling dt.  
As it’s just “one” solver. So no need to worry about wrong calculations.

> As a workaround, why not select a default timestep size in XDEM and give it to preCICE?

Yes currently I am using `time-window-size` = XDEM dt.  
But this was after can get a bit annoying as I use solver time steps which are much small than coupling time step. But it’s not that bad in terms of the time required for simulation.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/precice/original/1X/67d2a5fa6f80e597e2267ff17fa8c1673cd11caa.svg) [@system](https://precice.discourse.group/u/system)\
**Post date:** [November 8, 2021, 5:22pm UTC](https://precice.discourse.group/t/how-to-access-time-window-size-in-a-c-based-adapter/791/4 "2021-11-08T17:22:17Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![uekerman](https://yyz2.discourse-cdn.com/free1/user_avatar/precice.discourse.group/uekerman/32/2199_2.png) [@uekerman](https://precice.discourse.group/u/uekerman)\
**Post date:** [November 21, 2021, 4:41pm UTC](https://precice.discourse.group/t/how-to-access-time-window-size-in-a-c-based-adapter/791/5 "2021-11-21T16:41:56Z")

</div>

Related discussion: [Add getter functions for time · Issue #682 · precice/precice · GitHub](https://github.com/precice/precice/issues/682)
