funkalicious 0.1

density_expansion_contraction_test.c File Reference

#include <stdio.h>
#include <unistd.h>
#include <glib-2.0/glib.h>
#include <gio/gio.h>
#include <libpostproc/lp_wavefunction.c>
Include dependency graph for density_expansion_contraction_test.c:

Go to the source code of this file.

Functions

int main (int argc, char **argv)

Function Documentation

int main ( int  argc,
char **  argv 
)

Tests the density resolution-doubling and -halving routines

Copyright (C) 2011 Joseph Pingenot

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Definition at line 26 of file density_expansion_contraction_test.c.

References density::dx, e, get_density_storage_index_(), get_double_resolution_density(), get_half_resolution_density(), density::N, density::storage, write_density_slice(), and X.

                                {
  g_type_init();
  struct density d = {.N = {2, 2, 2}, .dx={0.5, 0.5, 0.5}, .storage=NULL};
  d.storage=(double*)malloc(2*2*2*sizeof(double));
  if(d.storage == NULL) {
    fprintf(stderr, "ERROR: unable to allocate storage for test density");
    return 1;
  }
  d.storage[get_density_storage_index_(0, 0, 0, d.N[0], d.N[1])] = 0;
  d.storage[get_density_storage_index_(0, 0, 1, d.N[0], d.N[1])] = 1;
  d.storage[get_density_storage_index_(0, 1, 0, d.N[0], d.N[1])] = 2;
  d.storage[get_density_storage_index_(0, 1, 1, d.N[0], d.N[1])] = 3;
  d.storage[get_density_storage_index_(1, 0, 0, d.N[0], d.N[1])] = 4;
  d.storage[get_density_storage_index_(1, 0, 1, d.N[0], d.N[1])] = 5;
  d.storage[get_density_storage_index_(1, 1, 0, d.N[0], d.N[1])] = 6;
  d.storage[get_density_storage_index_(1, 1, 1, d.N[0], d.N[1])] = 7;
  char* basedirnam = tempnam(NULL, "dect_");
  printf("WRITING ALL OUTPUT TO BASE DIRECTORY %s\n", basedirnam);
  GFile* basedir = g_file_new_for_path(basedirnam);
  GError *e = NULL;
  g_file_make_directory_with_parents(basedir, NULL, &e);
  if(e != NULL) {
    if(e->code != G_IO_ERROR_EXISTS) {
      fprintf(stderr, "ERROR creaing basedir (%s): %s", basedirnam, e->message);
      return 20;
    }
    g_error_free(e);
    e=NULL;
  }
  GFile* outdir = g_file_get_child(basedir, "original");
  char* filename = NULL;
  write_density_slice(X, NULL, &d, &filename, &e, outdir, '\t', "nm", "A.U.");
  if(e != NULL) {
    fprintf(stderr, "ERROR: unable to write original density slice to %s: %s\n", filename, e->message);
    return 2;
  }
  if(filename != NULL) free(filename);
  struct density *nd = get_double_resolution_density(&d, 7);
  if(nd == NULL) {
    fprintf(stderr, "ERROR: unable to allocate double-resolution grid.\n");
    return 3;
  }
  GFile* outdir2 = g_file_get_child(basedir, "double");
  write_density_slice(X, NULL, nd, &filename, &e, outdir2, '\t', "nm", "A.U.");
  if(e != NULL) {
    fprintf(stderr, "ERROR: unable to write double-resolution density slice to file %s: %s\n", filename, e->message);
    return 4;
  }
  if(filename != NULL) free(filename);
  int errored=0;
  for(int i=0; i<3; i++) {
    if(nd->N[i] != (d.N[i]*2-1)) {
      errored=1;
      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);
    }
    if(nd->dx[i] != d.dx[i]/2.0) {
      errored=1;
      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);
    }
    printf("GOT DOUBLE-GRID N[%d]=%d dx[%d]=%g\n", i, nd->N[i], i, nd->dx[i]);
  }
  if(errored) return 50;

  struct density *hd = get_half_resolution_density(nd, &e, 7);
  if((hd == NULL) || (e != NULL)) {
    if(e == NULL) {
      fprintf(stderr, "ERROR: error creating half-resolution grid, but no message was given!\n");
    }else{
      fprintf(stderr, "ERROR: error creating half-resolution grid: %s\n", e->message);
    }
    return 5;
  }
  GFile* outdir3 = g_file_get_child(basedir, "half");
  write_density_slice(X, NULL, hd, &filename, &e, outdir3, '\t', "nm", "A.U.");
  if(e != NULL) {
    fprintf(stderr, "ERROR: unable to write half-resolution density slice to file %s: %s\n", filename, e->message);
    return 6;
  }
  if(filename != NULL) free(filename);
  errored=0;
  for(int i=0; i<3; i++) {
    if(hd->N[i] != d.N[i]) {
      errored=1;
      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]);
    }
    if(hd->dx[i] != d.dx[i]) {
      errored=1;
      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]);
    }
  }
  if(errored) return 51;
  return 0;
}

Here is the call graph for this function:

 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines