/*********************************************************************************/ /* */ /* Animation of Schrödinger equation in a planar domain */ /* */ /* N. Berglund, May 2021 */ /* */ /* Feel free to reuse, but if doing so it would be nice to drop a */ /* line to nils.berglund@univ-orleans.fr - Thanks! */ /* */ /* compile with */ /* gcc -o schrodinger schrodinger.c */ /* -L/usr/X11R6/lib -ltiff -lm -lGL -lGLU -lX11 -lXmu -lglut -O3 -fopenmp */ /* */ /* To make a video, set MOVIE to 1 and create subfolder tif_schrod */ /* It may be possible to increase parameter PAUSE */ /* */ /* create movie using */ /* ffmpeg -i wave.%05d.tif -vcodec libx264 wave.mp4 */ /* */ /*********************************************************************************/ /*********************************************************************************/ /* */ /* NB: The algorithm used to simulate the wave equation is highly paralellizable */ /* One could make it much faster by using a GPU */ /* */ /*********************************************************************************/ #include #include #include #include #include #include #include /* Sam Leffler's libtiff library. */ #include #define MOVIE 0 /* set to 1 to generate movie */ /* General geometrical parameters */ #define WINWIDTH 1280 /* window width */ #define WINHEIGHT 720 /* window height */ #define NX 640 /* number of grid points on x axis */ #define NY 360 /* number of grid points on y axis */ /* setting NX to WINWIDTH and NY to WINHEIGHT increases resolution */ /* but will multiply run time by 4 */ #define XMIN -2.0 #define XMAX 2.0 /* x interval */ #define YMIN -1.125 #define YMAX 1.125 /* y interval for 9/16 aspect ratio */ /* Choice of the billiard table */ #define B_DOMAIN 3 /* choice of domain shape */ #define D_RECTANGLE 0 /* rectangular domain */ #define D_ELLIPSE 1 /* elliptical domain */ #define D_STADIUM 2 /* stadium-shaped domain */ #define D_SINAI 3 /* Sinai billiard */ #define D_DIAMOND 4 /* diamond-shaped billiard */ #define D_TRIANGLE 5 /* triangular billiard */ #define D_FLAT 6 /* flat interface */ #define D_ANNULUS 7 /* annulus */ #define D_POLYGON 8 /* polygon */ #define D_YOUNG 9 /* Young diffraction slits */ #define D_GRATING 10 /* diffraction grating */ #define D_EHRENFEST 11 /* Ehrenfest urn type geometry */ #define LAMBDA 0.2 /* parameter controlling the dimensions of domain */ #define MU 0.05 /* parameter controlling the dimensions of domain */ #define NPOLY 6 /* number of sides of polygon */ #define APOLY 1.0 /* angle by which to turn polygon, in units of Pi/2 */ #define FOCI 1 /* set to 1 to draw focal points of ellipse */ /* You can add more billiard tables by adapting the functions */ /* xy_in_billiard and draw_billiard in sub_wave.c */ /* Physical patameters of wave equation */ #define DT 0.00000002 // #define DT 0.000000005 #define HBAR 1.0 /* Boundary conditions */ #define B_COND 1 #define BC_DIRICHLET 0 /* Dirichlet boundary conditions */ #define BC_PERIODIC 1 /* periodic boundary conditions */ #define BC_ABSORBING 2 /* absorbing boundary conditions (beta version) */ /* Parameters for length and speed of simulation */ #define NSTEPS 4500 /* number of frames of movie */ #define NVID 750 /* number of iterations between images displayed on screen */ #define NSEG 100 /* number of segments of boundary */ #define PAUSE 1000 /* number of frames after which to pause */ #define PSLEEP 1 /* sleep time during pause */ #define SLEEP1 1 /* initial sleeping time */ #define SLEEP2 1 /* final sleeping time */ /* For debugging purposes only */ #define FLOOR 0 /* set to 1 to limit wave amplitude to VMAX */ #define VMAX 10.0 /* max value of wave amplitude */ /* Plot type */ #define PLOT 0 #define P_MODULE 0 /* plot module of wave function squared */ #define P_PHASE 1 /* plot phase of wave function */ #define P_REAL 2 /* plot real part */ #define P_IMAGINARY 3 /* plot imaginary part */ /* Color schemes */ #define BLACK 1 /* black background */ #define COLOR_SCHEME 1 /* choice of color scheme */ #define C_LUM 0 /* color scheme modifies luminosity (with slow drift of hue) */ #define C_HUE 1 /* color scheme modifies hue */ #define C_PHASE 2 /* color scheme shows phase */ #define SCALE 1 /* set to 1 to adjust color scheme to variance of field */ #define SLOPE 1.0 /* sensitivity of color on wave amplitude */ #define ATTENUATION 0.0 /* exponential attenuation coefficient of contrast with time */ #define COLORHUE 260 /* initial hue of water color for scheme C_LUM */ #define COLORDRIFT 0.0 /* how much the color hue drifts during the whole simulation */ #define LUMMEAN 0.5 /* amplitude of luminosity variation for scheme C_LUM */ #define LUMAMP 0.3 /* amplitude of luminosity variation for scheme C_LUM */ #define HUEMEAN 150.0 /* mean value of hue for color scheme C_HUE */ #define HUEAMP -150.0 /* amplitude of variation of hue for color scheme C_HUE */ /* Basic math */ #define PI 3.141592654 #define DPI 6.283185307 #define PID 1.570796327 #include "sub_wave.c" double courant2; /* Courant parameter squared */ double dx2; /* spatial step size squared */ double intstep; /* integration step */ double intstep1; /* integration step used in absorbing boundary conditions */ void init_coherent_state(x, y, px, py, scalex, phi, psi, xy_in) /* initialise field with coherent state of position (x,y) and momentum (px, py) */ /* phi is real part, psi is imaginary part */ double x, y, px, py, scalex, *phi[NX], *psi[NX]; short int * xy_in[NX]; { int i, j; double xy[2], dist2, module, phase, scale2; scale2 = scalex*scalex; for (i=0; i0)&&(i0)&&(j VMAX) phi[i][j] = VMAX; if (phi[i][j] < -VMAX) phi[i][j] = -VMAX; if (psi[i][j] > VMAX) psi[i][j] = VMAX; if (psi[i][j] < -VMAX) psi[i][j] = -VMAX; } } } } // printf("phi(0,0) = %.3lg, psi(0,0) = %.3lg\n", phi[NX/2][NY/2], psi[NX/2][NY/2]); } double compute_variance(phi, psi, xy_in) /* compute the variance (total probability) of the field */ double *phi[NX], *psi[NX]; short int * xy_in[NX]; { int i, j, n = 0; double variance = 0.0; for (i=1; i