本文介绍了将python嵌入fortran 90的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直在寻找将python嵌入到fortran90中的选项,以将python功能添加到我现有的fortran90代码中。我知道可以通过使用numpy的f2py用fortran90扩展python来实现。但是,我想将我的超级优化主循环保留在fortran中,并添加python来执行一些其他任务/评估进一步的开发,然后才能在fortran中进行处理,并简化代码维护。我正在寻找以下问题的答案:I was looking at the option of embedding python into fortran90 to add python functionality to my existing fortran90 code. I know that it can be done the other way around by extending python with fortran90 using the f2py from numpy. But, i want to keep my super optimized main loop in fortran and add python to do some additional tasks / evaluate further developments before I can do it in fortran, and also to ease up code maintenance. I am looking for answers for the following questions: 1)是否已经存在一个库,可以将python嵌入到fortran中? (我知道f2py,反之亦然) 2)我们如何处理从fortran到python的数据传输? 3)我们如何实现回叫功能? (让我稍微描述一下这种情况。...我在Fortran中有main_fortran程序,在python中调用了Func1_Python模块。现在,从这个Func1_Python中,我想调用另一个函数...在fortran中说Func2_Fortran) 4)就性能而言,将python的解释器嵌入fortran会有什么影响……如加载时间,运行时间,跨多个数据发送数据(双精度大数组)等。1) Is there a library that already exists from which I can embed python into fortran? (I am aware of f2py and it does it the other way around)2) How do we take care of data transfer from fortran to python and back?3) How can we have a call back functionality implemented? (Let me describe the scenario a bit....I have my main_fortran program in Fortran, that call Func1_Python module in python. Now, from this Func1_Python, I want to call another function...say Func2_Fortran in fortran)4) What would be the impact of embedding the interpreter of python inside fortran in terms of performance....like loading time, running time, sending data (a large array in double precision) across etc.非常感谢您的帮助! Edit1:我想通过添加一些内容来确定讨论的方向有关我正在做的工作的信息。我喜欢科学计算的东西。因此,我将以双精度处理大量数组/矩阵并进行浮点运算。因此,除了fortran之外,几乎没有其他选项可以真正为我完成工作。我想在代码中包含python的原因是,如有必要,我可以使用NumPy进行一些基本计算,并以最小的努力扩展代码的功能。例如,我可以使用几个可用的库在python和其他软件包之间进行链接(例如使用PyFoam库的OpenFoam)。 I want to set the direction of the discussion right by adding some more information about the work I am doing. I am into scientific computing stuff. So, I would be working a lot on huge arrays / matrices in double precision and doing floating point operations. So, there are very few options other than fortran really to do the work for me. The reason i want to include python into my code is that I can use NumPy for doing some basic computations if necessary and extend the capabilities of the code with minimal effort. For example, I can use several libraries available to link between python and some other package (say OpenFoam using PyFoam library).推荐答案 1。不要这样做 我知道您想在Fortan程序中添加Python代码,而不是使用具有Fortran扩展名的Python程序。我的第一条建议是不要这样做。在数组算术方面,Fortran比Python快,但是Python比Fortran更易于编写,使用OOP技术扩展Python代码更容易,并且Python可能会访问对您很重要的库。您提到在Fortran中有一个超级优化的主循环; Fortran非常适合用于超优化的 inner 循环。用Numpy在Python程序中传递Fortran数组的逻辑比正确处理Fortran中的Python对象要简单得多。1. Don't do itI know that you're wanting to add Python code inside a Fortan program, instead of having a Python program with Fortran extensions. My first piece of advice is to not do this. Fortran is faster than Python at array arithmetic, but Python is easier to write than Fortran, it's easier to extend Python code with OOP techniques, and Python may have access to libraries that are important to you. You mention having a super-optimized main loop in Fortran; Fortran is great for super-optimized inner loops. The logic for passing a Fortran array around in a Python program with Numpy is much more straightforward than what you would have to do to correctly handle a Python object in Fortran.何时我从头开始进行一个科学计算项目,我总是首先使用Python编写代码,确定性能瓶颈,然后将其转化为Fortran。能够相对于经过验证的Python代码更快地测试Fortran代码,可以更轻松地证明代码是否正常运行。When I start a scientific computing project from scratch, I always write first in Python, identify performance bottlenecks, and translate those into Fortran. Being able to test faster Fortran code against validated Python code makes it easier to show that the code is working correctly.由于您已有代码,因此可以使用Fortran中制造的模块将需要重构,但是此过程应该很简单。将初始化代码与主循环分开,将循环分成逻辑部分,将每个例程包装在Python函数中,然后您的主要Python代码可以调用Fortran子例程,并在适当时将它们与Python函数进行交错。在此过程中,您可以保留Fortran主循环中的许多优化。 F2PY是用于此目的的相当标准的工具,因此找到可以为您解决任何问题的人员提供帮助并不困难。Since you have existing code, extending the Python code with a module made in Fortran will require refactoring, but this process should be straightforward. Separate the initialization code from the main loop, break the loop into logical pieces, wrap each of these routines in a Python function, and then your main Python code can call the Fortran subroutines and interleave these with Python functions as appropriate. In this process, you may be able to preserve a lot of the optimizations you have in your main loop in Fortran. F2PY is a reasonably standard tool for this, so it won't be tough to find people who can help you with whatever problems will arise.如果您绝对必须具有调用Python代码的Fortran代码,而不是相反,执行此操作的最简单方法是让Fortran代码编写一些数据存储到磁盘,然后使用 SYSTEM 或 EXECUTE_COMMAND_LINE 运行Python代码。如果使用 EXECUTE_COMMAND_LINE ,则可以让Python代码将其结果输出到stdout,而Fortran代码可以将其读取为字符数据;如果您有很多输出(例如,一个大矩阵),则对于Python代码来说,输出一个供Fortran代码读取的文件会更有意义。磁盘读/写开销为此可能显得过高。同样,您将必须编写Fortran代码以输出数据,Python代码以读取数据,Python代码以再次输出数据以及Fortran代码以重新输入数据。该代码应该易于编写和测试,但是在编辑代码时使这四个部分保持同步可能会让人头疼。If you absolutely must have Fortran code calling Python code, instead of the other way around, the simplest way to do this is to just have the Fortran code write some data to disk, and run the Python code with a SYSTEM or EXECUTE_COMMAND_LINE. If you use EXECUTE_COMMAND_LINE, you can have the Python code output its result to stdout, and the Fortran code can read it as character data; if you have a lot of output (e.g., a big matrix), it would make more sense for the Python code to output a file that the Fortran code then reads. Disk read/write overhead could wind up being prohibitively significant for this. Also, you would have to write Fortran code to output your data, Python code to read it, Python code to output it again, and Fortran code to re-input the data. This code should be straightforward to write and test, but keeping these four parts in sync as you edit the code may turn into a headache.(这种方法在此堆栈溢出问题)据我所知,没有办法将内存中的Python对象直接传递给Fortran。但是,Fortran代码可以调用C代码,并且C代码中可以嵌入Python。 (请参见有关扩展和嵌入的Python教程。)通常,扩展Python(例如I建议在第1点中进行推荐,而不是将其嵌入C / C ++中。 (请参见扩展与嵌入:只有一个正确的决定。)这样做将是一场噩梦,因为Python和Fortran之间的任何通信问题都可能发生在Python和C之间,或C和Fortran之间。我不知道是否有人真的在Fortran中将Python嵌入C中,因此很难获得帮助。There is no way that I know of to directly pass a Python object in memory to Fortran. However, Fortran code can call C code, and C code can have Python embedded in it. (See the Python tutorial on extending and embedding.) In general, extending Python (like I recommend in point 1) is preferable to embedding it in C/C++. (See Extending Vs. Embedding: There is Only One Correct Decision.) Getting this to work will be a nightmare, because any communication problems between Python and Fortran could happen between Python and C, or between C and Fortran. I don't know if anyone is actually embedding Python in C in Fortran, and so getting help will be difficult. 这篇关于将python嵌入fortran 90的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 06:47