forked from plumed/plumed2
/
plumed_f08.f90
2602 lines (2413 loc) · 112 KB
/
plumed_f08.f90
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
! vim:ft=fortran
module plumed_f08_module
use iso_c_binding
implicit none
! names are private by default
private
! only these names are public
public :: plumed
public :: plumed_create
public :: plumed_installed
public :: plumed_error
! this type maps to the struct plumed defined in src/wrapper/Plumed.h
type, bind(C) :: cplumed
type(c_ptr) :: ptr
end type cplumed
! this type maps to the struct plumed_nothrow_handler defined in src/wrapper/Plumed.h
type, bind(C) :: cplumed_nothrow_handler
type(c_ptr) :: ptr
type(c_funptr) :: handler
end type cplumed_nothrow_handler
! this type maps to the struct plumed_safeptr defined in src/wrapper/Plumed.h
type, bind(C) :: cplumed_safeptr
type(c_ptr) :: ptr
integer(kind=c_size_t) :: nelem
type(c_ptr) :: shape
integer(kind=c_size_t) :: flags
type(c_ptr) :: opt
end type cplumed_safeptr
! this subroutine provides a typesafe, not throwing interfafe to plumed
interface
subroutine plumed_cmd_safe_nothrow(p,key,safeptr,nothrow) bind(C)
import
type(cplumed), value :: p
character(kind=c_char), intent(in) :: key(*)
type(cplumed_safeptr), value :: safeptr
type(cplumed_nothrow_handler), value :: nothrow
end subroutine plumed_cmd_safe_nothrow
end interface
integer(kind=c_size_t), parameter :: flags_ptr = 67108864 ! 0x2000000*2
integer(kind=c_size_t), parameter :: flags_const_ptr = 100663296 ! 0x2000000*3
integer(kind=c_size_t), parameter :: flags_nocopy = 268435456 ! 0x10000000
! this type is used to manipulate a plumed instance
! since it has a destructor, if contains a logical label to check if it has been initialized
! it also contains a number of methods
! notice that constructor (plumed_create) is NOT a member
type plumed
type(cplumed), private :: handle
logical, private :: initialized = .false.
contains
private
generic, public :: cmd => pl_cmd
procedure :: pl_cmd
generic, public :: cmd_val => pl_cmd_val_integer_0_0
procedure :: pl_cmd_val_integer_0_0
generic, public :: cmd_val => pl_cmd_val_integer_0_1
procedure :: pl_cmd_val_integer_0_1
generic, public :: cmd_val => pl_cmd_val_integer_0_2
procedure :: pl_cmd_val_integer_0_2
generic, public :: cmd_val => pl_cmd_val_integer_0_3
procedure :: pl_cmd_val_integer_0_3
generic, public :: cmd_val => pl_cmd_val_integer_0_4
procedure :: pl_cmd_val_integer_0_4
generic, public :: cmd_val => pl_cmd_val_integer_1_0
procedure :: pl_cmd_val_integer_1_0
generic, public :: cmd_val => pl_cmd_val_integer_1_1
procedure :: pl_cmd_val_integer_1_1
generic, public :: cmd_val => pl_cmd_val_integer_1_2
procedure :: pl_cmd_val_integer_1_2
generic, public :: cmd_val => pl_cmd_val_integer_1_3
procedure :: pl_cmd_val_integer_1_3
generic, public :: cmd_val => pl_cmd_val_integer_1_4
procedure :: pl_cmd_val_integer_1_4
generic, public :: cmd_val => pl_cmd_val_integer_2_0
procedure :: pl_cmd_val_integer_2_0
generic, public :: cmd_val => pl_cmd_val_integer_2_1
procedure :: pl_cmd_val_integer_2_1
generic, public :: cmd_val => pl_cmd_val_integer_2_2
procedure :: pl_cmd_val_integer_2_2
generic, public :: cmd_val => pl_cmd_val_integer_2_3
procedure :: pl_cmd_val_integer_2_3
generic, public :: cmd_val => pl_cmd_val_integer_2_4
procedure :: pl_cmd_val_integer_2_4
generic, public :: cmd_val => pl_cmd_val_real_0_0
procedure :: pl_cmd_val_real_0_0
generic, public :: cmd_val => pl_cmd_val_real_0_1
procedure :: pl_cmd_val_real_0_1
generic, public :: cmd_val => pl_cmd_val_real_0_2
procedure :: pl_cmd_val_real_0_2
generic, public :: cmd_val => pl_cmd_val_real_0_3
procedure :: pl_cmd_val_real_0_3
generic, public :: cmd_val => pl_cmd_val_real_0_4
procedure :: pl_cmd_val_real_0_4
generic, public :: cmd_val => pl_cmd_val_real_1_0
procedure :: pl_cmd_val_real_1_0
generic, public :: cmd_val => pl_cmd_val_real_1_1
procedure :: pl_cmd_val_real_1_1
generic, public :: cmd_val => pl_cmd_val_real_1_2
procedure :: pl_cmd_val_real_1_2
generic, public :: cmd_val => pl_cmd_val_real_1_3
procedure :: pl_cmd_val_real_1_3
generic, public :: cmd_val => pl_cmd_val_real_1_4
procedure :: pl_cmd_val_real_1_4
generic, public :: cmd_ref => pl_cmd_ref_integer_0_0
procedure :: pl_cmd_ref_integer_0_0
generic, public :: cmd_ref => pl_cmd_ref_integer_0_1
procedure :: pl_cmd_ref_integer_0_1
generic, public :: cmd_ref => pl_cmd_ref_integer_0_2
procedure :: pl_cmd_ref_integer_0_2
generic, public :: cmd_ref => pl_cmd_ref_integer_0_3
procedure :: pl_cmd_ref_integer_0_3
generic, public :: cmd_ref => pl_cmd_ref_integer_0_4
procedure :: pl_cmd_ref_integer_0_4
generic, public :: cmd_ref => pl_cmd_ref_integer_1_0
procedure :: pl_cmd_ref_integer_1_0
generic, public :: cmd_ref => pl_cmd_ref_integer_1_1
procedure :: pl_cmd_ref_integer_1_1
generic, public :: cmd_ref => pl_cmd_ref_integer_1_2
procedure :: pl_cmd_ref_integer_1_2
generic, public :: cmd_ref => pl_cmd_ref_integer_1_3
procedure :: pl_cmd_ref_integer_1_3
generic, public :: cmd_ref => pl_cmd_ref_integer_1_4
procedure :: pl_cmd_ref_integer_1_4
generic, public :: cmd_ref => pl_cmd_ref_integer_2_0
procedure :: pl_cmd_ref_integer_2_0
generic, public :: cmd_ref => pl_cmd_ref_integer_2_1
procedure :: pl_cmd_ref_integer_2_1
generic, public :: cmd_ref => pl_cmd_ref_integer_2_2
procedure :: pl_cmd_ref_integer_2_2
generic, public :: cmd_ref => pl_cmd_ref_integer_2_3
procedure :: pl_cmd_ref_integer_2_3
generic, public :: cmd_ref => pl_cmd_ref_integer_2_4
procedure :: pl_cmd_ref_integer_2_4
generic, public :: cmd_ref => pl_cmd_ref_real_0_0
procedure :: pl_cmd_ref_real_0_0
generic, public :: cmd_ref => pl_cmd_ref_real_0_1
procedure :: pl_cmd_ref_real_0_1
generic, public :: cmd_ref => pl_cmd_ref_real_0_2
procedure :: pl_cmd_ref_real_0_2
generic, public :: cmd_ref => pl_cmd_ref_real_0_3
procedure :: pl_cmd_ref_real_0_3
generic, public :: cmd_ref => pl_cmd_ref_real_0_4
procedure :: pl_cmd_ref_real_0_4
generic, public :: cmd_ref => pl_cmd_ref_real_1_0
procedure :: pl_cmd_ref_real_1_0
generic, public :: cmd_ref => pl_cmd_ref_real_1_1
procedure :: pl_cmd_ref_real_1_1
generic, public :: cmd_ref => pl_cmd_ref_real_1_2
procedure :: pl_cmd_ref_real_1_2
generic, public :: cmd_ref => pl_cmd_ref_real_1_3
procedure :: pl_cmd_ref_real_1_3
generic, public :: cmd_ref => pl_cmd_ref_real_1_4
procedure :: pl_cmd_ref_real_1_4
generic, public :: cmd_ptr => pl_cmd_ptr_integer_0_0
procedure :: pl_cmd_ptr_integer_0_0
generic, public :: cmd_ptr => pl_cmd_ptr_integer_0_1
procedure :: pl_cmd_ptr_integer_0_1
generic, public :: cmd_ptr => pl_cmd_ptr_integer_0_2
procedure :: pl_cmd_ptr_integer_0_2
generic, public :: cmd_ptr => pl_cmd_ptr_integer_0_3
procedure :: pl_cmd_ptr_integer_0_3
generic, public :: cmd_ptr => pl_cmd_ptr_integer_0_4
procedure :: pl_cmd_ptr_integer_0_4
generic, public :: cmd_ptr => pl_cmd_ptr_integer_1_0
procedure :: pl_cmd_ptr_integer_1_0
generic, public :: cmd_ptr => pl_cmd_ptr_integer_1_1
procedure :: pl_cmd_ptr_integer_1_1
generic, public :: cmd_ptr => pl_cmd_ptr_integer_1_2
procedure :: pl_cmd_ptr_integer_1_2
generic, public :: cmd_ptr => pl_cmd_ptr_integer_1_3
procedure :: pl_cmd_ptr_integer_1_3
generic, public :: cmd_ptr => pl_cmd_ptr_integer_1_4
procedure :: pl_cmd_ptr_integer_1_4
generic, public :: cmd_ptr => pl_cmd_ptr_integer_2_0
procedure :: pl_cmd_ptr_integer_2_0
generic, public :: cmd_ptr => pl_cmd_ptr_integer_2_1
procedure :: pl_cmd_ptr_integer_2_1
generic, public :: cmd_ptr => pl_cmd_ptr_integer_2_2
procedure :: pl_cmd_ptr_integer_2_2
generic, public :: cmd_ptr => pl_cmd_ptr_integer_2_3
procedure :: pl_cmd_ptr_integer_2_3
generic, public :: cmd_ptr => pl_cmd_ptr_integer_2_4
procedure :: pl_cmd_ptr_integer_2_4
generic, public :: cmd_ptr => pl_cmd_ptr_real_0_0
procedure :: pl_cmd_ptr_real_0_0
generic, public :: cmd_ptr => pl_cmd_ptr_real_0_1
procedure :: pl_cmd_ptr_real_0_1
generic, public :: cmd_ptr => pl_cmd_ptr_real_0_2
procedure :: pl_cmd_ptr_real_0_2
generic, public :: cmd_ptr => pl_cmd_ptr_real_0_3
procedure :: pl_cmd_ptr_real_0_3
generic, public :: cmd_ptr => pl_cmd_ptr_real_0_4
procedure :: pl_cmd_ptr_real_0_4
generic, public :: cmd_ptr => pl_cmd_ptr_real_1_0
procedure :: pl_cmd_ptr_real_1_0
generic, public :: cmd_ptr => pl_cmd_ptr_real_1_1
procedure :: pl_cmd_ptr_real_1_1
generic, public :: cmd_ptr => pl_cmd_ptr_real_1_2
procedure :: pl_cmd_ptr_real_1_2
generic, public :: cmd_ptr => pl_cmd_ptr_real_1_3
procedure :: pl_cmd_ptr_real_1_3
generic, public :: cmd_ptr => pl_cmd_ptr_real_1_4
procedure :: pl_cmd_ptr_real_1_4
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_0_0
procedure :: pl_cmd_const_ptr_integer_0_0
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_0_1
procedure :: pl_cmd_const_ptr_integer_0_1
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_0_2
procedure :: pl_cmd_const_ptr_integer_0_2
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_0_3
procedure :: pl_cmd_const_ptr_integer_0_3
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_0_4
procedure :: pl_cmd_const_ptr_integer_0_4
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_1_0
procedure :: pl_cmd_const_ptr_integer_1_0
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_1_1
procedure :: pl_cmd_const_ptr_integer_1_1
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_1_2
procedure :: pl_cmd_const_ptr_integer_1_2
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_1_3
procedure :: pl_cmd_const_ptr_integer_1_3
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_1_4
procedure :: pl_cmd_const_ptr_integer_1_4
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_2_0
procedure :: pl_cmd_const_ptr_integer_2_0
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_2_1
procedure :: pl_cmd_const_ptr_integer_2_1
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_2_2
procedure :: pl_cmd_const_ptr_integer_2_2
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_2_3
procedure :: pl_cmd_const_ptr_integer_2_3
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_integer_2_4
procedure :: pl_cmd_const_ptr_integer_2_4
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_0_0
procedure :: pl_cmd_const_ptr_real_0_0
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_0_1
procedure :: pl_cmd_const_ptr_real_0_1
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_0_2
procedure :: pl_cmd_const_ptr_real_0_2
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_0_3
procedure :: pl_cmd_const_ptr_real_0_3
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_0_4
procedure :: pl_cmd_const_ptr_real_0_4
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_1_0
procedure :: pl_cmd_const_ptr_real_1_0
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_1_1
procedure :: pl_cmd_const_ptr_real_1_1
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_1_2
procedure :: pl_cmd_const_ptr_real_1_2
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_1_3
procedure :: pl_cmd_const_ptr_real_1_3
generic, public :: cmd_const_ptr => pl_cmd_const_ptr_real_1_4
procedure :: pl_cmd_const_ptr_real_1_4
generic, public :: cmd_val => pl_cmd_val_char
procedure :: pl_cmd_val_char
generic, public :: cmd_ptr => pl_cmd_ptr_c
procedure :: pl_cmd_ptr_c
generic, public :: cmd_const_ptr => pl_cmd_ptr_c
procedure :: pl_cmd_const_ptr_c
procedure, public :: finalize => pl_finalize
procedure, public :: incref => pl_incref
procedure, public :: decref => pl_decref
generic, public :: assignment(=) => pl_assign
final :: pl_destructor
procedure, public :: valid => pl_valid
procedure, public :: use_count => pl_use_count
procedure :: pl_assign
end type plumed
! this type holds the information associated to a thrown exception
type :: plumed_error
integer :: code=0
character(len = :), allocatable :: what
end type plumed_error
! now there are interfaces to some of the classic C functions, only used internally
interface
function cplumed_create() bind(C,name="plumed_create")
import
type(cplumed) :: cplumed_create
end function cplumed_create
end interface
interface
function cplumed_create_dlopen(path) bind(C,name="plumed_create_dlopen")
import
character(kind=c_char), intent(in) :: path(*)
type(cplumed) :: cplumed_create_dlopen
end function cplumed_create_dlopen
end interface
interface
function cplumed_create_reference(p) bind(C,name="plumed_create_reference")
import
type(cplumed), value :: p
type(cplumed) :: cplumed_create_reference
end function cplumed_create_reference
end interface
interface
subroutine cplumed_finalize(p) bind(C,name="plumed_finalize")
import
type(cplumed), value :: p
end subroutine cplumed_finalize
end interface
interface
function cplumed_installed() bind(C,name="plumed_installed")
import
integer(kind=c_int) :: cplumed_installed
end function cplumed_installed
end interface
interface
function cplumed_valid(p) bind(C,name="plumed_valid")
import
type(cplumed), value :: p
integer(kind=c_int) :: cplumed_valid
end function cplumed_valid
end interface
interface
function cplumed_use_count(p) bind(C,name="plumed_use_count")
import
type(cplumed), value :: p
integer(kind=c_int) :: cplumed_use_count
end function cplumed_use_count
end interface
! here the interfaces to C functions to construct the plumed_safeptr object
interface
function plumed_f_safeptr_ptr(val,nelem,pass_shape,flags,opt) bind(C)
import
type(c_ptr), value :: val
integer(kind=c_size_t), value :: nelem
integer(kind=c_size_t) :: pass_shape(*)
integer(kind=c_size_t), value :: flags
type(c_ptr), value :: opt
type(cplumed_safeptr) :: plumed_f_safeptr_ptr
end function plumed_f_safeptr_ptr
end interface
interface
function plumed_f_safeptr_char(val,nelem,pass_shape,flags,opt) bind(C)
import
type(c_ptr), value :: val
integer(kind=c_size_t), value :: nelem
integer(kind=c_size_t) :: pass_shape(*)
integer(kind=c_size_t), value :: flags
type(c_ptr), value :: opt
type(cplumed_safeptr) :: plumed_f_safeptr_char
end function plumed_f_safeptr_char
end interface
interface
function plumed_f_safeptr_int(val,nelem,pass_shape,flags,opt) bind(C)
import
type(c_ptr), value :: val
integer(kind=c_size_t), value :: nelem
integer(kind=c_size_t) :: pass_shape(*)
integer(kind=c_size_t), value :: flags
type(c_ptr), value :: opt
type(cplumed_safeptr) :: plumed_f_safeptr_int
end function plumed_f_safeptr_int
end interface
interface
function plumed_f_safeptr_short(val,nelem,pass_shape,flags,opt) bind(C)
import
type(c_ptr), value :: val
integer(kind=c_size_t), value :: nelem
integer(kind=c_size_t) :: pass_shape(*)
integer(kind=c_size_t), value :: flags
type(c_ptr), value :: opt
type(cplumed_safeptr) :: plumed_f_safeptr_short
end function plumed_f_safeptr_short
end interface
interface
function plumed_f_safeptr_long(val,nelem,pass_shape,flags,opt) bind(C)
import
type(c_ptr), value :: val
integer(kind=c_size_t), value :: nelem
integer(kind=c_size_t) :: pass_shape(*)
integer(kind=c_size_t), value :: flags
type(c_ptr), value :: opt
type(cplumed_safeptr) :: plumed_f_safeptr_long
end function plumed_f_safeptr_long
end interface
interface
function plumed_f_safeptr_float(val,nelem,pass_shape,flags,opt) bind(C)
import
type(c_ptr), value :: val
integer(kind=c_size_t), value :: nelem
integer(kind=c_size_t) :: pass_shape(*)
integer(kind=c_size_t), value :: flags
type(c_ptr), value :: opt
type(cplumed_safeptr) :: plumed_f_safeptr_float
end function plumed_f_safeptr_float
end interface
interface
function plumed_f_safeptr_double(val,nelem,pass_shape,flags,opt) bind(C)
import
type(c_ptr), value :: val
integer(kind=c_size_t), value :: nelem
integer(kind=c_size_t) :: pass_shape(*)
integer(kind=c_size_t), value :: flags
type(c_ptr), value :: opt
type(cplumed_safeptr) :: plumed_f_safeptr_double
end function plumed_f_safeptr_double
end interface
contains
! this is a callback function.
! notice that it ends up in global namespace (no protection for being a module function!)
! be careful with name thus
subroutine plumed_f_f08_eh(error_ptr,code,what_ptr,opt_ptr) bind(C)
type(c_ptr), value :: error_ptr
integer(kind=c_int), value :: code
type(c_ptr), value :: what_ptr
type(c_ptr), value :: opt_ptr
type(plumed_error), pointer :: error
character(len=1, kind=C_CHAR), pointer :: p_chars(:)
integer :: i,j
call c_f_pointer(error_ptr,error)
error%code=code
if (.not. C_associated(what_ptr)) then
error%what=""
else
call C_F_pointer(what_ptr, p_chars, [huge(0)])
do i = 1, huge(0)
if (p_chars(i) == C_NULL_CHAR) exit
enddo
allocate(character(i-1) :: error%what)
do j = 1,i-1
error%what(j:j)=p_chars(j)
enddo
endif
end subroutine plumed_f_f08_eh
! we then define all the functions needed for overloading
subroutine plumed_f_cmd_ptr(p,key,val,const,nocopy,error)
type(cplumed), intent(in) :: p
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), intent(in) :: val
logical, intent(in) :: const
logical, intent(in) :: nocopy
type(plumed_error), optional, intent(out) :: error
type(plumed_error), target :: myerror
integer(kind=c_size_t) :: pass_shape(1)
integer(kind=c_size_t) :: flags
type(cplumed_nothrow_handler) :: nothrow
integer(kind=c_size_t) :: nelem
pass_shape=[0]
nelem=0
flags=flags_ptr
if (const) flags=flags_const_ptr
if (nocopy) flags=flags+flags_nocopy
nothrow%ptr=c_loc(myerror)
if(present(error)) then
nothrow%handler=c_funloc(plumed_f_f08_eh)
else
nothrow%handler=c_null_funptr
endif
call plumed_cmd_safe_nothrow(p,key, &
plumed_f_safeptr_ptr(val,nelem,pass_shape,flags,c_null_ptr),nothrow)
if(present(error)) then
error=myerror
endif
end subroutine plumed_f_cmd_ptr
subroutine plumed_f_cmd_char(p,key, val, const, nocopy, error)
type(cplumed), intent(in) :: p
character(kind=c_char,len=*), intent(in) :: key
character(kind=c_char,len=*), target, intent(in) :: val
logical, intent(in) :: const
logical, intent(in) :: nocopy
type(plumed_error), optional, intent(out) :: error
type(plumed_error), target :: myerror
integer(kind=c_size_t) :: pass_shape(2)
integer(kind=c_size_t) :: flags
type(cplumed_nothrow_handler) :: nothrow
integer(kind=c_size_t) :: nelem
pass_shape=[len(val) ,0]
nelem=0
flags=flags_ptr
if (const) flags=flags_const_ptr
if (nocopy) flags=flags + flags_nocopy
nothrow%ptr=c_loc(myerror)
if (present(error)) then
nothrow%handler=c_funloc(plumed_f_f08_eh)
else
nothrow%handler=c_null_funptr
endif
call plumed_cmd_safe_nothrow(p,key, &
plumed_f_safeptr_char(c_loc(val),nelem,pass_shape,flags,c_null_ptr),nothrow)
if(present(error)) then
error=myerror
endif
end subroutine plumed_f_cmd_char
subroutine plumed_f_cmd_integer_0(p, key, valptr, valshape, const, nocopy, error)
type(cplumed), intent(in) :: p
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), intent(in) :: valptr
integer, intent(in) :: valshape(:)
logical, intent(in) :: const
logical, intent(in) :: nocopy
type(plumed_error), optional, intent(out) :: error
type(plumed_error), target :: myerror
integer(kind=c_size_t) :: flags
type(cplumed_nothrow_handler) :: nothrow
integer(kind=c_size_t) :: nelem
integer(kind=c_size_t) :: pass_shape(max(size(valshape) + 1, 2))
if (size(valshape) == 0) then
pass_shape(:) = [1, 0]
else
pass_shape(:) = [valshape(size(valshape):1:-1), 0]
endif
nelem=0
flags = flags_ptr
if (const) flags = flags_const_ptr
if(nocopy) flags = flags + flags_nocopy
nothrow%ptr = c_loc(myerror)
if(present(error)) then
nothrow%handler = c_funloc(plumed_f_f08_eh)
else
nothrow%handler=c_null_funptr
endif
call plumed_cmd_safe_nothrow(p, key, &
plumed_f_safeptr_int(valptr, nelem, pass_shape, flags, c_null_ptr), nothrow)
if(present(error)) then
error = myerror
endif
end subroutine plumed_f_cmd_integer_0
subroutine plumed_f_cmd_integer_1(p, key, valptr, valshape, const, nocopy, error)
type(cplumed), intent(in) :: p
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), intent(in) :: valptr
integer, intent(in) :: valshape(:)
logical, intent(in) :: const
logical, intent(in) :: nocopy
type(plumed_error), optional, intent(out) :: error
type(plumed_error), target :: myerror
integer(kind=c_size_t) :: flags
type(cplumed_nothrow_handler) :: nothrow
integer(kind=c_size_t) :: nelem
integer(kind=c_size_t) :: pass_shape(max(size(valshape) + 1, 2))
if (size(valshape) == 0) then
pass_shape(:) = [1, 0]
else
pass_shape(:) = [valshape(size(valshape):1:-1), 0]
endif
nelem=0
flags = flags_ptr
if (const) flags = flags_const_ptr
if(nocopy) flags = flags + flags_nocopy
nothrow%ptr = c_loc(myerror)
if(present(error)) then
nothrow%handler = c_funloc(plumed_f_f08_eh)
else
nothrow%handler=c_null_funptr
endif
call plumed_cmd_safe_nothrow(p, key, &
plumed_f_safeptr_short(valptr, nelem, pass_shape, flags, c_null_ptr), nothrow)
if(present(error)) then
error = myerror
endif
end subroutine plumed_f_cmd_integer_1
subroutine plumed_f_cmd_integer_2(p, key, valptr, valshape, const, nocopy, error)
type(cplumed), intent(in) :: p
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), intent(in) :: valptr
integer, intent(in) :: valshape(:)
logical, intent(in) :: const
logical, intent(in) :: nocopy
type(plumed_error), optional, intent(out) :: error
type(plumed_error), target :: myerror
integer(kind=c_size_t) :: flags
type(cplumed_nothrow_handler) :: nothrow
integer(kind=c_size_t) :: nelem
integer(kind=c_size_t) :: pass_shape(max(size(valshape) + 1, 2))
if (size(valshape) == 0) then
pass_shape(:) = [1, 0]
else
pass_shape(:) = [valshape(size(valshape):1:-1), 0]
endif
nelem=0
flags = flags_ptr
if (const) flags = flags_const_ptr
if(nocopy) flags = flags + flags_nocopy
nothrow%ptr = c_loc(myerror)
if(present(error)) then
nothrow%handler = c_funloc(plumed_f_f08_eh)
else
nothrow%handler=c_null_funptr
endif
call plumed_cmd_safe_nothrow(p, key, &
plumed_f_safeptr_long(valptr, nelem, pass_shape, flags, c_null_ptr), nothrow)
if(present(error)) then
error = myerror
endif
end subroutine plumed_f_cmd_integer_2
subroutine plumed_f_cmd_real_0(p, key, valptr, valshape, const, nocopy, error)
type(cplumed), intent(in) :: p
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), intent(in) :: valptr
integer, intent(in) :: valshape(:)
logical, intent(in) :: const
logical, intent(in) :: nocopy
type(plumed_error), optional, intent(out) :: error
type(plumed_error), target :: myerror
integer(kind=c_size_t) :: flags
type(cplumed_nothrow_handler) :: nothrow
integer(kind=c_size_t) :: nelem
integer(kind=c_size_t) :: pass_shape(max(size(valshape) + 1, 2))
if (size(valshape) == 0) then
pass_shape(:) = [1, 0]
else
pass_shape(:) = [valshape(size(valshape):1:-1), 0]
endif
nelem=0
flags = flags_ptr
if (const) flags = flags_const_ptr
if(nocopy) flags = flags + flags_nocopy
nothrow%ptr = c_loc(myerror)
if(present(error)) then
nothrow%handler = c_funloc(plumed_f_f08_eh)
else
nothrow%handler=c_null_funptr
endif
call plumed_cmd_safe_nothrow(p, key, &
plumed_f_safeptr_float(valptr, nelem, pass_shape, flags, c_null_ptr), nothrow)
if(present(error)) then
error = myerror
endif
end subroutine plumed_f_cmd_real_0
subroutine plumed_f_cmd_real_1(p, key, valptr, valshape, const, nocopy, error)
type(cplumed), intent(in) :: p
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), intent(in) :: valptr
integer, intent(in) :: valshape(:)
logical, intent(in) :: const
logical, intent(in) :: nocopy
type(plumed_error), optional, intent(out) :: error
type(plumed_error), target :: myerror
integer(kind=c_size_t) :: flags
type(cplumed_nothrow_handler) :: nothrow
integer(kind=c_size_t) :: nelem
integer(kind=c_size_t) :: pass_shape(max(size(valshape) + 1, 2))
if (size(valshape) == 0) then
pass_shape(:) = [1, 0]
else
pass_shape(:) = [valshape(size(valshape):1:-1), 0]
endif
nelem=0
flags = flags_ptr
if (const) flags = flags_const_ptr
if(nocopy) flags = flags + flags_nocopy
nothrow%ptr = c_loc(myerror)
if(present(error)) then
nothrow%handler = c_funloc(plumed_f_f08_eh)
else
nothrow%handler=c_null_funptr
endif
call plumed_cmd_safe_nothrow(p, key, &
plumed_f_safeptr_double(valptr, nelem, pass_shape, flags, c_null_ptr), nothrow)
if(present(error)) then
error = myerror
endif
end subroutine plumed_f_cmd_real_1
! this is a soft wrapper to a C function
function plumed_installed() result(res)
logical :: res
res=cplumed_installed()>0
end function plumed_installed
! this is the constructor
impure elemental subroutine plumed_create(this,kernel)
type(plumed), intent(out) :: this
character(len=*), intent(in), optional :: kernel
if(present(kernel)) then
this%handle=cplumed_create_dlopen(kernel // c_null_char)
else
this%handle=cplumed_create()
endif
this%initialized=.true.
end subroutine plumed_create
! then we define all member functions
impure elemental subroutine pl_finalize(this)
class(plumed), intent(inout) :: this
if(this%initialized) then
call cplumed_finalize(this%handle)
this%initialized=.false.
endif
end subroutine pl_finalize
impure elemental subroutine pl_incref(this)
class(plumed), intent(inout) :: this
type(cplumed) :: that
if(.not.this%initialized) then
call plumed_create(this)
endif
that=cplumed_create_reference(this%handle)
end subroutine pl_incref
impure elemental subroutine pl_decref(this,to)
class(plumed), intent(inout) :: this
integer, optional, intent(in) :: to
if(.not.this%initialized) then
call plumed_create(this)
endif
if(present(to)) then
do while(this%use_count()>to)
call cplumed_finalize(this%handle)
end do
else
call cplumed_finalize(this%handle)
endif
end subroutine pl_decref
! "impure elemental" needed for the destructor to work on arrays
impure elemental subroutine pl_destructor(this)
type(plumed), intent(inout) :: this
call this%finalize()
end subroutine pl_destructor
impure elemental function pl_valid(this) result(valid)
class(plumed), intent(inout) :: this
logical :: valid
integer(c_int) :: i
if(.not.this%initialized) then
call plumed_create(this)
endif
valid=cplumed_valid(this%handle)>0
end function pl_valid
impure elemental function pl_use_count(this) result(use_count)
class(plumed), intent(inout) :: this
integer(c_int) :: use_count
if(.not.this%initialized) then
call plumed_create(this)
endif
use_count=cplumed_use_count(this%handle)
end function pl_use_count
impure elemental subroutine pl_assign(this,that)
class(plumed),intent(out) :: this
class(plumed),intent(in) :: that
if(that%initialized) then
this%handle=cplumed_create_reference(that%handle)
this%initialized=.true.
endif
end subroutine pl_assign
impure elemental subroutine pl_cmd(this,key,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
type(plumed_error), optional, intent(out) :: error
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_ptr(this%handle, key // c_null_char, c_null_ptr, const=.false.,&
& nocopy=.false., error=error)
end subroutine pl_cmd
subroutine pl_cmd_val_char(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
character(kind=c_char,len=*), intent(in) :: val
type(plumed_error), optional, intent(out) :: error
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_char(this%handle,key // c_null_char,val // c_null_char, &
& const=.true., nocopy=.true., error=error)
end subroutine pl_cmd_val_char
subroutine pl_cmd_ptr_c(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), value :: val
type(plumed_error), optional, intent(out) :: error
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_ptr(this%handle,key // c_null_char, val, const=.false., nocopy=.false.,&
& error=error)
end subroutine pl_cmd_ptr_c
subroutine pl_cmd_const_ptr_c(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
type(c_ptr), value :: val
type(plumed_error), optional, intent(out) :: error
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_ptr(this%handle,key // c_null_char,val, const=.true., nocopy=.false.,&
& error=error)
end subroutine pl_cmd_const_ptr_c
subroutine pl_cmd_val_integer_0_0(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), target, intent(in) :: val
type(plumed_error), optional, intent(out) :: error
integer :: valshape(0)
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.true., nocopy=.true., error=error)
end subroutine pl_cmd_val_integer_0_0
subroutine pl_cmd_ref_integer_0_0(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), target, intent(inout) :: val
type(plumed_error), optional, intent(out) :: error
integer :: valshape(0)
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.false., nocopy=.true., error=error)
end subroutine pl_cmd_ref_integer_0_0
subroutine pl_cmd_ptr_integer_0_0(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), pointer, intent(in) :: val
type(plumed_error), optional, intent(out) :: error
integer :: valshape(0)
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.false., nocopy=.false., error=error)
end subroutine pl_cmd_ptr_integer_0_0
subroutine pl_cmd_const_ptr_integer_0_0(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), pointer, intent(in) :: val
type(plumed_error), optional, intent(out) :: error
integer :: valshape(0)
if(.not.this%initialized) then
call plumed_create(this)
endif
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.true., nocopy=.false., error=error)
end subroutine pl_cmd_const_ptr_integer_0_0
subroutine pl_cmd_val_integer_0_1(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), contiguous, target, intent(in) :: val(:)
type(plumed_error), optional, intent(out) :: error
integer :: valshape(1)
if(.not.this%initialized) then
call plumed_create(this)
endif
valshape = shape(val)
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.true., nocopy=.true., error=error)
end subroutine pl_cmd_val_integer_0_1
subroutine pl_cmd_ref_integer_0_1(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), contiguous, target, intent(inout) :: val(:)
type(plumed_error), optional, intent(out) :: error
integer :: valshape(1)
if(.not.this%initialized) then
call plumed_create(this)
endif
valshape = shape(val)
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.false., nocopy=.true., error=error)
end subroutine pl_cmd_ref_integer_0_1
subroutine pl_cmd_ptr_integer_0_1(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), pointer, intent(in) :: val(:)
type(plumed_error), optional, intent(out) :: error
integer :: valshape(1)
if(.not.this%initialized) then
call plumed_create(this)
endif
if (.not. is_contiguous(val)) then
if (present(error)) then
error%code=20300
error%what="Obtained pointer to non-contiguous data"
return
else
error stop "Obtained pointer to non-contiguous data"
end if
end if
valshape = shape(val)
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.false., nocopy=.false., error=error)
end subroutine pl_cmd_ptr_integer_0_1
subroutine pl_cmd_const_ptr_integer_0_1(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), pointer, intent(in) :: val(:)
type(plumed_error), optional, intent(out) :: error
integer :: valshape(1)
if(.not.this%initialized) then
call plumed_create(this)
endif
if (.not. is_contiguous(val)) then
if (present(error)) then
error%code=20300
error%what="Obtained const pointer to non-contiguous data"
return
else
error stop "Obtained const pointer to non-contiguous data"
end if
end if
valshape = shape(val)
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.true., nocopy=.false., error=error)
end subroutine pl_cmd_const_ptr_integer_0_1
subroutine pl_cmd_val_integer_0_2(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), contiguous, target, intent(in) :: val(:,:)
type(plumed_error), optional, intent(out) :: error
integer :: valshape(2)
if(.not.this%initialized) then
call plumed_create(this)
endif
valshape = shape(val)
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.true., nocopy=.true., error=error)
end subroutine pl_cmd_val_integer_0_2
subroutine pl_cmd_ref_integer_0_2(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), contiguous, target, intent(inout) :: val(:,:)
type(plumed_error), optional, intent(out) :: error
integer :: valshape(2)
if(.not.this%initialized) then
call plumed_create(this)
endif
valshape = shape(val)
call plumed_f_cmd_integer_0(this%handle, key // c_null_char, c_loc(val), valshape,&
& const=.false., nocopy=.true., error=error)
end subroutine pl_cmd_ref_integer_0_2
subroutine pl_cmd_ptr_integer_0_2(this,key,val,error)
class(plumed), intent(inout) :: this ! inout to allow for initialization
character(kind=c_char,len=*), intent(in) :: key
integer(KIND=c_int), pointer, intent(in) :: val(:,:)
type(plumed_error), optional, intent(out) :: error
integer :: valshape(2)
if(.not.this%initialized) then
call plumed_create(this)