如何将方程式从txt文件插入python脚本并生成另一个文件?



我再次问这个问题,因为我还没有为我的问题收集到明确的答案。

我有一组方程:

ref_energy = (K-(C/2)comp(a)) + C/2
form_E2 = relaxed_E_per_atom - ref_energy(comp_x)

我需要让一个 python 脚本使用 txt 文件中的值使用这两个方程来生成另一个 x 和 y 列的值,这些值将绘制这些值。

这是我的 txt 文件的前几行。 配置是与四个值关联的结构的名称

comp(a),form_E,comp_x,relaxed_E_per_atom
0,0,0,-8.15382173
1,0,0.33333333,-5.25358563
0.5,0.18614484,0.2,-6.33922213
0.5,-0.69658919,0.2,-6.69231575
0.5,-0.70549249,0.2,-6.69587707

下面是我的脚本,但我不断收到错误,例如

Traceback (most recent call last):
File "attempt_#2.py", line 80, in <module>
x.append(float(row[0]))
ValueError: could not convert string to float: 'comp(a)'

我假设 x 值不是配置名称中的数值,但不确定。

#!/bin/env/python
import numpy as np
import matplotlib.pyplot as plt 
import csv
Columns = 'configname,comp(a),form_E,comp_x,relaxed_E_per_atom'.split(',')
testdata ='''
comp(a),form_E,comp_x,relaxed_E_per_atom
0,0,0,-8.15382173
1,0,0.33333333,-5.25358563
0.5,0.18614484,0.2,-6.33922213
0.5,-0.69658919,0.2,-6.69231575
0.5,-0.70549249,0.2,-6.69587707
'''
reader = csv.DictReader(StringIO(testdata))
desired_cols = (tuple(row[col] for col in columns) for row in reader)   
x=[]
y=[]
K=-2.69028905
C=-32.65176322
with open('values2.txt','r') as csvfile:
points = csv.reader(csvfile,delimiter=',')
for row in points:
x.append(float(row[0]))
y.append(float(row[1]))
allpoints=np.loadtxt('hmm.csv',delimiter=',')
ref_energy = (Kend-(Cend/2))*comp(a) + Cend/2
form_E = relaxed_E_per_atom-ref_energy(comp_x)
plt.scatter(x,y, label='Energy')
plt.xlabel('Composition KxC',fontsize=24)
plt.ylabel('Formation Energy per carbon (eV)',fontsize=18)
plt.title('Convex Hull Potassium Graphite')
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.tight_layout()
plt.ylim(-0.08,0)
plt.xlim(0,1.01)
plt.show()

跳过第一行

with open('values2.txt','r') as csvfile:
points = csv.reader(csvfile,delimiter=',')
next(points)
for row in points:
x.append(float(row[0]))
y.append(float(row[1]))

最新更新