Eötvös Quantum Utilities  v4.9.146
Providing the Horsepowers in the Quantum Realm
EQuUs_C.f90
Go to the documentation of this file.
1 !======================================================================
2 ! Wrapper module to support the conversion of Fortran types into C compatible types.
3 ! This file is part of the EQuUs package. (for details see: http://eqt.elte.hu/equus/home)
4 ! Copyright (C) 2016 Peter Rakyta, Ph.D.
5 !
6 ! This program is free software: you can redistribute it and/or modify
7 ! it under the terms of the GNU General Public License as published by
8 ! the Free Software Foundation, either version 3 of the License, or
9 ! (at your option) any later version.
10 !
11 ! This program is distributed in the hope that it will be useful,
12 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ! GNU General Public License for more details.
15 !
16 ! You should have received a copy of the GNU General Public License
17 ! along with this program. If not, see http://www.gnu.org/licenses/.
18 !
19 !======================================================================
20 MODULE equus_c
21 
22  use iso_c_binding
23 
24 
25 integer, parameter, public :: dp = selected_real_kind(14,100)
26 integer, parameter, public :: sp = selected_real_kind(6,30)
27 
28 
29 ! DESCRIPTION:
32 type, public :: eqelement
33 ! Number of dimensions of the element
34  TYPE(c_ptr) :: dim
35 ! Array of dimensions
36  TYPE(c_ptr) :: dims
37 ! C type pointer to the value of the element
38  TYPE(c_ptr) :: value
39 ! MATLAB class id of the value (see http://www.mathworks.com/help/matlab/apiref/mxclassid.html)
40  TYPE(c_ptr) :: classid
41 ! String containing the variable name
42  TYPE(c_ptr) :: elementname
43 end type
44 
45 ! DESCRIPTION:
48 type, public :: eqstruct
49 ! C type pointer pointing to the array of EQuUs element prototypes
50  TYPE(c_ptr) :: fields
51 ! Number of the stored EQuUs element prototypes
52  integer :: numfields
53 end type
54 
55 
56 interface assocelement
57  module procedure associntelement
58  module procedure assocrealelement
59  module procedure associntvectorelement
60  module procedure associntmatrixelement
61  module procedure assocrealvectorelement
62  module procedure assocrealmatrixelement
63  module procedure assoclogicalelement
64  module procedure assocstringelement
65  module procedure assocstringelement2
66  module procedure assocstructelement
67 end interface
68 
69 interface allocateelement
70  module procedure allocateelementarray
71  module procedure allocateelementsingle
72 end interface
73 
74 
75 !****************************************************************
76  CONTAINS
77 !****************************************************************
78 
79 
80 !-------------------------------------------------------------------------------------------------
81 ! DESCRIPTION:
87  subroutine addelement( eq_elements, eq_element )
88  TYPE(eqelement), pointer :: eq_element
89  TYPE(eqelement), pointer :: eq_elements(:)
90  TYPE(eqelement), pointer :: eq_elements_new(:)
91  integer elementNum
92 
93 
94 
95  if (.not.ASSOCIATED(eq_elements)) then
96  allocate(eq_elements(1))
97  eq_elements(1) = eq_element
98  return
99  end if
100 
101  elementnum = SIZE( eq_elements, 1)
102  allocate( eq_elements_new(elementnum+1) )
103 
104  do idx = 1, elementnum
105  eq_elements_new(idx) = eq_elements(idx)
106  end do
107 
108  eq_elements_new(elementnum+1) = eq_element
109 
110  deallocate( eq_elements )
111  allocate( eq_elements(elementnum+1) )
112  do idx = 1, elementnum+1
113  eq_elements(idx) = eq_elements_new(idx)
114  end do
115 
116  deallocate( eq_elements_new )
117 
118  end subroutine addelement
119 
120 
121 !-------------------------------------------------------------------------------------------------
122 ! DESCRIPTION:
128  subroutine allocateelementarray( eq_elements, num )
129  TYPE(eqelement), pointer :: eq_elements(:)
130  integer :: num
131  integer :: idx
132 
133  allocate( eq_elements(num) )
134 
135  do idx = 1, num
136  eq_elements(idx)%elementname = c_loc( null() )
137  eq_elements(idx)%dim = c_loc( null() )
138  eq_elements(idx)%dims = c_loc( null() )
139  eq_elements(idx)%value = c_loc( null() )
140  eq_elements(idx)%classID = c_loc( null() )
141  end do
142 
143  end subroutine allocateelementarray
144 
145 
146 !-------------------------------------------------------------------------------------------------
147 ! DESCRIPTION:
152  subroutine allocateelementsingle( eq_element )
153  TYPE(eqelement), pointer :: eq_element
154 
155  allocate( eq_element )
156 
157  eq_element%elementname = c_loc( null() )
158  eq_element%dim = c_loc( null() )
159  eq_element%dims = c_loc( null() )
160  eq_element%value = c_loc( null() )
161  eq_element%classID = c_loc( null() )
162 
163  end subroutine allocateelementsingle
164 
165 !-------------------------------------------------------------------------------------------------
166 ! DESCRIPTION:
171  subroutine deallocateelement( eq_element )
172  TYPE(eqelement) :: eq_element
173 
174  byte, pointer :: ptr => null()
175 
176  call c_f_pointer( eq_element%elementname, ptr )
177  if (ASSOCIATED(ptr)) then
178  deallocate( ptr )
179  nullify (ptr )
180  end if
181 
182  call c_f_pointer( eq_element%dim, ptr )
183  if (ASSOCIATED(ptr)) then
184  deallocate( ptr )
185  nullify (ptr )
186  end if
187 
188  call c_f_pointer( eq_element%dims, ptr )
189  if (ASSOCIATED(ptr)) then
190  deallocate( ptr )
191  nullify (ptr )
192  end if
193 
194  call c_f_pointer( eq_element%value, ptr )
195  if (ASSOCIATED(ptr)) then
196  deallocate( ptr )
197  nullify (ptr )
198  end if
199 
200  call c_f_pointer( eq_element%classID, ptr )
201  if (ASSOCIATED(ptr)) then
202  deallocate( ptr )
203  nullify (ptr )
204  end if
205 
206  end subroutine deallocateelement
207 
208 
209 !-------------------------------------------------------------------------------------------------
210 ! DESCRIPTION:
217  subroutine assocstructelement( eq_element, elementname, value )
218 
219  TYPE(eqelement) :: eq_element
220  character(len=*) :: elementname
221  character(len(elementname)+1), pointer :: pelementname
222  TYPE(eqstruct) :: value
223  integer, pointer :: dims(:)
224  integer, pointer :: dim(:)
225  integer, pointer :: classID(:)
226 
227  allocate( dims(1), stat=iostat)
228  dims(1) = 1
229 
230  allocate( dim(1), stat=iostat)
231  dim(1) = 1
232  allocate( classid(1), stat=iostat)
233  classid(1) = 2
234 
235 
236  allocate(pelementname)
237  pelementname = trim(elementname)//c_null_char
238 
239  eq_element%elementname = c_loc(pelementname)
240  eq_element%dim = c_loc(dim(1))
241  eq_element%dims = c_loc( dims(1) )
242  eq_element%value = c_loc(value)
243  eq_element%classID = c_loc(classid(1))
244 
245  end subroutine assocstructelement
246 
247 !-------------------------------------------------------------------------------------------------
248 ! DESCRIPTION:
255  subroutine assocstringelement( eq_element, elementname, value )
256 
257  TYPE(eqelement) :: eq_element
258  character(len=*) :: elementname
259  character(len(elementname)+1), pointer :: pelementname
260  character(len=*), pointer :: value(:)
261  TYPE(c_ptr), pointer :: values(:)
262  integer, pointer :: dims(:)
263  integer, pointer :: dim(:)
264  integer, pointer :: classID(:)
265  integer idx
266 
267  allocate( dims(1), stat=iostat)
268  dims(1) = SIZE( value,1)
269 
270  allocate( dim(1), stat=iostat)
271  dim(1) = 1
272  allocate( classid(1), stat=iostat)
273  classid(1) = 4
274 
275  allocate(values( dims(1) ))
276  do idx = 1, dims(1)
277  value(idx) = trim(value(idx))//c_null_char
278  values(idx) = c_loc( value(idx) )
279  end do
280 
281  allocate(pelementname)
282  pelementname = trim(elementname)//c_null_char
283 
284  eq_element%elementname = c_loc(pelementname)
285  eq_element%dim = c_loc(dim(1))
286  eq_element%dims = c_loc( dims(1) )
287  eq_element%value = c_loc( values(1) )
288  eq_element%classID = c_loc(classid(1))
289 
290  end subroutine assocstringelement
291 
292 
293 
294 !-------------------------------------------------------------------------------------------------
295 ! DESCRIPTION:
302  subroutine assocstringelement2( eq_element, elementname, value )
303 
304  TYPE(eqelement) :: eq_element
305  character(len=*) :: elementname
306  character(len(elementname)+1), pointer :: pelementname
307  character(len=*) :: value
308  character(len(trim(value))+1), pointer :: value_tmp
309  TYPE(c_ptr), pointer :: values(:)
310  integer, pointer :: dims(:)
311  integer, pointer :: dim(:)
312  integer, pointer :: classID(:)
313  integer idx
314 
315  allocate( dims(1), stat=iostat)
316  dims(1) = 1
317 
318  allocate( dim(1), stat=iostat)
319  dim(1) = 1
320  allocate( classid(1), stat=iostat)
321  classid(1) = 4
322 
323  allocate(values(1))
324  allocate(value_tmp)
325  value_tmp = trim(value)//c_null_char
326  values(1) = c_loc(value_tmp)
327 
328  allocate(pelementname)
329  pelementname = trim(elementname)//c_null_char
330 
331  eq_element%elementname = c_loc(pelementname)
332  eq_element%dim = c_loc(dim(1))
333  eq_element%dims = c_loc( dims(1) )
334  eq_element%value = c_loc( values(1) )
335  eq_element%classID = c_loc(classid(1))
336 
337  end subroutine assocstringelement2
338 
339 
340 !-------------------------------------------------------------------------------------------------
341 ! DESCRIPTION:
348  subroutine assoclogicalelement( eq_element, elementname, value )
349 
350  TYPE(eqelement) :: eq_element
351  character(len=*) :: elementname
352  character(len(elementname)+1), pointer :: pelementname
353  logical :: value
354  integer, pointer :: pvalue(:)
355  integer, pointer :: dims(:)
356  integer, pointer :: dim(:)
357  integer, pointer :: classID(:)
358 
359  allocate( dims(1), stat=iostat)
360  dims(1) = 1
361 
362  allocate( pvalue(1), stat=iostat)
363  if ( value.eqv..true.) then
364  pvalue(1) = 1
365  else
366  pvalue(1) = 0
367  endif
368 
369  allocate( dim(1), stat=iostat)
370  dim(1) = 1
371  allocate( classid(1), stat=iostat)
372  classid(1) = 3
373 
374  allocate(pelementname)
375  pelementname = trim(elementname)//c_null_char
376 
377  eq_element%elementname = c_loc(pelementname)
378  eq_element%dim = c_loc(dim(1))
379  eq_element%dims = c_loc( dims(1) )
380  eq_element%value = c_loc(pvalue(1))
381  eq_element%classID = c_loc(classid(1))
382 
383  end subroutine assoclogicalelement
384 
385 
386 !-------------------------------------------------------------------------------------------------
387 ! DESCRIPTION:
394  subroutine associntelement( eq_element, elementname, value )
395 
396  TYPE(eqelement) :: eq_element
397  character(len=*) :: elementname
398  character(len(elementname)+1), pointer :: pelementname
399  integer :: value
400  integer, pointer :: pvalue(:)
401  integer, pointer :: dims(:)
402  integer, pointer :: dim(:)
403  integer, pointer :: classID(:)
404 
405 
406  allocate( dims(1), stat=iostat)
407  dims(1) = 1
408 
409  allocate(pvalue(1), stat=iostat)
410  pvalue(1) = value
411 
412  allocate( dim(1), stat=iostat)
413  dim(1) = 1
414  allocate( classid(1), stat=iostat)
415  classid(1) = 12
416 
417  allocate(pelementname)
418  pelementname = trim(elementname)//c_null_char
419 
420  eq_element%elementname = c_loc(pelementname)
421  eq_element%dim = c_loc(dim(1))
422  eq_element%dims = c_loc( dims(1) )
423  eq_element%value = c_loc(pvalue(1))
424  eq_element%classID = c_loc(classid(1))
425 
426  end subroutine associntelement
427 
428 
429 !-------------------------------------------------------------------------------------------------
430 ! DESCRIPTION:
437  subroutine assocrealelement( eq_element, elementname, value )
438 
439  TYPE(eqelement) :: eq_element
440  character(len=*) :: elementname
441  character(len(elementname)+1), pointer :: pelementname
442  real(dp) :: value
443  real(dp), pointer :: pvalue
444  integer, pointer :: dims(:)
445  integer, pointer :: dim(:)
446  integer, pointer :: classID(:)
447 
448  allocate( dims(1), stat=iostat)
449  dims(1) = 1
450 
451  allocate(pvalue, stat=iostat)
452  pvalue = value
453 
454  allocate( dim(1), stat=iostat)
455  dim(1) = 1
456  allocate( classid(1), stat=iostat)
457  if ( dp.EQ.8) then
458  classid(1) = 6
459  else
460  print *, 'Undetermined real type !!!!!!!!!!!'
461  classid(1) = 0
462  end if
463 
464  allocate(pelementname)
465  pelementname = trim(elementname)//c_null_char
466 
467  eq_element%elementname = c_loc(pelementname)
468  eq_element%dim = c_loc(dim(1))
469  eq_element%dims = c_loc( dims(1) )
470  eq_element%value = c_loc(pvalue)
471  eq_element%classID = c_loc(classid(1))
472 
473  end subroutine assocrealelement
474 
475 
476 
477 !-------------------------------------------------------------------------------------------------
478 ! DESCRIPTION:
485  subroutine associntvectorelement( eq_element, elementname, value )
486 
487  TYPE(eqelement) :: eq_element
488  character(len=*) :: elementname
489  character(len(elementname)+1), pointer :: pelementname
490  integer, pointer :: value(:)
491  integer, pointer :: dims(:)
492  integer, pointer :: dim(:)
493  integer, pointer :: classID(:)
494 
495 
496  allocate( dims(1), stat=iostat)
497  dims(1) = SIZE(value, 1)
498 
499  allocate( dim(1), stat=iostat)
500  dim(1) = 1
501  allocate( classid(1), stat=iostat)
502  classid(1) = 12
503 
504 
505  allocate(pelementname)
506  pelementname = trim(elementname)//c_null_char
507 
508  eq_element%elementname = c_loc(pelementname)
509  eq_element%dim = c_loc(dim(1))
510  eq_element%dims = c_loc( dims(1) )
511  eq_element%value = c_loc(value(1))
512  eq_element%classID = c_loc(classid(1))
513 
514  end subroutine associntvectorelement
515 
516 
517 !-------------------------------------------------------------------------------------------------
518 ! DESCRIPTION:
525  subroutine associntmatrixelement( eq_element, elementname, value )
526 
527  TYPE(eqelement) :: eq_element
528  character(len=*) :: elementname
529  character(len(elementname)+1), pointer :: pelementname
530  integer, pointer :: value(:,:)
531  integer, pointer :: dims(:)
532  integer, pointer :: dim(:)
533  integer, pointer :: classID(:)
534 
535  allocate( dims(2), stat=iostat)
536  dims(1) = SIZE(value, 1)
537  dims(2) = SIZE(value, 2)
538 
539  allocate( dim(1), stat=iostat)
540  dim(1) = 2
541  allocate( classid(1), stat=iostat)
542  classid(1) = 12
543 
544  allocate(pelementname)
545  pelementname = trim(elementname)//c_null_char
546 
547  eq_element%elementname = c_loc(pelementname)
548  eq_element%dim = c_loc(dim(1))
549  eq_element%dims = c_loc( dims(1) )
550  eq_element%value = c_loc(value(1,1))
551  eq_element%classID = c_loc(classid(1))
552 
553  end subroutine associntmatrixelement
554 
555 
556 !-------------------------------------------------------------------------------------------------
557 ! DESCRIPTION:
564  subroutine assocrealvectorelement( eq_element, elementname, value )
565 
566  TYPE(eqelement) :: eq_element
567  character(len=*) :: elementname
568  character(len(elementname)+1), pointer :: pelementname
569  real(dp), pointer :: value(:)
570  integer, pointer :: dims(:)
571  integer, pointer :: dim(:)
572  integer, pointer :: classID(:)
573 
574  allocate( dims(1), stat=iostat)
575  dims(1) = SIZE(value, 1)
576 
577  allocate( dim(1), stat=iostat)
578  dim(1) = 1
579  allocate( classid(1), stat=iostat)
580  if ( dp.EQ.8) then
581  classid(1) = 6
582  else
583  print *, 'Undetermined real type !!!!!!!!!!!'
584  classid(1) = 0
585  end if
586 
587 
588  allocate(pelementname)
589  pelementname = trim(elementname)//c_null_char
590 
591  eq_element%elementname = c_loc(pelementname)
592  eq_element%dim = c_loc(dim(1))
593  eq_element%dims = c_loc( dims(1) )
594  eq_element%value = c_loc(value(1))
595  eq_element%classID = c_loc(classid(1))
596 
597  end subroutine assocrealvectorelement
598 
599 
600 
601 !-------------------------------------------------------------------------------------------------
602 ! DESCRIPTION:
609  subroutine assocrealmatrixelement( eq_element, elementname, value )
610 
611  TYPE(eqelement) :: eq_element
612  character(len=*) :: elementname
613  character(len(elementname)+1), pointer :: pelementname
614  real(dp), pointer :: value(:,:)
615  integer, pointer :: dims(:)
616  integer, pointer :: dim(:)
617  integer, pointer :: classID(:)
618 
619  allocate( dims(2), stat=iostat)
620 
621  dims(1) = SIZE(value, 1)
622  dims(2) = SIZE(value, 2)
623 
624  allocate( dim(1), stat=iostat)
625  dim(1) = 2
626  allocate( classid(1), stat=iostat)
627  if ( dp.EQ.8) then
628  classid(1) = 6
629  else
630  print *, 'Undetermined real type !!!!!!!!!!!'
631  classid(1) = 0
632  end if
633 
634  allocate(pelementname)
635  pelementname = trim(elementname)//c_null_char
636 
637  eq_element%elementname = c_loc(pelementname)
638  eq_element%dim = c_loc(dim(1))
639  eq_element%dims = c_loc( dims(1) )
640  eq_element%value = c_loc(value(1,1))
641  eq_element%classID = c_loc(classid(1))
642 
643  end subroutine assocrealmatrixelement
644 
645 
646 
647 
648 
649 
650 end module
subroutine associntmatrixelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:526
subroutine assocstringelement2(eq_element, elementname, value)
Definition: EQuUs_C.f90:303
subroutine associntelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:395
subroutine assoclogicalelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:349
subroutine assocrealmatrixelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:610
subroutine deallocateelement(eq_element)
Definition: EQuUs_C.f90:172
subroutine assocstructelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:218
subroutine assocrealelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:438
integer, parameter, public sp
Definition: EQuUs_C.f90:26
subroutine allocateelementsingle(eq_element)
Definition: EQuUs_C.f90:153
subroutine assocstringelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:256
subroutine associntvectorelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:486
subroutine addelement(eq_elements, eq_element)
Definition: EQuUs_C.f90:88
subroutine allocateelementarray(eq_elements, num)
Definition: EQuUs_C.f90:129
subroutine assocrealvectorelement(eq_element, elementname, value)
Definition: EQuUs_C.f90:565
integer, parameter, public dp
Definition: EQuUs_C.f90:25