Skip to content

Commit 2b68add

Browse files
authored
Tutorial improvements (#162)
* explaining Python version check * commenting future Tutorials * commenting missing * adding Changelog * more tests * improve ModelPart C tutorial * minor * adding ModelPart tutorial for C++ * adding python ModelPart tutorial * switch order * proper fix * Update test_model_part.c
1 parent 636784a commit 2b68add

File tree

10 files changed

+429
-94
lines changed

10 files changed

+429
-94
lines changed

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Changelog
2+
3+
All important and notable changes in the _CoSimIO_ will be documented in this file.
4+
5+
## 2.0.0
6+
- Changes in Connecting:
7+
- Now instead of specifying `connection_name` directly, `my_name` and `connect_to` are used for the call to `Connect`.
8+
- The `CoSimIO::Info` that is returned from `Connect` now contains the `connection_name` that needs to be used in subsequent calls to _CoSimIO_.
9+
- This change was done to make the connection more robust and to make the selection of primary/secondary partner clear. This is only used internally e.g. for who opens a port and who connects to it.
10+
- Introduced `CoSimIO::ModelPart` for exchanging of meshes
11+
- Simplified version of [`Kratos::ModelPart`](https://github.com/KratosMultiphysics/Kratos/blob/master/kratos/includes/model_part.h)
12+
- Simplifies and unifies the usage of `Import-/ExportMesh`
13+
- See the tutorials on how to use it:
14+
- [C++](tutorial/cpp/model_part.md)
15+
- [C](tutorial/c/model_part.md)
16+
- [Python](tutorial/python/model_part.md)
17+
- FileCommunication:
18+
- By default now done in folder. This way leftovers from previous simulations can be easily deleted (done automatically).
19+
- working directory can be specified
20+
- stability of initial connection was significantly improved.
21+
- Python interface: Data is no longer copied when going from Python to C++ and vice versa.
22+
- `Import-/ExportData` now uses `CoSimIO::DoubleVector` (small wrapper around `std::wrapper`)
23+
- `Import-/ExportMesh` now uses `CoSimIO::ModelPart`
24+
- Continuous Integration:
25+
- Adding Python 3.9 (now has Python v3.5 - v3.9)
26+
- Adding CentOS 7 build with GCC 4.8.5
27+
- Enforcing C89 standard
28+
29+
- Many improvements and cleanups under the hood

co_sim_io/c/co_sim_io_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ double CoSimIO_Node_Z(CoSimIO_Node I_Node)
290290

291291
double CoSimIO_Node_Coordinate(CoSimIO_Node I_Node, const int I_Index)
292292
{
293-
// add debug error if I_Index is out of bound (admissible values: 0,1,2)
293+
// TODO add debug error if I_Index is out of bound (admissible values: 0,1,2)
294294
return static_cast<CoSimIO::Node*>(I_Node.PtrCppNode)->Coordinates()[I_Index];
295295
}
296296

tests/co_sim_io/c/model_part/test_model_part.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ int main()
2020
/* declaring variables */
2121
int i;
2222
int connectivity[2];
23+
CoSimIO_Node node;
24+
CoSimIO_Element elem;
2325
CoSimIO_ModelPart model_part;
2426

2527
model_part = CoSimIO_CreateModelPart("my_model_part");
@@ -37,6 +39,16 @@ int main()
3739
-i);
3840
}
3941

42+
/* iterate the nodes */
43+
for (i=0; i<CoSimIO_ModelPart_NumberOfNodes(model_part); ++i) {
44+
node = CoSimIO_ModelPart_GetNodeByIndex(model_part, i);
45+
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Node_Id(node), i+1);
46+
}
47+
48+
/* get a specific node by Id */
49+
node = CoSimIO_ModelPart_GetNodeById(model_part, 3);
50+
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Node_Id(node), 3);
51+
4052
COSIMIO_CHECK_INT_EQUAL(CoSimIO_ModelPart_NumberOfNodes(model_part), 4);
4153
COSIMIO_CHECK_INT_EQUAL(CoSimIO_ModelPart_NumberOfElements(model_part), 0);
4254

@@ -50,6 +62,16 @@ int main()
5062
connectivity);
5163
}
5264

65+
/* iterate the elements */
66+
for (i=0; i<CoSimIO_ModelPart_NumberOfElements(model_part); ++i) {
67+
elem = CoSimIO_ModelPart_GetElementByIndex(model_part, i);
68+
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Element_Id(elem), i+1)
69+
}
70+
71+
/* get a specific element by Id */
72+
elem = CoSimIO_ModelPart_GetElementById(model_part, 1);
73+
COSIMIO_CHECK_INT_EQUAL(CoSimIO_Element_Id(elem), 1);
74+
5375
COSIMIO_CHECK_INT_EQUAL(CoSimIO_ModelPart_NumberOfNodes(model_part), 4);
5476
COSIMIO_CHECK_INT_EQUAL(CoSimIO_ModelPart_NumberOfElements(model_part), 2);
5577

