|
funkalicious 0.1
|
00001 /**Tests the density resolution-doubling and -halving routines 00002 00003 Copyright (C) 2011 Joseph Pingenot 00004 00005 This program is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU Affero General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU Affero General Public License for more details. 00014 00015 You should have received a copy of the GNU Affero General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 00018 */ 00019 00020 #include <stdio.h> 00021 #include <unistd.h> 00022 #include <glib-2.0/glib.h> 00023 #include <gio/gio.h> 00024 #include <libpostproc/lp_wavefunction.c> 00025 00026 int main(int argc, char** argv) { 00027 g_type_init(); 00028 struct density d = {.N = {2, 2, 2}, .dx={0.5, 0.5, 0.5}, .storage=NULL}; 00029 d.storage=(double*)malloc(2*2*2*sizeof(double)); 00030 if(d.storage == NULL) { 00031 fprintf(stderr, "ERROR: unable to allocate storage for test density"); 00032 return 1; 00033 } 00034 d.storage[get_density_storage_index_(0, 0, 0, d.N[0], d.N[1])] = 0; 00035 d.storage[get_density_storage_index_(0, 0, 1, d.N[0], d.N[1])] = 1; 00036 d.storage[get_density_storage_index_(0, 1, 0, d.N[0], d.N[1])] = 2; 00037 d.storage[get_density_storage_index_(0, 1, 1, d.N[0], d.N[1])] = 3; 00038 d.storage[get_density_storage_index_(1, 0, 0, d.N[0], d.N[1])] = 4; 00039 d.storage[get_density_storage_index_(1, 0, 1, d.N[0], d.N[1])] = 5; 00040 d.storage[get_density_storage_index_(1, 1, 0, d.N[0], d.N[1])] = 6; 00041 d.storage[get_density_storage_index_(1, 1, 1, d.N[0], d.N[1])] = 7; 00042 char* basedirnam = tempnam(NULL, "dect_"); 00043 printf("WRITING ALL OUTPUT TO BASE DIRECTORY %s\n", basedirnam); 00044 GFile* basedir = g_file_new_for_path(basedirnam); 00045 GError *e = NULL; 00046 g_file_make_directory_with_parents(basedir, NULL, &e); 00047 if(e != NULL) { 00048 if(e->code != G_IO_ERROR_EXISTS) { 00049 fprintf(stderr, "ERROR creaing basedir (%s): %s", basedirnam, e->message); 00050 return 20; 00051 } 00052 g_error_free(e); 00053 e=NULL; 00054 } 00055 GFile* outdir = g_file_get_child(basedir, "original"); 00056 char* filename = NULL; 00057 write_density_slice(X, NULL, &d, &filename, &e, outdir, '\t', "nm", "A.U."); 00058 if(e != NULL) { 00059 fprintf(stderr, "ERROR: unable to write original density slice to %s: %s\n", filename, e->message); 00060 return 2; 00061 } 00062 if(filename != NULL) free(filename); 00063 struct density *nd = get_double_resolution_density(&d, 7); 00064 if(nd == NULL) { 00065 fprintf(stderr, "ERROR: unable to allocate double-resolution grid.\n"); 00066 return 3; 00067 } 00068 GFile* outdir2 = g_file_get_child(basedir, "double"); 00069 write_density_slice(X, NULL, nd, &filename, &e, outdir2, '\t', "nm", "A.U."); 00070 if(e != NULL) { 00071 fprintf(stderr, "ERROR: unable to write double-resolution density slice to file %s: %s\n", filename, e->message); 00072 return 4; 00073 } 00074 if(filename != NULL) free(filename); 00075 int errored=0; 00076 for(int i=0; i<3; i++) { 00077 if(nd->N[i] != (d.N[i]*2-1)) { 00078 errored=1; 00079 fprintf(stderr, "ERROR: doubled-grid N[%d]=%d not the same as the predicted N[%d]=%d->%d!\n", i, nd->N[i], i, d.N[i], d.N[i]*2-1); 00080 } 00081 if(nd->dx[i] != d.dx[i]/2.0) { 00082 errored=1; 00083 fprintf(stderr, "ERROR: doubled-grid dx[%d]=%g not the same as the original dx[%d]=%g->%g!\n", i, nd->dx[i], i, d.dx[i], d.dx[i]/2.0); 00084 } 00085 printf("GOT DOUBLE-GRID N[%d]=%d dx[%d]=%g\n", i, nd->N[i], i, nd->dx[i]); 00086 } 00087 if(errored) return 50; 00088 00089 struct density *hd = get_half_resolution_density(nd, &e, 7); 00090 if((hd == NULL) || (e != NULL)) { 00091 if(e == NULL) { 00092 fprintf(stderr, "ERROR: error creating half-resolution grid, but no message was given!\n"); 00093 }else{ 00094 fprintf(stderr, "ERROR: error creating half-resolution grid: %s\n", e->message); 00095 } 00096 return 5; 00097 } 00098 GFile* outdir3 = g_file_get_child(basedir, "half"); 00099 write_density_slice(X, NULL, hd, &filename, &e, outdir3, '\t', "nm", "A.U."); 00100 if(e != NULL) { 00101 fprintf(stderr, "ERROR: unable to write half-resolution density slice to file %s: %s\n", filename, e->message); 00102 return 6; 00103 } 00104 if(filename != NULL) free(filename); 00105 errored=0; 00106 for(int i=0; i<3; i++) { 00107 if(hd->N[i] != d.N[i]) { 00108 errored=1; 00109 fprintf(stderr, "ERROR: half-doubled-grid N[%d]=%d not the same as the original N[%d]=%d!\n", i, hd->N[i], i, d.N[i]); 00110 } 00111 if(hd->dx[i] != d.dx[i]) { 00112 errored=1; 00113 fprintf(stderr, "ERROR: half-doubled-grid dx[%d]=%g not the same as the original dx[%d]=%g!\n", i, hd->dx[i], i, d.dx[i]); 00114 } 00115 } 00116 if(errored) return 51; 00117 return 0; 00118 }