$darkmode
DENOPTIM
ChemicalObjectModelTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2024 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.molecularmodeling;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertNotNull;
23import static org.junit.jupiter.api.Assertions.assertTrue;
24
25import java.util.ArrayList;
26import java.util.logging.Logger;
27
28import javax.vecmath.Point3d;
29
30import org.junit.jupiter.api.Test;
31import org.openscience.cdk.Atom;
32import org.openscience.cdk.interfaces.IAtom;
33import org.openscience.cdk.interfaces.IAtomContainer;
34import org.openscience.cdk.silent.SilentChemObjectBuilder;
35
36import denoptim.constants.DENOPTIMConstants;
37import denoptim.graph.DGraph;
38import denoptim.molecularmodeling.zmatrix.ZMatrix;
39import denoptim.molecularmodeling.zmatrix.ZMatrixAtom;
40
48{
49 private Logger logger = Logger.getLogger("TestLogger");
50
51//------------------------------------------------------------------------------
52
53 @Test
54 public void testUpdateXYZFromINT_FirstAtomAtOrigin() throws Exception
55 {
56 // Create a simple molecule with one atom
57 IAtomContainer fmol = SilentChemObjectBuilder.getInstance().newAtomContainer();
58 IAtom atom0 = new Atom("H");
59 fmol.addAtom(atom0);
60
61 ZMatrix zmat = new ZMatrix();
62 ZMatrixAtom zAtom0 = new ZMatrixAtom(
63 0, "H", "H.1", null, null, null,
64 null, null, null, null);
65 zmat.addAtom(zAtom0);
66
67 DGraph molGraph = new DGraph();
69 molGraph, fmol, zmat, "test",
70 new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
71 logger);
72
73 model.updateXYZFromINT();
74
75 Point3d coord0 = fmol.getAtom(0).getPoint3d();
76 assertNotNull(coord0);
77 assertEquals(0.0, coord0.x, 1e-10, "First atom X coordinate");
78 assertEquals(0.0, coord0.y, 1e-10, "First atom Y coordinate");
79 assertEquals(0.0, coord0.z, 1e-10, "First atom Z coordinate");
80 }
81
82//------------------------------------------------------------------------------
83
84 @Test
85 public void testUpdateXYZFromINT_SecondAtomAlongZAxis() throws Exception
86 {
87 // Create a molecule with two atoms - second along Z axis
88 IAtomContainer fmol = SilentChemObjectBuilder.getInstance().newAtomContainer();
89 IAtom atom0 = new Atom("H");
90 IAtom atom1 = new Atom("H");
91 fmol.addAtom(atom0);
92 fmol.addAtom(atom1);
93
94 ZMatrix zmat = new ZMatrix();
95 ZMatrixAtom zAtom0 = new ZMatrixAtom(
96 0, "H", "H.1", null, null, null,
97 null, null, null, null);
98 ZMatrixAtom zAtom1 = new ZMatrixAtom(
99 1, "H", "H.1", zAtom0, null, null,
100 1.5, null, null, null);
101 zmat.addAtom(zAtom0);
102 zmat.addAtom(zAtom1);
103
104 DGraph molGraph = new DGraph();
106 molGraph, fmol, zmat, "test",
107 new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
108 logger);
109
110 model.updateXYZFromINT();
111
112 Point3d coord0 = fmol.getAtom(0).getPoint3d();
113 Point3d coord1 = fmol.getAtom(1).getPoint3d();
114
115 assertEquals(0.0, coord0.x, 1e-10, "First atom X");
116 assertEquals(0.0, coord0.y, 1e-10, "First atom Y");
117 assertEquals(0.0, coord0.z, 1e-10, "First atom Z");
118
119 assertEquals(0.0, coord1.x, 1e-10, "Second atom X");
120 assertEquals(0.0, coord1.y, 1e-10, "Second atom Y");
121 assertEquals(1.5, coord1.z, 1e-10, "Second atom Z (bond length)");
122 }
123
124//------------------------------------------------------------------------------
125
126 @Test
127 public void testUpdateXYZFromINT_ThirdAtomWithAngle() throws Exception
128 {
129 // Create a molecule with three atoms - third with bond and angle
130 IAtomContainer fmol = SilentChemObjectBuilder.getInstance().newAtomContainer();
131 IAtom atom0 = new Atom("H");
132 IAtom atom1 = new Atom("O");
133 IAtom atom2 = new Atom("H");
134 fmol.addAtom(atom0);
135 fmol.addAtom(atom1);
136 fmol.addAtom(atom2);
137
138 ZMatrix zmat = new ZMatrix();
139 ZMatrixAtom zAtom0 = new ZMatrixAtom(
140 0, "H", "H.1", null, null, null,
141 null, null, null, null);
142 ZMatrixAtom zAtom1 = new ZMatrixAtom(
143 1, "O", "O.2", zAtom0, null, null,
144 1.0, null, null, null);
145 ZMatrixAtom zAtom2 = new ZMatrixAtom(
146 2, "H", "H.1", zAtom1, zAtom0, null,
147 1.0, 109.5, null, null);
148 zmat.addAtom(zAtom0);
149 zmat.addAtom(zAtom1);
150 zmat.addAtom(zAtom2);
151
152 DGraph molGraph = new DGraph();
154 molGraph, fmol, zmat, "test",
155 new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
156 logger);
157
158 model.updateXYZFromINT();
159
160 Point3d coord0 = fmol.getAtom(0).getPoint3d();
161 Point3d coord1 = fmol.getAtom(1).getPoint3d();
162 Point3d coord2 = fmol.getAtom(2).getPoint3d();
163
164 // First atom at origin
165 assertEquals(0.0, coord0.x, 1e-10);
166 assertEquals(0.0, coord0.y, 1e-10);
167 assertEquals(0.0, coord0.z, 1e-10);
168
169 // Second atom along Z axis
170 assertEquals(0.0, coord1.x, 1e-10);
171 assertEquals(0.0, coord1.y, 1e-10);
172 assertEquals(1.0, coord1.z, 1e-10);
173
174 // Third atom should have non-zero X coordinate due to angle
175 assertTrue(Math.abs(coord2.x) > 1e-6, "Third atom should have X != 0");
176 assertEquals(0.0, coord2.y, 1e-10, "Third atom Y should be 0");
177 assertTrue(coord2.z > 0, "Third atom Z should be positive");
178 }
179
180//------------------------------------------------------------------------------
181
182 @Test
183 public void testUpdateXYZFromINT_FourthAtomWithDihedral() throws Exception
184 {
185 // Create a molecule with four atoms - fourth with dihedral (chirality = 0)
186 IAtomContainer fmol = SilentChemObjectBuilder.getInstance().newAtomContainer();
187 IAtom atom0 = new Atom("C");
188 IAtom atom1 = new Atom("C");
189 IAtom atom2 = new Atom("H");
190 IAtom atom3 = new Atom("H");
191 fmol.addAtom(atom0);
192 fmol.addAtom(atom1);
193 fmol.addAtom(atom2);
194 fmol.addAtom(atom3);
195
196 ZMatrix zmat = new ZMatrix();
197 ZMatrixAtom zAtom0 = new ZMatrixAtom(
198 0, "C", "C.3", null, null, null,
199 null, null, null, null);
200 ZMatrixAtom zAtom1 = new ZMatrixAtom(
201 1, "C", "C.3", zAtom0, null, null,
202 1.5, null, null, null);
203 ZMatrixAtom zAtom2 = new ZMatrixAtom(
204 2, "H", "H.1", zAtom1, zAtom0, null,
205 1.1, 109.5, null, null);
206 ZMatrixAtom zAtom3 = new ZMatrixAtom(
207 3, "H", "H.1", zAtom2, zAtom1, zAtom0,
208 1.1, 109.5, 60.0, 0);
209 zmat.addAtom(zAtom0);
210 zmat.addAtom(zAtom1);
211 zmat.addAtom(zAtom2);
212 zmat.addAtom(zAtom3);
213
214 DGraph molGraph = new DGraph();
216 molGraph, fmol, zmat, "test",
217 new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
218 logger);
219
220 model.updateXYZFromINT();
221
222 // Verify all atoms have coordinates
223 for (int i = 0; i < 4; i++)
224 {
225 Point3d coord = fmol.getAtom(i).getPoint3d();
226 assertNotNull(coord, "Atom " + i + " should have coordinates");
227 }
228
229 // Fourth atom should have all three coordinates non-zero
230 Point3d coord3 = fmol.getAtom(3).getPoint3d();
231 assertTrue(Math.abs(coord3.x) > 1e-6 || Math.abs(coord3.y) > 1e-6
232 || Math.abs(coord3.z) > 1e-6,
233 "Fourth atom should have non-zero coordinates");
234 }
235
236//------------------------------------------------------------------------------
237
238 @Test
239 public void testUpdateXYZFromINT_ChiralityOne() throws Exception
240 {
241 // Test with chirality = 1
242 IAtomContainer fmol = SilentChemObjectBuilder.getInstance().newAtomContainer();
243 IAtom atom0 = new Atom("C");
244 IAtom atom1 = new Atom("C");
245 IAtom atom2 = new Atom("H");
246 IAtom atom3 = new Atom("H");
247 fmol.addAtom(atom0);
248 fmol.addAtom(atom1);
249 fmol.addAtom(atom2);
250 fmol.addAtom(atom3);
251
252 ZMatrix zmat = new ZMatrix();
253 ZMatrixAtom zAtom0 = new ZMatrixAtom(
254 0, "C", "C.3", null, null, null,
255 null, null, null, null);
256 ZMatrixAtom zAtom1 = new ZMatrixAtom(
257 1, "C", "C.3", zAtom0, null, null,
258 1.5, null, null, null);
259 ZMatrixAtom zAtom2 = new ZMatrixAtom(
260 2, "H", "H.1", zAtom1, zAtom0, null,
261 1.1, 109.5, null, null);
262 ZMatrixAtom zAtom3 = new ZMatrixAtom(
263 3, "H", "H.1", zAtom2, zAtom1, zAtom0,
264 1.1, 109.5, 60.0, 1); // chirality = 1
265 zmat.addAtom(zAtom0);
266 zmat.addAtom(zAtom1);
267 zmat.addAtom(zAtom2);
268 zmat.addAtom(zAtom3);
269
270 DGraph molGraph = new DGraph();
272 molGraph, fmol, zmat, "test",
273 new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
274 logger);
275
276 model.updateXYZFromINT();
277
278 // Verify all atoms have coordinates
279 Point3d coord3 = fmol.getAtom(3).getPoint3d();
280 assertNotNull(coord3, "Atom 3 should have coordinates");
281 assertTrue(Math.abs(coord3.x) > 1e-6 || Math.abs(coord3.y) > 1e-6
282 || Math.abs(coord3.z) > 1e-6,
283 "Atom 3 should have non-zero coordinates");
284 }
285
286//------------------------------------------------------------------------------
287
288 @Test
289 public void testUpdateXYZFromINT_ChiralityMinusOne() throws Exception
290 {
291 // Test with chirality = -1
292 IAtomContainer fmol = SilentChemObjectBuilder.getInstance().newAtomContainer();
293 IAtom atom0 = new Atom("C");
294 IAtom atom1 = new Atom("C");
295 IAtom atom2 = new Atom("H");
296 IAtom atom3 = new Atom("H");
297 fmol.addAtom(atom0);
298 fmol.addAtom(atom1);
299 fmol.addAtom(atom2);
300 fmol.addAtom(atom3);
301
302 ZMatrix zmat = new ZMatrix();
303 ZMatrixAtom zAtom0 = new ZMatrixAtom(
304 0, "C", "C.3", null, null, null,
305 null, null, null, null);
306 ZMatrixAtom zAtom1 = new ZMatrixAtom(
307 1, "C", "C.3", zAtom0, null, null,
308 1.5, null, null, null);
309 ZMatrixAtom zAtom2 = new ZMatrixAtom(
310 2, "H", "H.1", zAtom1, zAtom0, null,
311 1.1, 109.5, null, null);
312 ZMatrixAtom zAtom3 = new ZMatrixAtom(
313 3, "H", "H.1", zAtom2, zAtom1, zAtom0,
314 1.1, 109.5, 60.0, -1); // chirality = -1
315 zmat.addAtom(zAtom0);
316 zmat.addAtom(zAtom1);
317 zmat.addAtom(zAtom2);
318 zmat.addAtom(zAtom3);
319
320 DGraph molGraph = new DGraph();
322 molGraph, fmol, zmat, "test",
323 new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
324 logger);
325
326 model.updateXYZFromINT();
327
328 // Verify all atoms have coordinates
329 Point3d coord3 = fmol.getAtom(3).getPoint3d();
330 assertNotNull(coord3, "Atom 3 should have coordinates");
331 }
332
333//------------------------------------------------------------------------------
334
335 @Test
336 public void testUpdateXYZFromINT() throws Exception
337 {
338 IAtomContainer fmol = SilentChemObjectBuilder.getInstance().newAtomContainer();
339 IAtom atom0 = new Atom("C");
340 IAtom atom1 = new Atom("C");
341 IAtom atom2 = new Atom("H");
342 IAtom atom3 = new Atom("H");
343 IAtom atom4 = new Atom("P");
344 IAtom atom5 = new Atom("O");
345 IAtom atom6 = new Atom("Cl");
346 IAtom atom7 = new Atom("H");
347 fmol.addAtom(atom0);
348 fmol.addAtom(atom1);
349 fmol.addAtom(atom2);
350 fmol.addAtom(atom3);
351 fmol.addAtom(atom4);
352 fmol.addAtom(atom5);
353 fmol.addAtom(atom6);
354 fmol.addAtom(atom7);
355
356 ZMatrix zmat = new ZMatrix();
357 ZMatrixAtom zAtom0 = new ZMatrixAtom(
358 0, "C", "C.3", null, null, null,
359 null, null, null, null);
360 ZMatrixAtom zAtom1 = new ZMatrixAtom(
361 1, "C", "C.3", zAtom0, null, null,
362 1.0, null, null, null);
363 ZMatrixAtom zAtom2 = new ZMatrixAtom(
364 2, "H", "H.1", zAtom1, zAtom0, null,
365 1.0, 90.0, null, null);
366 ZMatrixAtom zAtom3 = new ZMatrixAtom(
367 3, "H", "H.1", zAtom2, zAtom1, zAtom0,
368 1.0, 90.0, 90.0, 0);
369 ZMatrixAtom zAtom4 = new ZMatrixAtom(
370 4, "P", "P.3", zAtom3, zAtom2, zAtom1,
371 1.0, 90.0, -90.0, 0);
372 ZMatrixAtom zAtom5 = new ZMatrixAtom(
373 5, "O", "O.3", zAtom4, zAtom3, zAtom2,
374 1.0, 90.0, 180.0, 0);
375 ZMatrixAtom zAtom6 = new ZMatrixAtom(
376 6, "Cl", "Cl.1", zAtom4, zAtom3, zAtom5,
377 1.0, 90.0, 90.0, 1);
378 ZMatrixAtom zAtom7 = new ZMatrixAtom(
379 7, "H", "H.1", zAtom4, zAtom3, zAtom5,
380 1.0, 90.0, 90.0, -1);
381
382 zmat.addAtom(zAtom0);
383 zmat.addAtom(zAtom1);
384 zmat.addAtom(zAtom2);
385 zmat.addAtom(zAtom3);
386 zmat.addAtom(zAtom4);
387 zmat.addAtom(zAtom5);
388 zmat.addAtom(zAtom6);
389 zmat.addAtom(zAtom7);
390
391 DGraph molGraph = new DGraph();
393 molGraph, fmol, zmat, "test",
394 new ArrayList<>(), new ArrayList<>(), new ArrayList<>(),
395 logger);
396
397 model.updateXYZFromINT();
398
399 Point3d coord0 = fmol.getAtom(0).getPoint3d();
400 Point3d coord1 = fmol.getAtom(1).getPoint3d();
401 Point3d coord2 = fmol.getAtom(2).getPoint3d();
402 Point3d coord3 = fmol.getAtom(3).getPoint3d();
403 Point3d coord4 = fmol.getAtom(4).getPoint3d();
404 Point3d coord5 = fmol.getAtom(5).getPoint3d();
405 Point3d coord6 = fmol.getAtom(6).getPoint3d();
406 Point3d coord7 = fmol.getAtom(7).getPoint3d();
407
408 assertEquals(0.0, coord0.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
409 assertEquals(0.0, coord0.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
410 assertEquals(0.0, coord0.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
411
412 assertEquals(0.0, coord1.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
413 assertEquals(0.0, coord1.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
414 assertEquals(1.0, coord1.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
415
416 assertEquals(1.0, coord2.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
417 assertEquals(0.0, coord2.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
418 assertEquals(1.0, coord2.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
419
420 assertEquals(1.0, coord3.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
421 assertEquals(1.0, coord3.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
422 assertEquals(1.0, coord3.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
423
424 assertEquals(1.0, coord4.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
425 assertEquals(1.0, coord4.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
426 assertEquals(0.0, coord4.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
427
428 assertEquals(1.0, coord5.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
429 assertEquals(2.0, coord5.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
430 assertEquals(0.0, coord5.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
431
432 assertEquals(0.0, coord6.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
433 assertEquals(1.0, coord6.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
434 assertEquals(0.0, coord6.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
435
436 assertEquals(2.0, coord7.x, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
437 assertEquals(1.0, coord7.y, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
438 assertEquals(0.0, coord7.z, DENOPTIMConstants.FLOATCOMPARISONTOLERANCE);
439 }
440
441//------------------------------------------------------------------------------
442
443}
444
General set of constants used in DENOPTIM.
static final double FLOATCOMPARISONTOLERANCE
Smallest difference for comparison of double and float numbers.
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
Collector of molecular information, related to a single chemical object, that is deployed within the ...
void updateXYZFromINT()
Converts currently loaded internal coordinates into Cartesian overwriting the current XYZ.
Representation of an atom in the ZMatrix.
Definition: ZMatrixAtom.java:9
Representation of an atom container's geometry with internal coordinates.
Definition: ZMatrix.java:27
void addAtom(ZMatrixAtom atom)
Add an atom to the ZMatrix.
Definition: ZMatrix.java:61