k-dot-p 0.1

generic_structure.c++

Go to the documentation of this file.
00001 /*
00002 
00003     Copyright (C) 2009 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 <stdlib.h>
00021 #include <glib.h>
00022 #include <hdf5.h>
00023 #include <libmodelxx/grid.h++>
00024 #include <libmodelxx/material.h++>
00025 #include <libmodelxx/generic_structure.h++>
00026 
00027 namespace kdotp {
00028   namespace libmodelxx {
00029     namespace structure {
00030 
00031       #define GERROR_DOMAIN_GENERIC_STRUCTURE 51
00032 
00033       static bool material_is_same(material** a, material** b) {
00034   return **a == **b;
00035       }
00036       
00037       char* material_to_string(libmodelxx::structure::material** value) {
00038   return (*value)->get_name();
00039       }
00040 
00041       char* double_to_string(double* value) {
00042   GString *s = g_string_new_len("", 1024);
00043   g_string_printf(s, "%g", *value);
00044   char* str = s->str;
00045   g_string_free(s, FALSE);
00046   return str;
00047       }
00048 
00049       GError* generic_structure_save(struct generic_structure *s, const char* filename) {
00050   /*
00051   hid_t file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
00052   herr_t status;
00053   hsize_t one = 1;
00054   */
00055   /*Create a one-item space*/
00056   /*
00057   hid_t dspace_id_single = H5Create_simple(1, &one, NULL);
00058   hid_t dset_id_size;
00059   */
00060   return g_error_new(GERROR_DOMAIN_GENERIC_STRUCTURE, 0, "kdotp::libmodelxx::structure::generic_structure::generic_structure_save: this function is unimplemented.");
00061       }
00062 
00063       GError* generic_structure_restore(struct generic_structure **s, const char* filename) {
00064   return g_error_new(GERROR_DOMAIN_GENERIC_STRUCTURE, 0, "kdotp::libmodelxx::structure::generic_structure::generic_structure_restore: this function is unimplemented.");
00065       }
00066 
00067       GError* generic_structure_dump(struct generic_structure *s, GFile* file) {
00068   GError *e = NULL;
00069   GFileOutputStream* gfos = g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &e);
00070   if(e != NULL) {
00071     GError *err = NULL;
00072     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_dump: failed to open file.");
00073     return err;
00074   }
00075 
00076   e = grid::dump_indexed_grid<libmodelxx::structure::material*>((GOutputStream*)gfos, s->g->t, s->g->dimensions, s->material_grid, material_is_same, material_to_string);
00077   if(e != NULL) {
00078     GError *err = NULL;
00079     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_dump: failed to dump indexed material grid to file");
00080     return err;
00081   }
00082 
00083   e = grid::dump_grid<double>((GOutputStream*)gfos, s->g->t, s->g->dimensions, s->potential_grid, double_to_string);
00084   if(e != NULL) {
00085     GError *err = NULL;
00086     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_dump: failed to dump potential grid to file");
00087     return err;
00088   }
00089 
00090   e = grid::dump_grid<double>((GOutputStream*)gfos, s->g->t, s->g->dimensions, s->doping_grid, double_to_string);
00091   if(e != NULL) {
00092     GError *err = NULL;
00093     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_dump: failed to dump doping grid to file");
00094     return err;
00095   }
00096 
00097   /*
00098   e = grid::dump_grid<double, 6>((GOutputStream*)gfos, s->g->t, s->g->dimensions, s->strain_grid, double_to_string);
00099   if(e != NULL) {
00100     GError *err = NULL;
00101     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_dump: failed to dump strain grid to file");
00102     return err;
00103   }
00104   */
00105 
00106   g_object_unref(gfos);
00107   return NULL;
00108       }
00109 
00110       struct generic_structure* generic_structure_new(struct grid::grid *g) {
00111   struct generic_structure *s = (struct generic_structure*)malloc(sizeof(struct generic_structure));
00112   if(s == NULL) return NULL;
00113   s->g = g;
00114   GError *e = NULL;
00115   //s->material_grid = grid::set_up_grid<libmodelxx::structure::material>(g, NULL, &e);
00116   s->material_grid = grid::set_up_grid<libmodelxx::structure::material*>(g, NULL, &e);
00117   if(e != NULL) {
00118     GError *err = NULL;
00119     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_new: failed to create new material grid");
00120     //return err;
00121   }
00122   s->potential_grid = grid::set_up_grid<double>(g, 0, &e);
00123   if(e != NULL) {
00124     GError *err = NULL;
00125     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_new: failed to create new potential grid");
00126     //return err;
00127   }
00128   s->doping_grid = grid::set_up_grid<double>(g, 0, &e);
00129   if(e != NULL) {
00130     GError *err = NULL;
00131     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_new: failed to create new doping grid");
00132     //return err;
00133   }
00134   /*
00135   s->strain_grid = grid::set_up_grid<double, 6>(g, 0, &e);
00136   if(e != NULL) {
00137     GError *err = NULL;
00138     g_propagate_prefixed_error(&err, e, "kdotp::libmodelxx::structure::generic_structure.c++::generic_structure_new: failed to create new strain grid");
00139     //return err;
00140   }
00141   */
00142   return s;
00143       }
00144 
00145       GError* generic_structure_free(struct generic_structure* s) {
00146   /*TODO: hash out how to free specific arrays.*/
00147   GError *e=NULL;
00148   e=grid::free_grid_class<libmodelxx::structure::material*>(s->g->t, s->g->dimensions, s->material_grid);
00149   if(e != NULL) return e;
00150   e=grid::free_grid_class<double>(s->g->t, s->g->dimensions, s->potential_grid);
00151   if(e != NULL) return e;
00152   e=grid::free_grid_class<double>(s->g->t, s->g->dimensions, s->doping_grid);
00153   if(e != NULL) return e;
00154   /*
00155   e=grid::free_grid_class<double, 6>(s->g->t, s->g->dimensions, s->strain_grid);
00156   if(e != NULL) return e;
00157   */
00158   free(s);
00159   return NULL;
00160       }
00161 
00162       void generic_structure_free_potential(struct generic_structure* s) {
00163   grid::free_grid_class<double>(s->g->t, s->g->dimensions, s->potential_grid);
00164   s->potential_grid = NULL;
00165       }
00166 
00167     
00168     }
00169   }
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines