-
-
Notifications
You must be signed in to change notification settings - Fork 71
Added documentation for nearest-neighbor-gradient #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1742018
Changes to configuration page
ariguiba 4b80ec9
Added page about gradient data
ariguiba 669346c
added link gradient data page
ariguiba 46d9f05
Merge branch 'precice:master' into nng-docu
ariguiba c04f16c
Typo fixes
ariguiba 9506658
Added gradient data page to sidebar
ariguiba 7919416
Fixed trailing spaces
ariguiba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
pages/docs/couple-your-code/couple-your-code-gradient-data.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
--- | ||
title: Step 9 – Gradient data | ||
permalink: couple-your-code-gradient-data.html | ||
keywords: api, adapter, mapping, gradient | ||
summary: "So far, our mesh contains only data. This is sufficient for most of the numerical methods that preCICE offers. For nearest-neighbor-gradient mapping, however, preCICE also requires additional gradient data information. In this step, you learn how to add gradient data to the mesh." | ||
--- | ||
|
||
{% version 2.4.0 %} | ||
This feature is available since version 2.4.0. | ||
{% endversion %} | ||
|
||
When using `nearest-neighbor-gradient` mapping, we require coupling data and additional gradient data. We have seen in [Step 3](couple-your-code-mesh-and-data-access.html) how to write data to the mesh. | ||
Now, we will learn how to write gradient data to the mesh. | ||
|
||
For this purpose, we use the following API methods: | ||
|
||
```cpp | ||
bool isDataGradientRequired(int dataID); | ||
|
||
void writeScalarGradientData ( | ||
int dataID, | ||
int valueIndex, | ||
const double* gradientValues ); | ||
|
||
void writeBlockScalarGradientData ( | ||
int dataID, | ||
int size, | ||
const int* valueIndices, | ||
const double* gradientValues ); | ||
|
||
void writeVectorGradientData ( | ||
int dataID, | ||
int valueIndex, | ||
const double* gradientValues, | ||
bool rowsFirst = false ); | ||
|
||
void writeBlockVectorGradientData ( | ||
int dataID, | ||
int size, | ||
const int* valueIndices, | ||
const double* gradientValues, | ||
bool rowsFirst = false ); | ||
``` | ||
|
||
* `isDataGradientRequired` returns a boolean, indicates if the data corresponding to the ID `dataID` has gradient data. | ||
* `writeScalarGradientData` writes scalar-valued gradient data, derived in each spatial dimension to the coupling data structure. | ||
* `ẁriteBlockScalarGradintData` writes multiple scalar gradient data at once, for performance reasons. | ||
* `writeVectorGradientData` writes vector-valued gradient data to the coupling data structure. The matrix is entered as a 1D-array of each component differentiated first. The parameter `rowsFirst` allows to write the values of the matrix, such as the components are differentiated in the spatial dimensions first. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, of course, what you mean, but I have troubles understanding the last two sentences. |
||
* `ẁriteBlockVectorGradintData` writes multiple vector gradient data at once, for performance reasons. | ||
|
||
Let's consider an example for writing block vector gradient data corresponding to the vector data `v0 = (v0x, v0y) , v1 = (v1x, v1y), ... , vn = (vnx, vny)` differentiated in spatial directions x and y. | ||
Per default, the values are passed as following: | ||
|
||
```cpp | ||
( v0x_dx, v0x_dy, v0y_dx, v0y_dy, | ||
v1x_dx, v1x_dy, v1y_dx, v1y_dy, | ||
... , | ||
vnx_dx, vnx_dy, vny_dx, vny_dy ) | ||
``` | ||
|
||
When using the `rowsFirst` parameter, the following format is required: | ||
|
||
```cpp | ||
( v0x_dx, v0y_dx, v1x_dx, v1y_dx, ... , vnx_dx, vny_dx, | ||
v0x_dy, v0y_dy, v1x_dy, v1y_dy, ... , vnx_dy, vny_dy ) | ||
``` | ||
|
||
{% note %} | ||
Gradient data must be added to an existing data ID. Therefore, we must create data first, then the corresponding gradient data. | ||
{% endnote %} | ||
davidscn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Let's define access coupling data and gradient data in our example code: | ||
davidscn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```cpp | ||
turnOnSolver(); //e.g. setup and partition mesh | ||
|
||
precice::SolverInterface precice("FluidSolver","precice-config.xml",rank,size); // constructor | ||
|
||
// set mesh elements | ||
int dim = precice.getDimensions(); | ||
int meshID = precice.getMeshID("FluidMesh"); | ||
int vertexSize; // number of vertices at wet surface | ||
|
||
// determine vertexSize | ||
double* coords = new double[vertexSize*dim]; // coords of coupling vertices | ||
// determine coordinates | ||
int* vertexIDs = new int[vertexSize]; | ||
precice.setMeshVertices(meshID, vertexSize, coords, vertexIDs); | ||
delete[] coords; | ||
|
||
// create data first | ||
int displID = precice.getDataID("Displacements", meshID); | ||
int forceID = precice.getDataID("Forces", meshID); | ||
double* forces = new double[vertexSize*dim]; | ||
double* displacements = new double[vertexSize*dim]; | ||
|
||
// create gradient data | ||
double* forces_gradient = new double[vertexSize*dim*dim] | ||
|
||
double dt; // solver timestep size | ||
double precice_dt; // maximum precice timestep size | ||
|
||
precice_dt = precice.initialize(); | ||
while (not simulationDone()){ // time loop | ||
precice.readBlockVectorData(displID, vertexSize, vertexIDs, displacements); | ||
setDisplacements(displacements); | ||
dt = beginTimeStep(); // e.g. compute adaptive dt | ||
dt = min(precice_dt, dt); | ||
solveTimeStep(dt); | ||
computeForces(forces); | ||
|
||
precice.writeBlockVectorData(forceID, vertexSize, vertexIDs, forces); | ||
|
||
// write gradient data | ||
if (isGradientDataRequired(dataID)){ | ||
precice.writeBlockVectorGradientData(forceID, vertexSize, vertexIDs, forces_gradient); | ||
} | ||
|
||
precice_dt = precice.advance(dt); | ||
endTimeStep(); // e.g. update variables, increment time | ||
} | ||
precice.finalize(); // frees data structures and closes communication channels | ||
delete[] vertexIDs, forces, displacements; | ||
turnOffSolver(); | ||
``` | ||
|
||
{% experimental %} | ||
This is an experimental feature. | ||
{% endexperimental %} | ||
|
||
As a last step, you need to set the flag `gradient="on"` in the configuration file, whenever you require to write gradient data. | ||
|
||
For the example, you can use the following `precice-config.xml`: | ||
|
||
```xml | ||
<?xml version="1.0"?> | ||
|
||
<precice-configuration> | ||
|
||
<solver-interface dimensions="3"> | ||
|
||
<data:vector name="Forces" gradient="on"/> | ||
<data:vector name="Displacements"/> | ||
|
||
<mesh name="FluidMesh"> | ||
<use-data name="Forces"/> | ||
<use-data name="Displacements"/> | ||
</mesh> | ||
|
||
<mesh name="StructureMesh"> | ||
<use-data name="Forces"/> | ||
<use-data name="Displacements"/> | ||
</mesh> | ||
|
||
<participant name="FluidSolver"> | ||
<use-mesh name="FluidMesh" provide="yes"/> | ||
<use-mesh name="StructureMesh" from="SolidSolver"/> | ||
<write-data name="Forces" mesh="FluidMesh"/> | ||
<read-data name="Displacements" mesh="FluidMesh"/> | ||
<mapping:nearest-neighbor direction="write" from="FluidMesh" | ||
to="StructureMesh" constraint="conservative"/> | ||
<mapping:nearest-neighbor direction="read" from="StructureMesh" | ||
to="FluidMesh" constraint="consistent"/> | ||
</participant> | ||
|
||
<participant name="SolidSolver"> | ||
<use-mesh name="StructureMesh" provide="yes"/> | ||
<write-data name="Displacements" mesh="StructureMesh"/> | ||
<read-data name="Forces" mesh="StructureMesh"/> | ||
</participant> | ||
|
||
<m2n:sockets from="FluidSolver" to="SolidSolver"/> | ||
|
||
<coupling-scheme:serial-explicit> | ||
<participants first="FluidSolver" second="SolidSolver"/> | ||
<max-time-windows value="10" /> | ||
<time-window-size value="1.0" /> | ||
<exchange data="Forces" mesh="StructureMesh" from="FluidSolver" to="SolidSolver"/> | ||
<exchange data="Displacements" mesh="StructureMesh" from="SolidSolver" to="FluidSolver"/> | ||
</coupling-scheme:serial-explicit> | ||
|
||
</solver-interface> | ||
|
||
</precice-configuration> | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.