Skip to content

Commit 1a9e709

Browse files
authored
Update tutorials (#152)
* minor * updated c++ tutorials * updated c tutorials * updated python tutorials * leftover
1 parent bd2dff8 commit 1a9e709

4 files changed

Lines changed: 133 additions & 142 deletions

File tree

tutorial/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tutorials for integrating the _CoSimIO_
22

3-
This folder contains step-by-step instructions on how to integrate the _CoSimIO_ into a solver/software-tool. Since the _CoSimIO_ is independent of Kratos, this can be done completely standalone.
3+
This folder contains step-by-step instructions on how to integrate the _CoSimIO_ into a solver/software-tool. Since the _CoSimIO_ is independent of Kratos, this is done completely standalone.
44

55
Each subfolder has the tutorial for an specific language:
66
* **C++ Interface**: Is the natural interface of the _CoSimIO_ providing its full capabilities.

tutorial/c/README.md

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tutorial for integrating the _CoSimIO_ using the C interface
22

3-
This tutorial helps you through to integrate the _CoSimIO_ into a solver/software-tool using the C interface.
3+
This tutorial helps you to integrate the _CoSimIO_ into a solver/software-tool using the C interface.
44

55
## Overview
66

@@ -96,13 +96,13 @@ CoSimIO_Info settings = CoSimIO_CreateInfo();
9696
It is important to mention that the `CoSimIO_Info` is a pointer to the cpp info class which is allocated by the `CoSimIO_CreateInfo()`. So it is don't forget to free it when is not needed anymore using `CoSimIO_FreeInfo()` function. This container can be used to pass additional information about the solver/software-tool or connection settings to the CoSimIO:
9797

9898
```c
99-
CoSimIO_Info_SetString(settings, "my_name", "the_name_of_this_solver"); // The name of this solver
100-
CoSimIO_Info_SetString(settings, "connect_to", "the_other_solver_name"); // The name of the solver to connect to
99+
CoSimIO_Info_SetString(settings, "my_name", "the_name_of_this_solver"); // my name
100+
CoSimIO_Info_SetString(settings, "connect_to", "the_other_solver_name"); // to whom I want to connect toto
101101
CoSimIO_Info_SetInt(settings, "echo_level", 1);
102-
CoSimIO_Info_SetString(settings, "solver_version", "1.25");
102+
CoSimIO_Info_SetString(settings, "version", "1.25");
103103
```
104-
This function returns a `Info` object containing information about the connection which can be queried using `CoSimIO_Info_Get...` functions:
105104
105+
This method returns a `Info` object containing information about the connection which can be queried using `CoSimIO_Info_Get...` method. For further calls to `CoSimIO` it is necessary to get the `connection_name`:
106106
```c
107107
CoSimIO_Info_GetString(info, "connection_name");
108108
CoSimIO_Info_GetInt(info, "connection_status");
@@ -117,10 +117,10 @@ Now putting together everything:
117117
int main()
118118
{
119119
CoSimIO_Info settings=CoSimIO_CreateInfo();
120-
CoSimIO_Info_SetString(settings, "my_name", "the_name_of_this_solver"); // The name of this solver
121-
CoSimIO_Info_SetString(settings, "connect_to", "the_other_solver_name"); // The name of the solver to connect to
120+
CoSimIO_Info_SetString(settings, "my_name", "c_connect_disconnect_a");
121+
CoSimIO_Info_SetString(settings, "connect_to", "c_connect_disconnect_b");
122122
CoSimIO_Info_SetInt(settings, "echo_level", 1);
123-
CoSimIO_Info_SetString(settings, "solver_version", "1.25");
123+
CoSimIO_Info_SetString(settings, "version", "1.25");
124124

125125
// The connect must be called before any CosimIO function called
126126
CoSimIO_Info connect_info = CoSimIO_Connect(settings);
@@ -130,33 +130,31 @@ int main()
130130
// - The name of the connection ("connection_name") to be used for further calls to CoSimIO
131131
// - The status of the connection ("connection_status")
132132

133-
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name");
133+
const char* connection_name = CoSimIO_Info_GetString(connect_info, "connection_name"); // getting name of connection for future calls
134134

135135
if (CoSimIO_Info_GetInt(connect_info, "connection_status") != CoSimIO_Connected)
136136
return 1;
137137

138-
// Don't forget to release the connect_info after getting your information
139-
CoSimIO_FreeInfo(connect_info);
140-
141138
// Now you may call any CoSimIO functions
142139
// ...
143140

144141
// Here you may use the info but cannot call any CoSimIO function anymore
145142
CoSimIO_Info disconnect_settings=CoSimIO_CreateInfo();
146-
CoSimIO_Info_SetString(settings, "connection_name", connection_name);
143+
CoSimIO_Info_SetString(settings, "connection_name", connection_name); // connection_name is obtained from calling "Connect"
147144
CoSimIO_Info disconnect_info = CoSimIO_Disconnect(disconnect_settings); // disconnect afterwards
148145
if(CoSimIO_Info_GetInt(disconnect_info, "connection_status") != CoSimIO_Disconnected)
149146
return 1;
150147

151148
// Don't forget to release the settings and info
149+
CoSimIO_FreeInfo(connect_info);
152150
CoSimIO_FreeInfo(disconnect_settings);
153151
CoSimIO_FreeInfo(disconnect_info);
154152

155153
return 0;
156154
}
157155
```
158156

159-
This example can be found in [integration_tutorials/c/connect_disconnect.c](../../tests/integration_tutorials/c/connect_disconnect.c).
157+
This example can be found in [integration_tutorials/c/connect_disconnect_a.c](../../tests/integration_tutorials/c/connect_disconnect_a.c) and [integration_tutorials/c/connect_disconnect_b.c](../../tests/integration_tutorials/c/connect_disconnect_b.c).
160158

161159

162160
## Tutorial 4: Data Exchange
@@ -171,7 +169,7 @@ First we should create a setting which provides an identifier (like "velocity_of
171169
// Creatint the export_settings
172170
CoSimIO_Info export_settings = CoSimIO_CreateInfo();
173171
CoSimIO_Info_SetString(export_settings, "identifier", "vector_of_pi");
174-
CoSimIO_Info_SetString(export_settings, "connection_name", "test_connection");
172+
CoSimIO_Info_SetString(export_settings, "connection_name", connection_name); // connection_name is obtained from calling "Connect"
175173
```
176174
And then use it to export the data:
177175
```c
@@ -195,7 +193,7 @@ int data_allocated_size = 0;
195193
// Creatint the import_settings
196194
CoSimIO_Info import_settings = CoSimIO_CreateInfo();
197195
CoSimIO_Info_SetString(import_settings, "identifier", "vector_of_pi");
198-
CoSimIO_Info_SetString(import_settings, "connection_name", "test_connection");
196+
CoSimIO_Info_SetString(import_settings, "connection_name", connection_name); // connection_name is obtained from calling "Connect"
199197
```
200198
201199
In this case we just pass an empty pointer and specifying to the `ImportData()` that should allocate the data by itself. So, in order to ensure the memory coherance, the `CoSimIO_Free()` function should be used instead of standard `free()` function:
@@ -216,54 +214,50 @@ After seeing how we transfer raw data between solvers/software-tools, it is time
216214
```c
217215
CoSimIO_Info export_settings = CoSimIO_CreateInfo();
218216
CoSimIO_Info_SetString(export_settings, "identifier", "fluid_mesh");
219-
CoSimIO_Info_SetString(export_settings, "connection_name", "test_connection");
220-
CoSimIO_Info export_info = CoSimIO_ExportMesh(export_settings
221-
, number_of_nodes,number_of_elements,number_of_elements_connectivities
222-
, nodal_coordinates, elements_connectivities, elements_types);
217+
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);
223219
```
224220
225-
The arguments are:
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`.
226222
227-
* `number_of_nodes`: Number of Nodes (== size(`nodal_coordinates`)/3)
228-
* `number_of_elements`: Number of Elements (== size(`elements_types`))
229-
* `number_of_elements_connectivities`: Number of Elements connectivities (== size(`elements_connectivities`))
230-
231-
* `nodal_coordinates`: A vector of doubles of 3D coordinates of each node in x1,y1,z1,x2,y2,z2,... format:
223+
Nodes can be created like this:
232224
```c
233-
double nodal_coordinates[] = {
234-
0.0, 2.5, 1.0, /*0*/
235-
2.0, 0.0, 1.5, /*1*/
236-
2.0, 2.5, 1.5, /*2*/
237-
4.0, 2.5, 1.7, /*3*/
238-
4.0, 0.0, 1.7, /*4*/
239-
6.0, 0.0, 1.8 /*5*/
240-
};
241-
```
242-
* `elements_connectivities`: A vector of int containing the zero based index of each node in e1_1,e1_2,...,e2_1, e2_2,... format:
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+
```
235+
236+
Elements can be created after nodes were created:
243237
```c
244-
int elements_connectivities[] = {
245-
0, 1, 2, /*1*/
246-
1, 3, 2, /*2*/
247-
1, 4, 3, /*3*/
248-
3, 4, 5, /*4*/
249-
};
238+
int connectivity[2] = {1,2};
239+
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+
);
250246
```
251247
252-
* `elements_types`: A vector of int containing the type of the elements. They are according to the vtk cell types, see [this link](https://vtk.org/wp-content/uploads/2015/04/file-formats.pdf), page 9 & 10.
248+
Don't forget to free the `CoSimIO_ModelPart` after using it with
253249
```c
254-
int elements_types[] = {5,5,5,5}; // VTK_TRIANGLE
250+
CoSimIO_FreeModelPart(model_part);
255251
```
256252

257253
On the other side one can use the `ImportMesh()` method to get the mesh sent by the export:
258254

259255
```c
260256
CoSimIO_Info import_settings=CoSimIO_CreateInfo();
261257
CoSimIO_Info_SetString(import_settings, "identifier", "fluid_mesh");
262-
CoSimIO_Info_SetString(import_settings, "connection_name", "test_connection");
258+
CoSimIO_Info_SetString(import_settings, "connection_name", connection_name); // connection_name is obtained from calling "Connect"
263259

