1#[cxx::bridge(namespace = "precice::rust")]
2mod ffi {
3 unsafe extern "C++" {
4 include!("precice/src/precice-bridge.hpp");
5 type Participant;
6
7 fn create_participant(
8 participant: &str,
9 config: &str,
10 rank: i32,
11 size: i32,
12 ) -> Result<UniquePtr<Participant>>;
13
14 fn initialize(self: Pin<&mut Participant>) -> Result<()>;
16 fn advance(self: Pin<&mut Participant>, dt: f64) -> Result<()>;
17 fn finalize(self: Pin<&mut Participant>) -> Result<()>;
18
19 fn requires_reading_checkpoint(self: Pin<&mut Participant>) -> Result<bool>;
21 fn requires_writing_checkpoint(self: Pin<&mut Participant>) -> Result<bool>;
22
23 fn get_mesh_dimensions(self: &Participant, mesh_name: &str) -> Result<i32>;
25 fn get_data_dimensions(self: &Participant, mesh_name: &str, data_name: &str)
26 -> Result<i32>;
27 fn is_coupling_ongoing(self: &Participant) -> Result<bool>;
28 fn is_time_window_complete(self: &Participant) -> Result<bool>;
29 fn get_max_time_step_size(self: &Participant) -> Result<f64>;
30
31 fn requires_mesh_connectivity_for(self: &Participant, mesh_name: &str) -> Result<bool>;
34 fn reset_mesh(self: Pin<&mut Participant>, mesh_name: &str) -> Result<()>;
35 fn set_mesh_vertex(
36 self: Pin<&mut Participant>,
37 mesh_name: &str,
38 position: &[f64],
39 ) -> Result<i32>;
40 fn get_mesh_vertex_size(self: &Participant, mesh_name: &str) -> Result<i32>;
41 fn set_mesh_vertices(
42 self: Pin<&mut Participant>,
43 mesh_name: &str,
44 positions: &[f64],
45 ids: &mut [i32],
46 ) -> Result<()>;
47 fn set_mesh_edge(
48 self: Pin<&mut Participant>,
49 mesh_name: &str,
50 first_vertex_id: i32,
51 second_vertex_id: i32,
52 ) -> Result<()>;
53 fn set_mesh_edges(
54 self: Pin<&mut Participant>,
55 mesh_name: &str,
56 vertices: &[i32],
57 ) -> Result<()>;
58 fn set_mesh_triangle(
59 self: Pin<&mut Participant>,
60 mesh_name: &str,
61 first_vertex_id: i32,
62 second_vertex_id: i32,
63 third_vertex_id: i32,
64 ) -> Result<()>;
65 fn set_mesh_triangles(
66 self: Pin<&mut Participant>,
67 mesh_name: &str,
68 vertices: &[i32],
69 ) -> Result<()>;
70 fn set_mesh_quad(
71 self: Pin<&mut Participant>,
72 mesh_name: &str,
73 first_vertex_id: i32,
74 second_vertex_id: i32,
75 third_vertex_id: i32,
76 fourth_vertex_id: i32,
77 ) -> Result<()>;
78 fn set_mesh_quads(
79 self: Pin<&mut Participant>,
80 mesh_name: &str,
81 vertices: &[i32],
82 ) -> Result<()>;
83 fn set_mesh_tetrahedron(
84 self: Pin<&mut Participant>,
85 mesh_name: &str,
86 first_vertex_id: i32,
87 second_vertex_id: i32,
88 third_vertex_id: i32,
89 fourth_vertex_id: i32,
90 ) -> Result<()>;
91 fn set_mesh_tetrahedra(
92 self: Pin<&mut Participant>,
93 mesh_name: &str,
94 vertices: &[i32],
95 ) -> Result<()>;
96
97 fn requires_initial_data(self: Pin<&mut Participant>) -> Result<bool>;
100
101 fn write_data(
102 self: Pin<&mut Participant>,
103 mesh_name: &str,
104 data_name: &str,
105 vertices: &[i32],
106 values: &[f64],
107 ) -> Result<()>;
108 fn read_data(
109 self: &Participant,
110 mesh_name: &str,
111 data_name: &str,
112 vertices: &[i32],
113 relative_read_dt: f64,
114 values: &mut [f64],
115 ) -> Result<()>;
116
117 pub fn start_profiling_section(
120 self: Pin<&mut Participant>,
121 section_name: &str,
122 ) -> Result<()>;
123
124 pub fn stop_last_profiling_section(self: Pin<&mut Participant>) -> Result<()>;
125
126 fn set_mesh_access_region(
129 self: Pin<&mut Participant>,
130 mesh_name: &str,
131 bounding_box: &[f64],
132 ) -> Result<()>;
133 fn get_mesh_vertex_ids_and_coordinates(
134 self: &Participant,
135 mesh_name: &str,
136 ids: &mut [i32],
137 coordinates: &mut [f64],
138 ) -> Result<()>;
139
140 fn requires_gradient_data_for(
143 self: &Participant,
144 mesh_name: &str,
145 data_name: &str,
146 ) -> Result<bool>;
147 fn write_gradient_data(
148 self: Pin<&mut Participant>,
149 mesh_name: &str,
150 data_name: &str,
151 vertices: &[i32],
152 gradients: &[f64],
153 ) -> Result<()>;
154
155 fn write_and_map_data(
158 self: Pin<&mut Participant>,
159 mesh_name: &str,
160 data_name: &str,
161 coordinates: &[f64],
162 values: &[f64],
163 ) -> Result<()>;
164
165 fn map_and_read_data(
166 self: &Participant,
167 mesh_name: &str,
168 data_name: &str,
169 coordinates: &[f64],
170 relative_read_dt: f64,
171 values: &mut [f64],
172 ) -> Result<()>;
173 }
174}
175
176pub struct Participant {
177 internal: cxx::UniquePtr<ffi::Participant>,
178}
179
180pub type VertexID = i32;
181pub type Error = cxx::Exception;
182
183impl Participant {
184 pub fn new(participant: &str, config: &str, rank: i32, size: i32) -> Result<Self, Error> {
185 Ok(Participant {
186 internal: ffi::create_participant(participant, config, rank, size)?,
187 })
188 }
189
190 pub fn initialize(&mut self) -> Result<(), Error> {
192 self.internal.pin_mut().initialize()
193 }
194 pub fn advance(&mut self, dt: f64) -> Result<(), Error> {
195 self.internal.pin_mut().advance(dt)
196 }
197 pub fn finalize(&mut self) -> Result<(), Error> {
198 self.internal.pin_mut().finalize()
199 }
200
201 pub fn requires_reading_checkpoint(&mut self) -> Result<bool, Error> {
203 self.internal.pin_mut().requires_reading_checkpoint()
204 }
205 pub fn requires_writing_checkpoint(&mut self) -> Result<bool, Error> {
206 self.internal.pin_mut().requires_writing_checkpoint()
207 }
208
209 pub fn get_mesh_dimensions(&self, mesh_name: &str) -> Result<i32, Error> {
211 self.internal.get_mesh_dimensions(mesh_name)
212 }
213 pub fn get_data_dimensions(&self, mesh_name: &str, data_name: &str) -> Result<i32, Error> {
214 self.internal.get_data_dimensions(mesh_name, data_name)
215 }
216 pub fn is_coupling_ongoing(&self) -> Result<bool, Error> {
217 self.internal.is_coupling_ongoing()
218 }
219 pub fn is_time_window_complete(&self) -> Result<bool, Error> {
220 self.internal.is_time_window_complete()
221 }
222 pub fn get_max_time_step_size(&self) -> Result<f64, Error> {
223 self.internal.get_max_time_step_size()
224 }
225
226 pub fn requires_mesh_connectivity_for(&self, mesh_name: &str) -> Result<bool, Error> {
229 self.internal.requires_mesh_connectivity_for(mesh_name)
230 }
231 pub fn reset_mesh(&mut self, mesh_name: &str) -> Result<(), Error> {
232 self.internal.pin_mut().reset_mesh(mesh_name)
233 }
234 pub fn set_mesh_vertex(
235 &mut self,
236 mesh_name: &str,
237 position: &[f64],
238 ) -> Result<VertexID, Error> {
239 self.internal.pin_mut().set_mesh_vertex(mesh_name, position)
240 }
241 pub fn get_mesh_vertex_size(&self, mesh_name: &str) -> Result<i32, Error> {
242 self.internal.get_mesh_vertex_size(mesh_name)
243 }
244 pub fn set_mesh_vertices(
245 &mut self,
246 mesh_name: &str,
247 positions: &[f64],
248 ids: &mut [VertexID],
249 ) -> Result<(), Error> {
250 self.internal
251 .pin_mut()
252 .set_mesh_vertices(mesh_name, positions, ids)
253 }
254 pub fn set_mesh_edge(
255 &mut self,
256 mesh_name: &str,
257 first_vertex_id: VertexID,
258 second_vertex_id: VertexID,
259 ) -> Result<(), Error> {
260 self.internal
261 .pin_mut()
262 .set_mesh_edge(mesh_name, first_vertex_id, second_vertex_id)
263 }
264 pub fn set_mesh_edges(&mut self, mesh_name: &str, vertices: &[VertexID]) -> Result<(), Error> {
265 self.internal.pin_mut().set_mesh_edges(mesh_name, vertices)
266 }
267 pub fn set_mesh_triangle(
268 &mut self,
269 mesh_name: &str,
270 first_vertex_id: VertexID,
271 second_vertex_id: VertexID,
272 third_vertex_id: VertexID,
273 ) -> Result<(), Error> {
274 self.internal.pin_mut().set_mesh_triangle(
275 mesh_name,
276 first_vertex_id,
277 second_vertex_id,
278 third_vertex_id,
279 )
280 }
281 pub fn set_mesh_triangles(
282 &mut self,
283 mesh_name: &str,
284 vertices: &[VertexID],
285 ) -> Result<(), Error> {
286 self.internal
287 .pin_mut()
288 .set_mesh_triangles(mesh_name, vertices)
289 }
290 pub fn set_mesh_quad(
291 &mut self,
292 mesh_name: &str,
293 first_vertex_id: VertexID,
294 second_vertex_id: VertexID,
295 third_vertex_id: VertexID,
296 fourth_vertex_id: VertexID,
297 ) -> Result<(), Error> {
298 self.internal.pin_mut().set_mesh_quad(
299 mesh_name,
300 first_vertex_id,
301 second_vertex_id,
302 third_vertex_id,
303 fourth_vertex_id,
304 )
305 }
306 pub fn set_mesh_quads(&mut self, mesh_name: &str, vertices: &[VertexID]) -> Result<(), Error> {
307 self.internal.pin_mut().set_mesh_quads(mesh_name, vertices)
308 }
309 pub fn set_mesh_tetrahedron(
310 &mut self,
311 mesh_name: &str,
312 first_vertex_id: VertexID,
313 second_vertex_id: VertexID,
314 third_vertex_id: VertexID,
315 fourth_vertex_id: VertexID,
316 ) -> Result<(), Error> {
317 self.internal.pin_mut().set_mesh_tetrahedron(
318 mesh_name,
319 first_vertex_id,
320 second_vertex_id,
321 third_vertex_id,
322 fourth_vertex_id,
323 )
324 }
325 pub fn set_mesh_tetrahedra(
326 &mut self,
327 mesh_name: &str,
328 vertices: &[VertexID],
329 ) -> Result<(), Error> {
330 self.internal
331 .pin_mut()
332 .set_mesh_tetrahedra(mesh_name, vertices)
333 }
334
335 pub fn requires_initial_data(&mut self) -> Result<bool, Error> {
338 self.internal.pin_mut().requires_initial_data()
339 }
340
341 pub fn write_data(
342 &mut self,
343 mesh_name: &str,
344 data_name: &str,
345 vertices: &[VertexID],
346 values: &[f64],
347 ) -> Result<(), Error> {
348 self.internal
349 .pin_mut()
350 .write_data(mesh_name, data_name, vertices, values)
351 }
352 pub fn read_data(
353 &self,
354 mesh_name: &str,
355 data_name: &str,
356 vertices: &[VertexID],
357 relative_read_dt: f64,
358 values: &mut [f64],
359 ) -> Result<(), Error> {
360 self.internal
361 .read_data(mesh_name, data_name, vertices, relative_read_dt, values)
362 }
363
364 pub fn start_profiling_section(&mut self, section_name: &str) -> Result<(), Error> {
367 self.internal
368 .pin_mut()
369 .start_profiling_section(section_name)
370 }
371
372 pub fn stop_last_profiling_section(&mut self) -> Result<(), Error> {
373 self.internal.pin_mut().stop_last_profiling_section()
374 }
375
376 pub fn set_mesh_access_region(
379 &mut self,
380 mesh_name: &str,
381 bounding_box: &[f64],
382 ) -> Result<(), Error> {
383 self.internal
384 .pin_mut()
385 .set_mesh_access_region(mesh_name, bounding_box)
386 }
387 pub fn get_mesh_vertex_ids_and_coordinates(
388 &self,
389 mesh_name: &str,
390 ids: &mut [VertexID],
391 coordinates: &mut [f64],
392 ) -> Result<(), Error> {
393 self.internal
394 .get_mesh_vertex_ids_and_coordinates(mesh_name, ids, coordinates)
395 }
396
397 pub fn requires_gradient_data_for(
400 &self,
401 mesh_name: &str,
402 data_name: &str,
403 ) -> Result<bool, Error> {
404 self.internal
405 .requires_gradient_data_for(mesh_name, data_name)
406 }
407 pub fn write_gradient_data(
408 &mut self,
409 mesh_name: &str,
410 data_name: &str,
411 vertices: &[VertexID],
412 gradients: &[f64],
413 ) -> Result<(), Error> {
414 self.internal
415 .pin_mut()
416 .write_gradient_data(mesh_name, data_name, vertices, gradients)
417 }
418
419 pub fn write_and_map_data(
422 &mut self,
423 mesh_name: &str,
424 data_name: &str,
425 coordinates: &[f64],
426 values: &[f64],
427 ) -> Result<(), Error> {
428 self.internal
429 .pin_mut()
430 .write_and_map_data(mesh_name, data_name, coordinates, values)
431 }
432
433 pub fn map_and_read_data(
434 &self,
435 mesh_name: &str,
436 data_name: &str,
437 coordinates: &[f64],
438 relative_read_dt: f64,
439 values: &mut [f64],
440 ) -> Result<(), Error> {
441 self.internal
442 .map_and_read_data(mesh_name, data_name, coordinates, relative_read_dt, values)
443 }
444}