readcon-core 0.14.0

An oxidized single and multiple CON file reader and writer with FFI bindings for ergonomic C/C++ usage.
Documentation
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
! ReadCon — production Fortran bindings (ISO_C_BINDING) over include/readcon-core.h
! fpm package: fortran/ReadCon  |  Tests: fpm test  |  Link libreadcon_core
module readcon
  use, intrinsic :: iso_c_binding
  use, intrinsic :: iso_fortran_env, only: real64, int64, int32
  implicit none
  private

  public :: rkr_status_success, rkr_status_null_pointer, rkr_status_selection_error
  public :: rkr_status_invalid_utf8, rkr_status_invalid_json, rkr_status_io_error
  public :: rkr_status_index_out_of_bounds, rkr_status_buffer_too_small
  public :: rkr_status_internal_error, rkr_status_section_absent, rkr_status_validation_error
  public :: rkr_status_feature_disabled
  public :: catom_t, cframe_t
  public :: dl_tensor_t, dl_managed_tensor_versioned_t, dl_device_t, dl_data_type_t
  public :: dlpack_inspect, dlpack_data_ptr
  public :: frame_metatensor_positions_block, frame_metatensor_velocities_block
  public :: frame_metatensor_forces_block, frame_metatensor_atom_energies_block
  public :: mts_block_free_rkr
  public :: library_version, con_spec_version, has_chemfiles_support, status_message
  public :: symbol_to_z, z_to_symbol
  public :: frame_t, iterator_t, builder_t, writer_t
  public :: read_first_frame, read_all_frames, open_iterator, new_builder, open_writer
  public :: open_writer_gzip, open_writer_zstd
  public :: open_writer_gzip_with_precision, open_writer_zstd_with_precision
  public :: read_chemfiles_first

  ! Mirror include/readcon-core.h RKRStatus (keep in sync with src/ffi.rs)
  integer(c_int), parameter :: rkr_status_success = 0
  integer(c_int), parameter :: rkr_status_null_pointer = -1
  integer(c_int), parameter :: rkr_status_invalid_utf8 = -2
  integer(c_int), parameter :: rkr_status_invalid_json = -3
  integer(c_int), parameter :: rkr_status_io_error = -4
  integer(c_int), parameter :: rkr_status_index_out_of_bounds = -5
  integer(c_int), parameter :: rkr_status_buffer_too_small = -6
  integer(c_int), parameter :: rkr_status_internal_error = -7
  integer(c_int), parameter :: rkr_status_section_absent = -8
  integer(c_int), parameter :: rkr_status_validation_error = -9
  integer(c_int), parameter :: rkr_status_selection_error = -10
  integer(c_int), parameter :: rkr_status_feature_disabled = -11

  type, bind(C), public :: catom_t
    integer(c_int64_t) :: atomic_number = 0_c_int64_t
    real(c_double) :: x = 0.0_c_double, y = 0.0_c_double, z = 0.0_c_double
    integer(c_int64_t) :: atom_id = 0_c_int64_t
    real(c_double) :: mass = 0.0_c_double
    logical(c_bool) :: is_fixed = .false._c_bool
    logical(c_bool) :: fixed_x = .false._c_bool, fixed_y = .false._c_bool, fixed_z = .false._c_bool
    real(c_double) :: vx = 0.0_c_double, vy = 0.0_c_double, vz = 0.0_c_double
    logical(c_bool) :: has_velocity = .false._c_bool
    real(c_double) :: fx = 0.0_c_double, fy = 0.0_c_double, fz = 0.0_c_double
    logical(c_bool) :: has_forces = .false._c_bool
    real(c_double) :: energy = 0.0_c_double
    logical(c_bool) :: has_energy = .false._c_bool
  end type


  ! DLPack C layout (dlpack.h DLManagedTensorVersioned / DLTensor) — field access
  type, bind(C), public :: dl_device_t
    integer(c_int32_t) :: device_type = 1_c_int32_t  ! kDLCPU
    integer(c_int32_t) :: device_id = 0_c_int32_t
  end type

  type, bind(C), public :: dl_data_type_t
    integer(c_int8_t) :: code = 0_c_int8_t
    integer(c_int8_t) :: bits = 0_c_int8_t
    integer(c_int16_t) :: lanes = 0_c_int16_t
  end type

  type, bind(C), public :: dl_tensor_t
    type(c_ptr) :: data = c_null_ptr
    type(dl_device_t) :: device
    integer(c_int32_t) :: ndim = 0_c_int32_t
    type(dl_data_type_t) :: dtype
    type(c_ptr) :: shape = c_null_ptr   ! int64_t*
    type(c_ptr) :: strides = c_null_ptr
    integer(c_int64_t) :: byte_offset = 0_c_int64_t
  end type

  type, bind(C), public :: dl_pack_version_t
    integer(c_int32_t) :: major = 0_c_int32_t
    integer(c_int32_t) :: minor = 0_c_int32_t
  end type

  type, bind(C), public :: dl_managed_tensor_versioned_t
    type(dl_pack_version_t) :: version
    type(c_ptr) :: manager_ctx = c_null_ptr
    type(c_funptr) :: deleter = c_null_funptr
    integer(c_int64_t) :: flags = 0_c_int64_t
    type(dl_tensor_t) :: dl_tensor
  end type

  type, bind(C), public :: cframe_t
    type(c_ptr) :: atoms = c_null_ptr
    integer(c_size_t) :: num_atoms = 0_c_size_t
    real(c_double) :: cell(3) = 0.0_c_double
    real(c_double) :: angles(3) = 0.0_c_double
    logical(c_bool) :: has_velocities = .false._c_bool
    logical(c_bool) :: has_forces = .false._c_bool
    logical(c_bool) :: has_energies = .false._c_bool
  end type

  type :: frame_t
    private
    type(c_ptr) :: handle = c_null_ptr
    type(c_ptr) :: cview = c_null_ptr
  contains
    procedure :: valid => fr_valid
    procedure :: free => fr_free
    procedure :: natoms => fr_natoms
    procedure :: atom => fr_atom
    procedure :: cell_lengths => fr_cell
    procedure :: cell_angles => fr_angles
    procedure :: has_velocities => fr_has_vel
    procedure :: has_forces => fr_has_frc
    procedure :: metadata_json => fr_meta
    procedure :: energy => fr_energy
    procedure :: index_energy => fr_index_energy
    procedure :: composition_formula => fr_composition_formula
    procedure :: total_mass => fr_total_mass
    procedure :: cell_volume => fr_cell_volume
    procedure :: fmax => fr_fmax
    procedure :: sections_mask => fr_sections_mask
    procedure :: index_natoms => fr_index_natoms
    procedure :: index_projection_json => fr_index_projection_json
    procedure :: atom_count => fr_atom_count
    procedure :: copy_positions => fr_copy_positions
    procedure :: copy_velocities => fr_copy_velocities
    procedure :: copy_forces => fr_copy_forces
    procedure :: copy_masses => fr_copy_masses
    procedure :: potential_type => fr_pot
    procedure :: frame_index => fr_fidx
    procedure :: sim_time => fr_time
    procedure :: timestep => fr_dt
    procedure :: neb_bead => fr_neb_bead
    procedure :: neb_band => fr_neb_band
    procedure :: bond_count => fr_nbonds
    procedure :: bond_at => fr_bond_at
    procedure :: atom_index_by_id => fr_id_index
    procedure :: spec_version => fr_spec
    procedure :: select => fr_select
    procedure :: select_primary => fr_select_primary
    procedure :: write_path => fr_write_path
    procedure :: handle_ptr => fr_handle
  end type

  type :: iterator_t
    private
    type(c_ptr) :: it = c_null_ptr
  contains
    procedure :: valid => it_valid
    procedure :: next => it_next
    procedure :: free => it_free
  end type

  type :: builder_t
    private
    type(c_ptr) :: b = c_null_ptr
  contains
    procedure :: valid => bd_valid
    procedure :: free => bd_free
    procedure :: add_atom => bd_add_atom
    procedure :: set_energy => bd_set_energy
    procedure :: set_metadata_json => bd_set_meta
    procedure :: set_frame_index => bd_set_fidx
    procedure :: build => bd_build
    procedure :: copy_positions => bd_copy_positions
    procedure :: copy_masses => bd_copy_masses
    procedure :: positions_dlpack => bd_positions_dlpack
    procedure :: velocities_dlpack => bd_velocities_dlpack
    procedure :: forces_dlpack => bd_forces_dlpack
    procedure :: atom_energies_dlpack => bd_atom_energies_dlpack
    procedure :: masses_dlpack => bd_masses_dlpack
    procedure :: atom_ids_dlpack => bd_atom_ids_dlpack
    procedure :: dlpack_delete => bd_dlpack_delete
  end type

  type :: writer_t
    private
    type(c_ptr) :: w = c_null_ptr
  contains
    procedure :: valid => wr_valid
    procedure :: free => wr_free
    procedure :: extend_one => wr_extend_one
    procedure :: set_canonical => wr_set_canonical
    procedure :: is_canonical => wr_is_canonical
  end type

  interface
    function c_rkr_con_spec_version() bind(C, name="rkr_con_spec_version")
      import :: c_int32_t
      integer(c_int32_t) :: c_rkr_con_spec_version
    end function
    function c_rkr_library_version() bind(C, name="rkr_library_version")
      import :: c_ptr
      type(c_ptr) :: c_rkr_library_version
    end function
    function c_rkr_has_chemfiles_support() bind(C, name="rkr_has_chemfiles_support")
      import :: c_int8_t
      integer(c_int8_t) :: c_rkr_has_chemfiles_support
    end function
    function c_rkr_status_message(st) bind(C, name="rkr_status_message")
      import :: c_int, c_ptr
      integer(c_int), value :: st
      type(c_ptr) :: c_rkr_status_message
    end function
    function c_rkr_read_first_frame(fn) bind(C, name="rkr_read_first_frame")
      import :: c_char, c_ptr
      character(kind=c_char), intent(in) :: fn(*)
      type(c_ptr) :: c_rkr_read_first_frame
    end function
    subroutine c_free_rkr_frame(f) bind(C, name="free_rkr_frame")
      import :: c_ptr
      type(c_ptr), value :: f
    end subroutine
    function c_rkr_frame_to_c_frame(f) bind(C, name="rkr_frame_to_c_frame")
      import :: c_ptr
      type(c_ptr), value :: f
      type(c_ptr) :: c_rkr_frame_to_c_frame
    end function
    subroutine c_free_c_frame(f) bind(C, name="free_c_frame")
      import :: c_ptr
      type(c_ptr), value :: f
    end subroutine
    function c_rkr_frame_metadata_json(f) bind(C, name="rkr_frame_metadata_json")
      import :: c_ptr
      type(c_ptr), value :: f
      type(c_ptr) :: c_rkr_frame_metadata_json
    end function

    function c_rkr_frame_atom_count(f) bind(C, name="rkr_frame_atom_count")
      import :: c_ptr, c_size_t
      type(c_ptr), value :: f
      integer(c_size_t) :: c_rkr_frame_atom_count
    end function
    function c_rkr_frame_copy_positions(f, out, n) bind(C, name="rkr_frame_copy_positions")
      import :: c_ptr, c_int, c_double, c_size_t
      type(c_ptr), value :: f
      real(c_double), intent(out) :: out(*)
      integer(c_size_t), value :: n
      integer(c_int) :: c_rkr_frame_copy_positions
    end function
    function c_rkr_frame_copy_velocities(f, out, n) bind(C, name="rkr_frame_copy_velocities")
      import :: c_ptr, c_int, c_double, c_size_t
      type(c_ptr), value :: f
      real(c_double), intent(out) :: out(*)
      integer(c_size_t), value :: n
      integer(c_int) :: c_rkr_frame_copy_velocities
    end function
    function c_rkr_frame_copy_forces(f, out, n) bind(C, name="rkr_frame_copy_forces")
      import :: c_ptr, c_int, c_double, c_size_t
      type(c_ptr), value :: f
      real(c_double), intent(out) :: out(*)
      integer(c_size_t), value :: n
      integer(c_int) :: c_rkr_frame_copy_forces
    end function
    function c_rkr_frame_copy_masses(f, out, n) bind(C, name="rkr_frame_copy_masses")
      import :: c_ptr, c_int, c_double, c_size_t
      type(c_ptr), value :: f
      real(c_double), intent(out) :: out(*)
      integer(c_size_t), value :: n
      integer(c_int) :: c_rkr_frame_copy_masses
    end function
    subroutine c_free_rkr_frame_ptr_array(arr, n) bind(C, name="free_rkr_frame_ptr_array")
      import :: c_ptr, c_size_t
      type(c_ptr), value :: arr
      integer(c_size_t), value :: n
    end subroutine

    function c_rkr_read_all_frames(fn, nout) bind(C, name="rkr_read_all_frames")
      import :: c_char, c_ptr, c_size_t
      character(kind=c_char), intent(in) :: fn(*)
      integer(c_size_t), intent(out) :: nout
      type(c_ptr) :: c_rkr_read_all_frames
    end function
    subroutine c_free_rkr_frame_array(arr, n) bind(C, name="free_rkr_frame_array")
      import :: c_ptr, c_size_t
      type(c_ptr), value :: arr
      integer(c_size_t), value :: n
    end subroutine
    function c_rkr_frame_energy(f) bind(C, name="rkr_frame_energy")
      import :: c_ptr, c_double
      type(c_ptr), value :: f
      real(c_double) :: c_rkr_frame_energy
    end function
    function c_rkr_frame_index_energy(f) bind(C, name="rkr_frame_index_energy")
      import :: c_ptr, c_double
      type(c_ptr), value :: f
      real(c_double) :: c_rkr_frame_index_energy
    end function
    function c_rkr_frame_composition_formula(f) bind(C, name="rkr_frame_composition_formula")
      import :: c_ptr
      type(c_ptr), value :: f
      type(c_ptr) :: c_rkr_frame_composition_formula
    end function
    function c_rkr_frame_total_mass(f) bind(C, name="rkr_frame_total_mass")
      import :: c_ptr, c_double
      type(c_ptr), value :: f
      real(c_double) :: c_rkr_frame_total_mass
    end function
    function c_rkr_frame_cell_volume(f) bind(C, name="rkr_frame_cell_volume")
      import :: c_ptr, c_double
      type(c_ptr), value :: f
      real(c_double) :: c_rkr_frame_cell_volume
    end function
    function c_rkr_frame_fmax(f) bind(C, name="rkr_frame_fmax")
      import :: c_ptr, c_double
      type(c_ptr), value :: f
      real(c_double) :: c_rkr_frame_fmax
    end function
    function c_rkr_frame_sections_mask(f) bind(C, name="rkr_frame_sections_mask")
      import :: c_ptr, c_int8_t
      type(c_ptr), value :: f
      integer(c_int8_t) :: c_rkr_frame_sections_mask
    end function
    function c_rkr_frame_index_natoms(f) bind(C, name="rkr_frame_index_natoms")
      import :: c_ptr, c_int32_t
      type(c_ptr), value :: f
      integer(c_int32_t) :: c_rkr_frame_index_natoms
    end function
    function c_rkr_frame_index_projection_json(f) bind(C, name="rkr_frame_index_projection_json")
      import :: c_ptr
      type(c_ptr), value :: f
      type(c_ptr) :: c_rkr_frame_index_projection_json
    end function
    function c_rkr_frame_potential_type(f) bind(C, name="rkr_frame_potential_type")
      import :: c_ptr
      type(c_ptr), value :: f
      type(c_ptr) :: c_rkr_frame_potential_type
    end function
    function c_rkr_frame_frame_index(f) bind(C, name="rkr_frame_frame_index")
      import :: c_ptr, c_int64_t
      type(c_ptr), value :: f
      integer(c_int64_t) :: c_rkr_frame_frame_index
    end function
    function c_rkr_frame_time(f) bind(C, name="rkr_frame_time")
      import :: c_ptr, c_double
      type(c_ptr), value :: f
      real(c_double) :: c_rkr_frame_time
    end function
    function c_rkr_frame_timestep(f) bind(C, name="rkr_frame_timestep")
      import :: c_ptr, c_double
      type(c_ptr), value :: f
      real(c_double) :: c_rkr_frame_timestep
    end function
    function c_rkr_frame_neb_bead(f) bind(C, name="rkr_frame_neb_bead")
      import :: c_ptr, c_int64_t
      type(c_ptr), value :: f
      integer(c_int64_t) :: c_rkr_frame_neb_bead
    end function
    function c_rkr_frame_neb_band(f) bind(C, name="rkr_frame_neb_band")
      import :: c_ptr, c_int64_t
      type(c_ptr), value :: f
      integer(c_int64_t) :: c_rkr_frame_neb_band
    end function
    function c_rkr_frame_bond_count(f) bind(C, name="rkr_frame_bond_count")
      import :: c_ptr, c_int64_t
      type(c_ptr), value :: f
      integer(c_int64_t) :: c_rkr_frame_bond_count
    end function
    function c_rkr_frame_bond_at(f, idx, i, j, has_o, ord) bind(C, name="rkr_frame_bond_at")
      import :: c_ptr, c_int, c_int32_t, c_int64_t, c_int8_t
      type(c_ptr), value :: f
      integer(c_int64_t), value :: idx
      integer(c_int32_t), intent(out) :: i, j, ord
      integer(c_int8_t), intent(out) :: has_o
      integer(c_int) :: c_rkr_frame_bond_at
    end function
    function c_rkr_frame_atom_index_by_id(f, aid) bind(C, name="rkr_frame_atom_index_by_id")
      import :: c_ptr, c_int64_t
      type(c_ptr), value :: f
      integer(c_int64_t), value :: aid
      integer(c_int64_t) :: c_rkr_frame_atom_index_by_id
    end function
    function c_rkr_frame_spec_version(f) bind(C, name="rkr_frame_spec_version")
      import :: c_ptr, c_int32_t
      type(c_ptr), value :: f
      integer(c_int32_t) :: c_rkr_frame_spec_version
    end function
    function c_rkr_symbol_to_z(s) bind(C, name="rkr_symbol_to_z")
      import :: c_char, c_int64_t
      character(kind=c_char), intent(in) :: s(*)
      integer(c_int64_t) :: c_rkr_symbol_to_z
    end function
    function c_rkr_z_to_symbol(z) bind(C, name="rkr_z_to_symbol")
      import :: c_ptr, c_int64_t
      integer(c_int64_t), value :: z
      type(c_ptr) :: c_rkr_z_to_symbol
    end function
    function c_read_con_file_iterator(fn) bind(C, name="read_con_file_iterator")
      import :: c_char, c_ptr
      character(kind=c_char), intent(in) :: fn(*)
      type(c_ptr) :: c_read_con_file_iterator
    end function
    function c_con_frame_iterator_next(it) bind(C, name="con_frame_iterator_next")
      import :: c_ptr
      type(c_ptr), value :: it
      type(c_ptr) :: c_con_frame_iterator_next
    end function
    subroutine c_free_con_frame_iterator(it) bind(C, name="free_con_frame_iterator")
      import :: c_ptr
      type(c_ptr), value :: it
    end subroutine
    function c_create_writer_from_path_c(fn) bind(C, name="create_writer_from_path_c")
      import :: c_char, c_ptr
      character(kind=c_char), intent(in) :: fn(*)
      type(c_ptr) :: c_create_writer_from_path_c
    end function
    function c_create_writer_gzip_c(fn) bind(C, name="create_writer_gzip_c")
      import :: c_char, c_ptr
      character(kind=c_char), intent(in) :: fn(*)
      type(c_ptr) :: c_create_writer_gzip_c
    end function
    function c_create_writer_gzip_with_precision_c(fn, prec) bind(C, name="create_writer_gzip_with_precision_c")
      import :: c_char, c_ptr, c_int8_t
      character(kind=c_char), intent(in) :: fn(*)
      integer(c_int8_t), value :: prec
      type(c_ptr) :: c_create_writer_gzip_with_precision_c
    end function
    function c_create_writer_zstd_with_precision_c(fn, prec) bind(C, name="create_writer_zstd_with_precision_c")
      import :: c_char, c_ptr, c_int8_t
      character(kind=c_char), intent(in) :: fn(*)
      integer(c_int8_t), value :: prec
      type(c_ptr) :: c_create_writer_zstd_with_precision_c
    end function
    function c_create_writer_zstd_c(fn) bind(C, name="create_writer_zstd_c")
      import :: c_char, c_ptr
      character(kind=c_char), intent(in) :: fn(*)
      type(c_ptr) :: c_create_writer_zstd_c
    end function
    subroutine c_free_rkr_writer(w) bind(C, name="free_rkr_writer")
      import :: c_ptr
      type(c_ptr), value :: w
    end subroutine
    function c_rkr_writer_extend(w, frames, n) bind(C, name="rkr_writer_extend")
      import :: c_ptr, c_int, c_size_t
      type(c_ptr), value :: w
      type(c_ptr), intent(in) :: frames(*)
      integer(c_size_t), value :: n
      integer(c_int) :: c_rkr_writer_extend
    end function
    function c_rkr_writer_set_canonical(w, canonical) bind(C, name="rkr_writer_set_canonical")
      import :: c_ptr, c_int8_t, c_int
      type(c_ptr), value :: w
      integer(c_int8_t), value :: canonical
      integer(c_int) :: c_rkr_writer_set_canonical
    end function
    function c_rkr_writer_is_canonical(w) bind(C, name="rkr_writer_is_canonical")
      import :: c_ptr, c_int8_t
      type(c_ptr), value :: w
      integer(c_int8_t) :: c_rkr_writer_is_canonical
    end function
    function c_rkr_frame_new(cell, angles, pb0, pb1, pob0, pob1) bind(C, name="rkr_frame_new")
      import :: c_ptr, c_double, c_char
      real(c_double), intent(in) :: cell(*)
      real(c_double), intent(in) :: angles(*)
      type(c_ptr), value :: pb0, pb1, pob0, pob1
      type(c_ptr) :: c_rkr_frame_new
    end function
    function c_rkr_frame_add_atom_with_fixed_mask(b, sym, x, y, z, aid, mass, fx, fy, fz) &
         bind(C, name="rkr_frame_add_atom_with_fixed_mask")
      import :: c_ptr, c_char, c_double, c_int64_t, c_bool, c_int
      type(c_ptr), value :: b
      character(kind=c_char), intent(in) :: sym(*)
      real(c_double), value :: x, y, z, mass
      integer(c_int64_t), value :: aid
      logical(c_bool), value :: fx, fy, fz
      integer(c_int) :: c_rkr_frame_add_atom_with_fixed_mask
    end function
    function c_rkr_frame_builder_set_energy(b, e) bind(C, name="rkr_frame_builder_set_energy")
      import :: c_ptr, c_double, c_int
      type(c_ptr), value :: b
      real(c_double), value :: e
      integer(c_int) :: c_rkr_frame_builder_set_energy
    end function
    function c_rkr_frame_builder_set_metadata_json(b, j) bind(C, name="rkr_frame_builder_set_metadata_json")
      import :: c_ptr, c_char, c_int
      type(c_ptr), value :: b
      character(kind=c_char), intent(in) :: j(*)
      integer(c_int) :: c_rkr_frame_builder_set_metadata_json
    end function
    function c_rkr_frame_builder_set_frame_index(b, i) bind(C, name="rkr_frame_builder_set_frame_index")
      import :: c_ptr, c_int64_t, c_int
      type(c_ptr), value :: b
      integer(c_int64_t), value :: i
      integer(c_int) :: c_rkr_frame_builder_set_frame_index
    end function
    function c_rkr_frame_builder_build(b) bind(C, name="rkr_frame_builder_build")
      import :: c_ptr
      type(c_ptr), value :: b
      type(c_ptr) :: c_rkr_frame_builder_build
    end function
    subroutine c_free_rkr_frame_builder(b) bind(C, name="free_rkr_frame_builder")
      import :: c_ptr
      type(c_ptr), value :: b
    end subroutine
    function c_rkr_frame_select(f, sel, out) bind(C, name="rkr_frame_select")
      import :: c_ptr, c_char, c_int
      type(c_ptr), value :: f
      character(kind=c_char), intent(in) :: sel(*)
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_select
    end function
    function c_rkr_selection_result_match_count(r) bind(C, name="rkr_selection_result_match_count")
      import :: c_ptr, c_int64_t
      type(c_ptr), value :: r
      integer(c_int64_t) :: c_rkr_selection_result_match_count
    end function
    subroutine c_rkr_selection_result_free(r) bind(C, name="rkr_selection_result_free")
      import :: c_ptr
      type(c_ptr), value :: r
    end subroutine

    function c_rkr_read_chemfiles_first(fn) bind(C, name="rkr_read_chemfiles_first")
      import :: c_char, c_ptr
      character(kind=c_char), intent(in) :: fn(*)
      type(c_ptr) :: c_rkr_read_chemfiles_first
    end function
    function c_rkr_frame_builder_positions_data(b) bind(C, name="rkr_frame_builder_positions_data")
      import :: c_ptr
      type(c_ptr), value :: b
      type(c_ptr) :: c_rkr_frame_builder_positions_data
    end function
    function c_rkr_frame_builder_masses_data(b) bind(C, name="rkr_frame_builder_masses_data")
      import :: c_ptr
      type(c_ptr), value :: b
      type(c_ptr) :: c_rkr_frame_builder_masses_data
    end function
    function c_rkr_frame_builder_atom_count(b) bind(C, name="rkr_frame_builder_atom_count")
      import :: c_ptr, c_size_t
      type(c_ptr), value :: b
      integer(c_size_t) :: c_rkr_frame_builder_atom_count
    end function
    function c_rkr_frame_builder_positions_dlpack(b, out) bind(C, name="rkr_frame_builder_positions_dlpack")
      import :: c_ptr, c_int
      type(c_ptr), value :: b
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_builder_positions_dlpack
    end function
    function c_rkr_frame_builder_masses_dlpack(b, out) bind(C, name="rkr_frame_builder_masses_dlpack")
      import :: c_ptr, c_int
      type(c_ptr), value :: b
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_builder_masses_dlpack
    end function

    function c_rkr_frame_builder_velocities_dlpack(b, out) bind(C, name="rkr_frame_builder_velocities_dlpack")
      import :: c_ptr, c_int
      type(c_ptr), value :: b
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_builder_velocities_dlpack
    end function
    function c_rkr_frame_builder_forces_dlpack(b, out) bind(C, name="rkr_frame_builder_forces_dlpack")
      import :: c_ptr, c_int
      type(c_ptr), value :: b
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_builder_forces_dlpack
    end function
    function c_rkr_frame_builder_atom_energies_dlpack(b, out) bind(C, name="rkr_frame_builder_atom_energies_dlpack")
      import :: c_ptr, c_int
      type(c_ptr), value :: b
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_builder_atom_energies_dlpack
    end function
    function c_rkr_frame_builder_atom_ids_dlpack(b, out) bind(C, name="rkr_frame_builder_atom_ids_dlpack")
      import :: c_ptr, c_int
      type(c_ptr), value :: b
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_builder_atom_ids_dlpack
    end function
    subroutine c_rkr_dlpack_delete(tensor) bind(C, name="rkr_dlpack_delete")
      import :: c_ptr
      type(c_ptr), value :: tensor
    end subroutine
    ! Metatensor cbindgen C API handles (mts_block_t*) — ownership via rkr_mts_block_free
    function c_rkr_frame_metatensor_positions_block(f, out) &
         bind(C, name="rkr_frame_metatensor_positions_block")
      import :: c_ptr, c_int
      type(c_ptr), value :: f
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_metatensor_positions_block
    end function
    function c_rkr_frame_metatensor_velocities_block(f, out) &
         bind(C, name="rkr_frame_metatensor_velocities_block")
      import :: c_ptr, c_int
      type(c_ptr), value :: f
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_metatensor_velocities_block
    end function
    function c_rkr_frame_metatensor_forces_block(f, out) &
         bind(C, name="rkr_frame_metatensor_forces_block")
      import :: c_ptr, c_int
      type(c_ptr), value :: f
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_metatensor_forces_block
    end function
    function c_rkr_frame_metatensor_atom_energies_block(f, out) &
         bind(C, name="rkr_frame_metatensor_atom_energies_block")
      import :: c_ptr, c_int
      type(c_ptr), value :: f
      type(c_ptr), intent(out) :: out
      integer(c_int) :: c_rkr_frame_metatensor_atom_energies_block
    end function
    subroutine c_rkr_mts_block_free(block) bind(C, name="rkr_mts_block_free")
      import :: c_ptr
      type(c_ptr), value :: block
    end subroutine
    function c_rkr_selection_result_primary_indices(r, out_idx, capacity, out_written) &
         bind(C, name="rkr_selection_result_primary_indices")
      import :: c_ptr, c_int, c_int64_t
      type(c_ptr), value :: r
      integer(c_int64_t), intent(out) :: out_idx(*)
      integer(c_int64_t), value :: capacity
      integer(c_int64_t), intent(out) :: out_written
      integer(c_int) :: c_rkr_selection_result_primary_indices
    end function
    subroutine c_rkr_free_string(s) bind(C, name="rkr_free_string")
      import :: c_ptr
      type(c_ptr), value :: s
    end subroutine
  end interface

contains

  subroutine to_c(f, c)
    character(len=*), intent(in) :: f
    character(kind=c_char), allocatable, intent(out) :: c(:)
    integer :: n, i
    n = len_trim(f)
    allocate(c(n+1))
    do i = 1, n
      c(i) = f(i:i)
    end do
    c(n+1) = c_null_char
  end subroutine

  function from_c(p, owned) result(s)
    type(c_ptr), intent(in) :: p
    logical, intent(in) :: owned
    character(len=:), allocatable :: s
    character(kind=c_char), pointer :: fp(:)
    integer :: n, i
    if (.not. c_associated(p)) then
      s = ""
      return
    end if
    call c_f_pointer(p, fp, [65536])
    n = 0
    do i = 1, 65536
      if (fp(i) == c_null_char) exit
      n = n + 1
    end do
    allocate(character(len=n) :: s)
    do i = 1, n
      s(i:i) = fp(i)
    end do
    if (owned) call c_rkr_free_string(p)
  end function

  integer function con_spec_version()
    con_spec_version = int(c_rkr_con_spec_version())
  end function

  function library_version() result(s)
    character(len=:), allocatable :: s
    s = from_c(c_rkr_library_version(), .false.)
  end function

  logical function has_chemfiles_support()
    has_chemfiles_support = (c_rkr_has_chemfiles_support() /= 0_c_int8_t)
  end function

  function status_message(st) result(s)
    integer, intent(in) :: st
    character(len=:), allocatable :: s
    s = from_c(c_rkr_status_message(int(st, c_int)), .false.)
  end function

  integer(int64) function symbol_to_z(sym)
    character(len=*), intent(in) :: sym
    character(kind=c_char), allocatable :: c(:)
    call to_c(sym, c)
    symbol_to_z = c_rkr_symbol_to_z(c)
  end function

  function z_to_symbol(z) result(s)
    integer(int64), intent(in) :: z
    character(len=:), allocatable :: s
    s = from_c(c_rkr_z_to_symbol(int(z, c_int64_t)), .false.)
  end function

  function read_first_frame(path) result(fr)
    character(len=*), intent(in) :: path
    type(frame_t) :: fr
    character(kind=c_char), allocatable :: c(:)
    call to_c(path, c)
    fr%handle = c_rkr_read_first_frame(c)
    fr%cview = c_null_ptr
  end function


  integer(c_size_t) function fr_atom_count(self)
    class(frame_t), intent(in) :: self
    fr_atom_count = 0_c_size_t
    if (.not. c_associated(self%handle)) return
    fr_atom_count = c_rkr_frame_atom_count(self%handle)
  end function

  integer function fr_copy_positions(self, pos)
    ! pos(3, n) column-major Fortran layout from row-major C buffer
    class(frame_t), intent(in) :: self
    real(real64), intent(out) :: pos(:,:)
    real(c_double), allocatable :: flat(:)
    integer :: i, n, st
    integer(c_size_t) :: nn
    fr_copy_positions = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    nn = c_rkr_frame_atom_count(self%handle)
    n = int(nn)
    if (size(pos, 2) < n) then
      fr_copy_positions = -6
      return
    end if
    allocate(flat(3*n))
    st = int(c_rkr_frame_copy_positions(self%handle, flat, int(3*n, c_size_t)))
    fr_copy_positions = st
    if (st /= 0) return
    do i = 1, n
      pos(1, i) = real(flat(3*(i-1)+1), real64)
      pos(2, i) = real(flat(3*(i-1)+2), real64)
      pos(3, i) = real(flat(3*(i-1)+3), real64)
    end do
  end function

  function read_all_frames(path) result(frames)
    character(len=*), intent(in) :: path
    type(frame_t), allocatable :: frames(:)
    character(kind=c_char), allocatable :: c(:)
    type(c_ptr) :: arr, fp
    integer(c_size_t) :: n, i
    type(c_ptr), pointer :: ptrs(:)
    call to_c(path, c)
    arr = c_rkr_read_all_frames(c, n)
    if (.not. c_associated(arr) .or. n == 0_c_size_t) then
      allocate(frames(0))
      return
    end if
    call c_f_pointer(arr, ptrs, [n])
    allocate(frames(n))
    do i = 1_c_size_t, n
      frames(i)%handle = ptrs(i)
      frames(i)%cview = c_null_ptr
    end do
    call c_free_rkr_frame_ptr_array(arr, n)
  end function

  function open_iterator(path) result(it)
    character(len=*), intent(in) :: path
    type(iterator_t) :: it
    character(kind=c_char), allocatable :: c(:)
    call to_c(path, c)
    it%it = c_read_con_file_iterator(c)
  end function

  logical function fr_valid(self)
    class(frame_t), intent(in) :: self
    fr_valid = c_associated(self%handle)
  end function

  subroutine fr_free(self)
    class(frame_t), intent(inout) :: self
    if (c_associated(self%cview)) then
      call c_free_c_frame(self%cview)
      self%cview = c_null_ptr
    end if
    if (c_associated(self%handle)) then
      call c_free_rkr_frame(self%handle)
      self%handle = c_null_ptr
    end if
  end subroutine

  type(c_ptr) function fr_handle(self)
    class(frame_t), intent(in) :: self
    fr_handle = self%handle
  end function

  subroutine ensure_view(self)
    class(frame_t), intent(inout) :: self
    if (c_associated(self%handle) .and. .not. c_associated(self%cview)) then
      self%cview = c_rkr_frame_to_c_frame(self%handle)
    end if
  end subroutine

  integer function fr_natoms(self)
    class(frame_t), intent(inout) :: self
    type(cframe_t), pointer :: cf
    fr_natoms = 0
    call ensure_view(self)
    if (.not. c_associated(self%cview)) return
    call c_f_pointer(self%cview, cf)
    fr_natoms = int(cf%num_atoms)
  end function

  function fr_atom(self, i) result(a)
    class(frame_t), intent(inout) :: self
    integer, intent(in) :: i
    type(catom_t) :: a
    type(cframe_t), pointer :: cf
    type(catom_t), pointer :: atoms(:)
    a = catom_t()
    call ensure_view(self)
    if (.not. c_associated(self%cview)) return
    call c_f_pointer(self%cview, cf)
    if (i < 1 .or. i > int(cf%num_atoms) .or. .not. c_associated(cf%atoms)) return
    call c_f_pointer(cf%atoms, atoms, [int(cf%num_atoms)])
    a = atoms(i)
  end function

  subroutine fr_cell(self, v)
    class(frame_t), intent(inout) :: self
    real(real64), intent(out) :: v(3)
    type(cframe_t), pointer :: cf
    v = 0.0_real64
    call ensure_view(self)
    if (.not. c_associated(self%cview)) return
    call c_f_pointer(self%cview, cf)
    v = real(cf%cell, real64)
  end subroutine

  subroutine fr_angles(self, v)
    class(frame_t), intent(inout) :: self
    real(real64), intent(out) :: v(3)
    type(cframe_t), pointer :: cf
    v = 0.0_real64
    call ensure_view(self)
    if (.not. c_associated(self%cview)) return
    call c_f_pointer(self%cview, cf)
    v = real(cf%angles, real64)
  end subroutine

  logical function fr_has_vel(self)
    class(frame_t), intent(inout) :: self
    type(cframe_t), pointer :: cf
    fr_has_vel = .false.
    call ensure_view(self)
    if (.not. c_associated(self%cview)) return
    call c_f_pointer(self%cview, cf)
    fr_has_vel = logical(cf%has_velocities)
  end function

  logical function fr_has_frc(self)
    class(frame_t), intent(inout) :: self
    type(cframe_t), pointer :: cf
    fr_has_frc = .false.
    call ensure_view(self)
    if (.not. c_associated(self%cview)) return
    call c_f_pointer(self%cview, cf)
    fr_has_frc = logical(cf%has_forces)
  end function

  function fr_meta(self) result(s)
    class(frame_t), intent(in) :: self
    character(len=:), allocatable :: s
    s = ""
    if (.not. c_associated(self%handle)) return
    s = from_c(c_rkr_frame_metadata_json(self%handle), .true.)
  end function

  real(real64) function fr_energy(self)
    class(frame_t), intent(in) :: self
    fr_energy = 0.0_real64
    if (.not. c_associated(self%handle)) return
    fr_energy = real(c_rkr_frame_energy(self%handle), real64)
  end function

  real(real64) function fr_index_energy(self)
    class(frame_t), intent(in) :: self
    fr_index_energy = 0.0_real64
    if (.not. c_associated(self%handle)) return
    fr_index_energy = real(c_rkr_frame_index_energy(self%handle), real64)
  end function

  function fr_composition_formula(self) result(s)
    class(frame_t), intent(in) :: self
    character(len=:), allocatable :: s
    s = ""
    if (.not. c_associated(self%handle)) return
    s = from_c(c_rkr_frame_composition_formula(self%handle), .true.)
  end function

  real(real64) function fr_total_mass(self)
    class(frame_t), intent(in) :: self
    fr_total_mass = 0.0_real64
    if (.not. c_associated(self%handle)) return
    fr_total_mass = real(c_rkr_frame_total_mass(self%handle), real64)
  end function

  real(real64) function fr_cell_volume(self)
    class(frame_t), intent(in) :: self
    fr_cell_volume = 0.0_real64
    if (.not. c_associated(self%handle)) return
    fr_cell_volume = real(c_rkr_frame_cell_volume(self%handle), real64)
  end function

  real(real64) function fr_fmax(self)
    class(frame_t), intent(in) :: self
    fr_fmax = 0.0_real64
    if (.not. c_associated(self%handle)) return
    fr_fmax = real(c_rkr_frame_fmax(self%handle), real64)
  end function

  integer(c_int8_t) function fr_sections_mask(self)
    class(frame_t), intent(in) :: self
    fr_sections_mask = 0_c_int8_t
    if (.not. c_associated(self%handle)) return
    fr_sections_mask = c_rkr_frame_sections_mask(self%handle)
  end function

  integer(c_int32_t) function fr_index_natoms(self)
    class(frame_t), intent(in) :: self
    fr_index_natoms = 0_c_int32_t
    if (.not. c_associated(self%handle)) return
    fr_index_natoms = c_rkr_frame_index_natoms(self%handle)
  end function

  function fr_index_projection_json(self) result(s)
    class(frame_t), intent(in) :: self
    character(len=:), allocatable :: s
    s = "{}"
    if (.not. c_associated(self%handle)) return
    s = from_c(c_rkr_frame_index_projection_json(self%handle), .true.)
  end function

  function fr_pot(self) result(s)
    class(frame_t), intent(in) :: self
    character(len=:), allocatable :: s
    s = ""
    if (.not. c_associated(self%handle)) return
    s = from_c(c_rkr_frame_potential_type(self%handle), .true.)
  end function

  integer(int64) function fr_fidx(self)
    class(frame_t), intent(in) :: self
    fr_fidx = -1_int64
    if (.not. c_associated(self%handle)) return
    fr_fidx = c_rkr_frame_frame_index(self%handle)
  end function

  real(real64) function fr_time(self)
    class(frame_t), intent(in) :: self
    fr_time = 0.0_real64
    if (.not. c_associated(self%handle)) return
    fr_time = real(c_rkr_frame_time(self%handle), real64)
  end function

  real(real64) function fr_dt(self)
    class(frame_t), intent(in) :: self
    fr_dt = 0.0_real64
    if (.not. c_associated(self%handle)) return
    fr_dt = real(c_rkr_frame_timestep(self%handle), real64)
  end function

  integer(int64) function fr_neb_bead(self)
    class(frame_t), intent(in) :: self
    fr_neb_bead = -1_int64
    if (.not. c_associated(self%handle)) return
    fr_neb_bead = c_rkr_frame_neb_bead(self%handle)
  end function

  integer(int64) function fr_neb_band(self)
    class(frame_t), intent(in) :: self
    fr_neb_band = -1_int64
    if (.not. c_associated(self%handle)) return
    fr_neb_band = c_rkr_frame_neb_band(self%handle)
  end function

  integer function fr_nbonds(self)
    class(frame_t), intent(in) :: self
    fr_nbonds = 0
    if (.not. c_associated(self%handle)) return
    fr_nbonds = int(c_rkr_frame_bond_count(self%handle))
  end function

  integer function fr_bond_at(self, idx0, i, j, order, has_order)
    class(frame_t), intent(in) :: self
    integer, intent(in) :: idx0
    integer, intent(out) :: i, j, order
    logical, intent(out) :: has_order
    integer(c_int32_t) :: ci, cj, co
    integer(c_int8_t) :: ho
    integer(c_int) :: st
    i = 0; j = 0; order = 0; has_order = .false.
    fr_bond_at = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    st = c_rkr_frame_bond_at(self%handle, int(idx0, c_int64_t), ci, cj, ho, co)
    fr_bond_at = int(st)
    if (st == 0) then
      i = int(ci); j = int(cj); order = int(co)
      has_order = (ho /= 0_c_int8_t)
    end if
  end function

  integer(int64) function fr_id_index(self, atom_id)
    class(frame_t), intent(in) :: self
    integer(int64), intent(in) :: atom_id
    fr_id_index = -1_int64
    if (.not. c_associated(self%handle)) return
    fr_id_index = c_rkr_frame_atom_index_by_id(self%handle, int(atom_id, c_int64_t))
    if (fr_id_index == huge(0_c_int64_t)) fr_id_index = -1_int64
  end function

  integer function fr_spec(self)
    class(frame_t), intent(in) :: self
    fr_spec = 0
    if (.not. c_associated(self%handle)) return
    fr_spec = int(c_rkr_frame_spec_version(self%handle))
  end function

  integer function fr_select(self, selection, nmatch)
    class(frame_t), intent(in) :: self
    character(len=*), intent(in) :: selection
    integer, intent(out) :: nmatch
    character(kind=c_char), allocatable :: csel(:)
    type(c_ptr) :: res
    integer(c_int) :: st
    nmatch = 0
    fr_select = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    call to_c(selection, csel)
    st = c_rkr_frame_select(self%handle, csel, res)
    fr_select = int(st)
    if (st == 0 .and. c_associated(res)) then
      nmatch = int(c_rkr_selection_result_match_count(res))
      call c_rkr_selection_result_free(res)
    end if
  end function

  integer function fr_write_path(self, path)
    class(frame_t), intent(in) :: self
    character(len=*), intent(in) :: path
    type(writer_t) :: w
    fr_write_path = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    w = open_writer(path)
    if (.not. w%valid()) return
    fr_write_path = w%extend_one(self)
    call w%free()
  end function

  logical function it_valid(self)
    class(iterator_t), intent(in) :: self
    it_valid = c_associated(self%it)
  end function

  function it_next(self) result(fr)
    class(iterator_t), intent(inout) :: self
    type(frame_t) :: fr
    fr%handle = c_null_ptr
    fr%cview = c_null_ptr
    if (.not. c_associated(self%it)) return
    fr%handle = c_con_frame_iterator_next(self%it)
  end function

  subroutine it_free(self)
    class(iterator_t), intent(inout) :: self
    if (c_associated(self%it)) then
      call c_free_con_frame_iterator(self%it)
      self%it = c_null_ptr
    end if
  end subroutine

  function new_builder(cell, angles) result(bd)
    real(real64), intent(in) :: cell(3), angles(3)
    type(builder_t) :: bd
    real(c_double) :: cc(3), aa(3)
    cc = real(cell, c_double)
    aa = real(angles, c_double)
    bd%b = c_rkr_frame_new(cc, aa, c_null_ptr, c_null_ptr, c_null_ptr, c_null_ptr)
  end function

  logical function bd_valid(self)
    class(builder_t), intent(in) :: self
    bd_valid = c_associated(self%b)
  end function

  subroutine bd_free(self)
    class(builder_t), intent(inout) :: self
    if (c_associated(self%b)) then
      call c_free_rkr_frame_builder(self%b)
      self%b = c_null_ptr
    end if
  end subroutine

  integer function bd_add_atom(self, symbol, x, y, z, atom_id, mass, fixed_x, fixed_y, fixed_z)
    class(builder_t), intent(inout) :: self
    character(len=*), intent(in) :: symbol
    real(real64), intent(in) :: x, y, z, mass
    integer(int64), intent(in) :: atom_id
    logical, intent(in) :: fixed_x, fixed_y, fixed_z
    character(kind=c_char), allocatable :: cs(:)
    bd_add_atom = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    call to_c(symbol, cs)
    bd_add_atom = int(c_rkr_frame_add_atom_with_fixed_mask(self%b, cs, &
         real(x,c_double), real(y,c_double), real(z,c_double), int(atom_id,c_int64_t), &
         real(mass,c_double), logical(fixed_x,c_bool), logical(fixed_y,c_bool), logical(fixed_z,c_bool)))
  end function

  integer function bd_set_energy(self, energy)
    class(builder_t), intent(inout) :: self
    real(real64), intent(in) :: energy
    bd_set_energy = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    bd_set_energy = int(c_rkr_frame_builder_set_energy(self%b, real(energy, c_double)))
  end function

  integer function bd_set_meta(self, json)
    class(builder_t), intent(inout) :: self
    character(len=*), intent(in) :: json
    character(kind=c_char), allocatable :: cj(:)
    bd_set_meta = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    call to_c(json, cj)
    bd_set_meta = int(c_rkr_frame_builder_set_metadata_json(self%b, cj))
  end function

  integer function bd_set_fidx(self, idx)
    class(builder_t), intent(inout) :: self
    integer(int64), intent(in) :: idx
    bd_set_fidx = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    bd_set_fidx = int(c_rkr_frame_builder_set_frame_index(self%b, int(idx, c_int64_t)))
  end function

  function bd_build(self) result(fr)
    class(builder_t), intent(inout) :: self
    type(frame_t) :: fr
    fr%handle = c_null_ptr
    fr%cview = c_null_ptr
    if (.not. c_associated(self%b)) return
    fr%handle = c_rkr_frame_builder_build(self%b)
    self%b = c_null_ptr  ! consumed
  end function

  function open_writer(path) result(w)
    character(len=*), intent(in) :: path
    type(writer_t) :: w
    character(kind=c_char), allocatable :: c(:)
    call to_c(path, c)
    w%w = c_create_writer_from_path_c(c)
  end function

  function open_writer_gzip_with_precision(path, precision) result(w)
    character(len=*), intent(in) :: path
    integer, intent(in) :: precision
    type(writer_t) :: w
    character(kind=c_char), allocatable :: c(:)
    call to_c(path, c)
    w%w = c_create_writer_gzip_with_precision_c(c, int(precision, c_int8_t))
  end function

  function open_writer_zstd_with_precision(path, precision) result(w)
    character(len=*), intent(in) :: path
    integer, intent(in) :: precision
    type(writer_t) :: w
    character(kind=c_char), allocatable :: c(:)
    call to_c(path, c)
    w%w = c_create_writer_zstd_with_precision_c(c, int(precision, c_int8_t))
  end function

  function open_writer_gzip(path) result(w)
    ! Always in C ABI (create_writer_gzip_c)
    character(len=*), intent(in) :: path
    type(writer_t) :: w
    character(kind=c_char), allocatable :: c(:)
    call to_c(path, c)
    w%w = c_create_writer_gzip_c(c)
  end function

  function open_writer_zstd(path) result(w)
    character(len=*), intent(in) :: path
    type(writer_t) :: w
    character(kind=c_char), allocatable :: c(:)
    call to_c(path, c)
    w%w = c_create_writer_zstd_c(c)
  end function

  logical function wr_valid(self)
    class(writer_t), intent(in) :: self
    wr_valid = c_associated(self%w)
  end function

  subroutine wr_free(self)
    class(writer_t), intent(inout) :: self
    if (c_associated(self%w)) then
      call c_free_rkr_writer(self%w)
      self%w = c_null_ptr
    end if
  end subroutine

  integer function wr_set_canonical(self, on)
    class(writer_t), intent(inout) :: self
    logical, intent(in) :: on
    integer(c_int8_t) :: flag
    wr_set_canonical = rkr_status_null_pointer
    if (.not. c_associated(self%w)) return
    flag = merge(1_c_int8_t, 0_c_int8_t, on)
    wr_set_canonical = int(c_rkr_writer_set_canonical(self%w, flag))
  end function

  logical function wr_is_canonical(self)
    class(writer_t), intent(in) :: self
    wr_is_canonical = .false.
    if (.not. c_associated(self%w)) return
    wr_is_canonical = (c_rkr_writer_is_canonical(self%w) /= 0_c_int8_t)
  end function

  integer function wr_extend_one(self, fr)
    class(writer_t), intent(inout) :: self
    class(frame_t), intent(in) :: fr
    type(c_ptr) :: arr(1)
    wr_extend_one = rkr_status_null_pointer
    if (.not. c_associated(self%w) .or. .not. c_associated(fr%handle)) return
    arr(1) = fr%handle
    wr_extend_one = int(c_rkr_writer_extend(self%w, arr, 1_c_size_t))
  end function

  function read_chemfiles_first(path) result(fr)
    character(len=*), intent(in) :: path
    type(frame_t) :: fr
    character(kind=c_char), allocatable :: c(:)
    fr%handle = c_null_ptr
    fr%cview = c_null_ptr
    call to_c(path, c)
    fr%handle = c_rkr_read_chemfiles_first(c)
  end function

  integer function fr_select_primary(self, selection, indices, nwritten)
    class(frame_t), intent(in) :: self
    character(len=*), intent(in) :: selection
    integer(int64), intent(out) :: indices(:)
    integer, intent(out) :: nwritten
    character(kind=c_char), allocatable :: csel(:)
    type(c_ptr) :: res
    integer(c_int) :: st
    integer(c_int64_t) :: written, i
    integer(c_int64_t), allocatable :: buf(:)
    nwritten = 0
    fr_select_primary = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    call to_c(selection, csel)
    st = c_rkr_frame_select(self%handle, csel, res)
    fr_select_primary = int(st)
    if (st /= 0 .or. .not. c_associated(res)) return
    allocate(buf(size(indices)))
    st = c_rkr_selection_result_primary_indices(res, buf, int(size(indices), c_int64_t), written)
    fr_select_primary = int(st)
    if (st == 0) then
      nwritten = int(written)
      do i = 1_c_int64_t, written
        indices(i) = buf(i)
      end do
    end if
    call c_rkr_selection_result_free(res)
  end function

  integer function bd_copy_positions(self, pos)
    class(builder_t), intent(inout) :: self
    real(real64), intent(out) :: pos(:,:)
    type(c_ptr) :: p
    real(c_double), pointer :: flat(:)
    integer :: n, i, j
    bd_copy_positions = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    n = int(c_rkr_frame_builder_atom_count(self%b))
    if (n <= 0) return
    if (size(pos, 1) < 3 .or. size(pos, 2) < n) then
      bd_copy_positions = -6
      return
    end if
    p = c_rkr_frame_builder_positions_data(self%b)
    if (.not. c_associated(p)) return
    call c_f_pointer(p, flat, [3 * n])
    do i = 1, n
      do j = 1, 3
        pos(j, i) = real(flat((i - 1) * 3 + j), real64)
      end do
    end do
    bd_copy_positions = 0
  end function

  integer function bd_copy_masses(self, masses)
    class(builder_t), intent(inout) :: self
    real(real64), intent(out) :: masses(:)
    type(c_ptr) :: p
    real(c_double), pointer :: flat(:)
    integer :: n, i
    bd_copy_masses = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    n = int(c_rkr_frame_builder_atom_count(self%b))
    if (n <= 0 .or. size(masses) < n) then
      bd_copy_masses = -6
      return
    end if
    p = c_rkr_frame_builder_masses_data(self%b)
    if (.not. c_associated(p)) return
    call c_f_pointer(p, flat, [n])
    do i = 1, n
      masses(i) = real(flat(i), real64)
    end do
    bd_copy_masses = 0
  end function

  integer function bd_positions_dlpack(self, tensor)
    class(builder_t), intent(inout) :: self
    type(c_ptr), intent(out) :: tensor
    bd_positions_dlpack = rkr_status_null_pointer
    tensor = c_null_ptr
    if (.not. c_associated(self%b)) return
    bd_positions_dlpack = int(c_rkr_frame_builder_positions_dlpack(self%b, tensor))
  end function

  integer function bd_masses_dlpack(self, tensor)
    class(builder_t), intent(inout) :: self
    type(c_ptr), intent(out) :: tensor
    bd_masses_dlpack = rkr_status_null_pointer
    tensor = c_null_ptr
    if (.not. c_associated(self%b)) return
    bd_masses_dlpack = int(c_rkr_frame_builder_masses_dlpack(self%b, tensor))
  end function

  subroutine bd_dlpack_delete(self, tensor)
    class(builder_t), intent(in) :: self
    type(c_ptr), intent(inout) :: tensor
    if (c_associated(self%b)) continue
    if (c_associated(tensor)) then
      call c_rkr_dlpack_delete(tensor)
      tensor = c_null_ptr
    end if
  end subroutine

  subroutine dlpack_inspect(tensor, ndim, shape0, shape1, dtype_bits, ok)
    ! Mirror DLPack C header fields: DLManagedTensorVersioned.dl_tensor.{ndim,shape,dtype.bits}
    type(c_ptr), intent(in) :: tensor
    integer, intent(out) :: ndim, dtype_bits
    integer(int64), intent(out) :: shape0, shape1
    logical, intent(out) :: ok
    type(dl_managed_tensor_versioned_t), pointer :: mt
    integer(c_int64_t), pointer :: shp(:)
    ok = .false.
    ndim = 0
    shape0 = 0_int64
    shape1 = 0_int64
    dtype_bits = 0
    if (.not. c_associated(tensor)) return
    call c_f_pointer(tensor, mt)
    ndim = int(mt%dl_tensor%ndim)
    dtype_bits = int(mt%dl_tensor%dtype%bits)
    if (ndim >= 1 .and. c_associated(mt%dl_tensor%shape)) then
      call c_f_pointer(mt%dl_tensor%shape, shp, [ndim])
      shape0 = shp(1)
      if (ndim >= 2) shape1 = shp(2)
    end if
    ok = c_associated(mt%dl_tensor%data) .and. ndim > 0
  end subroutine

  function dlpack_data_ptr(tensor) result(p)
    ! DLTensor.data from dlpack.h layout
    type(c_ptr), intent(in) :: tensor
    type(c_ptr) :: p
    type(dl_managed_tensor_versioned_t), pointer :: mt
    p = c_null_ptr
    if (.not. c_associated(tensor)) return
    call c_f_pointer(tensor, mt)
    p = mt%dl_tensor%data
  end function

  integer function bd_velocities_dlpack(self, tensor)
    class(builder_t), intent(inout) :: self
    type(c_ptr), intent(out) :: tensor
    tensor = c_null_ptr
    bd_velocities_dlpack = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    bd_velocities_dlpack = int(c_rkr_frame_builder_velocities_dlpack(self%b, tensor))
  end function

  integer function bd_forces_dlpack(self, tensor)
    class(builder_t), intent(inout) :: self
    type(c_ptr), intent(out) :: tensor
    tensor = c_null_ptr
    bd_forces_dlpack = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    bd_forces_dlpack = int(c_rkr_frame_builder_forces_dlpack(self%b, tensor))
  end function

  integer function bd_atom_energies_dlpack(self, tensor)
    class(builder_t), intent(inout) :: self
    type(c_ptr), intent(out) :: tensor
    tensor = c_null_ptr
    bd_atom_energies_dlpack = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    bd_atom_energies_dlpack = int(c_rkr_frame_builder_atom_energies_dlpack(self%b, tensor))
  end function

  integer function bd_atom_ids_dlpack(self, tensor)
    class(builder_t), intent(inout) :: self
    type(c_ptr), intent(out) :: tensor
    tensor = c_null_ptr
    bd_atom_ids_dlpack = rkr_status_null_pointer
    if (.not. c_associated(self%b)) return
    bd_atom_ids_dlpack = int(c_rkr_frame_builder_atom_ids_dlpack(self%b, tensor))
  end function

  integer function frame_metatensor_positions_block(fr, block)
    type(frame_t), intent(in) :: fr
    type(c_ptr), intent(out) :: block
    block = c_null_ptr
    frame_metatensor_positions_block = rkr_status_null_pointer
    if (.not. c_associated(fr%handle)) return
    frame_metatensor_positions_block = int(c_rkr_frame_metatensor_positions_block(fr%handle, block))

  end function

  integer function frame_metatensor_velocities_block(fr, block)
    type(frame_t), intent(in) :: fr
    type(c_ptr), intent(out) :: block
    block = c_null_ptr
    frame_metatensor_velocities_block = rkr_status_null_pointer
    if (.not. c_associated(fr%handle)) return
    frame_metatensor_velocities_block = int(c_rkr_frame_metatensor_velocities_block(fr%handle, block))

  end function

  integer function frame_metatensor_forces_block(fr, block)
    type(frame_t), intent(in) :: fr
    type(c_ptr), intent(out) :: block
    block = c_null_ptr
    frame_metatensor_forces_block = rkr_status_null_pointer
    if (.not. c_associated(fr%handle)) return
    frame_metatensor_forces_block = int(c_rkr_frame_metatensor_forces_block(fr%handle, block))

  end function

  integer function frame_metatensor_atom_energies_block(fr, block)
    type(frame_t), intent(in) :: fr
    type(c_ptr), intent(out) :: block
    block = c_null_ptr
    frame_metatensor_atom_energies_block = rkr_status_null_pointer
    if (.not. c_associated(fr%handle)) return
    frame_metatensor_atom_energies_block = int(c_rkr_frame_metatensor_atom_energies_block(fr%handle, block))

  end function

  subroutine mts_block_free_rkr(block)
    type(c_ptr), intent(inout) :: block
    if (c_associated(block)) call c_rkr_mts_block_free(block)
    block = c_null_ptr
  end subroutine


  integer function fr_copy_velocities(self, vel)
    class(frame_t), intent(in) :: self
    real(real64), intent(out) :: vel(:,:)
    real(c_double), allocatable :: flat(:)
    integer :: i, n, st
    integer(c_size_t) :: nn
    fr_copy_velocities = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    nn = c_rkr_frame_atom_count(self%handle)
    n = int(nn)
    if (size(vel, 2) < n) then
      fr_copy_velocities = rkr_status_buffer_too_small
      return
    end if
    allocate(flat(3*n))
    st = int(c_rkr_frame_copy_velocities(self%handle, flat, int(3*n, c_size_t)))
    fr_copy_velocities = st
    if (st /= 0) return
    do i = 1, n
      vel(1, i) = real(flat(3*(i-1)+1), real64)
      vel(2, i) = real(flat(3*(i-1)+2), real64)
      vel(3, i) = real(flat(3*(i-1)+3), real64)
    end do
  end function

  integer function fr_copy_forces(self, frc)
    class(frame_t), intent(in) :: self
    real(real64), intent(out) :: frc(:,:)
    real(c_double), allocatable :: flat(:)
    integer :: i, n, st
    integer(c_size_t) :: nn
    fr_copy_forces = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    nn = c_rkr_frame_atom_count(self%handle)
    n = int(nn)
    if (size(frc, 2) < n) then
      fr_copy_forces = rkr_status_buffer_too_small
      return
    end if
    allocate(flat(3*n))
    st = int(c_rkr_frame_copy_forces(self%handle, flat, int(3*n, c_size_t)))
    fr_copy_forces = st
    if (st /= 0) return
    do i = 1, n
      frc(1, i) = real(flat(3*(i-1)+1), real64)
      frc(2, i) = real(flat(3*(i-1)+2), real64)
      frc(3, i) = real(flat(3*(i-1)+3), real64)
    end do
  end function

  integer function fr_copy_masses(self, mass)
    class(frame_t), intent(in) :: self
    real(real64), intent(out) :: mass(:)
    real(c_double), allocatable :: flat(:)
    integer :: n, st
    integer(c_size_t) :: nn
    fr_copy_masses = rkr_status_null_pointer
    if (.not. c_associated(self%handle)) return
    nn = c_rkr_frame_atom_count(self%handle)
    n = int(nn)
    if (size(mass) < n) then
      fr_copy_masses = rkr_status_buffer_too_small
      return
    end if
    allocate(flat(n))
    st = int(c_rkr_frame_copy_masses(self%handle, flat, int(n, c_size_t)))
    fr_copy_masses = st
    if (st /= 0) return
    mass(1:n) = real(flat(1:n), real64)
  end function


end module readcon