264-
CoSimIO_Info import_info = CoSimIO_ImportMesh(import_settings
265-
, &number_of_nodes,&number_of_elements,&number_of_elements_connectivities
266-
, &nodal_coordinates, &elements_connectivities, &elements_types);
260+
CoSimIO_Info import_info = CoSimIO_ImportMesh(import_settings, model_part);
267261
```
268262
269263
This example can be found in [integration_tutorials/c/export_mesh.c](../../tests/integration_tutorials/c/export_mesh.c) and [integration_tutorials/c/import_mesh.c](../../tests/integration_tutorials/c/import_mesh.c).
@@ -276,7 +270,8 @@ The overview of the Kratos CoSimulation Library can be found [here](../README.md
276270
The building instructions for the Kratos CoSimulation Library can be found [here](../README.md#building-kratos-with-cosimulation).
277271
278272
## Tutorial 8: Connecting/Disconnecting to/from Kratos
279-
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):
273+
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):
280275
281276
```Python
282277
from KratosMultiphysics.CoSimulationApplication import CoSimIO
@@ -302,10 +297,11 @@ Then you may run your executable with python script of Kratos from your working
302297

303298
```shell
304299
path/to/bin/tests_c/connect_disconnect_c_test & python3 path/to/connect_disconnect.py
305-
```
300+
``` -->
306301
307302
## Tutorial 9: Data Exchange with Kratos
308-
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):
303+
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):
309305
310306
311307
```python
@@ -379,10 +375,11 @@ Now for running the test:
379375
380376
```shell
381377
path/to/bin/tests_c/export_import_data_c_test & python3 path/to/import_export_data.py
382-
```
378+
``` -->
383379
384380
## Tutorial 10: Mesh Exchange with Kratos
385-
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).
381+
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).
386383
387384
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:
388385
@@ -508,10 +505,11 @@ Now for running the test:
508505
509506
```shell
510507
path/to/bin/tests_c/export_import_mesh_c_test & python3 path/to/import_export_mesh.py
511-
```
508+
``` -->
512509
513510
## Tutorial 11: Mapping with Kratos
514-
This tutorial shows how to map data between (non matching) meshes with Kratos. It is based on tutorials 9 & 10.
511+
coming soon!
512+
<!-- This tutorial shows how to map data between (non matching) meshes with Kratos. It is based on tutorials 9 & 10.
515513
516514
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.
517515
@@ -809,4 +807,4 @@ disconnect_settings.SetString("connection_name", "mesh_mapping")
809807
info = CoSimIO.Disconnect(disconnect_settings)
810808
if info.GetInt("connection_status") != CoSimIO.ConnectionStatus.Disconnected:
811809
raise Exception("Disconnecting failed")
812-
```
810+
``` -->

0 commit comments

Comments
 (0)