-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi_c.jl
More file actions
618 lines (421 loc) · 19.3 KB
/
api_c.jl
File metadata and controls
618 lines (421 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
############################################################################################
# Version information #
############################################################################################
"""
trixi_version_library_major()::Cint
Return major version number of libtrixi.
This function is thread-safe. It must be run after `trixi_initialize` has been called.
"""
function trixi_version_library_major end
Base.@ccallable function trixi_version_library_major()::Cint
return VersionNumber(_version_libtrixi).major
end
trixi_version_library_major_cfptr() = @cfunction(trixi_version_library_major, Cint, ())
"""
trixi_version_library_minor()::Cint
Return minor version number of libtrixi.
This function is thread-safe. It must be run after `trixi_initialize` has been called.
"""
function trixi_version_library_minor end
Base.@ccallable function trixi_version_library_minor()::Cint
return VersionNumber(_version_libtrixi).minor
end
trixi_version_library_minor_cfptr() = @cfunction(trixi_version_library_minor, Cint, ())
"""
trixi_version_library_patch()::Cint
Return patch version number of libtrixi.
This function is thread-safe. It must be run after `trixi_initialize` has been called.
"""
function trixi_version_library_patch end
Base.@ccallable function trixi_version_library_patch()::Cint
return VersionNumber(_version_libtrixi).patch
end
trixi_version_library_patch_cfptr() = @cfunction(trixi_version_library_patch, Cint, ())
"""
trixi_version_library()::Cstring
Return full version string of libtrixi.
The return value is a read-only pointer to a NULL-terminated string with the version
information. This may include not just MAJOR.MINOR.PATCH but possibly also additional
build or development version information.
The returned pointer is to static memory and must not be used to change the contents of
the version string. Multiple calls to the function will return the same address.
This function is thread-safe. It must be run after `trixi_initialize` has been called.
"""
function trixi_version_library end
Base.@ccallable function trixi_version_library()::Cstring
return pointer(_version_libtrixi)
end
trixi_version_library_cfptr() = @cfunction(trixi_version_library, Cstring, ())
"""
trixi_version_julia()::Cstring
Return name and version of loaded Julia packages LibTrixi directly depends on.
The return value is a read-only pointer to a NULL-terminated string with the name and
version information of the loaded Julia packages, separated by newlines.
The returned pointer is to static memory and must not be used to change the contents of
the version string. Multiple calls to the function will return the same address.
This function is thread-safe. It must be run after `trixi_initialize` has been called.
"""
function trixi_version_julia end
Base.@ccallable function trixi_version_julia()::Cstring
return pointer(_version_info)
end
trixi_version_julia_cfptr() = @cfunction(trixi_version_julia, Cstring, ())
"""
trixi_version_julia_extended()::Cstring
Return name and version of all loaded Julia packages.
The return value is a read-only pointer to a NULL-terminated string with the name and
version information of all loaded Julia packages, including implicit dependencies,
separated by newlines.
The returned pointer is to static memory and must not be used to change the contents of
the version string. Multiple calls to the function will return the same address.
This function is thread-safe. It must be run after `trixi_initialize` has been called.
"""
function trixi_version_julia_extended end
Base.@ccallable function trixi_version_julia_extended()::Cstring
return pointer(_version_info_extended)
end
trixi_version_julia_extended_cfptr() = @cfunction(trixi_version_julia_extended, Cstring, ())
############################################################################################
# Simulation control #
############################################################################################
"""
trixi_initialize_simulation(libelixir::Cstring)::Cint
trixi_initialize_simulation(libelixir::AbstractString)::Cint
Initialize a new simulation based on the file `libelixir`and return a handle to the
corresponding [`SimulationState`](@ref) as a `Cint` (i.e, a plain C `int`).
The libelixir has a similar purpose as a regular "elixir" in Trixi.jl, as it completely
defines a simulation setup in Julia code. A key difference (and thus the name libelixir) is
that instead of running a simulation directly, it should define an argument-less function
named `init_simstate()` that returns a [`SimulationState`](@ref) with the complete
simulation setup. `trixi_initialize_simulation` will store the `SimulationState` object
internally and allow one to use it in subsequent calls to libtrixi via the handle returned
from this function.
For convenience, when using LibTrixi.jl directly from Julia, one can also pass a regular
`String` in the `libelixir` argument.
!!! note "Libelixir hygiene and `init_simstate`"
The libelixir file will be evaluated in the `Main` module. Thus any previously defined
function `init_simstate` will be overwritten, and any variables defined outside the
function will live throughout the lifetime of the Julia process.
!!! warning "Thread safety"
**This function is not thread safe.** Since the libelixir file will be evaluated in the
`Main` module, calling `trixi_initialize_simulation` simultaneously from different
threads can lead to undefined behavior.
"""
function trixi_initialize_simulation end
Base.@ccallable function trixi_initialize_simulation(libelixir::Cstring)::Cint
# Create string from Cstring
filename = unsafe_string(libelixir)
# Create new simulation state and store in global dict
simstate = trixi_initialize_simulation_jl(filename)
simstate_handle = store_simstate(simstate)
# Return handle for usage/storage on C side
return simstate_handle
end
trixi_initialize_simulation_cfptr() =
@cfunction(trixi_initialize_simulation, Cint, (Cstring,))
# Convenience function when using this directly from Julia
function trixi_initialize_simulation(libelixir::String)
# Convert string to byte array
bytes = Vector{UInt8}(libelixir)
# Make it a proper, NULL-terminated C-style char array
push!(bytes, '\0')
# Call `trixi_initialize_simulation` above with a raw pointer to the bytes array,
# which needs to be protected from garbage collection
GC.@preserve bytes begin
simstate_handle = trixi_initialize_simulation(Cstring(pointer(bytes)))
end
return simstate_handle
end
"""
trixi_is_finished(simstate_handle::Cint)::Cint
Return `0` if the simulation time has not yet reached the final time, and `1` otherwise.
"""
function trixi_is_finished end
Base.@ccallable function trixi_is_finished(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
is_finished = trixi_is_finished_jl(simstate)
return is_finished ? 1 : 0
end
trixi_is_finished_cfptr() = @cfunction(trixi_is_finished, Cint, (Cint,))
"""
trixi_step(simstate_handle::Cint)::Cvoid
Advance the simulation in time by one step.
"""
function trixi_step end
Base.@ccallable function trixi_step(simstate_handle::Cint)::Cvoid
simstate = load_simstate(simstate_handle)
trixi_step_jl(simstate)
return nothing
end
trixi_step_cfptr() = @cfunction(trixi_step, Cvoid, (Cint,))
"""
trixi_finalize_simulation(simstate_handle::Cint)::Cvoid
Finalize a simulation and attempt to free the underlying memory.
"""
function trixi_finalize_simulation end
Base.@ccallable function trixi_finalize_simulation(simstate_handle::Cint)::Cvoid
# Load simulation state and call finalizer
simstate = load_simstate(simstate_handle)
trixi_finalize_simulation_jl(simstate)
# Remove all references to simulation state and call garbage collection
simstate = nothing
delete_simstate!(simstate_handle)
GC.gc()
return nothing
end
trixi_finalize_simulation_cfptr() = @cfunction(trixi_finalize_simulation, Cvoid, (Cint,))
############################################################################################
# Simulation data #
############################################################################################
"""
trixi_calculate_dt(simstate_handle::Cint)::Cdouble
Compute, store, and return the time step size for the next time integration step.
"""
function trixi_calculate_dt end
Base.@ccallable function trixi_calculate_dt(simstate_handle::Cint)::Cdouble
simstate = load_simstate(simstate_handle)
dt = trixi_calculate_dt_jl(simstate)
return dt
end
trixi_calculate_dt_cfptr() = @cfunction(trixi_calculate_dt, Cdouble, (Cint,))
"""
trixi_ndims(simstate_handle::Cint)::Cint
Return number of spatial dimensions.
"""
function trixi_ndims end
Base.@ccallable function trixi_ndims(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_ndims_jl(simstate)
end
trixi_ndims_cfptr() = @cfunction(trixi_ndims, Cint, (Cint,))
"""
trixi_nelements(simstate_handle::Cint)::Cint
Return number of elements local to the MPI rank.
"""
function trixi_nelements end
Base.@ccallable function trixi_nelements(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_nelements_jl(simstate)
end
trixi_nelements_cfptr() = @cfunction(trixi_nelements, Cint, (Cint,))
"""
trixi_nelementsglobal(simstate_handle::Cint)::Cint
Return global number of elements on all MPI ranks.
"""
function trixi_nelementsglobal end
Base.@ccallable function trixi_nelementsglobal(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_nelementsglobal_jl(simstate)
end
trixi_nelementsglobal_cfptr() = @cfunction(trixi_nelementsglobal, Cint, (Cint,))
"""
trixi_ndofs(simstate_handle::Cint)::Cint
Return number of degrees of freedom (all quadrature points on all elements of current rank).
"""
function trixi_ndofs end
Base.@ccallable function trixi_ndofs(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_ndofs_jl(simstate)
end
trixi_ndofs_cfptr() = @cfunction(trixi_ndofs, Cint, (Cint,))
"""
trixi_ndofsglobal(simstate_handle::Cint)::Cint
Return global number of degrees of freedom (all quadrature points on all elements on all ranks).
"""
function trixi_ndofsglobal end
Base.@ccallable function trixi_ndofsglobal(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_ndofsglobal_jl(simstate)
end
trixi_ndofsglobal_cfptr() = @cfunction(trixi_ndofsglobal, Cint, (Cint,))
"""
trixi_ndofselement(simstate_handle::Cint)::Cint
Return number of degrees of freedom per element.
"""
function trixi_ndofselement end
Base.@ccallable function trixi_ndofselement(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_ndofselement_jl(simstate)
end
trixi_ndofselement_cfptr() = @cfunction(trixi_ndofselement, Cint, (Cint,))
"""
trixi_nvariables(simstate_handle::Cint)::Cint
Return number of variables.
"""
function trixi_nvariables end
Base.@ccallable function trixi_nvariables(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_nvariables_jl(simstate)
end
trixi_nvariables_cfptr() = @cfunction(trixi_nvariables, Cint, (Cint,))
"""
trixi_nnodes(simstate_handle::Cint)::Cint
Return number of quadrature nodes per dimension.
"""
function trixi_nnodes end
Base.@ccallable function trixi_nnodes(simstate_handle::Cint)::Cint
simstate = load_simstate(simstate_handle)
return trixi_nnodes_jl(simstate)
end
trixi_nnodes_cfptr() = @cfunction(trixi_nnodes, Cint, (Cint,))
"""
trixi_load_node_reference_coordinates(simstate_handle::Cint, data::Ptr{Cdouble})::Cvoid
Get reference coordinates of 1D quadrature nodes.
"""
function trixi_load_node_reference_coordinates end
Base.@ccallable function trixi_load_node_reference_coordinates(simstate_handle::Cint,
data::Ptr{Cdouble})::Cvoid
simstate = load_simstate(simstate_handle)
# convert C to Julia array
size = trixi_nnodes_jl(simstate)
data_jl = unsafe_wrap(Array, data, size)
trixi_load_node_reference_coordinates_jl(simstate, data_jl)
return nothing
end
trixi_load_node_reference_coordinates_cfptr() =
@cfunction(trixi_load_node_reference_coordinates, Cvoid, (Cint, Ptr{Cdouble}))
"""
trixi_load_node_weights(simstate_handle::Cint, data::Ptr{Cdouble})::Cvoid
Get weights of 1D quadrature nodes.
"""
function trixi_load_node_weights end
Base.@ccallable function trixi_load_node_weights(simstate_handle::Cint,
data::Ptr{Cdouble})::Cvoid
simstate = load_simstate(simstate_handle)
# convert C to Julia array
size = trixi_nnodes_jl(simstate)
data_jl = unsafe_wrap(Array, data, size)
return trixi_load_node_weights_jl(simstate, data_jl)
end
trixi_load_node_weights_cfptr() =
@cfunction(trixi_load_node_weights, Cvoid, (Cint, Ptr{Cdouble}))
"""
trixi_load_primitive_vars(simstate_handle::Cint, variable_id::Cint,
data::Ptr{Cdouble})::Cvoid
Load primitive variable.
The values for the primitive variable at position `variable_id` at every degree of freedom
are stored in the given array `data`.
The given array has to be of correct size (ndofs) and memory has to be allocated beforehand.
"""
function trixi_load_primitive_vars end
Base.@ccallable function trixi_load_primitive_vars(simstate_handle::Cint, variable_id::Cint,
data::Ptr{Cdouble})::Cvoid
simstate = load_simstate(simstate_handle)
# convert C to Julia array
size = trixi_ndofs_jl(simstate)
data_jl = unsafe_wrap(Array, data, size)
trixi_load_primitive_vars_jl(simstate, variable_id, data_jl)
return nothing
end
trixi_load_primitive_vars_cfptr() =
@cfunction(trixi_load_primitive_vars, Cvoid, (Cint, Cint, Ptr{Cdouble}))
"""
trixi_register_data(data::Ptr{Cdouble}, size::Cint, index::Cint,
simstate_handle::Cint)::Cvoid
Store data vector in current simulation's registry.
A reference to the passed data array `data` will be stored in the registry of the simulation
given by `simstate_handle` at given `index`. The registry object has to be created in
`init_simstate()` of the running libelixir and can be used throughout the simulation.
The registry object has to exist, has to be of type `LibTrixiDataRegistry`, and has to hold
enough data references such that access at `index` is valid.
Memory storage remains on the user side. It must not be deallocated as long as it might be
accessed via the registry. The size of `data` has to match `size`.
"""
function trixi_register_data end
Base.@ccallable function trixi_register_data(simstate_handle::Cint, index::Cint,
size::Cint, data::Ptr{Cdouble})::Cvoid
simstate = load_simstate(simstate_handle)
# convert C to Julia array
data_jl = unsafe_wrap(Array, data, size)
trixi_register_data_jl(simstate, index, data_jl)
return nothing
end
trixi_register_data_cfptr() =
@cfunction(trixi_register_data, Cvoid, (Cint, Cint, Cint, Ptr{Cdouble},))
"""
trixi_get_simulation_time(simstate_handle::Cint)::Cdouble
Return current physical time.
"""
function trixi_get_simulation_time end
Base.@ccallable function trixi_get_simulation_time(simstate_handle::Cint)::Cdouble
simstate = load_simstate(simstate_handle)
return trixi_get_simulation_time_jl(simstate)
end
trixi_get_simulation_time_cfptr() = @cfunction(trixi_get_simulation_time, Cdouble, (Cint,))
"""
trixi_load_element_averaged_primitive_vars(simstate_handle::Cint, variable_id::Cint,
data::Ptr{Cdouble})::Cvoid
Load element averages for primitive variable.
Element averaged values for the primitive variable at position `variable_id` for each
element are stored in the given array `data`.
The given array has to be of correct size (nelements) and memory has to be allocated
beforehand.
"""
function trixi_load_element_averaged_primitive_vars end
Base.@ccallable function trixi_load_element_averaged_primitive_vars(simstate_handle::Cint,
variable_id::Cint, data::Ptr{Cdouble})::Cvoid
simstate = load_simstate(simstate_handle)
# convert C to Julia array
size = trixi_nelements_jl(simstate)
data_jl = unsafe_wrap(Array, data, size)
trixi_load_element_averaged_primitive_vars_jl(simstate, variable_id, data_jl)
return nothing
end
trixi_load_element_averaged_primitive_vars_cfptr() =
@cfunction(trixi_load_element_averaged_primitive_vars, Cvoid, (Cint, Cint, Ptr{Cdouble}))
"""
trixi_get_t8code_forest(simstate_handle::Cint)::Ptr{Trixi.t8_forest}
Return t8code forest of the current T8codeMesh.
!!! warning "Experimental"
The interface to t8code is experimental and implementation details may change at any
time without warning.
"""
function trixi_get_t8code_forest end
Base.@ccallable function trixi_get_t8code_forest(simstate_handle::Cint)::Ptr{Trixi.t8_forest}
simstate = load_simstate(simstate_handle)
return trixi_get_t8code_forest_jl(simstate)
end
trixi_get_t8code_forest_cfptr() =
@cfunction(trixi_get_t8code_forest, Ptr{Trixi.t8_forest}, (Cint,))
"""
trixi_get_p4est_mesh(simstate_handle::Cint)::Ptr{Trixi.p4est_t}
Return pointer to p4est data structure of the current P4estMesh (2D).
!!! warning "Experimental"
The interface to p4est is experimental and implementation details may change at any
time without warning.
"""
function trixi_get_p4est_mesh end
Base.@ccallable function trixi_get_p4est_mesh(simstate_handle::Cint)::Ptr{Trixi.p4est_t}
simstate = load_simstate(simstate_handle)
return trixi_get_p4est_mesh_jl(simstate)
end
trixi_get_p4est_mesh_cfptr() = @cfunction(trixi_get_p4est_mesh, Ptr{Trixi.p4est_t}, (Cint,))
"""
trixi_get_p8est_mesh(simstate_handle::Cint)::Ptr{Trixi.p8est_t}
Return pointer to the p8est data structure of the current P4estMesh (3D).
Note: In Trixi.jl, the mesh type is `P4estMesh` no matter whether it is a 2D or 3D mesh.
!!! warning "Experimental"
The interface to p4est is experimental and implementation details may change at any
time without warning.
"""
function trixi_get_p8est_mesh end
Base.@ccallable function trixi_get_p8est_mesh(simstate_handle::Cint)::Ptr{Trixi.p8est_t}
simstate = load_simstate(simstate_handle)
return trixi_get_p8est_mesh_jl(simstate)
end
trixi_get_p8est_mesh_cfptr() = @cfunction(trixi_get_p8est_mesh, Ptr{Trixi.p8est_t}, (Cint,))
############################################################################################
# Auxiliary
############################################################################################
"""
trixi_eval_julia(code::Cstring)::Cvoid
Execute the provided code in the current Julia runtime environment.
!!! warning "Only for development"
Code is not checked prior to execution.
"""
function trixi_eval_julia end
Base.@ccallable function trixi_eval_julia(code::Cstring)::Cvoid
trixi_eval_julia_jl(unsafe_string(code))
return # Return nothing to not pass arbitrary Julia objects to C
end
trixi_eval_julia_cfptr() = @cfunction(trixi_eval_julia, Cvoid, (Cstring,))