gimme-alpha 0.1

nextnano_wavefunction_conversion.c

Go to the documentation of this file.
00001 /*
00002 ** nextnano_wavefunction_conversion.c
00003 ** 
00004 ** Made by (Johnny Q. Hacker)
00005 ** Login   <solarion@borkborkbork>
00006 **
00007     Copyright (C) 2009 Joseph Pingenot
00008 
00009     This program is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Affero General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Affero General Public License for more details.
00018 
00019     You should have received a copy of the GNU Affero General Public License
00020     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 ** Started on  Thu Mar 26 12:22:33 2009 Johnny Q. Hacker
00022 ** Last update Sun May 12 01:17:25 2002 Speed Blue
00023 */
00024 
00025 #include <math.h>
00026 #include <stdio.h>
00027 #include <libnextnano/nextnano_wavefunction_conversion.h>
00028 #include <libmodeling/wavefunction.h>
00029 #include <libcommon/gcc_hints.h>
00030 
00031 #define CLOSE_ENOUGH 1e-8
00032 
00033 /*Size of buffer to use to hold nextnano ouput file field names when
00034   generating names, e.g. netnano_wavefunction_covnersion*/
00035 #define NEXTNANO_FIELD_TITLE_BUFFER_LEN 32
00036 
00037 /*Same,but for parts of an output file name*/
00038 #define NEXTNANO_FILENAME_PART_BUFFER_LEN 32
00039 
00040 
00041 /*Faster, if you're not gonna worry about roundoff*/
00042 static gint coordinate_density_sorter(gconstpointer a, gconstpointer b) {
00043   double *c;
00044   double *d;
00045   if((c=g_hash_table_lookup((GHashTable*)a, "x")) == NULL) {
00046     if((d=g_hash_table_lookup((GHashTable*)b, "x")) == NULL) return 0;
00047     return -1;
00048   }else if((d=g_hash_table_lookup((GHashTable*)b, "x")) == NULL) {
00049     return 1;
00050   }
00051   if(*c < *d) return -1;
00052   if(*c > *d) return 1;
00053   return 0;
00054 }
00055 
00056 /*Slower, but gets the answer right if you have roundoff*/
00057 static gint approx_coordinate_density_sorter(gconstpointer a, gconstpointer b) {
00058   double *c;
00059   double *d;
00060   if((c=g_hash_table_lookup((GHashTable*)a, "x")) == NULL) {
00061     if((d=g_hash_table_lookup((GHashTable*)b, "x")) == NULL) return 0;
00062     return -1;
00063   }else if((d=g_hash_table_lookup((GHashTable*)b, "x")) == NULL) {
00064     return 1;
00065   }
00066   //fprintf(stderr, "got x values c=%g(%p) and d=%g(%p) (a=%p,b=%p)\n", *c, c, *d, d, a, b);
00067   if(fabs(*c - *d) < fabs(CLOSE_ENOUGH*(*c))) return 0;
00068   if(*c < *d) return -1;
00069   if(*c > *d) return 1;
00070   return 0;
00071 }
00072 
00073 struct mstar_wavefunction_1d* convert_nextnano_mstar_wavefunction_to_general_1d(GList* wf, const char* xtitle, const char* real_title, const char* imaginary_title, const char* title_terminator, unsigned short int eigval) {
00074   char rtitle[NEXTNANO_FIELD_TITLE_BUFFER_LEN];
00075   char ititle[NEXTNANO_FIELD_TITLE_BUFFER_LEN];
00076   snprintf(rtitle, NEXTNANO_FIELD_TITLE_BUFFER_LEN, "%s%03d%s", real_title, eigval, title_terminator);
00077   rtitle[NEXTNANO_FIELD_TITLE_BUFFER_LEN-1]='\0';
00078   snprintf(ititle, NEXTNANO_FIELD_TITLE_BUFFER_LEN, "%s%03d%s", imaginary_title, eigval, title_terminator);
00079   ititle[NEXTNANO_FIELD_TITLE_BUFFER_LEN-1]='\0';
00080   struct mstar_wavefunction_1d* wavefunc;
00081   if(unlikely((wavefunc = mstar_wavefunction_1d_new(g_list_length(wf))) == NULL)) {
00082     return NULL;
00083   }
00084   register int i;
00085   GList *l;
00086   double *v;
00087   /*We make a copy because we don't want to modify the calling wavefuntion.*/
00088   GList *m = g_list_sort(g_list_copy(wf), approx_coordinate_density_sorter);
00089   for(i=0, l=g_list_first(m); l != NULL; i++, l=g_list_next(l)) {
00090     if(unlikely((v = g_hash_table_lookup((GHashTable*)(l->data), xtitle)) == NULL)) {
00091       fprintf(stderr, "convert_netnano_mstar_wavefunction_to_general_1d: INTERNAL ERROR: a wavefunction's x coordinate (i=%d; title=\"%s\") was NULL!\n", i, xtitle);
00092       g_list_free(m);
00093       mstar_wavefunction_1d_free(wavefunc);
00094       return NULL;
00095     }
00096     ((wavefunc->points)[i]).x = *v;
00097     if(unlikely((v = g_hash_table_lookup((GHashTable*)(l->data), rtitle)) == NULL)) {
00098       fprintf(stderr, "convert_netnano_mstar_wavefunction_to_general_1d: INTERNAL ERROR: a wavefunction's real value (i=%d; title=\"%s\") was NULL!\n", i, rtitle);
00099       g_list_free(m);
00100       mstar_wavefunction_1d_free(wavefunc);
00101       return NULL;
00102     }
00103     ((wavefunc->points)[i]).re = *v;
00104     if(unlikely((v = g_hash_table_lookup((GHashTable*)(l->data), ititle)) == NULL)) {
00105       fprintf(stderr, "convert_netnano_mstar_wavefunction_to_general_1d: INTERNAL ERROR: a wavefunction's imaginary value (i=%d; title=\"%s\") was NULL!\n", i, ititle);
00106       g_list_free(m);
00107       mstar_wavefunction_1d_free(wavefunc);
00108       return NULL;
00109     }
00110     ((wavefunc->points)[i]).im = *v;
00111   }
00112   g_list_free(g_list_first(m));
00113   return wavefunc;
00114 }
 All Classes Files Functions Variables Enumerations Enumerator Defines