|
gimme-alpha 0.1
|
00001 /* 00002 ** alpha_calc-suite.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 Fri Apr 3 13:56: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 <stdlib.h> 00028 #include <CUnit/CUnit.h> 00029 #include "tests/cunit-local.h" 00030 #include <glib.h> 00031 #include "argdefaults.h" 00032 #include "matdb.h" 00033 #include "alpha_calc.h" 00034 #include "alpha_calc-suite.h" 00035 #include "wavefunction.h" 00036 00037 double* average_arrays(double *arrays[], int n_arrays, int array_size); 00038 char* find_left_right_of_iface(double iface, int* prev, int* next, double *x, int N); 00039 00040 void check_iface_indices(void) { 00041 00042 } 00043 00044 void check_average_arrays(void) { 00045 double arr1[] = {0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; 00046 double arr2[] = {7.0, 3.0, -4.0, 5.0, 7.0, 12.0, 13.0, 0.0, -5.0}; 00047 double arr3[] = {7.0/2.0, 4.0/2.0, -2.0/2.0, 8.0/2.0, 11.0/2.0, 17.0/2.0, 19.0/2.0, 7.0/2.0, 3.0/2.0}; 00048 double *arrs[] = {arr1, arr2}; 00049 double *avg = average_arrays(arrs, 2, 9); 00050 CU_ASSERT(avg != NULL); 00051 if(avg == NULL) return; 00052 GString *s = g_string_new_len("", 1024); 00053 for(int i=0; i<9; i++) { 00054 if(avg[i] == arr3[i]) { 00055 g_string_printf(s, "OK: avg[%d](%g) == arg3[%d](%g)", i, avg[i], i, arr3[i]); 00056 CU_PASS_STRING(s->str); 00057 }else{ 00058 g_string_printf(s, "FAIL: avg[%d](%g) != arg3[%d](%g)", i, avg[i], i, arr3[i]); 00059 CU_FAIL_STRING(s->str); 00060 fprintf(stderr, "%s\n", s->str); 00061 } 00062 } 00063 g_string_free(s, TRUE); 00064 if(avg != NULL) free(avg); 00065 } 00066 00067 #define NUM_POINTS 4096 00068 #define MINX -10.0 00069 #define MAXX 10.0 00070 #define SIGMA 1.0 00071 void avg_discrete_func_gaussian_1(void) { 00072 struct mstar_wavefunction_1d* wf = mstar_wavefunction_1d_new_gaussian(NUM_POINTS, MINX, MAXX, SIGMA); 00073 CU_ASSERT(wf != NULL); 00074 double func[NUM_POINTS]; 00075 double x[NUM_POINTS]; 00076 double err; 00077 GString *s = g_string_new_len("", 1024); 00078 for(int i=0; i<NUM_POINTS; i++) {func[i]=1.0, x[i] = (wf->points)[i].x;} 00079 double result = average_discrete_function(func, x, NUM_POINTS, wf, 0, 1e-8, 1e-8, &err); 00080 if(fabs(result - 1.0) <= fabs(CLOSE_ENOUGH)) { 00081 g_string_printf(s, "OK: expectation value of 1.0 with gaussian wavefunc (%g) == 1.0 (err=%g", result, err); 00082 CU_PASS_STRING(s->str); 00083 }else{ 00084 g_string_printf(s, "FAIL: expectation value of 1.0 with gaussian wavefunc (%g) == 1.0 (err=%g", result, err); 00085 CU_FAIL_STRING(s->str); 00086 } 00087 g_string_free(s, TRUE); 00088 mstar_wavefunction_1d_free(wf); 00089 } 00090 00091 void avg_discrete_func_gaussian_x(void) { 00092 struct mstar_wavefunction_1d* wf = mstar_wavefunction_1d_new_gaussian(NUM_POINTS, MINX, MAXX, SIGMA); 00093 CU_ASSERT(wf != NULL); 00094 double func[NUM_POINTS]; 00095 double x[NUM_POINTS]; 00096 double err; 00097 GString *s = g_string_new_len("", 1024); 00098 for(int i=0; i<NUM_POINTS; i++) {x[i] = (wf->points)[i].x; func[i] = x[i];} 00099 double result = average_discrete_function(func, x, NUM_POINTS, wf, 0, 1e-8, 1e-8, &err); 00100 if(fabs(result - 0.0) <= fabs(CLOSE_ENOUGH)) { 00101 g_string_printf(s, "OK: expectation value of x with gaussian wavefunc (%g) == 0.0 (err=%g)", result, err); 00102 CU_PASS_STRING(s->str); 00103 }else{ 00104 g_string_printf(s, "FAIL: expectation value of x with gaussian wavefunc (%g) != 0.0 (err=%g)", result, err); 00105 CU_FAIL_STRING(s->str); 00106 } 00107 g_string_free(s, TRUE); 00108 mstar_wavefunction_1d_free(wf); 00109 } 00110 00111 void alpha_calc_suite_add_tests(CU_pSuite suite) { 00112 CU_add_test(suite, "interface_indices", check_iface_indices); 00113 CU_add_test(suite, "check average arrays", check_average_arrays); 00114 CU_add_test(suite, "averaging 1 with gaussian", avg_discrete_func_gaussian_1); 00115 CU_add_test(suite, "averaging x with gaussian", avg_discrete_func_gaussian_x); 00116 } 00117