-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtst_aot_passreturn.f90
310 lines (242 loc) · 7.37 KB
/
tst_aot_passreturn.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
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 flu_binding
use aotus_module
use m_array
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'
integer :: err
character(255) :: err_string
! the lua embedded state
type(flu_State) :: L
! Allocate arrays
call aalloc(3)
print '(/,a)','Fortran initialized'
call print_a()
! Open new lua state.
L = fluL_newstate()
! Register some fortran function to a lua function call
call flu_register(L, "update_atoms", array_size_pass)
call flu_register(L, "get_atom_info", array_pass)
call flu_register(L, "return_atom_info", array_return)
! Add standard code to the parser
call open_config_chunk(L, fortran_static_lua)
call open_config_chunk(L, fortran_lua_crt_mat)
call open_config_chunk(L, fortran_lua_geom)
! Updated number of atoms and initialize the geometry
! to be ready to be parsed
call open_config_chunk(L, 'geom.update_atoms() geom:init()')
call open_config_file(L, 'tst_passreturn.lua', err, err_string)
if ( err /= 0 ) print *,'Error: ',trim(err_string)
call flu_close(L)
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 flu_binding
use aotus_module
use aot_table_module
use aotus_module
use m_array
! Define the state
type(c_ptr), value :: state
! Define the in/out
integer(c_int) :: npush
! Local variables
integer :: geom
type(flu_State) :: L
! Copy the c-pointer to the lua-state
L = flu_copyptr(state)
geom = 0
! Open table named
call aot_table_open(L, thandle = geom, key='geom' )
call aot_table_set_val(na_u,L,thandle=geom, key = 'size')
call aot_table_close(L,geom)
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 flu_binding
use aotus_module
use aot_table_module
use aotus_module
use m_array
! Define the state
type(c_ptr), value :: state
! Define the in/out
integer(c_int) :: npush
! Local variables
integer :: geom
type(flu_State) :: L
! Copy the c-pointer to the lua-state
L = flu_copyptr(state)
geom = 0
! Open table named
call aot_table_open(L, thandle = geom, key='geom' )
! Within this table we pass the xa value
call pass(L,geom,'xa',na_u,xa)
call pass(L,geom,'fa',na_u,fa)
call aot_table_close(L,geom)
! we have no return values
npush = 0
end function array_pass
subroutine pass(L,h,k,n,a)
use aot_table_module
type(flu_State), intent(in) :: L
character(len=*), intent(in) :: k
integer, intent(in) :: h,n
real(dp), intent(in) :: a(3,n)
integer :: t, xyz, i
if ( .not. aot_exists(L,h, key = k) ) then
call aot_table_open(L, thandle = t )
call aot_table_set_top(L, h, key = k)
call aot_table_close(L, t)
end if
call aot_table_open(L, h, thandle = t, key = k )
do i = 1 , n
if ( .not. aot_exists(L, t , pos = i) ) then
call aot_table_open(L, thandle = xyz)
call aot_table_set_top(L, t, pos = i)
call aot_table_close(L, xyz)
end if
call aot_table_open(L, t, thandle=xyz, pos = i)
call aot_table_set_val(a(1,i),L,thandle=xyz, pos = 1)
call aot_table_set_val(a(2,i),L,thandle=xyz, pos = 2)
call aot_table_set_val(a(3,i),L,thandle=xyz, pos = 3)
call aot_table_close(L, xyz)
end do
! Attach the new table to the main handle
! This has to be done before it is closed (else the
! stack is altered)
call aot_table_close(L, t)
end subroutine 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 flu_binding
use aotus_module
use aot_table_module
use aot_fun_module
use aotus_module
use m_array
! Define the state
type(c_ptr), value :: state
! Define the in/out
integer(c_int) :: npush
! Local variables
integer :: geom
type(flu_State) :: L
! Copy the c-pointer to the lua-state
L = flu_copyptr(state)
! Open table named
call aot_table_open(L, thandle = geom, key='geom' )
! Within this table we pass the xa value
call return(L,geom,'xa',na_u,xa)
call return(L,geom,'fa',na_u,fa)
call aot_table_close(L,geom)
npush = 0
end function array_return
subroutine return(L,h,k,n,a)
use aot_table_module
use aot_err_module
type(flu_State), intent(inout) :: L
character(len=*), intent(in) :: k
integer, intent(in) :: h,n
real(dp), intent(out) :: a(3,n)
integer :: t, xyz, i
integer :: err
character(255) :: err_string
t = 0
xyz = 0
call aot_table_open(L, h, thandle = t, key = k )
do i = 1 , n
call aot_table_open(L, t, xyz, pos = i)
call aot_table_get_val(a(1,i),err,L,thandle=xyz, pos = 1)
call aot_err_handler(L,err,'None',ErrString=err_string)
if ( err/=0 ) print*,'Error ',k,err,trim(err_string)
call aot_table_get_val(a(2,i),err,L,thandle=xyz, pos = 2)
call aot_table_get_val(a(3,i),err,L,thandle=xyz, pos = 3)
call aot_table_close(L, xyz)
end do
call aot_table_close(L, t)
end subroutine return
end program main