|
gimme-alpha 0.1
|
00001 /* 00002 ** wavefunction.c 00003 ** 00004 ** Made by (Johnny Q. Hacker) 00005 ** Login <solarion@borkborkbork> 00006 ** 00007 ** Stuff to deal with generalized wavefunctions. 00008 Copyright (C) 2009 Joseph Pingenot 00009 00010 This program is free software: you can redistribute it and/or modify 00011 it under the terms of the GNU Affero General Public License as published by 00012 the Free Software Foundation, either version 3 of the License, or 00013 (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU Affero General Public License for more details. 00019 00020 You should have received a copy of the GNU Affero General Public License 00021 along with this program. If not, see <http://www.gnu.org/licenses/>. 00022 ** Started on Thu Mar 26 11:47:00 2009 Johnny Q. Hacker 00023 ** Last update Sun May 12 01:17:25 2002 Speed Blue 00024 */ 00025 00026 #include <stdlib.h> 00027 #include <math.h> 00028 #include <libmodeling/wavefunction.h> 00029 #include <libcommon/gcc_hints.h> 00030 00031 struct mstar_wavefunction_1d* mstar_wavefunction_1d_new(int N) { 00032 struct mstar_wavefunction_1d *wf = malloc(sizeof(struct mstar_wavefunction_1d)); 00033 if(unlikely(wf == NULL)) return NULL; 00034 wf->points = malloc(N*sizeof(struct mstar_wavefunction_point_1d)); 00035 if(unlikely(wf->points == NULL)) { 00036 free(wf); 00037 return NULL; 00038 } 00039 wf->N = N; 00040 return wf; 00041 } 00042 00043 void mstar_wavefunction_1d_free(struct mstar_wavefunction_1d* wf) { 00044 free(wf->points); 00045 free(wf); 00046 } 00047 00048 00049 struct mstar_wavefunction_1d* mstar_wavefunction_1d_new_gaussian(int N, double minx, double maxx, double sigma) { 00050 struct mstar_wavefunction_1d* wf = mstar_wavefunction_1d_new(N); 00051 if(unlikely(wf == NULL)) return NULL; 00052 double oosqrtsigma = 1.0/pow(sigma*M_PI, 0.25); 00053 double oosigma = 1.0/(sigma*2); 00054 for(int i=0; i<N; i++) { 00055 double x = (maxx-minx)/N*i + minx; 00056 (wf->points)[i].x = x; 00057 (wf->points)[i].re = oosqrtsigma*exp(-x*x*oosigma); 00058 (wf->points)[i].im = 0.0; 00059 } 00060 return wf; 00061 } 00062 00063 struct mstar_wavefunction_1d* mstar_wavefunction_1d_from_LAPACK_normlized(const struct mstar_wavefunction_1d* wf) { 00064 struct mstar_wavefunction_1d* nwf = (struct mstar_wavefunction_1d*)malloc(sizeof(struct mstar_wavefunction_1d)); 00065 if(nwf == NULL) return NULL; 00066 nwf->N = wf->N; 00067 nwf->points = (struct mstar_wavefunction_point_1d*)malloc(sizeof(struct mstar_wavefunction_point_1d)*nwf->N); 00068 if(nwf->points == NULL) { 00069 free(nwf); 00070 return NULL; 00071 } 00072 /*handle crazy cases gracefully*/ 00073 if(unlikely(nwf->N < 3)) { 00074 if(nwf->N == 2) { 00075 nwf->points[0].x = wf->points[0].x; 00076 nwf->points[0].re = wf->points[0].re/sqrt(wf->points[1].x-wf->points[0].x); 00077 nwf->points[0].im = wf->points[0].im/sqrt(wf->points[1].x-wf->points[0].x); 00078 nwf->points[1].x = wf->points[1].x; 00079 nwf->points[1].re = wf->points[1].re/sqrt(wf->points[1].x-wf->points[0].x); 00080 nwf->points[1].im = wf->points[1].im/sqrt(wf->points[1].x-wf->points[0].x); 00081 }else{ 00082 nwf->points[0].x = wf->points[0].x; 00083 nwf->points[0].re = wf->points[0].re; 00084 nwf->points[0].im = wf->points[0].im; 00085 } 00086 }else{ 00087 /*Do average of forward and backward grid spaces, which necessitates special 00088 * handling for the first and last points.*/ 00089 nwf->points[0].x = wf->points[0].x; 00090 nwf->points[0].re = wf->points[0].re/sqrt(wf->points[1].x-wf->points[0].x); 00091 nwf->points[0].im = wf->points[0].im/sqrt(wf->points[1].x-wf->points[0].x); 00092 for(int i=1; i<(nwf->N-1); i++) { 00093 nwf->points[i].x = wf->points[i].x; 00094 nwf->points[i].re = wf->points[i].re/sqrt(0.5*(wf->points[i+1].x-wf->points[i-1].x)); 00095 nwf->points[i].im = wf->points[i].im/sqrt(0.5*(wf->points[i+1].x-wf->points[i-1].x)); 00096 } 00097 nwf->points[nwf->N-1].x = wf->points[nwf->N-1].x; 00098 nwf->points[nwf->N-1].re = wf->points[nwf->N-1].re/sqrt(wf->points[nwf->N-2].x-wf->points[nwf->N-1].x); 00099 nwf->points[nwf->N-1].im = wf->points[nwf->N-1].im/sqrt(wf->points[nwf->N-2].x-wf->points[nwf->N-1].x); 00100 } 00101 return nwf; 00102 } 00103 00104 void mstar_wavefunction_1d_repack(const struct mstar_wavefunction_1d* wf, double **x, double **re, double **im, short int convert_lapack_to_density_normalization) { 00105 *x = malloc((wf->N)*sizeof(double)); 00106 *re = malloc((wf->N)*sizeof(double)); 00107 *im = malloc((wf->N)*sizeof(double)); 00108 if(convert_lapack_to_density_normalization) { 00109 /*handle crazy cases gracefully*/ 00110 if(unlikely(wf->N < 3)) { 00111 if(wf->N == 2) { 00112 (*x)[0] = wf->points[0].x; 00113 (*re)[0] = wf->points[0].re/sqrt(wf->points[1].x-wf->points[0].x); 00114 (*im)[0] = wf->points[0].im/sqrt(wf->points[1].x-wf->points[0].x); 00115 (*x)[1] = wf->points[1].x; 00116 (*re)[1] = wf->points[1].re/sqrt(wf->points[1].x-wf->points[0].x); 00117 (*im)[1] = wf->points[1].im/sqrt(wf->points[1].x-wf->points[0].x); 00118 }else{ 00119 (*x)[0] = wf->points[0].x; 00120 (*re)[0] = wf->points[0].re; 00121 (*im)[0] = wf->points[0].im; 00122 } 00123 }else{ 00124 /*Do average of forward and backward grid spaces, which necessitates special 00125 * handling for the first and last points.*/ 00126 (*x)[0] = wf->points[0].x; 00127 (*re)[0] = wf->points[0].re/sqrt(wf->points[1].x-wf->points[0].x); 00128 (*im)[0] = wf->points[0].im/sqrt(wf->points[1].x-wf->points[0].x); 00129 for(int i=1; i<(wf->N-1); i++) { 00130 (*x)[i] = wf->points[i].x; 00131 (*re)[i] = wf->points[i].re/sqrt(0.5*(wf->points[i+1].x-wf->points[i-1].x)); 00132 (*im)[i] = wf->points[i].im/sqrt(0.5*(wf->points[i+1].x-wf->points[i-1].x)); 00133 } 00134 (*x)[wf->N-1] = wf->points[wf->N-1].x; 00135 (*re)[wf->N-1] = wf->points[wf->N-1].re/sqrt(wf->points[wf->N-1].x-wf->points[wf->N-2].x); 00136 (*im)[wf->N-1] = wf->points[wf->N-1].im/sqrt(wf->points[wf->N-1].x-wf->points[wf->N-2].x); 00137 } 00138 }else{ 00139 for(int i=0; i<wf->N; i++) { 00140 (*x)[i] = (wf->points)[i].x; 00141 (*re)[i] = (wf->points)[i].re; 00142 (*im)[i] = (wf->points)[i].im; 00143 } 00144 } 00145 }