tests/co_sim_io/impl/test_model_part.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ TEST_CASE("model_part_ostream")
413413

414414
SUBCASE("with_entities")
415415
{
416-
const int node_ids[] = {2, 159, 61};
416+
const std::vector<IdType> node_ids {2, 159, 61};
417417
const std::array<double, 3> node_coords = {1.0, -2.7, 9.44};
418418
model_part.CreateNewNode(node_ids[0], node_coords[0], node_coords[1], node_coords[2]);
419419
model_part.CreateNewNode(node_ids[1], node_coords[1], node_coords[2], node_coords[0]);

tutorial/c/README.md

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ This tutorial helps you to integrate the _CoSimIO_ into a solver/software-tool u
1414
- [Tutorial 5: Mesh Exchange](#tutorial-5-mesh-exchange)
1515
- [Tutorial 6: Kratos CoSimulation Library Overview](#tutorial-6-kratos-cosimulation-library-overview)
1616
- [Tutorial 7: Building Kratos with CoSimulation](#tutorial-7-building-kratos-with-cosimulation)
17-
- [Tutorial 8: Connecting/Disconnecting to/from Kratos](#tutorial-8-connectingdisconnecting-tofrom-kratos)
17+
<!-- - [Tutorial 8: Connecting/Disconnecting to/from Kratos](#tutorial-8-connectingdisconnecting-tofrom-kratos)
1818
- [Tutorial 9: Data Exchange with Kratos](#tutorial-9-data-exchange-with-kratos)
1919
- [Tutorial 10: Mesh Exchange with Kratos](#tutorial-10-mesh-exchange-with-kratos)
20-
- [Tutorial 11: Mapping with Kratos](#tutorial-11-mapping-with-kratos)
20+
- [Tutorial 11: Mapping with Kratos](#tutorial-11-mapping-with-kratos) -->
2121

2222
## What you need
2323
- Downloading the _CosimIO_ from the repository:
@@ -215,40 +215,13 @@ After seeing how we transfer raw data between solvers/software-tools, it is time
215215
CoSimIO_Info export_settings = CoSimIO_CreateInfo();
216216
CoSimIO_Info_SetString(export_settings, "identifier", "fluid_mesh");
217217
CoSimIO_Info_SetString(export_settings, "connection_name", connection_name); // connection_name is obtained from calling "Connect"
218-
CoSimIO_Info export_info = CoSimIO_ExportMesh(export_settings, model_part);
219-
```
220-
221-
The argument `model_part` is a container for mesh, it contains nodes and elements. Check the [implementation](../../co_sim_io/c/co_sim_io_c_model_part.h) and the [tests](../../tests/co_sim_io/c/model_part/test_model_part.c) for details of `CoSimIO::ModelPart`.
222-
223-
Nodes can be created like this:
224-
```c
225-
CoSimIO_ModelPart model_part = CoSimIO_CreateModelPart("my_model_part");
226-
227-
CoSimIO_ModelPart_CreateNewNode(
228-
model_part,
229-
1, // Id
230-
0.0, // X-Coordinate
231-
1.5, // Y-Coordinate
232-
-4.22 // Z-Coordinate
233-
);
234-
```
235218

236-
Elements can be created after nodes were created:
237-
```c
238-
int connectivity[2] = {1,2};
219+
CoSimIO_ModelPart model_part = CoSimIO_CreateModelPart("name_of_model_part_to_export");
239220

240-
CoSimIO_ModelPart_CreateNewElement(
241-
model_part,
242-
2, // Id
243-
CoSimIO_Line2D2, // Type of element, see "co_sim_io/c/co_sim_io_c_model_part.h"
244-
connectivity // Connectivity information, i.e. Ids of nodes that the element has
245-
);
221+
CoSimIO_Info export_info = CoSimIO_ExportMesh(export_settings, model_part);
246222
```
247223
248-
Don't forget to free the `CoSimIO_ModelPart` after using it with
249-
```c
250-
CoSimIO_FreeModelPart(model_part);
251-
```
224+
The argument `model_part` is of type `CoSimIO_ModelPart`. Its usage is explained [here](model_part.md).
252225
253226
On the other side one can use the `ImportMesh()` method to get the mesh sent by the export:
254227
@@ -257,6 +230,8 @@ CoSimIO_Info import_settings=CoSimIO_CreateInfo();
257230
CoSimIO_Info_SetString(import_settings, "identifier", "fluid_mesh");
258231
CoSimIO_Info_SetString(import_settings, "connection_name", connection_name); // connection_name is obtained from calling "Connect"
259232
233+
CoSimIO_ModelPart model_part = CoSimIO_CreateModelPart("name_of_imported_model_part");
234+
260235
CoSimIO_Info import_info = CoSimIO_ImportMesh(import_settings, model_part);
261236
```
262237

@@ -268,10 +243,10 @@ The overview of the Kratos CoSimulation Library can be found [here](../README.md
268243

269244
## Tutorial 7: Building Kratos with CoSimulation
270245
The building instructions for the Kratos CoSimulation Library can be found [here](../README.md#building-kratos-with-cosimulation).
271-
246+
<!--
272247
## Tutorial 8: Connecting/Disconnecting to/from Kratos
273248
coming soon!
274-
<!-- For connecting to Kratos it is very important to have in mind that Kratos also uses *CoSimIO* for interprocess communication so its python interface reflects the CoSimIO. So we may create a python script for connecting and disconnecting in the same way described in the [python tutorial](https://github.com/KratosMultiphysics/CoSimIO/blob/master/tutorial/python/README.md):
249+
For connecting to Kratos it is very important to have in mind that Kratos also uses *CoSimIO* for interprocess communication so its python interface reflects the CoSimIO. So we may create a python script for connecting and disconnecting in the same way described in the [python tutorial](https://github.com/KratosMultiphysics/CoSimIO/blob/master/tutorial/python/README.md):
275250
276251
```Python
277252
from KratosMultiphysics.CoSimulationApplication import CoSimIO
@@ -297,11 +272,11 @@ Then you may run your executable with python script of Kratos from your working
297272
298273
```shell
299274
path/to/bin/tests_c/connect_disconnect_c_test & python3 path/to/connect_disconnect.py
300-
``` -->
275+
```
301276
302277
## Tutorial 9: Data Exchange with Kratos
303278
coming soon!
304-
<!-- Here we try to send some data to Kratos and get it back from it. Then we can check if both data are the same. Again the python file for Kratos side is very similar to the one descirbed in the [python tutorial](https://github.com/KratosMultiphysics/CoSimIO/blob/master/tutorial/python/README.md):
279+
Here we try to send some data to Kratos and get it back from it. Then we can check if both data are the same. Again the python file for Kratos side is very similar to the one descirbed in the [python tutorial](https://github.com/KratosMultiphysics/CoSimIO/blob/master/tutorial/python/README.md):
305280
306281
307282
```python
@@ -375,11 +350,11 @@ Now for running the test:
375350
376351
```shell
377352
path/to/bin/tests_c/export_import_data_c_test & python3 path/to/import_export_data.py
378-
``` -->
353+
```
379354
380355
## Tutorial 10: Mesh Exchange with Kratos
381356
coming soon!
382-
<!-- In this step we send a mesh to Kratos and receive it back and we will check if they are the same. (like previous tutorial with data).
357+
In this step we send a mesh to Kratos and receive it back and we will check if they are the same. (like previous tutorial with data).
383358
384359
Recalling from what we had in tutorial 5 we just merge the export mesh and import mesh codes into one as we did for data exchage in previous tutorial:
385360
@@ -505,11 +480,11 @@ Now for running the test:
505480
506481
```shell
507482
path/to/bin/tests_c/export_import_mesh_c_test & python3 path/to/import_export_mesh.py
508-
``` -->
483+
```
509484
510485
## Tutorial 11: Mapping with Kratos
511486
coming soon!
512-
<!-- This tutorial shows how to map data between (non matching) meshes with Kratos. It is based on tutorials 9 & 10.
487+
This tutorial shows how to map data between (non matching) meshes with Kratos. It is based on tutorials 9 & 10.
513488
514489
In this tutorial we first send two meshes based on the same geometry but with different discretizations to Kratos. Those meshes are used as basis for the mapping. In Kratos teminology those are the origin and the destination.
515490

tutorial/c/model_part.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# ModelPart
2+
3+
The `ModelPart` is a container for mesh, it contains nodes and elements.
4+
It is a simplified version of [`Kratos::ModelPart`](https://github.com/KratosMultiphysics/Kratos/blob/master/kratos/includes/model_part.h).
5+
6+
### Interface of _CoSimIO_ModelPart_
7+
Create and free a `ModelPart` like this:
8+
```c
9+
// create CoSimIO_ModelPart
10+
CoSimIO_ModelPart model_part = CoSimIO_CreateModelPart("my_model_part");
11+
12+
// use model_part
13+
// ...
14+
15+
// access the name of the ModelPart:
16+
const char* name = CoSimIO_ModelPart_Name(model_part);
17+
18+
// don't forget to free it after using it
19+
CoSimIO_FreeModelPart(model_part);
20+
```
21+
22+
Nodes can be created like this:
23+
```c
24+
// memory of node is managed by model_part!
25+
CoSimIO_Node node = CoSimIO_ModelPart_CreateNewNode(
26+
model_part,
27+
1, // Id
28+
0.0, // X-Coordinate
29+
1.5, // Y-Coordinate
30+
-4.22 // Z-Coordinate
31+
);
32+
```
33+
34+
Elements can be created after nodes were created:
35+
```c
36+
int connectivity[2] = {1,2}; // Ids of the Nodes
37+
38+
// memory of element is managed by model_part!
39+
CoSimIO_Element element = CoSimIO_ModelPart_CreateNewElement(
40+
model_part,
41+
2, // Id
42+
CoSimIO_Line2D2, // Type of element, see "co_sim_io/c/co_sim_io_c_model_part.h"
43+
connectivity // Connectivity information, i.e. Ids of nodes that the element has
44+
);
45+
```
46+
47+
Use the following functions to get the number of nodes and elements:
48+
```c
49+
int number_of_nodes = CoSimIO_ModelPart_NumberOfNodes(model_part);
50+
51+
int number_of_elements = CoSimIO_ModelPart_NumberOfElements(model_part);
52+
```
53+
54+
The nodes and elements can be iterated with:
55+
```c
56+
// iterate nodes
57+
for (int i=0; i<CoSimIO_ModelPart_NumberOfNodes(model_part); ++i) {
58+
CoSimIO_Node node = CoSimIO_ModelPart_GetNodeByIndex(model_part, i);
59+
// do sth with node
60+
}
61+
62+
// iterate elements
63+
for (int i=0; i<CoSimIO_ModelPart_NumberOfElements(model_part); ++i) {
64+
CoSimIO_Element element = CoSimIO_ModelPart_GetElementByIndex(model_part, i);
65+
// do sth with element
66+
}
67+
```
68+
69+
Nodes and elements can also be accessed by Id:
70+
```c
71+
// get the node with Id 3
72+
CoSimIO_Node node = CoSimIO_ModelPart_GetNodeById(model_part, 3);
73+
74+
// get the element with Id 12
75+
CoSimIO_Element element = CoSimIO_ModelPart_GetElementById(model_part, 12);
76+
```
77+
78+
Removing all nodes and elements can be done with the following:
79+
```c
80+
// removing all nodes and elements
81+
CoSimIO_ModelPart_Clear(model_part);
82+
```
83+
84+
### Interface of _CoSimIO_Node_
85+
The _CoSimIO_Node_ an be used in the following way:
86+
```c
87+
// access Id of node:
88+
int node_id = CoSimIO_Node_Id(node);
89+
90+
// access the coordinates:
91+
double node_x = CoSimIO_Node_X(node);
92+
double node_y = CoSimIO_Node_Y(node);
93+
double node_z = CoSimIO_Node_Z(node);
94+
95+
// or with index:
96+
double node_x_idx = CoSimIO_Node_Coordinate(node, 0);
97+
double node_y_idx = CoSimIO_Node_Coordinate(node, 1);
98+
double node_z_idx = CoSimIO_Node_Coordinate(node, 2);
99+
```
100+
101+
### Interface of _CoSimIO_Element_
102+
The _CoSimIO_Element_ provides the following interface:
103+
```c
104+
// access Id of element:
105+
int element_id = CoSimIO_Element_Id(element);
106+
107+
// the type can be accessed:
108+
CoSimIO_ElementType element_type = CoSimIO_Element_Type(element); // e.g. CoSimIO_Point3D or CoSimIO_Tetrahedra3D4
109+
110+
// number of nodes of the element:
111+
int num_nodes_element = CoSimIO_Element_NumberOfNodes(element);
112+
113+
// iterate the nodes of the element:
114+
for (int i=0; i<CoSimIO_Element_NumberOfNodes(element); ++i) {
115+
CoSimIO_Node node = CoSimIO_Element_GetNodeByIndex(element, i);
116+
// do sth with node
117+
}
118+
```
119+
120+
### Further information
121+
For more information check the [implementation](../../co_sim_io/c/co_sim_io_c_model_part.h) and the [tests](../../tests/co_sim_io/c/model_part/test_model_part.c).

0 commit comments

Comments
 (0)