|
13 | 13 | * |
14 | 14 | * You should have received a copy of the GNU Lesser General Public License |
15 | 15 | * along with dftd4. If not, see <https://www.gnu.org/licenses/>. |
16 | | -**/ |
| 16 | + **/ |
17 | 17 |
|
18 | 18 | #include <stdio.h> |
19 | 19 | #include <stdlib.h> |
20 | 20 |
|
21 | 21 | #include "dftd4.h" |
22 | 22 |
|
23 | | -int main (void) |
| 23 | +static inline void |
| 24 | +show_error(dftd4_error error) |
24 | 25 | { |
25 | | - int const natoms = 7; |
26 | | - int const nat_sq = natoms * natoms; |
27 | | - int const nat3 = natoms * 3; |
28 | | - int const nat3_sq = nat3 * nat3; |
29 | | - int const attyp[7] = {6,6,6,1,1,1,1}; |
30 | | - double const coord[21] = |
31 | | - {0.00000000000000, 0.00000000000000,-1.79755622305860, |
32 | | - 0.00000000000000, 0.00000000000000, 0.95338756106749, |
33 | | - 0.00000000000000, 0.00000000000000, 3.22281255790261, |
34 | | - -0.96412815539807,-1.66991895015711,-2.53624948351102, |
35 | | - -0.96412815539807, 1.66991895015711,-2.53624948351102, |
36 | | - 1.92825631079613, 0.00000000000000,-2.53624948351102, |
37 | | - 0.00000000000000, 0.00000000000000, 5.23010455462158}; |
38 | | - double energy; |
39 | | - double sigma[9]; |
40 | | - double* pair_disp2; |
41 | | - double* pair_disp3; |
42 | | - double* gradient; |
43 | | - double* hessian; |
44 | | - double* c6; |
45 | | - |
46 | | - |
47 | | - pair_disp2 = (double*) malloc(nat_sq * sizeof(double)); |
48 | | - pair_disp3 = (double*) malloc(nat_sq * sizeof(double)); |
49 | | - gradient = (double*) malloc(nat3 * sizeof(double)); |
50 | | - hessian = (double*) malloc(nat3_sq * sizeof(double)); |
51 | | - c6 = (double*) malloc(nat_sq * sizeof(double)); |
52 | | - |
53 | | - |
54 | | - if (dftd4_get_version() <= 0) {return 1;} |
55 | | - |
56 | | - dftd4_error error; |
57 | | - dftd4_structure mol; |
58 | | - dftd4_model disp; |
59 | | - dftd4_param param; |
60 | | - |
61 | | - error = dftd4_new_error(); |
62 | | - if (!error) {return 1;} |
63 | | - |
64 | | - mol = dftd4_new_structure(error, natoms, attyp, coord, NULL, NULL, NULL); |
65 | | - if (dftd4_check_error(error)) {return 1;}; |
66 | | - if (!mol) {return 1;} |
67 | | - |
68 | | - disp = dftd4_new_d4_model(error, mol); |
69 | | - if (dftd4_check_error(error)) {return 1;} |
70 | | - if (!disp) {return 1;} |
71 | | - |
72 | | - // C6 coefficients |
73 | | - dftd4_get_properties(error, mol, disp, NULL, NULL, c6, NULL); |
74 | | - if (dftd4_check_error(error)) {return 1;} |
75 | | - |
76 | | - // PBE-D4 |
77 | | - param = dftd4_new_rational_damping(error, 1.0, 0.95948085, 0.0, 0.38574991, 4.80688534, 16.0); |
78 | | - if (dftd4_check_error(error)) {return 1;} |
79 | | - if (!param) {return 1;} |
80 | | - |
81 | | - dftd4_get_dispersion(error, mol, disp, param, &energy, NULL, NULL); |
82 | | - if (dftd4_check_error(error)) {return 1;} |
83 | | - dftd4_get_dispersion(error, mol, disp, param, &energy, gradient, sigma); |
84 | | - if (dftd4_check_error(error)) {return 1;} |
85 | | - dftd4_get_numerical_hessian(error, mol, disp, param, hessian); |
86 | | - if (dftd4_check_error(error)) {return 1;} |
87 | | - |
88 | | - dftd4_get_pairwise_dispersion(error, mol, disp, param, pair_disp2, pair_disp3); |
89 | | - if (dftd4_check_error(error)) {return 1;} |
90 | | - dftd4_delete(param); |
91 | | - |
92 | | - // DSD-BLYP-D4-ATM |
93 | | - param = dftd4_load_rational_damping(error, "dsdblyp", true); |
94 | | - if (dftd4_check_error(error)) {return 1;} |
95 | | - if (!param) {return 1;} |
96 | | - |
97 | | - dftd4_get_dispersion(error, mol, disp, param, &energy, NULL, NULL); |
98 | | - if (dftd4_check_error(error)) {return 1;} |
99 | | - dftd4_get_dispersion(error, mol, disp, param, &energy, gradient, sigma); |
100 | | - if (dftd4_check_error(error)) {return 1;} |
101 | | - dftd4_get_numerical_hessian(error, mol, disp, param, hessian); |
102 | | - if (dftd4_check_error(error)) {return 1;} |
103 | | - dftd4_delete(param); |
104 | | - |
105 | | - dftd4_delete(disp); |
106 | | - dftd4_delete(mol); |
107 | | - dftd4_delete(error); |
108 | | - |
109 | | - free(pair_disp2); |
110 | | - free(pair_disp3); |
111 | | - free(gradient); |
112 | | - free(hessian); |
113 | | - free(c6); |
114 | | - |
115 | | - if (param) {return 1;} |
116 | | - if (disp) {return 1;} |
117 | | - if (mol) {return 1;} |
118 | | - if (error) {return 1;} |
119 | | - |
120 | | - return 0; |
| 26 | + char message[512]; |
| 27 | + dftd4_get_error(error, message, NULL); |
| 28 | + printf("[Message] %s\n", message); |
| 29 | +} |
| 30 | + |
| 31 | +int test_uninitialized_error(void) |
| 32 | +{ |
| 33 | + printf("Start test: uninitialized error\n"); |
| 34 | + dftd4_error error = NULL; |
| 35 | + return dftd4_check_error(error) ? 0 : 1; |
| 36 | +} |
| 37 | + |
| 38 | +int test_uninitialized_structure(void) |
| 39 | +{ |
| 40 | + printf("Start test: uninitialized structure\n"); |
| 41 | + dftd4_error error = NULL; |
| 42 | + dftd4_structure mol = NULL; |
| 43 | + |
| 44 | + error = dftd4_new_error(); |
| 45 | + |
| 46 | + double xyz[6] = { 0.0 }; |
| 47 | + dftd4_update_structure(error, mol, xyz, NULL); |
| 48 | + if (!dftd4_check_error(error)) |
| 49 | + goto unexpected; |
| 50 | + |
| 51 | + show_error(error); |
| 52 | + |
| 53 | + dftd4_delete(error); |
| 54 | + return 0; |
| 55 | + |
| 56 | +unexpected: |
| 57 | + printf("[Fatal] Unexpected pass for unititalized-structure test\n"); |
| 58 | + dftd4_delete(error); |
| 59 | + return 1; |
| 60 | +} |
| 61 | + |
| 62 | +int test_example(void) |
| 63 | +{ |
| 64 | + printf("Start test: example\n"); |
| 65 | + int const natoms = 7; |
| 66 | + int const nat_sq = natoms * natoms; |
| 67 | + int const nat3 = natoms * 3; |
| 68 | + int const nat3_sq = nat3 * nat3; |
| 69 | + int const attyp[7] = { 6, 6, 6, 1, 1, 1, 1 }; |
| 70 | + double const coord[21] = { |
| 71 | + +0.00000000000000, +0.00000000000000, -1.79755622305860, |
| 72 | + +0.00000000000000, +0.00000000000000, +0.95338756106749, |
| 73 | + +0.00000000000000, +0.00000000000000, +3.22281255790261, |
| 74 | + -0.96412815539807, -1.66991895015711, -2.53624948351102, |
| 75 | + -0.96412815539807, +1.66991895015711, -2.53624948351102, |
| 76 | + +1.92825631079613, +0.00000000000000, -2.53624948351102, |
| 77 | + +0.00000000000000, +0.00000000000000, +5.23010455462158 }; |
| 78 | + double energy; |
| 79 | + double sigma[9]; |
| 80 | + double* pair_disp2; |
| 81 | + double* pair_disp3; |
| 82 | + double* gradient; |
| 83 | + double* hessian; |
| 84 | + double* c6; |
| 85 | + |
| 86 | + pair_disp2 = (double*)malloc(nat_sq * sizeof(double)); |
| 87 | + pair_disp3 = (double*)malloc(nat_sq * sizeof(double)); |
| 88 | + gradient = (double*)malloc(nat3 * sizeof(double)); |
| 89 | + hessian = (double*)malloc(nat3_sq * sizeof(double)); |
| 90 | + c6 = (double*)malloc(nat_sq * sizeof(double)); |
| 91 | + |
| 92 | + if (dftd4_get_version() <= 0) { |
| 93 | + goto err; |
| 94 | + } |
| 95 | + |
| 96 | + dftd4_error error; |
| 97 | + dftd4_structure mol; |
| 98 | + dftd4_model disp; |
| 99 | + dftd4_param param; |
| 100 | + |
| 101 | + error = dftd4_new_error(); |
| 102 | + if (!error) { |
| 103 | + goto err; |
| 104 | + } |
| 105 | + |
| 106 | + mol = dftd4_new_structure(error, natoms, attyp, coord, NULL, NULL, NULL); |
| 107 | + if (dftd4_check_error(error)) { |
| 108 | + goto err; |
| 109 | + }; |
| 110 | + if (!mol) { |
| 111 | + goto err; |
| 112 | + } |
| 113 | + |
| 114 | + disp = dftd4_new_d4_model(error, mol); |
| 115 | + if (dftd4_check_error(error)) { |
| 116 | + goto err; |
| 117 | + } |
| 118 | + if (!disp) { |
| 119 | + goto err; |
| 120 | + } |
| 121 | + |
| 122 | + // C6 coefficients |
| 123 | + dftd4_get_properties(error, mol, disp, NULL, NULL, c6, NULL); |
| 124 | + if (dftd4_check_error(error)) { |
| 125 | + goto err; |
| 126 | + } |
| 127 | + |
| 128 | + // PBE-D4 |
| 129 | + param = dftd4_new_rational_damping(error, 1.0, 0.95948085, 0.0, 0.38574991, 4.80688534, 16.0); |
| 130 | + if (dftd4_check_error(error)) { |
| 131 | + goto err; |
| 132 | + } |
| 133 | + if (!param) { |
| 134 | + goto err; |
| 135 | + } |
| 136 | + |
| 137 | + dftd4_get_dispersion(error, mol, disp, param, &energy, NULL, NULL); |
| 138 | + if (dftd4_check_error(error)) { |
| 139 | + goto err; |
| 140 | + } |
| 141 | + dftd4_get_dispersion(error, mol, disp, param, &energy, gradient, sigma); |
| 142 | + if (dftd4_check_error(error)) { |
| 143 | + goto err; |
| 144 | + } |
| 145 | + dftd4_get_numerical_hessian(error, mol, disp, param, hessian); |
| 146 | + if (dftd4_check_error(error)) { |
| 147 | + goto err; |
| 148 | + } |
| 149 | + |
| 150 | + dftd4_get_pairwise_dispersion(error, mol, disp, param, pair_disp2, pair_disp3); |
| 151 | + if (dftd4_check_error(error)) { |
| 152 | + goto err; |
| 153 | + } |
| 154 | + dftd4_delete(param); |
| 155 | + |
| 156 | + // DSD-BLYP-D4-ATM |
| 157 | + param = dftd4_load_rational_damping(error, "dsdblyp", true); |
| 158 | + if (dftd4_check_error(error)) { |
| 159 | + goto err; |
| 160 | + } |
| 161 | + if (!param) { |
| 162 | + goto err; |
| 163 | + } |
| 164 | + |
| 165 | + dftd4_get_dispersion(error, mol, disp, param, &energy, NULL, NULL); |
| 166 | + if (dftd4_check_error(error)) { |
| 167 | + goto err; |
| 168 | + } |
| 169 | + dftd4_get_dispersion(error, mol, disp, param, &energy, gradient, sigma); |
| 170 | + if (dftd4_check_error(error)) { |
| 171 | + goto err; |
| 172 | + } |
| 173 | + dftd4_get_numerical_hessian(error, mol, disp, param, hessian); |
| 174 | + if (dftd4_check_error(error)) { |
| 175 | + goto err; |
| 176 | + } |
| 177 | + dftd4_delete(param); |
| 178 | + |
| 179 | + dftd4_delete(disp); |
| 180 | + dftd4_delete(mol); |
| 181 | + dftd4_delete(error); |
| 182 | + |
| 183 | + if (param) { |
| 184 | + goto err; |
| 185 | + } |
| 186 | + if (disp) { |
| 187 | + goto err; |
| 188 | + } |
| 189 | + if (mol) { |
| 190 | + goto err; |
| 191 | + } |
| 192 | + if (error) { |
| 193 | + goto err; |
| 194 | + } |
| 195 | + |
| 196 | + free(pair_disp2); |
| 197 | + free(pair_disp3); |
| 198 | + free(gradient); |
| 199 | + free(hessian); |
| 200 | + free(c6); |
| 201 | + |
| 202 | + return 0; |
| 203 | + |
| 204 | +err: |
| 205 | + if (dftd4_check_error(error)) { |
| 206 | + char message[512]; |
| 207 | + dftd4_get_error(error, message, NULL); |
| 208 | + printf("[Fatal] %s\n", message); |
| 209 | + } |
| 210 | + |
| 211 | + dftd4_delete(param); |
| 212 | + dftd4_delete(disp); |
| 213 | + dftd4_delete(mol); |
| 214 | + dftd4_delete(error); |
| 215 | + |
| 216 | + free(pair_disp2); |
| 217 | + free(pair_disp3); |
| 218 | + free(gradient); |
| 219 | + free(hessian); |
| 220 | + free(c6); |
| 221 | + |
| 222 | + return 1; |
| 223 | +} |
| 224 | + |
| 225 | +int main(void) |
| 226 | +{ |
| 227 | + int stat = 0; |
| 228 | + stat += test_uninitialized_error(); |
| 229 | + stat += test_uninitialized_structure(); |
| 230 | + stat += test_example(); |
| 231 | + |
| 232 | + return stat == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
121 | 233 | } |
0 commit comments