微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何附加对象的返回值?

如何解决如何附加对象的返回值?

我正在尝试创建对耦合 ODE 的响应。输入电流 I 为脉冲形式。我试图得到如下响应:

  1. 在名为 I 的列表中创建当前配置文件
  2. 调用 I 的每个元素并每次使用 .set 将其作为参数发送。

不幸的是,在新的循环运行中,r 的值被覆盖了。

如何在旧值的末尾附加 r 以便根据 I 的值获得完整响应?

from scipy import *
from scipy.integrate import ode
from pylab import *
import matplotlib.pyplot as plt
import numpy as np


### Simualtion Outputs ###
N = []      # y[0] Carrier concentration
S = []      # y[1] Photon concentration



### Simualtion input  parameters ###
# def gaussian(x,mu,sig):
#     I = np.exp(-np.power(x - mu,2.) / (2 * np.power(sig,2.)))
#     return none
I = []
wm =2
x_values = np.linspace(0,60,1200)
for x in x_values:

    for mu,sig in [(30,1)]:
        a = np.exp(-np.power(x - mu,2.)))
        a1=a+wm
        a2=a1*1e-2
        I.append(a2)

print (I)

    
# x_values = np.linspace(-3,3,120)
# mu = 0
# sig = 0
# for a,b in [(-1,1),(0,2),(2,3)]:
#     mu = a
#     sig = b
# I = 2.0e-2
# I =lambda x,sig: np.exp(-np.power(x - mu,2.)))                                      # p[0] Pumping current (A)
q = 1.6e-19                                     # p[1] Electron charge (C)
V = 2e-11                                       # p[2] Device volume (cm^3)
tn = 1.0e-9                                     # p[3] Carrier relaxation time in seconds (s)
g0 = 1.5e-5                                     # p[4] Gain slope constant (cm^3/s)
Nth = 1e18                                      # p[5] Threshold carrier density (cm^-3)
EPS = 1.5e-17                                   # p[6] Gain compression factor (cm^3)
Gamma = 0.2                                     # p[7] Confinement factor
tp = 1.0e-12                                    # p[8] Photon lifetime in cavity (s)
Beta = 1.0e-4                                   # p[9] Spontaneous Emission Factor

### Define equations to be solved
def Laser_rates(t,y,p):
    for i in I:
    
        i = p[0]; q = p[1]; V = p[2]; tn = p[3]; g0 = p[4]; Nth = p[5]; EPS = p[6];  Gamma = p[7]; tp = p[8]; Beta = p[9]
        n = len(y)      # 2: implies we have two ODEs
        dy = np.zeros((n,1))
        ###dy = zeros([2])
        dy[0] = (i/(q* V)) - (y[0]/tn) -  g0*(y[0] - Nth)*(y[1]/(1 + EPS* y[1]))
        dy[1] = Gamma* g0* (y[0] - Nth)*(y[1]/(1 + EPS* y[1])) - y[1]/tp + (Gamma* Beta* y[0]) / tn
        
        return dy
    

### Time and initial conditions ###  
t0 = 0; tEnd = 1e-9; dt = 1e-11             #Time constraints
y0 = [0,0]                                 #Initial conditions
Y=[]; T=[]                                  #Create empty lists

p = [I,1.6e-19,2e-11,1e-9,1.5e-5,1e18,1.5e-17,0.2,1e-12,1e-4]             #Parameters for odes
#p = [ I,q,V,tn,g0,Nth,EPS,Gamma,tp,Beta]

#Setup integrator with desired parameters
#Setup integrator with desired parameters
for i in I:
    r = ode(Laser_rates).set_integrator('vode',method = 'bdf')
    r = ode(Laser_rates).set_integrator('dopri5',nsteps = 1e4)

    p = [i,1e-4]             #Parameters for odes

    r.set_f_params(p).set_initial_value(y0,t0)

### Simualtion check ###
    while r.successful() and r.t+dt < tEnd:
        r.integrate(r.t + dt)
        Y.append(r.y)        #Makes a list of 1d arrays
        T.append(r.t)

    ### Format output ###
    Y = array(Y)             #Convert from list to 2d array
    N = Y[:,0]
    S = Y[:,1] 


    ### Plotting ###
    f,axarr = plt.subplots(2,sharex=True) # Two subplots,the axes array is 1-d
    axarr[0].plot(T,N,'G')
    axarr[0].set_ylabel("Carrier Conc (cm^-3)")
    axarr[0].set_title('Laser-Rate Simulation')
    axarr[1].plot(T,S,'B')
    axarr[1].set_ylabel("Photon Conc (cm^-3)")
    axarr[1].set_xlabel("Time (s)")
    show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。