-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtst_passreturn_f03.f90
229 lines (176 loc) · 4.9 KB
/
tst_passreturn_f03.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
module m_array
implicit none
integer, parameter :: dp = selected_real_kind(p=14)
save
integer :: na_u
real(dp), allocatable :: xa(:,:), fa(:,:)
contains
subroutine aalloc(na)
integer, intent(in) :: na
allocate(xa(3,na),fa(3,na))
xa = 1._dp
fa = 1._dp
na_u = na
end subroutine aalloc
subroutine print_a()
print '(3000(3(tr1,f6.2),/))',xa
print '(3000(3(tr1,f6.2),/))',fa
end subroutine print_a
end module m_array
program main
use flook
use m_array
implicit none
character(*), parameter :: fortran_static_lua = '&
--[[ Print out hello statement directly, and immediately when &
Using the LUA interface &
--]] &
print("Hello from FORTRAN") &
flook = {} &
function flook.print(arg) &
print(arg) &
end'
character(*), parameter :: fortran_lua_crt_mat = '&
--[[ &
Initialize a lua matrix &
--]] &
function flook.crt_mat(a,b,c,d,e,f) &
if a == nil then &
return 0. &
else &
local t = {} &
for i = 1 , a do &
t[i] = flook.crt_mat(b,c,d,e,f) &
end &
return t &
end &
end'
character(*), parameter :: fortran_lua_geom = '&
geom = { &
unit = "Bohr", &
size = 0, &
--[[ &
Create initialization function for &
setting up the different variables &
needed for communication &
--]] &
init = function (self) &
self["cell"] = flook.crt_mat(3,3) &
setmetatable(self["cell"],self) &
for _,v in pairs({"xa"}) do &
self[v] = flook.crt_mat(3,self.size) &
end &
self["fa"] = {} &
setmetatable(self["xa"],self) &
setmetatable(self["fa"],self) &
end, &
print = function (self,msg) print("") &
for _,v in pairs({"xa","fa"}) do &
if #msg > 0 then print(msg .. " " .. v) end &
print(self[v].unit) &
for ia,xyz in pairs(self[v]) do &
if type(xyz) == "table" then &
a = "" &
for _,x in pairs(xyz) do a = a .. " " .. x end &
print(a) &
end &
end &
end &
print("") &
end, &
update_atoms = update_atoms, &
} &
geom.__index = geom'
! the lua embedded state
type(luaState) :: lua
! Allocate arrays
call aalloc(3)
print '(/,a)','Fortran initialized'
call print_a()
! Open new lua state.
call lua%init()
! Register some fortran function to a lua function call
call lua%register("update_atoms", array_size_pass)
call lua%register("get_atom_info", array_pass)
call lua%register("return_atom_info", array_return)
! Add standard code to the parser
call lua%run(code = fortran_static_lua)
call lua%run(code = fortran_lua_crt_mat)
call lua%run(code = fortran_lua_geom)
! Updated number of atoms and initialize the geometry
! to be ready to be parsed
call lua%run(code = 'geom.update_atoms() geom:init()')
call lua%run('tst_passreturn.lua')
call lua%close()
print '(/,a)','LUA parsed'
call print_a()
contains
! Create function to be called by LUA
function array_size_pass(state) result(npush) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_int
use flook
use m_array
! Define the state
type(c_ptr), value :: state
! Define the in/out
integer(c_int) :: npush
type(luaState) :: lua
type(luaTbl) :: tbl
! Copy the c-pointer to the lua-state
call lua%init(state)
! Open table named
tbl = lua%table('geom')
call tbl%set('size', na_u)
call tbl%set('tmp', 1.5_8)
call tbl%close()
npush = 0
end function array_size_pass
! Create function to be called by LUA
function array_pass(state) result(npush) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_int
use flook
use m_array
! Define the state
type(c_ptr), value :: state
! Define the in/out
integer(c_int) :: npush
type(luaState) :: lua
type(luaTbl) :: tbl
! Copy the c-pointer to the lua-state
call lua%init(state)
! Open table named
tbl = lua%table('geom.xa')
! Within this table we pass the xa value
call tbl%set(xa)
call tbl%close_open('fa')
call tbl%set(fa)
call tbl%close(.true.)
! we have no return values
npush = 0
end function array_pass
! Create function to be called by LUA
function array_return(state) result(npush) bind(c)
use, intrinsic :: iso_c_binding, only: c_ptr, c_int
use flook
use m_array
! Define the state
type(c_ptr), value :: state
! Define the in/out
integer(c_int) :: npush
type(luaState) :: lua
type(luaTbl) :: tbl
! Copy the c-pointer to the lua-state
call lua%init(state)
! Open table named
tbl = lua%table('geom')
! Within this table we pass the xa value
call tbl%open('xa')
call tbl%get(xa)
call tbl%close()
call tbl%get('fa',fa)
! Ensure the entire table has been closed
call tbl%close(.true.)
! we have no return values
npush = 0
end function array_return
end program main