大概就是一个RK4,可以运行,但是CPU占用只有10%几qwq,萌新的第一个程序,不知道哪里有问题
import taichi
m=20
k=1
step=0.01
begin=0
end=70
xlong= 20
xlist1=[0,0.2,0.4,0.8,1.2,1.6,2.4,3.2,4,5.6,7.2,8.8,12,15.2,18.4,24.8,31.2,37.6,50.4,63.2]
ylist1=[
[0,0.44721,0.63246,0.89443,1.09545,1.26491,1.54919,1.78885,2,2.36643,2.68328,2.96648,3.4641,3.89872,4.28952,4.97996,5.5857,6.13188,7.0993,7.94984]
,[0.9742,0.74616,0.69505,0.60067,0.56271,0.52789,0.46539,0.43908,0.40448,0.36854,0.33803,0.32939,0.30931,0.30803,0.2712,0.27834,0.27448,0.27278,0.27213,0.24438]
,[0.02214,0.20704,0.23472,0.32203,0.33179,0.37326,0.4045,0.43486,0.48729,0.48547,0.52775,0.55154,0.57585,0.58932,0.60244,0.60759,0.60589,0.60845,0.60477,0.61639]
]
taichi.init(arch=taichi.cpu)
xlist=taichi.field(float,shape=(1,xlong))
ylist=taichi.field(float,shape=(3,xlong))
templist=taichi.field(float,(1,m))
y=taichi.field(float,shape=(5*m,m))
for i in range(m):
for j in range(i):
y[5*i,j]=1
for i in range(xlong):
xlist[0,i]=xlist1[i]
for j in range(3):
ylist[j,i]=ylist1[j][i]
@taichi.func
def fieldmult(yindex,toindex):
tempy0=temp=y[yindex,0]
y[toindex,0]=k*y[yindex,m-1]+k*y[yindex,1]-2*k*y[yindex,0]
for i in taichi.static(range(1,m-1)): #期望这里可以并行
tempnext=y[yindex,i]
y[toindex,i]=k*temp+k*y[yindex,i+1]-2*k*y[yindex,i]
temp=tempnext
y[toindex,m-1] = k * temp + k * tempy0 - 2 * k * y[yindex,m-1]
@taichi.func
def fieldadd(aindex1,aindex2,bindex,mult1,mult2):
for i in taichi.static(range(m)): #期望这里可以并行
y[bindex,i]=mult2*y[aindex2,i]+mult1*y[aindex1,i]
@taichi.func
def RK4_L2(x:float,x1:float,h:float,oe:taichi.int8)->float:
t0 = 0
L2SUM=taichi.Vector([0.0,0.0,0.0])
ysum=taichi.Vector([0.0,0.0,0.0])
while(taichi.abs(x-x1)>h*0.5):
if (taichi.abs(x - xlist[0,t0]) < h * 0.5):
ysum[0]=0.0
for i in range(0,oe):
ysum[0]+=y[5*oe,i]
ysum[0] = ysum[0] / oe #fi/fc
ysum[1] = (y[5*oe,m-1] + y[5*oe,oe]) / oe #fra
ysum[2] = 1 - ysum[1] - ysum[0] #fa
for i in taichi.static(range(3)):
L2SUM[i] += (ysum[i] - ylist[i,t0]) ** 2
t0 += 1
# k1
fieldmult(5*oe,5*oe+1)
# k2
fieldadd(5*oe,5*oe+1,5*oe+2,1,h*0.5)
fieldmult(5*oe+2,5*oe+2)
# k3
fieldadd(5*oe,5*oe+2,5*oe+3,1,h*0.5)
fieldmult(5*oe+3,5*oe+3)
# k4
fieldadd(5*oe,5*oe+3,5*oe+4,1,h*0.5)
fieldmult(5 * oe + 4,5 * oe + 4)
for i in taichi.static(range(m)):
y[5*oe, i] += h/6.0*(y[5*oe+1, i] + 2 * y[5*oe+2, i]+y[5*oe+4, i] + 2 * y[5*oe+3, i])
x += h
return (L2SUM[0]+L2SUM[1]+L2SUM[2])**0.5
@taichi.kernel
def fitting()->int:
for i in taichi.static(range(2,m-2)):
templist[0,i]=RK4_L2(0.0,end,step,i) #首先是期望这里可以并行
temp=2
tempvalue=templist[0,2]
for j in range(1):
for i in taichi.static(range(3,m-2)):
if(templist[0,i]<tempvalue):
temp=i
tempvalue=templist[0,i]
print(tempvalue)
return temp
print(fitting())