疯不觉 发表于 2020-4-27 15:46:11

Python在lammps中的调用

在lammps中调用python记一下在lammps中调用python的方法和实例,下面主要是对lammps中python命令的说明文档的翻译,以及一些例子的解释。调用python前期准备PYTHON package must be installedLAMMPS must be compiled as shared-library(Optional)-DLAMMPS_EXCEPTIONS for better error handlingLAMMPS Python module (lammps.py) must beinstalled简单来说就是想要在lammps中调用python,必须在lammps中安装python包,具体的安装流程可以查看说明文档,一步一步跟着执行下来就可以了。安装完成之后就可以联合使用lammps和python了。
https://ask.qcloudimg.com/http-save/developer-news/dn47uoyps8.jpeg?imageView2/2/w/1620
https://ask.qcloudimg.com/http-save/developer-news/da11cyiy08.jpeg?imageView2/2/w/1620
从上面的模式图中可以看出,可以在lammps中的输入文件中调用python,也可以在python的程序中调用lammps。本文主要讲如何在lammps中调用python。1python command 语法pythonfunckeyword args ...func是指调用的python函数名keyword = invoke or input or return or format or length or file or here or exists or sourcekeyword中invoke只能单独使用,其他的keyword可以混合使用,下面主要解释input,return,format,file,here这几个用法2keyword解释inputargs = N i1 i2 ... iNN:输入参数个数i1i2... iN:具体输入的参数returnarg = varReturn返回一个变量到lammps,例如写return v_a,返回一个值到lammps输入文件中的变量aformatarg = fstring with M characters需要和input,return配合使用,用于说明input,return的数据类型。'i' = integer, 'f' = floating point, 's' = string, 'p' = SELF例如:python func input 1 v_a format fpython func input 2 v_a v_b return v_c format iiifilearg = filename这个命令就比较简单粗暴了,直接调用你已经写好的python程序,例如:python sample file sample.py调用了sample.py中的sample函数可以配合使用invoke,例如:python sample file sample.pypython sample invoke就会运行sample函数herearg = inlinehere大概就是表示接下来我要插入函数了用法:python func here """def func():函数内容"""pythonfuncinvoke3例子及解释python   factorial &input    1 v_n &return   v_fact &formatii &here """def factorial(n):if n == 1: return 1return   n*factorial(n-1)"""variable   fact   python    factorial阅读一下这个例子:定义了一个factorial函数;输入参数为1个,变量n;返回值为fact;n,fact都是整型;variable调用python时得到fact值,返回到lammps中。转载自企鹅号 - 干一杯温开水
页: [1]
查看完整版本: Python在lammps中的调用