需要让f2py工作,但不知道任何fortran

时间:2015-06-09 20:24:30

标签: python fortran f2py

我想从这个源代码中使用函数OPAC: http://opalopacity.llnl.gov/codes/xztrin21.f

我不太了解代码,我只想将它用作Python模块。我运行以下内容:

f2py -c xztrin21.f -m opal_opacity

但我总是得到这个错误:

/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c:1819:71: error: expected ‘;’, ‘,’ or ‘)’ before ‘!’ token
/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c: In function ‘f2py_init_cst’:
/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c:1828:35: error: ‘f2py_setup_cst’ undeclared (first use in this function)
/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c:1828:35: note: each undeclared identifier is reported only once for each function it appears in
/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c:1819:71: error: expected ‘;’, ‘,’ or ‘)’ before ‘!’ token
/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c: In function ‘f2py_init_cst’:
/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c:1828:35: error: ‘f2py_setup_cst’ undeclared (first use in this function)
/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c:1828:35: note: each undeclared identifier is reported only once for each function it appears in
error: Command "gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/tmp/tmpWe2VM7/src.linux-x86_64-2.7 -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c /tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.c -o /tmp/tmpWe2VM7/tmp/tmpWe2VM7/src.linux-x86_64-2.7/opal_opacitymodule.o" failed with exit status 1

我不认为代码确实有任何问题。我相信其他人让它工作正常,所以我想我必须有一个错误的Fortran编译器或其他东西。我不知道该怎么办。

任何有用的帮助

编辑: 如果我尝试使用gfortran编译代码,我会得到以下内容:

xztrin21.f:1025.72:

      IF (H.EQ.0.) PAUSE 'Bad XA input.'                                
                                                                        1
Warning: Deleted feature: PAUSE statement at (1)
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

3 个答案:

答案 0 :(得分:1)

好的,这对你链接的Fortran代码来说不是一个问题。

您发现的所有错误均为Accumulator

  • import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.Spliterator; import java.util.Spliterators; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.StreamSupport; public class StopStreamReduction { public static void main(String[] args) { List<Integer> collection = new ArrayList<Integer>(Collections.nCopies(20, 1)); System.out.println(compute(collection)); } private static String compute(List<Integer> collection) { Predicate<Accumulator> stopCondition = (a) -> a.set.size() > 10; Accumulator result = reduceStopping(collection, new Accumulator(), StopStreamReduction::accumulate, stopCondition); return (result.set.size() > 11) ? "invalid" : String.valueOf(result.set); } private static int counter; private static Accumulator accumulate(Accumulator a, Integer element) { System.out.print("Step " + counter); if (a.set.size() <= 10) { System.out.print(" expensive"); a.set.add(counter); } System.out.println(); counter++; return a; } static <U, T> U reduceStopping( Collection<T> collection, U identity, BiFunction<U, ? super T, U> accumulator, Predicate<U> stopCondition) { // This assumes that the accumulator always returns // the identity instance (with the accumulated values). // This may not always be true! return StreamSupport.stream( new StoppingSpliterator<T>( collection.spliterator(), () -> stopCondition.test(identity)), false). reduce(identity, accumulator, (x, y) -> null); } } class Accumulator { final Set<Integer> set = new HashSet<Integer>(); } class StoppingSpliterator<T> extends Spliterators.AbstractSpliterator<T> { private final Spliterator<T> delegate; private final Supplier<Boolean> stopCondition; StoppingSpliterator(Spliterator<T> delegate, Supplier<Boolean> stopCondition) { super(delegate.estimateSize(), 0); this.delegate = delegate; this.stopCondition = stopCondition; } @Override public boolean tryAdvance(Consumer<? super T> action) { if (stopCondition.get()) { return false; } return delegate.tryAdvance(action); } }
  • In function ‘f2py_init_cst’

从名称来看,这表明python包:1828:35: error: ‘f2py_setup_cst’ undeclared (first use in this function)存在问题。 上述语法意味着编译器在:1819:71: error: expected ‘;’, ‘,’ or ‘)’ before ‘!’ token代码中的f2pyline: 1828, column:35上发现了错误。

  • 我建议您查看line:1819, column:71网站上的文档并确保正确编译
  • 作为测试,f2py文档中显示work through the examples并确保您可以对其进行编译

答案 1 :(得分:0)

所以我先通过创建签名文件解决了这个问题。我跑了命令

f2py -h opal.pyf -m opal xztrin21.f

而不是

f2py -c add.pyf add.f95

现在工作正常。我用过这个教程: http://docs.scipy.org/doc/numpy/user/c-info.python-as-glue.html

答案 2 :(得分:0)

f2py -c --fcompiler=gnu95 -m modulename filename.f95

此命令应该有效。

http://www.ucs.cam.ac.uk/docs/course-notes/unixcourses/pythonfortran/files/f2py.pdf

这是一个非常简单的文档,介绍了如何在python中使用fortran模块。这对我很有帮助。