/*********************************************************************************/ /* */ /* 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 */ #define SAVE_MEMORY 0 /* set to 1 to save memory when writing tiff images */ /* General geometrical parameters */ #define WINWIDTH 1280 /* window width */ #define WINHEIGHT 720 /* window height */ // #define NX 1280 /* number of grid points on x axis */ // #define NX 720 /* number of grid points on x axis */ #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 */ #define JULIA_SCALE 1.0 /* scaling for Julia sets */ /* Choice of the billiard table, see list in global_pdes.c */ #define B_DOMAIN 10 /* choice of domain shape */ #define CIRCLE_PATTERN 0 /* pattern of circles, see list in global_pdes.c */ #define P_PERCOL 0.25 /* probability of having a circle in C_RAND_PERCOL arrangement */ #define NPOISSON 300 /* number of points for Poisson C_RAND_POISSON arrangement */ #define PDISC_FACTOR 3.25 /* controls density of Poisson disc process (default: 3.25) */ #define RANDOM_POLY_ANGLE 1 /* set to 1 to randomize angle of polygons */ #define LAMBDA 0.1 /* parameter controlling the dimensions of domain */ #define MU 0.03 /* 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 MDEPTH 5 /* depth of computation of Menger gasket */ #define MRATIO 3 /* ratio defining Menger gasket */ #define MANDELLEVEL 1000 /* iteration level for Mandelbrot set */ #define MANDELLIMIT 10.0 /* limit value for approximation of Mandelbrot set */ #define FOCI 1 /* set to 1 to draw focal points of ellipse */ #define NGRIDX 15 /* number of grid point for grid of disks */ #define NGRIDY 20 /* number of grid point for grid of disks */ #define X_SHOOTER -0.2 #define Y_SHOOTER -0.6 #define X_TARGET 0.4 #define Y_TARGET 0.7 /* shooter and target positions in laser fight */ #define ISO_XSHIFT_LEFT -1.65 #define ISO_XSHIFT_RIGHT 0.4 #define ISO_YSHIFT_LEFT -0.05 #define ISO_YSHIFT_RIGHT -0.05 #define ISO_SCALE 0.85 /* coordinates for isospectral billiards */ /* 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.00000001 // #define DT 0.00000001 // #define DT 0.000000005 // #define DT 0.000000005 #define HBAR 1.0 /* Boundary conditions, see list in global_pdes.c */ #define B_COND 1 /* Parameters for length and speed of simulation */ #define NSTEPS 2500 /* number of frames of movie */ // #define NVID 2000 /* number of iterations between images displayed on screen */ #define NVID 1200 /* number of iterations between images displayed on screen */ #define NSEG 100 /* number of segments of boundary */ #define BOUNDARY_WIDTH 2 /* width of billiard 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 */ #define END_FRAMES 100 /* still frames at end of movie */ /* 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, see list in global_pdes.c */ #define PLOT 11 /* Color schemes, see list in global_pdes.c */ #define COLOR_PALETTE 10 /* Color palette, see list in global_pdes.c */ #define BLACK 1 /* black background */ #define COLOR_SCHEME 3 /* choice of color scheme */ #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 PHASE_SHIFT 0.0 /* shift of phase in color scheme P_3D_PHASE */ #define E_SCALE 150.0 /* scaling factor for energy representation */ #define LOG_SCALE 1.0 /* scaling factor for energy log representation */ #define LOG_SHIFT 0.0 /* shift of colors on log scale */ #define RESCALE_COLOR_IN_CENTER 0 /* set to 1 to decrease color intentiy in the center (for wave escaping ring) */ #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 180.0 /* mean value of hue for color scheme C_HUE */ #define HUEAMP 180.0 /* amplitude of variation of hue for color scheme C_HUE */ #define DRAW_COLOR_SCHEME 1 /* set to 1 to plot the color scheme */ #define COLORBAR_RANGE 2.0 /* scale of color scheme bar */ #define COLORBAR_RANGE_B 12.0 /* scale of color scheme bar for 2nd part */ #define ROTATE_COLOR_SCHEME 0 /* set to 1 to draw color scheme horizontally */ /* the following constants are only used by wave_billiard and wave_3d so far */ #define COMPARISON 0 /* set to 1 to compare two different patterns */ #define B_DOMAIN_B 20 /* second domain shape, for comparisons */ #define CIRCLE_PATTERN_B 0 /* second pattern of circles or polygons */ #define OSCILLATION_SCHEDULE 3 /* oscillation schedule, see list in global_pdes.c */ #define ACHIRP 0.2 /* acceleration coefficient in chirp */ #define DAMPING 0.0 /* damping of periodic excitation */ #define OMEGA 0.001 /* frequency of periodic excitation */ #define AMPLITUDE 0.8 /* amplitude of periodic excitation */ /* end of constants only used by wave_billiard and wave_3d */ /* for compatibility with sub_wave and sub_maze */ #define NXMAZE 7 /* width of maze */ #define NYMAZE 7 /* height of maze */ #define MAZE_MAX_NGBH 4 /* max number of neighbours of maze cell */ #define RAND_SHIFT 24 /* seed of random number generator */ #define MAZE_XSHIFT 0.0 /* horizontal shift of maze */ #define MAZE_WIDTH 0.02 /* half width of maze walls */ #define ADD_POTENTIAL 0 #define POT_MAZE 7 #define POTENTIAL 0 #define VARIABLE_IOR 1 /* set to 1 for a variable index of refraction */ #define IOR 7 /* choice of index of refraction, see list in global_pdes.c */ #define IOR_TOTAL_TURNS 1.5 /* total angle of rotation for IOR_PERIODIC_WELLS_ROTATING */ #define MANDEL_IOR_SCALE -0.05 /* parameter controlling dependence of IoR on Mandelbrot escape speed */ #define COURANT 0.04 /* Courant number */ #define COURANTB 0.0 /* Courant number in medium B */ #define INITIAL_AMP 0.5 /* amplitude of initial condition */ #define INITIAL_VARIANCE 0.0003 /* variance of initial condition */ #define INITIAL_WAVELENGTH 0.015 /* wavelength of initial condition */ #define TWOSPEEDS 0 /* set to 1 to replace hardcore boundary by medium with different speed */ #define WAVE_PACKET_SOURCE_TYPE 1 /* type of wave packet sources */ #define N_WAVE_PACKETS 15 /* number of wave packets */ #define OSCIL_LEFT_YSHIFT 0.0 /* y-dependence of left oscillation (for non-horizontal waves) */ #define OSCILLATING_SOURCE_PERIOD 20 /* period of oscillating source */ #define MU_B 1.0 /* parameter controlling the dimensions of domain */ #define GAMMA 0.0 /* damping factor in wave equation */ #define GAMMAB 0.0 /* damping factor in wave equation */ #define DRAW_WAVE_PROFILE 0 /* set to 1 to draw a profile of the wave */ #define VERTICAL_WAVE_PROFILE 0 /* set to 1 to draw wave profile vertically */ #define WALL_WIDTH 0.1 /* width of wall separating lenses */ #define RADIUS_FACTOR 0.3 /* controls inner radius for C_RING arrangements */ #define INITIAL_TIME 50 /* time after which to start saving frames */ #define OSCIL_YMAX 0.35 /* defines oscillation range */ #define MESSAGE_LDASH 14 /* length of dash for Morse code message */ #define MESSAGE_LDOT 8 /* length of dot for Morse code message */ #define MESSAGE_LINTERVAL 54 /* length of interval between dashes/dots for Morse code message */ #define MESSAGE_LINTERLETTER 60 /* length of interval between letters for Morse code message */ #define MESSAGE_LSPACE 48 /* length of space for Morse code message */ #define MESSAGE_INITIAL_TIME 100 /* initial time before starting message for Morse code message */ #define WAVE_PROFILE_X 2.1 /* value of x to sample wave profile */ #define HRES 1 /* dummy, only used by rde.c */ #define INITIAL_SHIFT 20.0 /* time shift of initial wave packet (in oscillation periods) */ #define WAVE_PACKET_SHIFT 200.0 /* time shift between wave packets (in oscillation periods) */ #define FADE_IN_OBSTACLE 0 /* set to 1 to fade color inside obstacles */ #define SHADE_2D 0 /* set to 1 to add pseudo-3d shading effect */ #define SHADE_SCALE_2D 0.05 /* lower value increases sensitivity of shading */ #define N_SOURCES 1 /* number of sources, for option draw_sources */ double light[2] = {0.40824829, 0.816496581}; /* location of light source for SHADE_2D option*/ /* end of constants only used by sub_wave and sub_maze */ #include "global_pdes.c" #include "sub_maze.c" #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(double x, double y, double px, double py, double scalex, double *phi[NX], double *psi[NX], short int *xy_in[NX]) /* initialise field with coherent state of position (x,y) and momentum (px, py) */ /* phi is real part, psi is imaginary part */ { int i, j; double xy[2], dist2, module, phase, scale2; scale2 = scalex*scalex; for (i=0; i0)&&(i0)&&(j VMAX) phi_out[i][j] = VMAX; if (phi_out[i][j] < -VMAX) phi_out[i][j] = -VMAX; if (psi_out[i][j] > VMAX) psi_out[i][j] = VMAX; if (psi_out[i][j] < -VMAX) psi_out[i][j] = -VMAX; } } } } // printf("phi(0,0) = %.3lg, psi(0,0) = %.3lg\n", phi[NX/2][NY/2], psi[NX/2][NY/2]); } void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX], double *psi_out[NX], short int *xy_in[NX]) // void evolve_wave_half(phi_in, psi_in, phi_out, psi_out, xy_in) // /* time step of field evolution */ // /* phi is real part, psi is imaginary part */ { int i, j, iplus, iminus, jplus, jminus; double delta1, delta2, x, y; #pragma omp parallel for private(i,j,iplus,iminus,jplus,jminus,delta1,delta2,x,y) for (i=1; i VMAX) phi_out[i][j] = VMAX; if (phi_out[i][j] < -VMAX) phi_out[i][j] = -VMAX; if (psi_out[i][j] > VMAX) psi_out[i][j] = VMAX; if (psi_out[i][j] < -VMAX) psi_out[i][j] = -VMAX; } } } } void evolve_wave(double *phi[NX], double *psi[NX], double *phi_tmp[NX], double *psi_tmp[NX], short int *xy_in[NX]) /* time step of field evolution */ /* phi is real part, psi is imaginary part */ { evolve_wave_half(phi, psi, phi_tmp, psi_tmp, xy_in); evolve_wave_half(phi_tmp, psi_tmp, phi, psi, xy_in); } double compute_variance(double *phi[NX], double *psi[NX], short int *xy_in[NX]) // 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