funkalicious 0.1

dc_sweep_n_slice.c

Go to the documentation of this file.
00001 /**Gets sweeps and slices of a dotcode wavefucntion.
00002 
00003 
00004     Copyright (C) 2011 Joseph Pingenot
00005 
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU Affero General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU Affero General Public License for more details.
00015 
00016     You should have received a copy of the GNU Affero General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <glib-2.0/glib.h>
00024 #include <gio/gio.h>
00025 #include <libpostproc/unit_conversion.h>
00026 #include <libpostproc/lp_wavefunction.h>
00027 #include <libdotcode/dotcode_wavefunction.h>
00028 #include "dc_sweep_n_slice_args.h"
00029 
00030 void write_slices_n_sweeps(const struct wavefunction* wf, const struct density* d, int* planes, int* sweeps, int* axes, int* gridsite, GFile* slices_base, GFile* sweeps_base, GFile* axes_base);
00031 
00032 int main(int argc, char* argv[]) {
00033   g_type_init();
00034   struct args args;
00035   GError *e = NULL;
00036   get_args(argc, argv, &args, &e);
00037   if(e != NULL) {
00038     if(e->code != 1) {
00039       fprintf(stderr, "ERROR processing arguments: %s\n", e->message);
00040       g_error_free(e);
00041       return 1;
00042     }
00043   }
00044   load_wavefunctions(args.wavefunctions, &e);
00045   if(e != NULL) {
00046     fprintf(stderr, "ERROR loading a requested wavefunction: %s\n", e->message);
00047     return 2;
00048   }
00049   for(GList *l = args.wavefunctions; l!=NULL; l=l->next) {
00050     struct wavefunction *wf = (struct wavefunction*)(l->data);
00051     int gridsite[3];
00052     struct density* d = NULL;
00053     if(args.use_max_probability_point) {
00054       GList *wfl = g_list_append(NULL, wf);
00055       wf->charge=1.0;
00056       d = get_density_for_list_of_wavefunctions(wfl, &e);
00057       if(e != NULL) {
00058   gchar* s =  g_file_get_uri(wf->file);
00059   fprintf(stderr, "ERROR getting density for wavefunction (%s): %s\n", s, e->message);
00060   g_free(s);
00061   return 5;
00062       }
00063       g_list_free(wfl);
00064       double max = density_get_max_and_loc(d, 1, gridsite);
00065       printf("maxium probability density (*dx^3) is %g\n", max);
00066     }else{
00067       for(int i=0; i<3; ++i) {
00068   if(args.use_gridsite) {
00069     gridsite[i] = args.gridsite[i];
00070   }else{
00071     gridsite[i] = wf->N[i]/2 + args.point[i]/wf->dx[i];
00072   }
00073       }
00074     }
00075     printf("gridsite is set to <%d, %d, %d> (<%gnm, %gnm, %gnm>)\n", gridsite[0], gridsite[1], gridsite[2], bohr_to_nm((gridsite[0]-wf->N[0]/2)*wf->dx[0]), bohr_to_nm((gridsite[1]-wf->N[1]/2)*wf->dx[1]), bohr_to_nm((gridsite[2]-wf->N[2]/2)*wf->dx[2]));
00076     if(args.bands == NULL) {
00077       char* filename = g_file_get_uri(wf->file);
00078       GString *slices_basedir = g_string_new(filename);
00079       slices_basedir = g_string_append(slices_basedir, "_dcsns/slices");
00080       GString *axes_basedir = g_string_new(filename);
00081       axes_basedir = g_string_append(axes_basedir, "_dcsns/axes");
00082       GString *sweeps_basedir = g_string_new(filename);
00083       sweeps_basedir = g_string_append(sweeps_basedir, "_dcsns/sweeps");
00084       g_free(filename);
00085       GFile* slices_base = g_file_new_for_uri(slices_basedir->str);
00086       GFile* axes_base = g_file_new_for_uri(axes_basedir->str);
00087       GFile* sweeps_base = g_file_new_for_uri(sweeps_basedir->str);
00088       g_string_free(slices_basedir, TRUE);
00089       g_string_free(axes_basedir, TRUE);
00090       g_string_free(sweeps_basedir, TRUE);
00091       GList *wfl = g_list_append(NULL, wf);
00092       wf->charge=1.0;
00093       /*If we fetched the density to get the point of maximum density, we'll reuse it.*/
00094       if(d == NULL) {
00095   d = get_density_for_list_of_wavefunctions(wfl, &e);
00096   if(e != NULL) {
00097     gchar* s =  g_file_get_uri(wf->file);
00098     fprintf(stderr, "ERROR getting density for wavefunction (%s): %s\n", s, e->message);
00099     g_free(s);
00100     return 5;
00101   }
00102       }
00103       /*Convert the coordinates to nm*/
00104       for(int i=0; i<3; ++i) d->dx[i]=bohr_to_nm(d->dx[i]);
00105       write_slices_n_sweeps(wf, d, args.planes, args.sweeps, args.axes, gridsite, slices_base, sweeps_base, axes_base);
00106       free_density(d);
00107       g_object_unref(slices_base);
00108       g_object_unref(axes_base);
00109       g_object_unref(sweeps_base);
00110     }else{
00111       /*If we fetched the density to get the point of maximum density, free it.*/
00112       if(d != NULL) free_density(d);
00113       for(GList *l = args.bands; l!=NULL; l=l->next) {
00114   d = dotcode_wavefunction_get_density_in_band(wf, *((int*)(l->data)));
00115   char* filename = g_file_get_uri(wf->file);
00116   GString *slices_basedir = g_string_new(filename);
00117   g_string_append_printf(slices_basedir, "_dcsns/band_%s_slices", dotcode_band_short_names[*(int*)(l->data)]);
00118   GString *axes_basedir = g_string_new(filename);
00119   g_string_append_printf(axes_basedir, "_dcsns/band_%s_axes", dotcode_band_short_names[*(int*)(l->data)]);
00120   GString *sweeps_basedir = g_string_new(filename);
00121   g_string_append_printf(sweeps_basedir, "_dcsns/band_%s_sweeps", dotcode_band_short_names[*(int*)(l->data)]);
00122   g_free(filename);
00123   GFile* slices_base = g_file_new_for_uri(slices_basedir->str);
00124   GFile* axes_base = g_file_new_for_uri(axes_basedir->str);
00125   GFile* sweeps_base = g_file_new_for_uri(sweeps_basedir->str);
00126   /*Convert the coordinates to nm*/
00127   for(int i=0; i<3; ++i) d->dx[i]=bohr_to_nm(d->dx[i]);
00128   write_slices_n_sweeps(wf, d, args.planes, args.sweeps, args.axes, gridsite, slices_base, sweeps_base, axes_base);
00129   free_density(d);
00130   g_string_free(slices_basedir, TRUE);
00131   g_string_free(axes_basedir, TRUE);
00132   g_string_free(sweeps_basedir, TRUE);
00133   g_object_unref(slices_base);
00134   g_object_unref(axes_base);
00135   g_object_unref(sweeps_base);
00136       }
00137     }
00138   }
00139   return 0;
00140 }
00141 
00142 
00143 void write_slices_n_sweeps(const struct wavefunction* wf, const struct density* d, int* planes, int* sweeps, int* axes, int* gridsite, GFile* slices_base, GFile* sweeps_base, GFile* axes_base) {
00144   GError *e = NULL;
00145   for(int i=0; i<3; ++i) {
00146     char* filename;
00147     if(sweeps[i]) {
00148       g_file_make_directory_with_parents(sweeps_base, NULL, &e);
00149       if(e != NULL) {
00150   if(e->code != G_IO_ERROR_EXISTS) {
00151     gchar* s = g_file_get_uri(wf->file);
00152     fprintf(stderr, "ERROR making directory for sweep along %d(%c) (directory %s): %s", i, (i==0)?'X':(i==1)?'Y':(i==2)?'Z':'?', s, e->message);
00153     g_free(s);
00154     exit(6);
00155   }
00156   g_error_free(e);
00157   e=NULL;
00158       }
00159       write_density_sweep(i, d, &filename, &e, sweeps_base, FALSE);
00160       if(e != NULL) {
00161   gchar* s = g_file_get_uri(wf->file);
00162   fprintf(stderr, "ERROR writing sweep along axis %d(%c) for wavefunction %s: %s", i, (i==0)?'X':(i==1)?'Y':(i==2)?'Z':'?', s, e->message);
00163   g_free(s);
00164   exit(6);
00165       }
00166       g_free(filename);
00167     }
00168     if(axes[i]) {
00169       g_file_make_directory_with_parents(axes_base, NULL, &e);
00170       if(e != NULL) {
00171   if(e->code != G_IO_ERROR_EXISTS) {
00172     gchar* s = g_file_get_uri(wf->file);
00173     fprintf(stderr, "ERROR making directory for axis along %d(%c) (directory %s): %s", i, (i==0)?'X':(i==1)?'Y':(i==2)?'Z':'?', s, e->message);
00174     g_free(s);
00175     exit(6);
00176   }
00177   g_error_free(e);
00178   e=NULL;
00179       }
00180       write_density_line(i, gridsite, d, &filename, &e, axes_base, '\t', "nm", "");
00181       if(e != NULL) {
00182   gchar* s = g_file_get_uri(wf->file);
00183   fprintf(stderr, "ERROR writing line along axis %d(%c) for wavefunction %s: %s", i, (i==0)?'X':(i==1)?'Y':(i==2)?'Z':'?', s, e->message);
00184   g_free(s);
00185   exit(7);
00186       }
00187       g_free(filename);
00188     }
00189     if(planes[i]) {
00190       int coord=0;
00191       /*XY->Z coordinate is to be taken*/
00192       if(planes[0]) coord=2;
00193       /*XZ->Y*/
00194       else if(planes[1]) coord=1;
00195       /*YZ->X*/
00196       else coord=0;
00197       g_file_make_directory_with_parents(slices_base, NULL, &e);
00198       if(e != NULL) {
00199   if(e->code != G_IO_ERROR_EXISTS) {
00200     gchar* s = g_file_get_uri(wf->file);
00201     fprintf(stderr, "ERROR making directory for %d(%s) plane (directory %s): %s", i, (i==0)?"XY":(i==1)?"XZ":(i==2)?"YZ":"?", s, e->message);
00202     g_free(s);
00203     exit(6);
00204   }
00205   g_error_free(e);
00206   e=NULL;
00207       }
00208       write_density_slice(coord, &gridsite[coord], d, &filename, &e, slices_base, '\t', "nm", "");
00209       if(e != NULL) {
00210   gchar* s = g_file_get_uri(wf->file);
00211   fprintf(stderr, "ERROR writing %d(%s) plane: %s", i, (i==0)?"XY":(i==1)?"XZ":(i==2)?"YZ":"?", e->message);
00212   g_free(s);
00213   exit(8);
00214       }
00215       g_free(filename);
00216     }
00217   }
00218 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines