简体   繁体   English

Python - 时间相关系数的微分方程求解器为不同的时间偏移提供不同的动态

[英]Python - Differential equation solver for time-dependent coefficients gives different dynamics for different time offsets

I am solving the dynamics of a system when it interacts with a pulse, which basically is solving a time-dependent differential equation.我正在解决系统与脉冲相互作用时的动力学问题,这基本上是在解决与时间相关的微分方程。 In general it works fine, but whenever I take the bandwidth of the pulse small, ie around unity, the solver depends on where the pulse starts t0.一般来说,它工作得很好,但是每当我把脉冲的带宽取小,即大约为单位时,求解器取决于脉冲从哪里开始 t0。 Let me give you the code and some pictures让我给你代码和一些图片

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from scipy.integrate import solve_ivp
import scipy as scipy

## Pulses 

def Box(t):
    return (abs(t-t0) < tau)
    #return  np.exp(-(t - t0)**2.0/(tau ** 2.0))


## Differential eq.
def Leq(t,u,pulse): 
    v=u[:16].reshape(4,4)
    a0=u[16] 
    da0=-a0+pulse(t)*E_0
    
    M=np.array([[-1,0,a0,0],\
                [0,-1,0,-a0],\
                [a0,0,-1,0],\
                [0,-a0,0,-1]])*kappa
    Dr=np.array([[1,1j,0,0],\
                [1j,1,0,0],\
                [0,0,1,1j],\
                [0,0,1j,1]])*kappa/2.0

    
    dv=M.dot(v)+v.dot(M)+Dr
    
    return np.concatenate([dv.flatten(), [da0]])
## Covariance matrix

cov0=np.array([[1,1j,0,0],\
                [1j,1,0,0],\
                [0,0,1,1j],\
                [0,0,1j,1]])/4 ##initial vector
cov0 = cov0.reshape(-1);   ## vectorize initial vector

a0_in=np.zeros((1,1),dtype=np.complex64) ##initial vector
a0_in = a0_in.reshape(-1);   ## vectorize initial vector
u_0=np.concatenate([cov0, a0_in])

### Parameters 
kappa=1.0 ##adimenstional kappa: kappa0/kappa 
tau=1.0  ##bandwidth pump

#Eth=kappa0*kappa/g  ##threshold intensity 
E_0=0.8 # pump intensity normalized to threshold 

t0=2.0   ##off set 

Tmax=10 ##max value for time
Nmax=100000 ##number of steps
dt=Tmax/Nmax  ##increment of time

t=np.linspace(0.0,Tmax,Nmax+1)

Box_sol=solve_ivp(Leq, [min(t),max(t)] , u_0, t_eval= t, args=(Box,))
print(Box_sol.y[0:16,int(Nmax/2)].reshape(4,4))

and then just some expectation values and the plotting.然后只是一些期望值和绘图。

适当的动态

动态不好

As you can see from the pictures, the parameters are the same, the only difference is the time shift t0.从图片中可以看出,参数是一样的,唯一的区别是时间偏移t0。 It seems as whenever the pulse's bandwidth is small, I have to start really close to t=0, which I don't fully understand why.似乎只要脉冲的带宽很小,我就必须开始非常接近 t=0,我不完全理解为什么。 It should,'t be like that.应该,不应该是这样的。

It is a problem of the solver?是求解器的问题吗? If so, what can I do to fix it?如果是这样,我该怎么做才能解决它? I'm now worried that this problem can manifest in other simulations I am running and I don't realise.我现在担心这个问题会出现在我正在运行的其他模拟中,但我没有意识到。

Thanks, Joan.谢谢,琼。

This is a well-known behavior, there have been several questions on this topic.这是一个众所周知的行为,关于这个话题有几个问题。

In short, it is the step size controller.简而言之,就是步长 controller。 The assumption behind it is that the ODE function is smooth to a high order, and that local behavior informs the global behavior in a medium range.其背后的假设是 ODE function 平滑到高阶,并且局部行为在中等范围内通知全局行为。 Thus if you start flat, with vanishing higher derivatives, the step size is quickly increased.因此,如果你开始平坦,随着更高的导数消失,步长会迅速增加。 If the situation is unfortunate, this will jump over the non-smooth bump in all stages of the integrator step.如果情况不幸,这将跳过积分器步骤的所有阶段中的非平滑凸起。

There are two strategies to avoid that有两种策略可以避免这种情况

  • set max_step to 2*tau so that the bump is surely encountered, the step size selection will make sure that the sampling is dense around the jumps.max_step设置为2*tau以便肯定会遇到凹凸,步长选择将确保在跳跃周围采样密集。

  • use separate integration for the pieces of the jump, so that the control input inside each segment is a constant.对跳转的片段使用单独的积分,使每个片段内的控制输入是一个常数。

在此处输入图像描述

The first variant is in a small way an abuse of the step size controller, as it works outside specifications in some heuristic emergency mode around the jumps.第一个变体在某种程度上滥用了步长 controller,因为它在跳跃周围的一些启发式紧急模式中超出规范。 The second variant requires a little more coding effort, but will have less internal steps, as the ODE function for each segment is again completely smooth.第二个变体需要更多的编码工作,但内部步骤更少,因为每个段的 ODE function 再次完全平滑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM