質問編集履歴

1

追記の作成

2019/05/19 01:15

投稿

syoukera
syoukera

スコア10

test CHANGED
File without changes
test CHANGED
@@ -165,3 +165,207 @@
165
165
 
166
166
 
167
167
  ```
168
+
169
+ # 追記20190519
170
+
171
+ Python上で整数型の値を管理し,Fortranに渡して装置番号として使用するということを考えました.テストのためにPythonからOpen文を含むコードを書いて実行を.結果は以下.
172
+
173
+ |実行環境|結果|
174
+
175
+ |:---|:---|
176
+
177
+ |Pythonで装置番号を定義,Fortranに渡す|Segmentation fault|
178
+
179
+ |Pythonから引数なしでFortranを呼ぶ,Fortranで装置番号を定義|実行可能|
180
+
181
+
182
+
183
+ Fortran上で整数型を定義して渡すうまくいきますが,Pythonで管理しようとするとOPENの過程で怒られました.
184
+
185
+ 参考に使用したコードを置いておきます.
186
+
187
+ ```python
188
+
189
+ import ctypes
190
+
191
+ import numpy as np
192
+
193
+
194
+
195
+ # device number as argument
196
+
197
+ def call_openfiles(readDN, writeDN):
198
+
199
+ f = np.ctypeslib.load_library("libfort.so", ".")
200
+
201
+ f.openfiles_.argtypes = [
202
+
203
+ ctypes.c_int32,
204
+
205
+ ctypes.c_int32
206
+
207
+ ]
208
+
209
+ f.openfiles_.restype = ctypes.c_void_p
210
+
211
+
212
+
213
+ f.openfiles_(readDN, writeDN)
214
+
215
+
216
+
217
+ # non argument
218
+
219
+ def call_fortran():
220
+
221
+ f = np.ctypeslib.load_library("libfort.so", ".")
222
+
223
+ f.main_.restype = ctypes.c_void_p
224
+
225
+
226
+
227
+ f.main_()
228
+
229
+
230
+
231
+ # 装置番号を定義して引数として渡すとき
232
+
233
+ readDN = 10
234
+
235
+ writeDN = 11
236
+
237
+ print("**call fortran from python with device number as argument")
238
+
239
+ call_openfiles(readDN, writeDN)
240
+
241
+
242
+
243
+ # 引数なしで渡すとき
244
+
245
+ # print("**call fortran from python with non argument")
246
+
247
+ # call_fortran()
248
+
249
+ ```
250
+
251
+
252
+
253
+ ```fortran90
254
+
255
+ subroutine main()
256
+
257
+ implicit none
258
+
259
+ integer :: readDN = 10
260
+
261
+ integer :: writeDN = 11
262
+
263
+
264
+
265
+ call openFiles(readDN, writeDN)
266
+
267
+ call rwNumbers(readDN, writeDN)
268
+
269
+ call closeFiles(readDN, writeDN)
270
+
271
+ end subroutine main
272
+
273
+
274
+
275
+ subroutine rwNumbers(readDN, writeDN)
276
+
277
+ implicit none
278
+
279
+ real,dimension(30)::numbers
280
+
281
+ real temp
282
+
283
+ integer i, j
284
+
285
+ integer readDN
286
+
287
+ integer writeDN
288
+
289
+
290
+
291
+ ! read numbers
292
+
293
+ do i = 1, 30
294
+
295
+ read(readDN, *) numbers(i)
296
+
297
+ end do
298
+
299
+ close(10)
300
+
301
+
302
+
303
+ ! baggle sort
304
+
305
+ do i = 2, 30
306
+
307
+ do j = 2, 32-i
308
+
309
+ if (numbers(j) < numbers(j-1)) then
310
+
311
+ temp = numbers(j)
312
+
313
+ numbers(j) = numbers(j-1)
314
+
315
+ numbers(j-1) = temp
316
+
317
+ end if
318
+
319
+ end do
320
+
321
+ end do
322
+
323
+
324
+
325
+ ! print numbers
326
+
327
+ print *, "Sorted numbers is written in sortedNumbers.txt"
328
+
329
+ do i=1, 30
330
+
331
+ write(writeDN,*) numbers(i)
332
+
333
+ end do
334
+
335
+ end subroutine
336
+
337
+
338
+
339
+ subroutine openFiles(readDN, writeDN)
340
+
341
+ implicit none
342
+
343
+ integer(4),intent(in) :: readDN
344
+
345
+ integer(4),intent(in) :: writeDN
346
+
347
+
348
+
349
+ print *, "Open files from fortran"
350
+
351
+ open(readDN , file='numbers.txt', status='old')
352
+
353
+ open(writeDN , file='sortedNumbers.txt', status='replace')
354
+
355
+ end subroutine
356
+
357
+
358
+
359
+ subroutine closeFiles(readDN, writeDN)
360
+
361
+ implicit none
362
+
363
+ integer readDN, writeDN
364
+
365
+ close(readDN)
366
+
367
+ close(writeDN)
368
+
369
+ end subroutine
370
+
371
+ ```