Python Forum
periodic boundary contions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: periodic boundary contions (/thread-28426.html)



periodic boundary contions - grknkilicaslan - Jul-18-2020

Hi all

I want to add periodic boundary conditions to the plot below.
Conditions:
If x>pi then x=x - 2*pi

if x<-pi then x=x + 2* pi

If z>pi then z=z - 2 * pi

if z<-pi then z=z + 2* pi

I tried if,elif,else but didn't work

How can I do that? Can anyone help? Thank you



import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
 
def f1(t,r):

    theta,x,z=r
    ftheta = (-1/2)*(np.cos(x)*np.cos(z)+np.sin(theta)/124)
    fx = 2*np.sin(theta)-(1/2)*np.cos(x)*np.sin(z)
    fz = 2*np.cos(theta)+(1/2)*np.sin(x)*np.cos(z)
    return ftheta,fx, fz
 
sol=integrate.solve_ivp(f1,(0,2000),(np.pi/4,-np.pi/2,-np.pi/2), t_eval=np.linspace(0, 2000,100000))
theta,x,z=sol.y


#Plotting
plt.plot(x,z,label = 'with \u03A6')
plt.xlabel('x')
plt.ylabel('z')

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])    
plt.yticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

plt.xlim(-np.pi,np.pi)
plt.ylim(-np.pi,np.pi)

plt.show()



RE: periodic boundary contions - Gribouillis - Jul-18-2020

You can perhaps try
x = np.mod(x + np.pi, 2 * np.pi) - np.pi