Branch data Line data Source code
1 : : /*
2 : : * SPDX-License-Identifier: MIT
3 : : * Copyright (c) 2024 IUPAC and InChI Trust
4 : : */
5 : :
6 : : #include <stdlib.h>
7 : : #include <string.h>
8 : : #include <ctype.h>
9 : : #include <math.h>
10 : : #include <stdbool.h>
11 : : #include <limits.h>
12 : :
13 : : #include "mode.h"
14 : : #include "ichinorm.h" /* @nnuk */
15 : : #include "strutil.h"
16 : : #include "molecular_inorganics_data.h"
17 : : #include "ichister.h"
18 : : #include "ichi_io.h"
19 : : #include "ichimain.h"
20 : : #include "ichidrp.h" /* @nnuk */
21 : :
22 : : #include "bcf_s.h"
23 : :
24 : : /* Added fix to remove_ion_pairs() -- 2010-03-17 DT */
25 : : #define FIX_P_IV_Plus_O_Minus
26 : :
27 : : #ifndef COMPILE_ALL_CPP
28 : : #ifdef __cplusplus
29 : : extern "C"
30 : : {
31 : : #endif
32 : : #endif
33 : :
34 : : /* Defined in ichisort.c, prototype in ichicomn.h */
35 : : int insertions_sort_AT_RANK(AT_RANK *base, int num);
36 : :
37 : : #ifndef COMPILE_ALL_CPP
38 : : #ifdef __cplusplus
39 : : }
40 : : #endif
41 : : #endif
42 : :
43 : : typedef struct tagTreeAtom
44 : : {
45 : : AT_NUMB neighbor[MAXVAL]; /* positions (from 0) of the neighbors in the inp_ATOM array */
46 : : S_CHAR valence; /* number of bonds = number of neighbors */
47 : : AT_NUMB nRingSystem;
48 : : AT_NUMB nBlockSystem;
49 : : S_CHAR bCutVertex;
50 : : } tre_ATOM;
51 : :
52 : : /* Local prototypes */
53 : :
54 : : int cmp_iso_atw_diff_component_no(const void *a1, const void *a2);
55 : : int cmp_components(const void *a1, const void *a2);
56 : : /* int mark_one_struct_component( inp_ATOM* at,
57 : : int j,
58 : : AT_NUMB *mark,
59 : : AT_NUMB num_disconnected_components );
60 : : */
61 : : INChI_Stereo *Alloc_INChI_Stereo(int num_at, int num_bonds);
62 : : static int is_only_HDT_neighbors(const inp_ATOM* at, int num_atoms, int metal_idx);
63 : : int RemoveInpAtBond(inp_ATOM *at, int iat, int k);
64 : : int DisconnectInpAtBond(inp_ATOM *at,
65 : : AT_NUMB *nOldCompNumber,
66 : : int iat,
67 : : int neigh_ord);
68 : : int move_explicit_Hcation(inp_ATOM *at,
69 : : int num_at,
70 : : int iat,
71 : : int iat_H,
72 : : int bInAllComponents);
73 : : int DisconnectOneLigand(inp_ATOM *at,
74 : : AT_NUMB *nOldCompNumber,
75 : : S_CHAR *bMetal,
76 : : char *elnumber_Heteroat,
77 : : int num_halogens,
78 : : int num_atoms,
79 : : int iMetal,
80 : : int jLigand,
81 : : INCHI_MODE *bTautFlagsDone);
82 : : int bIsAmmoniumSalt(inp_ATOM *at,
83 : : int i,
84 : : int *piO,
85 : : int *pk,
86 : : S_CHAR *num_explicit_H);
87 : : int DisconnectAmmoniumSalt(inp_ATOM *at,
88 : : int i,
89 : : int iO,
90 : : int k,
91 : : S_CHAR *num_explicit_H);
92 : : /*int bIsMetalSalt( inp_ATOM *at, int i ); - moved to strutil,h */
93 : : int DisconnectMetalSalt(inp_ATOM *at, int i);
94 : : int bIsMetalToDisconnect(inp_ATOM *at, int i, int bCheckMetalValence);
95 : : int get_iat_number(int el_number);
96 : : int tot_unsat(int unsat[]);
97 : : int max_unsat(int unsat[]);
98 : : double dist3D(inp_ATOM *at1, inp_ATOM *at2);
99 : : double dist2D(inp_ATOM *at1, inp_ATOM *at2);
100 : : double dist_from_segm(double x, double y,
101 : : double x1, double y1,
102 : : double x2, double y2);
103 : : int segments_intersect(double x11, double y11,
104 : : double x12, double y12, /* segment #1 */
105 : : double x21, double y21,
106 : : double x22, double y22);
107 : : double GetMinDistDistribution(inp_ATOM *at,
108 : : int num_at,
109 : : int iat,
110 : : int iat_H,
111 : : int bInAllComponents,
112 : : double min_dist[],
113 : : int num_segm);
114 : : int nFindOneOM(inp_ATOM *at,
115 : : int at_no,
116 : : int ord_OM[],
117 : : int num_OM);
118 : : int the_only_doublet_neigh(inp_ATOM *at, int i1, int *ineigh1, int *ineigh2);
119 : : int fix_non_uniform_drawn_oxoanions(int num_atoms, inp_ATOM *at, int *num_changes);
120 : : int fix_non_uniform_drawn_amidiniums(int num_atoms, inp_ATOM *at, int *num_changes);
121 : :
122 : : void add_bond_if_unseen(subgraf_pathfinder *spf,
123 : : int node0, int node,
124 : : int *nbonds, int **bonds);
125 : :
126 : : /****************************************************************************/
127 : : #ifndef NUMH
128 : : #define NUM_ISO_H(AT, N) (AT[N].num_iso_H[0] + AT[N].num_iso_H[1] + AT[N].num_iso_H[2])
129 : : #define NUMH(AT, N) (AT[N].num_H + NUM_ISO_H(AT, N))
130 : : #endif
131 : : /****************************************************************************/
132 : :
133 : : /****************************************************************************/
134 : 113 : int cmp_iso_atw_diff_component_no(const void *a1, const void *a2)
135 : : {
136 : 113 : int ret = (int)((const inp_ATOM *)a1)->iso_atw_diff - (int)((const inp_ATOM *)a2)->iso_atw_diff;
137 [ + - ]: 113 : if (!ret) /* make the sort stable */
138 : : {
139 : 113 : ret = (int)((const inp_ATOM *)a1)->component - (int)((const inp_ATOM *)a2)->component;
140 : : }
141 : :
142 : 113 : return ret;
143 : : }
144 : :
145 : : /****************************************************************************/
146 : 0 : int the_only_doublet_neigh(inp_ATOM *at,
147 : : int i1,
148 : : int *ineigh1,
149 : : int *ineigh2)
150 : : {
151 : 0 : int i, neigh1, num_rad1 = 0, num_rad2 = 0;
152 : :
153 : 0 : inp_ATOM *a = at + i1, *b;
154 [ # # ]: 0 : if (RADICAL_DOUBLET != a->radical)
155 : : {
156 : 0 : return -1;
157 : : }
158 [ # # ]: 0 : for (i = 0; i < a->valence; i++)
159 : : {
160 : 0 : b = at + ((int)a->neighbor[i]); /* djb-rwth: removing redundant code */
161 [ # # ]: 0 : if (RADICAL_DOUBLET == b->radical)
162 : : {
163 : 0 : num_rad1++;
164 : 0 : *ineigh1 = i;
165 : : }
166 : : }
167 : :
168 [ # # ]: 0 : if (1 == num_rad1)
169 : : {
170 : 0 : a = at + (neigh1 = (int)a->neighbor[*ineigh1]);
171 [ # # ]: 0 : for (i = 0; i < a->valence; i++)
172 : : {
173 : 0 : b = at + (int)a->neighbor[i];
174 [ # # ]: 0 : if (RADICAL_DOUBLET == b->radical)
175 : : {
176 : 0 : num_rad2++;
177 : 0 : *ineigh2 = i;
178 : : }
179 : : }
180 : :
181 [ # # ]: 0 : if (1 == num_rad2)
182 : : {
183 : 0 : return neigh1;
184 : : }
185 : : }
186 : :
187 : 0 : return -1;
188 : : }
189 : :
190 : : /****************************************************************************
191 : : Correct non-uniformly drawn oxoanions
192 : : ****************************************************************************/
193 : 191 : int fix_non_uniform_drawn_oxoanions(int num_atoms,
194 : : inp_ATOM *at,
195 : : int *num_changes)
196 : : {
197 : : /* For central halogen, apply the following
198 : : correction rules:
199 : :
200 : : O O(-)
201 : : || |
202 : : O=Hal(-)=O ===> O=Hal=O
203 : : || ||
204 : : O O
205 : :
206 : : (perchlorate, etc.)
207 : :
208 : :
209 : : O O(-)
210 : : || |
211 : : Hal(-)=O ===> Hal=O
212 : : || ||
213 : : O O
214 : :
215 : : (chlorate, etc.)
216 : :
217 : : O O(-)
218 : : || |
219 : : Hal(-)=O ===> Hal=O
220 : :
221 : :
222 : : (chlorite, etc.)
223 : :
224 : : Hal(-)=O ===> Hal-O(-)
225 : :
226 : :
227 : : (hypochlorite, etc.)
228 : :
229 : :
230 : :
231 : : For halcogenes (S, Se, Te)
232 : : Y Y(-)
233 : : || |
234 : : RnX(-) ===> RnX
235 : :
236 : : if:
237 : : 1) (X = S, Y = O) || (X = Se, Y = S, O) || (X = Te, Y = O, S, Se)
238 : : 2) valence of X exceeds 6, in initially drawn form
239 : :
240 : :
241 : : So the following is corrected:
242 : :
243 : : O O(-)
244 : : || |
245 : : O=S(-)-R ===> O=S-R
246 : : || ||
247 : : O O
248 : :
249 : : Or
250 : :
251 : : O O-
252 : : || |
253 : : F5Te(-) ===> F5Te
254 : :
255 : : but the following remains unchanged:
256 : :
257 : :
258 : : O
259 : : ||
260 : : O=S(-)-R
261 : :
262 : :
263 : : The central atom (of IUPAC Group 16-17) is shown as negative but it contains double bond(s)
264 : : to terminal atom of greater electronegativity (of Group 16).
265 : : The central atom is halcogen (S,Se,Te) in highest oxidation state or halogen.
266 : :
267 : : Fix:
268 : : move negative charge to terminal atom and change double bond to a single one.
269 : :
270 : : Eligible central atom Eligible terminal atom at double bond's end
271 : : Cl O
272 : : Br O
273 : : I O
274 : : [At S,Se,Te]
275 : : S O
276 : : Se O,S
277 : : Te O, S, Se
278 : :
279 : : Comments:
280 : : 1. Central atoms of Groups 13-15 are not considered.
281 : : 2. Pauling electronegativities are:
282 : : F(3.98) > O(3.44) > Cl (3.16) > N (3.04) > Br(2.96) > I(2.66) > S(2.58) > Se(2.55) > At (2.2) > Te(2.1)
283 : :
284 : :
285 : : */
286 : :
287 : : /* constants for element numbers */
288 : : enum elems
289 : : {
290 : : dNone,
291 : : dCl = 17,
292 : : dBr = 35,
293 : : dI = 53,
294 : : dAt = 85,
295 : : dO = 8,
296 : : dS = 16,
297 : : dSe = 34,
298 : : dTe = 52,
299 : : dP = 15,
300 : : dC = 6,
301 : : dN = 7
302 : : };
303 : :
304 : : static U_CHAR allowed_elnums_center_halogen[] = {dCl, dBr, dI, dAt};
305 : : static U_CHAR allowed_elnums_center_halcogen[] = {dS, dSe, dTe};
306 : :
307 : : int en_center;
308 : : int i, j, k;
309 : :
310 [ + + ]: 9446 : for (i = 0; i < num_atoms; i++)
311 : : {
312 : : /* Find appropriate central atom. This center should be: ...*/
313 : :
314 : : /* charged exactly (-1) ... */
315 [ + + ]: 9255 : if (at[i].charge != -1)
316 : : {
317 : 9196 : continue;
318 : : }
319 : 59 : en_center = at[i].el_number;
320 : : /* from eligible element list ... */
321 [ + + ]: 59 : if (!memchr(allowed_elnums_center_halogen, en_center, sizeof(allowed_elnums_center_halogen)))
322 : : {
323 : : /* central atom is not not halogen; check if it is halcogen */
324 [ - + ]: 52 : if (memchr(allowed_elnums_center_halcogen, en_center, sizeof(allowed_elnums_center_halcogen)))
325 : : {
326 [ # # ]: 0 : if (at[i].chem_bonds_valence < 7)
327 : : {
328 : : /* central atom is anionic halcogen, but not in the highest oxidation state */
329 : 0 : continue;
330 : : }
331 : : }
332 : : else
333 : : {
334 : 52 : continue;
335 : : }
336 : : }
337 : :
338 : : /* OK, found central halogen or eligible central halcogen. */
339 : :
340 : : /* non-radical... */
341 [ - + - - ]: 7 : if (at[i].radical && (at[i].radical != RADICAL_SINGLET))
342 : : {
343 : 0 : continue;
344 : : }
345 : :
346 : : /* Center found, now examine the adjacent terminals... */
347 : : {
348 : 7 : int en_term, kk = 0, jj = 0, min_en = 999, iso = 0, min_iso = 999;
349 : 7 : jj = -1;
350 [ + + ]: 14 : for (k = 0; k < at[i].valence; k++)
351 : : {
352 : 7 : j = at[i].neighbor[k];
353 : :
354 : : /* Terminal should be: ... */
355 : :
356 : : /* terminal... */
357 [ + + ]: 7 : if (at[j].valence != 1)
358 : : {
359 : 6 : continue;
360 : : }
361 : : /* double-bonded ... */
362 [ + - ]: 1 : if (at[i].bond_type[k] != BOND_TYPE_DOUBLE)
363 : : {
364 : 1 : continue;
365 : : }
366 : : /* zero-charged ... */
367 [ # # ]: 0 : if (at[j].charge != 0)
368 : : {
369 : 0 : continue;
370 : : }
371 : : /* non-radical */
372 [ # # # # ]: 0 : if (at[j].radical && (at[j].radical != RADICAL_SINGLET))
373 : : {
374 : 0 : continue;
375 : : }
376 : : /* of eligible elements list ... */
377 : 0 : en_term = at[j].el_number;
378 [ # # # # : 0 : switch (en_term)
# ]
379 : : {
380 : 0 : case dO:
381 : 0 : break;
382 : 0 : case dS:
383 [ # # # # : 0 : if ((en_center == dSe) || (en_center == dAt) || (en_center == dTe))
# # ]
384 : : break;
385 : 0 : continue;
386 : 0 : case dSe:
387 [ # # # # ]: 0 : if ((en_center == dAt) || (en_center == dTe))
388 : : break;
389 : 0 : continue;
390 : 0 : case dTe:
391 [ # # ]: 0 : if (en_center == dAt)
392 : 0 : break;
393 : 0 : continue;
394 : 0 : default:
395 : 0 : continue;
396 : : }
397 : :
398 : : /* From several candidates, select one with less el. number (==more electronegative). */
399 [ # # ]: 0 : if (en_term < min_en)
400 : : {
401 : 0 : min_en = en_term;
402 : 0 : kk = k;
403 : 0 : jj = j;
404 [ # # ]: 0 : min_iso = at[j].iso_atw_diff > 0 ? at[i].iso_atw_diff - 1 : at[i].iso_atw_diff;
405 : 0 : continue;
406 : : }
407 : : /* From same-element candidates, select one with less isotopic mass (arbitrary choice). */
408 [ # # ]: 0 : else if (en_term == min_en)
409 : : {
410 [ # # ]: 0 : iso = at[j].iso_atw_diff > 0 ? at[i].iso_atw_diff - 1 : at[i].iso_atw_diff;
411 [ # # ]: 0 : if (iso < min_iso)
412 : : {
413 : 0 : min_iso = iso;
414 : 0 : kk = k;
415 : 0 : jj = j;
416 : 0 : continue;
417 : : }
418 : : }
419 : : } /* end of checking nbrs. */
420 : :
421 : : /* If OK, apply changes. */
422 [ - + ]: 7 : if (jj >= 0)
423 : : {
424 : 0 : at[i].charge = 0;
425 : 0 : at[jj].charge = -1;
426 : 0 : at[i].bond_type[kk] = BOND_TYPE_SINGLE;
427 : 0 : at[jj].bond_type[0] = BOND_TYPE_SINGLE;
428 : 0 : at[i].bond_stereo[kk] = at[jj].bond_stereo[0] = 0;
429 : 0 : at[i].chem_bonds_valence--;
430 : 0 : at[jj].chem_bonds_valence--;
431 : 0 : (*num_changes)++;
432 : : }
433 : : }
434 : : } /* end of search for candidate centers. */
435 : :
436 : 191 : return 0;
437 : : }
438 : :
439 : : /****************************************************************************
440 : : Correct non-uniformly drawn amidinium cations.
441 : : ****************************************************************************/
442 : 191 : int fix_non_uniform_drawn_amidiniums(int num_atoms,
443 : : inp_ATOM *at,
444 : : int *num_changes)
445 : :
446 : : {
447 : : /* Amidines include carboxamidines RC(=NR)NR2,
448 : : sulfinamidines RS(=NR)NR2 and phosphinamidines, R2P(=NR)NR2.
449 : :
450 : :
451 : : NR NR
452 : : | |
453 : : R"-Y-NHR' ===> R"-Y=N(+)HR'
454 : : (+)
455 : :
456 : :
457 : : Y = C, S, P
458 : :
459 : :
460 : : Fix:
461 : : move positive charge to nitrogen and change single bond to a double one.
462 : :
463 : :
464 : : Comment:
465 : : Fix is applied only if at least one of R's at N is hydrogen
466 : : (otherwise we have just a '+' delocalization which is already recognized).
467 : :
468 : :
469 : : */
470 : :
471 : : /* constants for element numbers */
472 : : enum elems
473 : : {
474 : : dNone,
475 : : dCl = 17,
476 : : dBr = 35,
477 : : dI = 53,
478 : : dAt = 85,
479 : : dO = 8,
480 : : dS = 16,
481 : : dSe = 34,
482 : : dTe = 52,
483 : : dP = 15,
484 : : dC = 6,
485 : : dN = 7
486 : : };
487 : :
488 : : static U_CHAR allowed_elnums_center[] = {dC, dS, dP};
489 : : int en_center;
490 : : int i, j, k, jj, kk, k1;
491 : 191 : int mismatch = 0, nuH = 0, nuN = 0, nitrogens[MAXVAL];
492 : :
493 [ + + ]: 9446 : for (i = 0; i < num_atoms; i++)
494 : : {
495 : : /* Find appropriate central atom. This center should be: ...*/
496 : :
497 : : /* charged exactly (+1) ... */
498 [ + + ]: 9255 : if (at[i].charge != 1)
499 : : {
500 : 9232 : continue;
501 : : }
502 : 23 : en_center = at[i].el_number;
503 : : /* from eligible element list ... */
504 [ + - ]: 23 : if (!memchr(allowed_elnums_center, en_center, sizeof(allowed_elnums_center)))
505 : : {
506 : 23 : continue;
507 : : }
508 : : /* has exactly 3 neighbours connected by single bonds*/
509 [ # # ]: 0 : if (at[i].valence != 3)
510 : : {
511 : 0 : continue;
512 : : }
513 [ # # ]: 0 : if (at[i].chem_bonds_valence != 3)
514 : : {
515 : 0 : continue;
516 : : }
517 : :
518 : : /* non-radical. */
519 [ # # # # ]: 0 : if (at[i].radical && (at[i].radical != RADICAL_SINGLET))
520 : : {
521 : 0 : continue;
522 : : }
523 : :
524 : : /* NB: center must have neutral neighbours, two of them are aliphatic N's of which at least one bears H. */
525 : 0 : mismatch = nuH = nuN = kk = 0; /* djb-rwth: removing redundant code */
526 : 0 : memset(nitrogens, 0, sizeof(nitrogens)); /* djb-rwth: memset_s C11/Annex K variant? */
527 : 0 : jj = -1;
528 [ # # ]: 0 : for (k = 0; k < at[i].valence; k++)
529 : : {
530 : 0 : j = at[i].neighbor[k];
531 : :
532 [ # # ]: 0 : if (at[j].charge != 0)
533 : : {
534 : 0 : mismatch = 1;
535 : 0 : break;
536 : : }
537 [ # # ]: 0 : if (at[j].el_number == dN)
538 : : {
539 [ # # # # ]: 0 : if ((at[j].valence > 3) || (at[j].chem_bonds_valence > 3))
540 : : {
541 : 0 : mismatch = 1;
542 : 0 : break;
543 : : }
544 : 0 : nuH += NUMH(at, j);
545 : 0 : nuN++;
546 [ # # ]: 0 : if (jj < 0)
547 : : {
548 : 0 : jj = j;
549 : 0 : kk = k;
550 : : }
551 : : }
552 : : }
553 : :
554 : : /* If OK, apply changes. */
555 [ # # ]: 0 : if (mismatch)
556 : : {
557 : 0 : continue;
558 : : }
559 [ # # ]: 0 : if (nuN != 2)
560 : : {
561 : 0 : continue;
562 : : }
563 [ # # ]: 0 : if (nuH < 1)
564 : : {
565 : 0 : continue;
566 : : }
567 [ # # ]: 0 : if (jj >= 0)
568 : : {
569 : 0 : at[i].charge = 0;
570 : 0 : at[jj].charge = 1;
571 : 0 : at[i].bond_type[kk] = BOND_TYPE_DOUBLE;
572 [ # # # # ]: 0 : for (k1 = 0; k1 < at[jj].valence && i != at[jj].neighbor[k1]; k1++)
573 : : ;
574 : 0 : at[jj].bond_type[k1] = BOND_TYPE_DOUBLE;
575 : 0 : at[i].chem_bonds_valence++;
576 : 0 : at[jj].chem_bonds_valence++;
577 : : /* NB: do nothing with wedge stereo bonds (retain wedge) */
578 : :
579 : 0 : (*num_changes)++;
580 : : }
581 : : } /* end of search for candidate centers. */
582 : :
583 : 191 : return 0;
584 : : }
585 : :
586 : : /****************************************************************************/
587 : 191 : int fix_odd_things(int num_atoms,
588 : : inp_ATOM *at,
589 : : int bFixBug,
590 : : int bFixNonUniformDraw)
591 : : {
592 : : /* N;P;As;Sb;O;S;Se;Te;C;Si */
593 : : static const U_CHAR en[] = {
594 : : EL_NUMBER_N,
595 : : EL_NUMBER_P,
596 : : EL_NUMBER_AS,
597 : : EL_NUMBER_SB,
598 : : EL_NUMBER_O,
599 : : EL_NUMBER_S,
600 : : EL_NUMBER_SE,
601 : : EL_NUMBER_TE};
602 : : static int ne = sizeof(en) / sizeof(en[0]);
603 : :
604 : : #define FIRST_NEIGHB2 4
605 : : #define FIRST_CENTER2 5
606 : :
607 : 191 : int i1, i2, k1, k2, c = -1, num_changes = 0;
608 : : /* djb-rwth: removing redundant variables */
609 : :
610 [ + - ]: 191 : if (bFixNonUniformDraw)
611 : : {
612 : : int ret1; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
613 : 191 : ret1 = fix_non_uniform_drawn_oxoanions(num_atoms, at, &num_changes); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
614 : 191 : ret1 = fix_non_uniform_drawn_amidiniums(num_atoms, at, &num_changes); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
615 : : }
616 : :
617 : : /* H(-)-X -> H-X(-); H(+)-X -> H-X(+) */
618 [ + + ]: 9446 : for (i1 = 0; i1 < num_atoms; i1++)
619 : : {
620 [ + + ]: 9255 : if (1 == at[i1].valence &&
621 [ + + ]: 563 : 1 == abs(at[i1].charge) &&
622 [ - + - - ]: 48 : (0 == at[i1].radical || RADICAL_SINGLET == at[i1].radical) &&
623 [ + - ]: 48 : BOND_TYPE_SINGLE == at[i1].bond_type[0] &&
624 [ + + + - ]: 48 : EL_NUMBER_H == at[i1].el_number && EL_NUMBER_H != at[i2 = (int)at[i1].neighbor[0]].el_number &&
625 [ + - + - ]: 4 : !NUMH(at, i1) && !NUMH(at, i2))
626 : : {
627 : 4 : at[i2].charge += at[i1].charge;
628 : 4 : at[i1].charge = 0;
629 : : }
630 : : }
631 : :
632 : : /* replace XHm(-)--Y==XHn(+) with XHm==Y--XHn, (n>=0 ,m>=0, X=N,P,As,Sb,O,S,Se,Te) */
633 [ + + ]: 9446 : for (i1 = 0; i1 < num_atoms; i1++)
634 : : {
635 [ + + ]: 9255 : if (1 != at[i1].charge ||
636 [ - + - - ]: 23 : (at[i1].radical && RADICAL_SINGLET != at[i1].radical) ||
637 [ + + ]: 23 : at[i1].chem_bonds_valence == at[i1].valence ||
638 [ + - ]: 1 : !memchr(en, at[i1].el_number, ne) ||
639 [ - + ]: 1 : get_el_valence(at[i1].el_number, at[i1].charge, 0) != at[i1].chem_bonds_valence + NUMH(at, i1)) /* djb-rwth: addressing LLVM warning */
640 : : {
641 : 9254 : continue;
642 : : }
643 : :
644 : : /* found a candidate at[i1] for X in XHn(+) */
645 [ - + ]: 1 : if (1 == at[i1].valence &&
646 [ # # ]: 0 : BOND_TYPE_DOUBLE == at[i1].bond_type[0])
647 : : {
648 : 0 : c = (int)at[i1].neighbor[0];
649 [ # # ]: 0 : for (k2 = 0; k2 < at[c].valence; k2++)
650 : : {
651 : 0 : i2 = at[c].neighbor[k2];
652 [ # # ]: 0 : if (1 == at[i2].valence &&
653 [ # # ]: 0 : -1 == at[i2].charge &&
654 [ # # ]: 0 : at[i2].el_number == at[i1].el_number && /* exact match */
655 [ # # # # ]: 0 : (0 == at[i2].radical || RADICAL_SINGLET == at[i2].radical) &&
656 [ # # ]: 0 : BOND_TYPE_SINGLE == at[i2].bond_type[0] &&
657 : : /*memchr(en, at[i2].el_number, ne) &&*/
658 [ # # ]: 0 : get_el_valence(at[i2].el_number, at[i2].charge, 0) == at[i2].chem_bonds_valence + NUMH(at, i2))
659 : : {
660 : : /* found both X(-) and X(+); change bonds and remove charges */
661 [ # # # # ]: 0 : for (k1 = 0; k1 < at[c].valence && i1 != at[c].neighbor[k1]; k1++)
662 : : ;
663 : 0 : at[i1].charge = at[i2].charge = 0;
664 : 0 : at[i1].bond_type[0] = at[c].bond_type[k1] = BOND_TYPE_SINGLE;
665 : 0 : at[i1].chem_bonds_valence--;
666 : 0 : at[i2].bond_type[0] = at[c].bond_type[k2] = BOND_TYPE_DOUBLE;
667 : 0 : at[i2].chem_bonds_valence++;
668 : 0 : num_changes++;
669 : 0 : break;
670 : : }
671 : : }
672 : : }
673 : : else
674 : : {
675 : : /* explicit H case: detect H-neighbors and Y */
676 : : int ineigh, neigh, i1_c, i2_c, num_H_i1, num_H_i2;
677 [ + + ]: 4 : for (ineigh = 0, num_H_i1 = 0, i1_c = -1; ineigh < at[i1].valence; ineigh++)
678 : : {
679 : 3 : neigh = at[i1].neighbor[ineigh];
680 [ - + ]: 3 : if (at[neigh].el_number == EL_NUMBER_H)
681 : : {
682 [ # # ]: 0 : if (at[neigh].chem_bonds_valence == 1 &&
683 [ # # # # ]: 0 : (0 == at[neigh].radical || RADICAL_SINGLET == at[neigh].radical))
684 : : {
685 : 0 : num_H_i1++; /* found H-neighbor */
686 : : }
687 : : else
688 : : {
689 : : break; /* wrong neighbor */
690 : : }
691 : : }
692 [ + + ]: 3 : else if (at[i1].bond_type[ineigh] == BOND_TYPE_DOUBLE)
693 : : {
694 : : /* found a candidate for Y; bond must be double */
695 : 1 : i1_c = ineigh;
696 : 1 : c = neigh;
697 : : }
698 : : }
699 [ + - + - ]: 1 : if (i1_c < 0 || num_H_i1 + 1 != at[i1].valence)
700 : : {
701 : 1 : continue;
702 : : }
703 [ # # ]: 0 : for (k2 = 0; k2 < at[c].valence; k2++)
704 : : {
705 : 0 : i2 = at[c].neighbor[k2];
706 [ # # ]: 0 : if (-1 == at[i2].charge &&
707 [ # # ]: 0 : at[i2].el_number == at[i1].el_number && /* exact match */
708 [ # # # # ]: 0 : (0 == at[i2].radical || RADICAL_SINGLET == at[i2].radical) &&
709 [ # # ]: 0 : get_el_valence(at[i2].el_number, at[i2].charge, 0) == at[i2].chem_bonds_valence + NUMH(at, i2))
710 : : {
711 [ # # ]: 0 : for (ineigh = 0, num_H_i2 = 0, i2_c = -1; ineigh < at[i2].valence; ineigh++)
712 : : {
713 : 0 : neigh = at[i2].neighbor[ineigh];
714 [ # # ]: 0 : if (at[neigh].el_number == EL_NUMBER_H)
715 : : {
716 [ # # ]: 0 : if (at[neigh].chem_bonds_valence == 1 &&
717 [ # # # # ]: 0 : (0 == at[neigh].radical || RADICAL_SINGLET == at[neigh].radical))
718 : : {
719 : 0 : num_H_i2++; /* found H-neighbor */
720 : : }
721 : : else
722 : : {
723 : : break; /* wrong neighbor */
724 : : }
725 : : }
726 : : else
727 : : {
728 [ # # # # ]: 0 : if (c == neigh && at[i2].bond_type[ineigh] == BOND_TYPE_SINGLE)
729 : : {
730 : 0 : i2_c = ineigh; /* position of Y neighbor; bond must be single */
731 : : }
732 : : else
733 : : {
734 : : break;
735 : : }
736 : : }
737 : : }
738 [ # # ]: 0 : if (num_H_i2 + (i2_c >= 0) != at[i2].valence)
739 : : {
740 : 0 : continue;
741 : : }
742 : : /* found both X(-) and X(+); change bonds and remove charges */
743 [ # # # # ]: 0 : for (k1 = 0; k1 < at[c].valence && i1 != at[c].neighbor[k1]; k1++)
744 : : ;
745 [ # # # # ]: 0 : if ((i1_c >= 0) && (i2_c >= 0)) /* djb-rwth: fixing coverity ID #499537 */
746 : : {
747 : 0 : at[i1].charge = at[i2].charge = 0;
748 : 0 : at[i1].bond_type[i1_c] = at[c].bond_type[k1] = BOND_TYPE_SINGLE;
749 : 0 : at[i1].chem_bonds_valence--;
750 : 0 : at[i2].bond_type[i2_c] = at[c].bond_type[k2] = BOND_TYPE_DOUBLE;
751 : 0 : at[i2].chem_bonds_valence++;
752 : 0 : num_changes++;
753 : 0 : break;
754 : : }
755 : : else
756 : : {
757 : 0 : continue;
758 : : }
759 : : }
760 : : } /* k2 */
761 : : }
762 : : }
763 : :
764 : : /* Replace
765 : :
766 : : X- X X=O,S,Se,Te -- terminal atoms (NEIGHB2)
767 : : \ | \ ||
768 : : >Y++ with >Y Y=S,Se,Te -- central cation (CENTER2)
769 : : / | / ||
770 : : X- X Y valence=4, original Y bond valence = 4
771 : :
772 : :
773 : : --- the following case of P is processed separately in remove_ion_pairs()
774 : : --- therefire, it has been disabled here, see #ifndef FIX_P_IV_Plus_O_Minus -- 2010-03-17 DT
775 : :
776 : : X- X X=O,S,Se,Te -- terminal atoms (NEIGHB2)
777 : : \ | \ ||
778 : : >P+ with >P
779 : : / | / |
780 : : X- X- Y valence=4, original Y bond valence = 4
781 : :
782 : : */
783 : :
784 [ + + ]: 9446 : for (i1 = 0; i1 < num_atoms; i1++)
785 : : {
786 [ + + ]: 9255 : if (1 == at[i1].valence &&
787 [ + + ]: 563 : -1 == at[i1].charge &&
788 [ - + - - ]: 43 : (0 == at[i1].radical || RADICAL_SINGLET == at[i1].radical) &&
789 [ + - ]: 43 : !NUMH(at, i1) &&
790 [ + - ]: 43 : BOND_TYPE_SINGLE == at[i1].bond_type[0] &&
791 [ + + ]: 43 : memchr(en + FIRST_NEIGHB2, at[i1].el_number, (long long)ne - FIRST_NEIGHB2)) /* djb-rwth: cast operator added */
792 : : {
793 : : int charge, i;
794 : : /* found a candidate for X */
795 : 36 : c = (int)at[i1].neighbor[0]; /* candidate for Y */
796 [ - + - - ]: 36 : if (((charge = 2) == at[c].charge && memchr(en + FIRST_CENTER2, at[c].el_number, (long long)ne - FIRST_CENTER2) /* djb-rwth: cast operator added */
797 : :
798 : : #ifndef FIX_P_IV_Plus_O_Minus
799 : : || (charge = 1) == at[c].charge && EL_NUMBER_P == at[c].el_number
800 : : #endif
801 : 0 : ) &&
802 [ # # ]: 0 : 4 == at[c].valence &&
803 [ # # # # ]: 0 : (0 == at[c].radical || RADICAL_SINGLET == at[c].radical) &&
804 [ # # ]: 0 : at[c].valence == at[c].chem_bonds_valence &&
805 [ # # ]: 0 : !NUMH(at, c))
806 : : {
807 : : ; /* accept */
808 : : }
809 : : else
810 : : {
811 : 36 : continue; /* ignore at[i1] */
812 : : }
813 [ # # ]: 0 : for (k2 = 0; k2 < at[c].valence; k2++)
814 : : {
815 : 0 : i2 = at[c].neighbor[k2];
816 [ # # ]: 0 : if (i2 == i1)
817 : : {
818 : 0 : continue;
819 : : }
820 [ # # ]: 0 : if (1 == at[i2].valence &&
821 [ # # ]: 0 : -1 == at[i2].charge &&
822 [ # # ]: 0 : memchr(en + FIRST_NEIGHB2, at[i2].el_number, (long long)ne - FIRST_NEIGHB2) && /* djb-rwth: cast operator added */
823 : : /*at[i2].el_number == at[i1].el_number &&*/ /* exact match */
824 [ # # # # ]: 0 : (0 == at[i2].radical || RADICAL_SINGLET == at[i2].radical) &&
825 [ # # ]: 0 : !NUMH(at, i2) &&
826 [ # # ]: 0 : BOND_TYPE_SINGLE == at[i2].bond_type[0])
827 : : {
828 : : /* found both X(-) and X(-); change bonds and remove charges */
829 [ # # # # ]: 0 : for (k1 = 0; k1 < at[c].valence && i1 != at[c].neighbor[k1]; k1++)
830 : : {
831 : : ;
832 : : }
833 [ # # ]: 0 : for (i = 0; i < charge; i++)
834 : : {
835 : : /* in case of P it does not matter which X atom is neutralized
836 : : because of tautomerism. However, neutral central atom is important
837 : : for the neutralization of the components */
838 [ # # # ]: 0 : switch (i)
839 : : {
840 : 0 : case 0:
841 : 0 : at[i1].charge++; /* = 0; changed 2010-03-17 DT*/
842 : 0 : at[i1].bond_type[0] = at[c].bond_type[k1] = BOND_TYPE_DOUBLE;
843 : 0 : at[i1].bond_stereo[0] = at[c].bond_stereo[k1] = 0;
844 : 0 : at[i1].chem_bonds_valence++;
845 : 0 : at[c].chem_bonds_valence++;
846 [ # # ]: 0 : if (bFixBug)
847 : 0 : at[c].charge--; /* added 2010-03-17 DT*/
848 : 0 : num_changes++;
849 : 0 : break;
850 : 0 : case 1:
851 : 0 : at[i2].charge++; /*= 0; changed 2010-03-17 DT*/
852 : 0 : at[i2].bond_type[0] = at[c].bond_type[k2] = BOND_TYPE_DOUBLE;
853 : 0 : at[i2].bond_stereo[0] = at[c].bond_stereo[k2] = 0;
854 : 0 : at[i2].chem_bonds_valence++;
855 : 0 : at[c].chem_bonds_valence++;
856 [ # # ]: 0 : if (bFixBug)
857 : 0 : at[c].charge--; /* added 2010-03-17 DT */
858 : 0 : num_changes++;
859 : 0 : break;
860 : : }
861 : : }
862 : :
863 : : /* -- removed -- 2010-03-17 DT
864 : : #if ( FIX_ODD_THINGS_REM_Plus_BUG == 1 )
865 : : at[c].charge -= charge;
866 : : #else
867 : : if ( bFixBug )
868 : : {
869 : : at[c].charge -= charge;
870 : : }
871 : : #endif
872 : : */
873 : 0 : break;
874 : : }
875 : : }
876 : : }
877 : : }
878 : :
879 : : /* A(doublet)-B(doublet) -> A=B (A and B have no other doublet neighbors) */
880 : : /* A(doublet)=B(doublet) -> A#B (A and B have no other doublet neighbors) */
881 [ + + ]: 9446 : for (i1 = 0; i1 < num_atoms; i1++)
882 : : {
883 [ - + - - ]: 9255 : if (RADICAL_DOUBLET == at[i1].radical &&
884 : 0 : 0 <= (i2 = the_only_doublet_neigh(at, i1, &k1, &k2)))
885 : : {
886 [ # # ]: 0 : if (at[i1].bond_type[k1] <= BOND_TYPE_DOUBLE)
887 : : {
888 : 0 : at[i1].bond_type[k1]++;
889 : 0 : at[i1].chem_bonds_valence++;
890 : 0 : at[i2].bond_type[k2]++;
891 : 0 : at[i2].chem_bonds_valence++;
892 : 0 : at[i1].radical = 0;
893 : 0 : at[i2].radical = 0;
894 : : }
895 : : }
896 : : }
897 : :
898 : : #if (REMOVE_ION_PAIRS_EARLY == 1)
899 : 191 : num_changes += remove_ion_pairs(num_atoms, at);
900 : : #endif
901 : :
902 : 191 : return num_changes;
903 : : }
904 : :
905 : : /****************************************************************************/
906 : 1 : int post_fix_odd_things(int num_atoms, inp_ATOM *at)
907 : : {
908 : 1 : int num_changes = 0;
909 : : /* currently does nothing */
910 : 1 : return num_changes;
911 : : }
912 : :
913 : : /****************************************************************************/
914 : 0 : int nFindOneOM(inp_ATOM *at, int at_no, int ord_OM[], int num_OM)
915 : : {
916 : : int i, n_OM, best_value, cur_value, diff; /* djb-rwth: removing redundant variables */
917 : : int num_best;
918 : :
919 [ # # ]: 0 : if (1 == num_OM)
920 : : {
921 : 0 : return ord_OM[0];
922 : : }
923 [ # # ]: 0 : if (1 > num_OM)
924 : : {
925 : 0 : return -1;
926 : : }
927 : :
928 : : /* select neighbors with min. number of bonds */
929 : 0 : num_best = 1;
930 : 0 : n_OM = (int)at[at_no].neighbor[ord_OM[0]];
931 : 0 : best_value = (int)at[n_OM].valence;
932 : : /* compare number of bonds; move indexes of the best neighbors to the first elements of ord_OM[] */
933 [ # # ]: 0 : for (i = 1; i < num_OM; i++)
934 : : {
935 : 0 : n_OM = at[at_no].neighbor[ord_OM[i]];
936 : 0 : cur_value = (int)at[n_OM].valence;
937 : 0 : diff = cur_value - best_value;
938 [ # # ]: 0 : if (diff < 0)
939 : : {
940 : : /* djb-rwth: removing redundant code */
941 : 0 : best_value = cur_value;
942 : 0 : ord_OM[0] = ord_OM[i];
943 : 0 : num_best = 1;
944 : : }
945 [ # # ]: 0 : else if (diff == 0)
946 : : { /* was '=', pointed by WDI */
947 : 0 : ord_OM[num_best++] = ord_OM[i];
948 : : }
949 : : }
950 : 0 : num_OM = num_best;
951 [ # # ]: 0 : if (1 == num_OM)
952 : : {
953 : 0 : return ord_OM[0];
954 : : }
955 : :
956 : : /* select neighbors with min. periodic numbers */
957 : 0 : num_best = 1;
958 : 0 : n_OM = (int)at[at_no].neighbor[ord_OM[0]];
959 : 0 : best_value = (int)at[n_OM].el_number;
960 : :
961 : : /* compare periodic numbers; move indexes of the best neighbors to the first elements of ord_OM[] */
962 [ # # ]: 0 : for (i = 1; i < num_OM; i++)
963 : : {
964 : 0 : n_OM = at[at_no].neighbor[ord_OM[i]];
965 : 0 : cur_value = (int)at[n_OM].el_number;
966 : 0 : diff = cur_value - best_value;
967 [ # # ]: 0 : if (diff < 0)
968 : : {
969 : : /* djb-rwth: removing redundant code */
970 : 0 : best_value = cur_value;
971 : 0 : ord_OM[0] = ord_OM[i];
972 : 0 : num_best = 1;
973 : : }
974 [ # # ]: 0 : else if (diff == 0)
975 : : { /* was '=', pointed by WDI */
976 : 0 : ord_OM[num_best++] = ord_OM[i];
977 : : }
978 : : }
979 : 0 : num_OM = num_best;
980 [ # # ]: 0 : if (1 == num_OM)
981 : : {
982 : 0 : return ord_OM[0];
983 : : }
984 : :
985 : : /* if neighbors are not terminal atoms then reject */
986 [ # # ]: 0 : if (1 < at[n_OM].valence)
987 : : {
988 : 0 : return -1;
989 : : }
990 : :
991 : : /* if neighbors are terminal atoms then the one without isotope or with lightest isotope */
992 : 0 : num_best = 1;
993 : 0 : n_OM = (int)at[at_no].neighbor[ord_OM[0]];
994 : 0 : best_value = (int)at[n_OM].iso_atw_diff;
995 : :
996 : : /* compare periodic numbers; move indexes of the best neighbors to the first elements of ord_OM[] */
997 [ # # ]: 0 : for (i = 1; i < num_OM; i++)
998 : : {
999 : 0 : n_OM = at[at_no].neighbor[ord_OM[i]];
1000 : 0 : cur_value = (int)at[n_OM].el_number;
1001 : 0 : diff = cur_value - best_value;
1002 [ # # # # : 0 : if ((!cur_value && best_value) || diff < 0)
# # ]
1003 : : {
1004 : : /* djb-rwth: removing redundant code */
1005 : 0 : best_value = cur_value;
1006 : 0 : ord_OM[0] = ord_OM[i];
1007 : 0 : num_best = 1;
1008 : : }
1009 [ # # ]: 0 : else if (diff == 0)
1010 : : {
1011 : : /* was '=', pointed by WDI */
1012 : 0 : ord_OM[num_best++] = ord_OM[i];
1013 : : }
1014 : : }
1015 : :
1016 : 0 : num_OM = num_best;
1017 [ # # ]: 0 : if (1 == num_OM)
1018 : : {
1019 : 0 : return ord_OM[0];
1020 : : }
1021 : :
1022 : : /* return any */
1023 : 0 : return ord_OM[0];
1024 : : }
1025 : :
1026 : : /****************************************************************************
1027 : : NB:
1028 : : the bonds are fixed in fix_special_bonds()
1029 : : ****************************************************************************/
1030 : 192 : int remove_ion_pairs(int num_atoms, inp_ATOM *at)
1031 : : {
1032 : 192 : int num_changes = 0;
1033 : : #define MAX_NEIGH 6
1034 : :
1035 : : int i, n, n2, i1, i2, i3, i4, type, chrg;
1036 : 192 : int num_C_II = 0, num_C_plus = 0, num_C_minus = 0, num_N_plus = 0, num_N_minus = 0, num_O_plus = 0, num_O_minus = 0, num_All;
1037 : :
1038 : : #ifdef FIX_P_IV_Plus_O_Minus
1039 : 192 : int num_P_IV_plus = 0; /* added 2010-03-17 DT */
1040 : : #endif
1041 : :
1042 : : inp_ATOM *a;
1043 : : /****** count candidates ********/
1044 [ + + ]: 9449 : for (i = 0, a = at; i < num_atoms; i++, a++)
1045 : : {
1046 [ + + + + ]: 9257 : if (1 == (chrg = a->charge) || -1 == chrg)
1047 : : {
1048 [ + + + + ]: 122 : switch (ion_el_group(a->el_number))
1049 : : {
1050 : 2 : case EL_NUMBER_C:
1051 [ - + ]: 2 : if (chrg > 0)
1052 : : {
1053 : 0 : num_C_plus++;
1054 : : }
1055 : : else
1056 : : {
1057 : 2 : num_C_minus++;
1058 : : }
1059 : 2 : break;
1060 : 40 : case EL_NUMBER_O:
1061 [ - + ]: 40 : if (chrg > 0)
1062 : : {
1063 : 0 : num_O_plus++;
1064 : : }
1065 : : else
1066 : : {
1067 : 40 : num_O_minus++;
1068 : : }
1069 : 40 : break;
1070 : 1 : case EL_NUMBER_N:
1071 [ + - ]: 1 : if (chrg > 0)
1072 : : {
1073 : 1 : num_N_plus++;
1074 : : }
1075 : : else
1076 : : {
1077 : 0 : num_N_minus++;
1078 : : }
1079 : : #ifdef FIX_P_IV_Plus_O_Minus
1080 [ # # ]: 0 : num_P_IV_plus += a->el_number != EL_NUMBER_N &&
1081 : 0 : chrg == 1 &&
1082 [ - + - - ]: 1 : a->valence == 4 &&
1083 [ # # ]: 0 : a->chem_bonds_valence == 4; /* added 2010-03-17 DT */
1084 : : #endif
1085 : 1 : break;
1086 : : }
1087 : : }
1088 [ + + + + : 9336 : else if (!chrg && a->chem_bonds_valence + NUMH(a, 0) == 2 &&
- + ]
1089 [ - - ]: 158 : get_el_valence(a->el_number, 0, 0) == 4 &&
1090 : 0 : ion_el_group(a->el_number) == EL_NUMBER_C)
1091 : : {
1092 : 0 : num_C_II++;
1093 : : }
1094 : : }
1095 : :
1096 : 192 : num_All = num_C_II + num_C_plus + num_C_minus + num_N_plus + num_N_minus + num_O_plus + num_O_minus;
1097 : :
1098 : : /* do not add num_P_IV_plus ! -- 2010-03-17 DT */
1099 [ + + ]: 192 : if (!num_All)
1100 : : {
1101 : 173 : return 0;
1102 : : }
1103 : :
1104 : : /**************************************************************************/
1105 : : /*************************** Terminal ion pairs ***************************/
1106 : : /**************************************************************************/
1107 : :
1108 : : /*-------------------------------------------------------------------------
1109 : : Pair type 1 N=N,P,As,Sb; O=O,S,Se,Te
1110 : : ===========
1111 : :
1112 : : X X if X is another -O(-) then neutralize O(-)
1113 : : | | that has the smallest periodic table number
1114 : : O=N(+)-O(-) => O=N=O
1115 : : i n
1116 : : --------------------------------------------------------------------------*/
1117 : :
1118 [ + + ]: 361 : for (type = 1; type <= 18; type++)
1119 : : {
1120 [ + - + + ]: 342 : if ((!type || 1 == type))
1121 : : {
1122 [ + + + + : 97 : for (i = 0; i < num_atoms && 0 < num_N_plus && 0 < num_O_minus; i++)
+ - ]
1123 : : {
1124 [ + + + - : 79 : if (1 == at[i].charge && 3 == nNoMetalNumBonds(at, i) &&
+ - ]
1125 [ + - ]: 2 : 4 == nNoMetalBondsValence(at, i) &&
1126 : 1 : ion_el_group(at[i].el_number) == EL_NUMBER_N)
1127 : : {
1128 : 1 : int num_OM = 0, ord_OM[3]; /* -O(-) */
1129 : 1 : int num_O = 0; /* =O */
1130 : 1 : int num_O_other = 0;
1131 [ + + ]: 4 : for (i1 = 0; i1 < at[i].valence; i1++)
1132 : : {
1133 : 3 : n = at[i].neighbor[i1];
1134 [ + + + - : 4 : if (1 == nNoMetalNumBonds(at, n) && 0 == num_of_H(at, n) &&
+ - ]
1135 : 1 : ion_el_group(at[n].el_number) == EL_NUMBER_O) /* djb-rwth: ignoring LLVM warning: variable used */
1136 : : {
1137 [ - + ]: 1 : if (BOND_TYPE_SINGLE == at[i].bond_type[i1] &&
1138 [ # # ]: 0 : -1 == at[n].charge)
1139 : : {
1140 : 0 : ord_OM[num_OM++] = i1;
1141 : : }
1142 [ + - ]: 1 : else if (BOND_TYPE_DOUBLE == at[n].bond_type[0] &&
1143 [ + - ]: 1 : 0 == at[n].charge)
1144 : : {
1145 : 1 : num_O++;
1146 : : }
1147 : : else
1148 : : {
1149 : 0 : num_O_other++;
1150 : : }
1151 : : }
1152 : : }
1153 [ - + - - : 1 : if (num_OM > 0 && num_O > 0 && !num_O_other &&
- - - - ]
1154 : 0 : 0 <= (i1 = nFindOneOM(at, i, ord_OM, num_OM)))
1155 : : {
1156 : : /* remove charges and increase bond order */
1157 : 0 : n = at[i].neighbor[i1];
1158 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1159 : 0 : at[i].bond_type[i1]++;
1160 : 0 : at[n].bond_type[i2]++;
1161 : 0 : at[i].chem_bonds_valence++;
1162 : 0 : at[n].chem_bonds_valence++;
1163 : 0 : at[i].charge--;
1164 : 0 : at[n].charge++;
1165 : 0 : at[i].radical = 0;
1166 : 0 : at[n].radical = 0;
1167 : 0 : num_changes++;
1168 : 0 : num_N_plus--;
1169 : 0 : num_O_minus--;
1170 : 0 : num_All -= 2;
1171 : : }
1172 : : }
1173 : : }
1174 : :
1175 : : #ifdef FIX_P_IV_Plus_O_Minus
1176 : :
1177 : : /*-------------------------------------------------------------------------
1178 : : Pair type 1a P=P,As,Sb; O=O,S,Se,Te -- added 2010-03-17
1179 : : =============
1180 : :
1181 : : X X if X, Y, or Z is another -O(-) then neutralize O(-)
1182 : : | | that has the smallest periodic table number
1183 : : Y-P(+)-O(-) => Y-P=O
1184 : : |i n |
1185 : : Z Z
1186 : :
1187 : : --------------------------------------------------------------------------*/
1188 : :
1189 [ + - - + : 19 : for (i = 0; i < num_atoms && 0 < num_P_IV_plus /*&& 0 < num_N_plus*/ && 0 < num_O_minus; i++)
- - ]
1190 : : {
1191 [ # # # # : 0 : if (1 == at[i].charge && 4 == nNoMetalNumBonds(at, i) &&
# # ]
1192 : 0 : 4 == nNoMetalBondsValence(at, i) &&
1193 [ # # # # ]: 0 : at[i].el_number != EL_NUMBER_N && ion_el_group(at[i].el_number) == EL_NUMBER_N)
1194 : : {
1195 : 0 : int num_OM = 0, ord_OM[4]; /* -O(-) */
1196 : : /*int num_O = 0;*/ /* =O */
1197 : : /* djb-rwth: removing redundant variables */
1198 [ # # ]: 0 : for (i1 = 0; i1 < at[i].valence; i1++)
1199 : : {
1200 : 0 : n = at[i].neighbor[i1];
1201 [ # # # # : 0 : if (1 == nNoMetalNumBonds(at, n) && 0 == num_of_H(at, n) &&
# # ]
1202 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_O) /* djb-rwth: ignoring LLVM warning: variable used */
1203 : : {
1204 [ # # ]: 0 : if (BOND_TYPE_SINGLE == at[i].bond_type[i1] &&
1205 [ # # ]: 0 : -1 == at[n].charge)
1206 : : {
1207 : 0 : ord_OM[num_OM++] = i1;
1208 : : /*
1209 : : }
1210 : : if ( BOND_TYPE_DOUBLE == at[n].bond_type[0] &&
1211 : : 0 == at[n].charge ) {
1212 : : num_O ++;
1213 : : */
1214 : : }
1215 : : /* djb-rwth: removing redundant code */
1216 : : }
1217 : : }
1218 [ # # # # ]: 0 : if (num_OM > 0 /*&& num_O > 0 && !num_O_other*/ &&
1219 : 0 : 0 <= (i1 = nFindOneOM(at, i, ord_OM, num_OM)))
1220 : : {
1221 : : /* remove charges and increase bond order */
1222 : 0 : n = at[i].neighbor[i1];
1223 : 0 : i2 = is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor;
1224 : 0 : at[i].bond_type[i1]++;
1225 : 0 : at[n].bond_type[i2]++;
1226 : 0 : at[i].chem_bonds_valence++;
1227 : 0 : at[n].chem_bonds_valence++;
1228 : 0 : at[i].charge--;
1229 : 0 : at[n].charge++;
1230 : 0 : at[i].radical = 0;
1231 : 0 : at[n].radical = 0;
1232 : 0 : num_changes++;
1233 : 0 : num_N_plus--;
1234 : 0 : num_O_minus--;
1235 : 0 : num_P_IV_plus--;
1236 : 0 : num_All -= 2;
1237 : : }
1238 : : }
1239 : : }
1240 : : #endif /* FIX_P_IV_Plus_O_Minus */
1241 : : }
1242 : :
1243 : : /*-------------------------------------------------------------------------
1244 : : Terminal pair types: 2,3,4,5,6,7,8,9 N=N,P,As,Sb; O=O,S,Se,Te; C=C,Si
1245 : : ====================================
1246 : : type #
1247 : : 2 2: O=N-C(II)- => O=N#C- N=N,P,As,Sb; O=O,S,Se,Te; C=C,Si
1248 : : 3 9: O=O(+)-C(-)(III) => O=O=C(IV)
1249 : : 4 3: O(-)-N(+)(IV) => O=N(V) (input structure has at least 1 double bond)
1250 : : 5 4: O(-)-O(+)(III) => O=O(IV)
1251 : : 6 8: O(-)-O-C(+)(III) => O=O=C(IV)
1252 : : 7 5: N(-)=N(+)(IV) => N#N(V) allow terminal H on N(-)
1253 : : 8 6: N(-)=O(+)(III) => N#O-
1254 : : 9 7: N(-)=C(+)(III) => N#C-
1255 : : --------------------------------------------------------------------------*/
1256 : :
1257 [ + - + + : 342 : if (!type || (2 <= type && type <= 9)) /* djb-rwth: addressing LLVM warning */
+ + ]
1258 : : {
1259 [ + + + - ]: 2160 : for (i = 0; i < num_atoms && 0 < num_All; i++)
1260 : : {
1261 [ + + + + : 2112 : if (0 == at[i].charge && 1 == nNoMetalNumBonds(at, i) && 2 == nNoMetalBondsValence(at, i) &&
+ + + - ]
1262 [ + - ]: 208 : 0 == num_of_H(at, i) &&
1263 [ + - ]: 208 : ion_el_group(at[i].el_number) == EL_NUMBER_O &&
1264 : 104 : 0 <= (i1 = nNoMetalNeighIndex(at, i)) &&
1265 [ + - ]: 104 : at[i].bond_type[i1] <= BOND_TYPE_TRIPLE)
1266 : : {
1267 : : /* terminal O= */
1268 : 104 : n = at[i].neighbor[i1];
1269 [ + - + + : 104 : if ((!type || type == 2) && 0 < num_C_II)
- + ]
1270 : : {
1271 : : /* avoid alternating bonds */
1272 [ # # # # ]: 0 : if (0 == at[n].charge &&
1273 [ # # # # ]: 0 : 2 == nNoMetalNumBonds(at, n) && 3 == nNoMetalBondsValence(at, n) &&
1274 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1275 [ # # ]: 0 : ion_el_group(at[n].el_number) == EL_NUMBER_N &&
1276 : 0 : 0 <= (i2 = nNoMetalOtherNeighIndex(at, n, i)) &&
1277 [ # # ]: 0 : at[n].bond_type[i2] <= BOND_TYPE_TRIPLE)
1278 : : {
1279 : : /* i2 = index of opposite to at[i] neighbor of at[n] */
1280 : : /*i2 = (at[n].neighbor[0] == i);*/
1281 : 0 : n2 = at[n].neighbor[i2];
1282 [ # # ]: 0 : if (0 == at[n2].charge &&
1283 [ # # # # : 0 : 2 == at[n2].valence && 2 == at[n2].chem_bonds_valence &&
# # ]
1284 [ # # ]: 0 : 0 == num_of_H(at, n2) &&
1285 : 0 : ion_el_group(at[n2].el_number) == EL_NUMBER_C)
1286 : : {
1287 : : /* i n n2 */
1288 : : /* found O=N-C(II)- */
1289 : : /* convert O=N-C(II)- => O=N#C- */
1290 : :
1291 : 0 : i3 = (at[n2].neighbor[0] != n); /* index of at[n] neighbor of n2 */
1292 : 0 : at[n].chem_bonds_valence = 5; /* N */
1293 : 0 : at[n2].chem_bonds_valence = 4; /* C */
1294 : 0 : at[n].bond_type[i2] = BOND_TYPE_TRIPLE;
1295 : 0 : at[n2].bond_type[i3] = BOND_TYPE_TRIPLE;
1296 : 0 : at[n2].radical = 0;
1297 : 0 : num_changes++;
1298 : 0 : num_C_II--;
1299 : 0 : num_All--;
1300 : 0 : continue;
1301 : : }
1302 : : }
1303 : : }
1304 : :
1305 [ + - + + : 104 : if ((!type || type == 3) && 0 < num_O_plus && 0 < num_C_minus)
- + - - ]
1306 : : {
1307 [ # # # # : 0 : if (1 == at[n].charge && 2 == nNoMetalNumBonds(at, n) && 3 == nNoMetalBondsValence(at, n) &&
# # # # ]
1308 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1309 [ # # ]: 0 : ion_el_group(at[n].el_number) == EL_NUMBER_O &&
1310 : 0 : 0 <= (i2 = nNoMetalOtherNeighIndex(at, n, i)) &&
1311 [ # # ]: 0 : at[n].bond_type[i2] <= BOND_TYPE_TRIPLE)
1312 : : {
1313 : : /* found O=O(+)- */
1314 : : /* i2 = index of opposite to at[i] neighbor of at[n] */
1315 : : /*i2 = (at[n].neighbor[0] == i);*/
1316 : 0 : n2 = at[n].neighbor[i2];
1317 [ # # # # : 0 : if (-1 == at[n2].charge && 3 >= nNoMetalNumBonds(at, n2) && 3 == nNoMetalBondsValence(at, n2) + NUMH(at, n2) &&
# # # # ]
1318 : 0 : ion_el_group(at[n2].el_number) == EL_NUMBER_C)
1319 : : {
1320 : : /* i n n2 */
1321 : : /* found found O=O(+)-C(-)(III) */
1322 : : /* convert O=O(+)-C(-)(III) => O=O=C(IV) */
1323 : 0 : i3 = (at[n2].neighbor[0] != n); /* index of at[n] neighbor of n2 */
1324 : 0 : at[n].charge--;
1325 : 0 : at[n2].charge++;
1326 : 0 : at[n].chem_bonds_valence += 1; /* =O- => =O= */
1327 : 0 : at[n2].chem_bonds_valence += 1; /* -C => =C */
1328 : 0 : at[n].bond_type[i2] = BOND_TYPE_DOUBLE;
1329 : 0 : at[n2].bond_type[i3] = BOND_TYPE_DOUBLE;
1330 : 0 : num_changes++;
1331 : 0 : num_O_plus--;
1332 : 0 : num_C_minus--;
1333 : 0 : num_All -= 2;
1334 : 0 : continue;
1335 : : }
1336 : : }
1337 : : }
1338 : : }
1339 : :
1340 [ + + ]: 1904 : else if (-1 == at[i].charge &&
1341 [ + + ]: 344 : 0 < num_O_minus + num_N_minus &&
1342 [ + + + - ]: 352 : 0 < num_N_plus + num_O_plus + num_C_plus &&
1343 [ + - + - ]: 72 : 1 == nNoMetalNumBonds(at, i) && 1 == nNoMetalBondsValence(at, i) &&
1344 [ + - ]: 48 : 0 == num_of_H(at, i) &&
1345 [ + - ]: 48 : ion_el_group(at[i].el_number) == EL_NUMBER_O &&
1346 : 24 : 0 <= (i1 = nNoMetalNeighIndex(at, i)) &&
1347 [ + - ]: 24 : at[i].bond_type[i1] <= BOND_TYPE_TRIPLE)
1348 : : {
1349 : : /* terminal O(-)- */
1350 : 24 : n = at[i].neighbor[i1];
1351 : :
1352 [ + - + + : 24 : if ((!type || type == 4) && 0 < num_O_minus && 0 < num_N_plus && /* O(-)-N(+)(IV) */
+ - + - ]
1353 [ - + - - : 3 : 1 == at[n].charge && 3 >= nNoMetalNumBonds(at, n) && 4 == nNoMetalBondsValence(at, n) &&
- - - - ]
1354 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1355 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_N /* except >O(+)- */
1356 : : )
1357 : : {
1358 : : /* found O(-)-N(+)(IV) */
1359 : : /* convert O(-)-N(+)(IV) => O=N(V) */
1360 : :
1361 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor); /* index of at[i] neighbor of at[n] */
1362 : 0 : at[i].charge++;
1363 : 0 : at[n].charge--;
1364 : 0 : at[i].chem_bonds_valence++;
1365 : 0 : at[n].chem_bonds_valence++;
1366 : 0 : at[i].bond_type[i1]++;
1367 : 0 : at[n].bond_type[i2]++;
1368 : 0 : num_changes++;
1369 : 0 : num_O_minus--;
1370 : 0 : num_N_plus--;
1371 : 0 : num_All -= 2;
1372 : 0 : continue;
1373 : : }
1374 : :
1375 [ + - + + : 24 : if ((!type || type == 5) && 0 < num_O_minus && 0 < num_O_plus && /* O(-)-O(+)(III) */
+ - - + ]
1376 [ # # # # : 0 : 1 == at[n].charge && 3 >= nNoMetalNumBonds(at, n) && 3 == nNoMetalBondsValence(at, n) &&
# # # # ]
1377 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1378 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_O /* except >O(+)- */
1379 : : )
1380 : : {
1381 : : /* found O(+)(III) */
1382 : : /* convert O(-)-O(+)(III) => O=O(IV) */
1383 : :
1384 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor); /* index of at[i] neighbor of at[n] */
1385 : 0 : at[i].charge++;
1386 : 0 : at[n].charge--;
1387 : 0 : at[i].chem_bonds_valence++;
1388 : 0 : at[n].chem_bonds_valence++;
1389 : 0 : at[i].bond_type[i1]++;
1390 : 0 : at[n].bond_type[i2]++;
1391 : 0 : num_changes++;
1392 : 0 : num_O_minus--;
1393 : 0 : num_O_plus--;
1394 : 0 : num_All -= 2;
1395 : 0 : continue;
1396 : : }
1397 : :
1398 : : /* i n n2 */
1399 [ + - + + : 24 : if ((!type || type == 6) && /* O(-)-O-C(+)(III) */
+ - ]
1400 [ - + ]: 3 : 0 < num_O_minus && 0 < num_C_plus &&
1401 [ # # # # : 0 : 0 == at[n].charge && 2 == nNoMetalNumBonds(at, n) && 2 == nNoMetalBondsValence(at, n) &&
# # # # ]
1402 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1403 [ # # ]: 0 : ion_el_group(at[n].el_number) == EL_NUMBER_O &&
1404 : 0 : 0 <= (i2 = nNoMetalOtherNeighIndex(at, n, i)) &&
1405 [ # # ]: 0 : at[n].bond_type[i2] <= BOND_TYPE_TRIPLE)
1406 : : {
1407 : : /* found O(-)-O- */
1408 : : /* i2 = index of opposite to at[i] neighbor of at[n] */
1409 : : /*i2 = (at[n].neighbor[0] == i);*/
1410 : 0 : n2 = at[n].neighbor[i2];
1411 [ # # # # ]: 0 : if (1 == at[n2].charge && 3 >= nNoMetalNumBonds(at, n2) &&
1412 [ # # # # ]: 0 : 3 == nNoMetalBondsValence(at, n2) + NUMH(at, n2) &&
1413 : 0 : ion_el_group(at[n2].el_number) == EL_NUMBER_C)
1414 : : {
1415 : : /* i n n2 */
1416 : : /* found O(-)-O-C(+)(III) */
1417 : : /* convert O(-)-O-C(+)(III) => O=O=C(IV) */
1418 : : /*i3 = (at[n2].neighbor[0] != n);*/ /* i3 = index of at[n] neighbor of at[n2] */
1419 : 0 : i3 = (int)(is_in_the_list(at[n2].neighbor, (AT_NUMB)n, at[n2].valence) - at[n2].neighbor);
1420 : : /*i4 = index of at[i] in the adjacency list of at[n] */
1421 : 0 : i4 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1422 : 0 : at[i].charge++;
1423 : 0 : at[n2].charge--;
1424 : 0 : at[i].chem_bonds_valence += 1; /* O- => O= */
1425 : 0 : at[n].chem_bonds_valence += 2; /* -O- => =O= */
1426 : 0 : at[n2].chem_bonds_valence += 1; /* -C => =C */
1427 : 0 : at[i].bond_type[i1] = BOND_TYPE_DOUBLE;
1428 : 0 : at[n].bond_type[i4] = BOND_TYPE_DOUBLE;
1429 : 0 : at[n].bond_type[i2] = BOND_TYPE_DOUBLE;
1430 : 0 : at[n2].bond_type[i3] = BOND_TYPE_DOUBLE;
1431 : 0 : num_changes++;
1432 : 0 : num_O_minus--;
1433 : 0 : num_C_plus--;
1434 : 0 : num_All -= 2;
1435 : 0 : continue;
1436 : : }
1437 : : }
1438 : : }
1439 [ + + - + : 1880 : else if (-1 == at[i].charge && 0 < num_N_minus && 0 < num_N_plus + num_O_plus + num_C_plus &&
- - - - ]
1440 [ # # # # ]: 0 : 1 == nNoMetalNumBonds(at, i) && 2 == nNoMetalBondsValence(at, i) + NUMH(at, i) &&
1441 : : /*0 == num_of_H( at, i ) &&*/
1442 [ # # ]: 0 : ion_el_group(at[i].el_number) == EL_NUMBER_N &&
1443 : 0 : 0 <= (i1 = nNoMetalNeighIndex(at, i)) &&
1444 [ # # ]: 0 : at[i].bond_type[i1] <= BOND_TYPE_TRIPLE)
1445 : : {
1446 : : /* terminal N(-)= */
1447 : 0 : n = at[i].neighbor[i1 = 0];
1448 [ # # # # : 0 : if ((!type || type == 7) && 0 < num_N_plus && /* N(-)=N(+)(IV) */
# # ]
1449 [ # # # # : 0 : 1 == at[n].charge && 3 >= nNoMetalNumBonds(at, n) && 4 == nNoMetalBondsValence(at, n) &&
# # # # ]
1450 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1451 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_N)
1452 : : {
1453 : : /* found N(-)-N(+)(IV) */
1454 : : /* convert N(-)=N(+)(IV) => N#N(V) */
1455 : :
1456 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor); /* index of at[i] neighbor of at[n] */
1457 : 0 : at[i].charge++;
1458 : 0 : at[n].charge--;
1459 : 0 : at[i].chem_bonds_valence++;
1460 : 0 : at[n].chem_bonds_valence++;
1461 : 0 : at[i].bond_type[i1]++;
1462 : 0 : at[n].bond_type[i2]++;
1463 : 0 : num_changes++;
1464 : 0 : num_N_minus--;
1465 : 0 : num_N_plus--;
1466 : 0 : num_All -= 2;
1467 : 0 : continue;
1468 : : }
1469 [ # # # # : 0 : if ((!type || type == 8) && 0 < num_O_plus && /* N(-)=O(+)(III) */
# # ]
1470 [ # # # # : 0 : 1 == at[n].charge && 2 == nNoMetalNumBonds(at, n) && 3 == nNoMetalBondsValence(at, n) &&
# # # # ]
1471 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1472 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_O)
1473 : : {
1474 : : /* found N(-)-O(+)(III) */
1475 : : /* convert N(-)=O(+)(III) => N#O(IV)- */
1476 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor); /* index of at[i] neighbor of at[n] */
1477 : 0 : at[i].charge++;
1478 : 0 : at[n].charge--;
1479 : 0 : at[i].chem_bonds_valence++;
1480 : 0 : at[n].chem_bonds_valence++;
1481 : 0 : at[i].bond_type[i1]++;
1482 : 0 : at[n].bond_type[i2]++;
1483 : 0 : num_changes++;
1484 : 0 : num_N_minus--;
1485 : 0 : num_O_plus--;
1486 : 0 : num_All -= 2;
1487 : 0 : continue;
1488 : : }
1489 [ # # # # : 0 : if ((!type || type == 9) && 0 < num_C_plus && /* N(-)=C(+)(III) */
# # ]
1490 [ # # # # : 0 : 1 == at[n].charge && 2 == at[n].valence && 3 == at[n].chem_bonds_valence &&
# # # # ]
1491 [ # # ]: 0 : 0 == num_of_H(at, n) &&
1492 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1493 : : {
1494 : : /* found N(-)=C(+)(III) */
1495 : : /* convert N(-)=C(+)(III) => N#C(IV)- */
1496 : :
1497 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor); /* index of at[i] neighbor of at[n] */
1498 : 0 : at[i].charge++;
1499 : 0 : at[n].charge--;
1500 : 0 : at[i].chem_bonds_valence++;
1501 : 0 : at[n].chem_bonds_valence++;
1502 : 0 : at[i].bond_type[i1]++;
1503 : 0 : at[n].bond_type[i2]++;
1504 : 0 : num_changes++;
1505 : 0 : num_N_minus--;
1506 : 0 : num_C_plus--;
1507 : 0 : num_All -= 2;
1508 : 0 : continue;
1509 : : }
1510 : : }
1511 : : }
1512 : : }
1513 : :
1514 : : /**************************************************************************/
1515 : : /*********************** NON-Terminal ion pairs ***************************/
1516 : : /**************************************************************************/
1517 : : /*-------------------------------------------------------------------------
1518 : : Non-Terminal pair types: 10,11,12,13,14 N=N,P,As,Sb; O=O,S,Se,Te; C=C,Si
1519 : : ========================================
1520 : :
1521 : : 10: N(+)(IV)-C(-)(III) => N(V)=C(IV) (N has 3 or 2 bonds)
1522 : : 11: N(+)(IV)=C(-)(III) => N(V)#C(IV) (N has 3 or 2 bonds)
1523 : : 12: N(+)(IV)-N(-)(II) => N(V)=N(III) (allow terminal H on N(-))
1524 : : 13: -O(+)-C(-)(III) => -O=C-
1525 : : 14: -O(+)=C(-)(III) => -O#C-
1526 : : 15: O(+)(III)-N(-)(II) => O(IV)=N(III) (allow terminal H on N(-))
1527 : : --------------------------------------------------------------------------*/
1528 : :
1529 [ + - + + : 342 : if (!type || (10 <= type && type <= 15)) /* djb-rwth: addressing LLVM warning */
+ + ]
1530 : : {
1531 [ + + + - ]: 1620 : for (i = 0; i < num_atoms && 0 < num_All; i++)
1532 : : {
1533 [ + + ]: 1506 : if (1 == at[i].charge &&
1534 [ + + - + : 90 : 0 < num_N_plus + num_O_plus && 0 < num_C_minus + num_N_minus &&
- - ]
1535 [ # # # # ]: 0 : 4 >= nNoMetalNumBonds(at, i) && 4 == nNoMetalBondsValence(at, i) &&
1536 [ # # ]: 0 : 0 == num_of_H(at, i) &&
1537 : 0 : ion_el_group(at[i].el_number) == EL_NUMBER_N)
1538 : : {
1539 : : /* found non-terminal N(+)(IV) */
1540 [ # # # # : 0 : if ((!type || 10 == type) && 0 < num_N_plus && 0 < num_C_minus)
# # # # ]
1541 : : {
1542 : 0 : int num_neigh = 0, pos_neigh = -1;
1543 [ # # ]: 0 : for (i1 = 0; i1 < at[i].valence; i1++)
1544 : : {
1545 : 0 : n = at[i].neighbor[i1];
1546 [ # # # # : 0 : if (-1 == at[n].charge && 3 >= at[n].valence && 3 == at[n].chem_bonds_valence + NUMH(at, n) &&
# # ]
1547 : : /*0 == at[n].num_H &&*/
1548 [ # # # # ]: 0 : at[i].bond_type[i1] == BOND_TYPE_SINGLE &&
1549 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1550 : : {
1551 : : /* found N(+)(IV)-C(-)(III); prepare conversion to N(V)=C(IV) */
1552 : 0 : num_neigh++;
1553 : 0 : pos_neigh = i1;
1554 : : }
1555 : : }
1556 : 0 : i1 = pos_neigh;
1557 [ # # ]: 0 : if (1 == num_neigh &&
1558 [ # # # # ]: 0 : at[i].bond_type[i1] <= BOND_TYPE_TRIPLE &&
1559 [ # # ]: 0 : !has_other_ion_neigh(at, i, n = at[i].neighbor[i1]) &&
1560 : 0 : !has_other_ion_neigh(at, n, i))
1561 : : {
1562 : : /*n = at[i].neighbor[i1=pos_neigh];*/
1563 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1564 : 0 : at[i].charge--;
1565 : 0 : at[n].charge++;
1566 : 0 : at[i].chem_bonds_valence++;
1567 : 0 : at[n].chem_bonds_valence++;
1568 : 0 : at[i].bond_type[i1]++;
1569 : 0 : at[n].bond_type[i2]++;
1570 : 0 : num_changes++;
1571 : 0 : num_C_minus--;
1572 : 0 : num_N_plus--;
1573 : 0 : num_All -= 2;
1574 : 0 : continue;
1575 : : }
1576 : : }
1577 [ # # # # : 0 : if ((!type || 11 == type) && 0 < num_N_plus && 0 < num_C_minus)
# # # # ]
1578 : : {
1579 : 0 : int num_neigh = 0, pos_neigh = -1;
1580 [ # # ]: 0 : for (i1 = 0; i1 < at[i].valence; i1++)
1581 : : {
1582 : 0 : n = at[i].neighbor[i1];
1583 [ # # # # : 0 : if (-1 == at[n].charge && 3 >= at[n].valence && 3 == at[n].chem_bonds_valence + NUMH(at, n) &&
# # ]
1584 : : /*0 == at[n].num_H &&*/
1585 [ # # # # ]: 0 : at[i].bond_type[i1] == BOND_TYPE_DOUBLE &&
1586 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1587 : : {
1588 : : /* found N(+)(IV)=C(-)(III); prepare conversion to N(V)#C(IV) */
1589 : 0 : num_neigh++;
1590 : 0 : pos_neigh = i1;
1591 : : }
1592 : : }
1593 [ # # # # ]: 0 : if (1 == num_neigh &&
1594 [ # # ]: 0 : !has_other_ion_neigh(at, i, n = at[i].neighbor[i1 = pos_neigh]) &&
1595 : 0 : !has_other_ion_neigh(at, n, i))
1596 : : {
1597 : : /*n = at[i].neighbor[i1=pos_neigh];*/
1598 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1599 : 0 : at[i].charge--;
1600 : 0 : at[n].charge++;
1601 : 0 : at[i].chem_bonds_valence++;
1602 : 0 : at[n].chem_bonds_valence++;
1603 : 0 : at[i].bond_type[i1]++;
1604 : 0 : at[n].bond_type[i2]++;
1605 : 0 : num_changes++;
1606 : 0 : num_C_minus--;
1607 : 0 : num_N_plus--;
1608 : 0 : num_All -= 2;
1609 : 0 : continue;
1610 : : }
1611 : : }
1612 [ # # # # : 0 : if (!type || (12 == type && 0 < num_N_plus && 0 < num_N_minus)) /* djb-rwth: addressing LLVM warning */
# # # # ]
1613 : : {
1614 : 0 : int num_neigh = 0, pos_neigh = -1;
1615 [ # # ]: 0 : for (i1 = 0; i1 < at[i].valence; i1++)
1616 : : {
1617 : 0 : n = at[i].neighbor[i1];
1618 [ # # # # ]: 0 : if (-1 == at[n].charge && 2 >= nNoMetalNumBonds(at, n) &&
1619 [ # # ]: 0 : 2 == nNoMetalBondsValence(at, n) + NUMH(at, n) &&
1620 : : /*0 == num_of_H( at, n ) &&*/
1621 [ # # # # ]: 0 : at[i].bond_type[i1] == BOND_TYPE_SINGLE &&
1622 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_N)
1623 : : {
1624 : : /* found N(+)(IV)=N(-)(II); prepare conversion to N(V)#N(III) */
1625 : 0 : num_neigh++;
1626 : 0 : pos_neigh = i1;
1627 : : }
1628 : : }
1629 [ # # # # ]: 0 : if (1 == num_neigh &&
1630 [ # # ]: 0 : !has_other_ion_neigh(at, i, n = at[i].neighbor[i1 = pos_neigh]) &&
1631 : 0 : !has_other_ion_neigh(at, n, i))
1632 : : {
1633 : : /*n = at[i].neighbor[i1=pos_neigh];*/
1634 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1635 : 0 : at[i].charge--;
1636 : 0 : at[n].charge++;
1637 : 0 : at[i].chem_bonds_valence++;
1638 : 0 : at[n].chem_bonds_valence++;
1639 : 0 : at[i].bond_type[i1]++;
1640 : 0 : at[n].bond_type[i2]++;
1641 : 0 : num_changes++;
1642 : 0 : num_N_minus--;
1643 : 0 : num_N_plus--;
1644 : 0 : num_All -= 2;
1645 : 0 : continue;
1646 : : }
1647 : : }
1648 : : }
1649 [ + + - + ]: 1506 : else if (1 == at[i].charge &&
1650 [ # # # # ]: 0 : 0 < num_O_plus && 0 < num_C_minus + num_N_minus &&
1651 [ # # # # ]: 0 : 3 >= nNoMetalNumBonds(at, i) && 3 == nNoMetalBondsValence(at, i) &&
1652 [ # # ]: 0 : 0 == num_of_H(at, i) &&
1653 : 0 : ion_el_group(at[i].el_number) == EL_NUMBER_O)
1654 : : {
1655 : : /* found non-terminal O(+)(III) */
1656 [ # # # # : 0 : if ((!type || 13 == type) && 0 < num_C_minus)
# # ]
1657 : : {
1658 : 0 : int num_neigh = 0, pos_neigh = -1;
1659 [ # # ]: 0 : for (i1 = 0; i1 < at[i].valence; i1++)
1660 : : {
1661 : 0 : n = at[i].neighbor[i1];
1662 [ # # # # : 0 : if (-1 == at[n].charge && 3 >= at[n].valence && 3 == at[n].chem_bonds_valence + NUMH(at, n) &&
# # ]
1663 : : /*0 == at[n].num_H &&*/
1664 [ # # # # ]: 0 : at[i].bond_type[i1] == BOND_TYPE_SINGLE &&
1665 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1666 : : {
1667 : : /* found O(+)(III)-C(-)(II); prepare conversion to O(IV)=C(IV) */
1668 : 0 : num_neigh++;
1669 : 0 : pos_neigh = i1;
1670 : : }
1671 : : }
1672 [ # # # # ]: 0 : if (1 == num_neigh &&
1673 [ # # ]: 0 : !has_other_ion_neigh(at, i, n = at[i].neighbor[i1 = pos_neigh]) &&
1674 : 0 : !has_other_ion_neigh(at, n, i))
1675 : : {
1676 : : /*n = at[i].neighbor[i1=pos_neigh];*/
1677 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1678 : 0 : at[i].charge--;
1679 : 0 : at[n].charge++;
1680 : 0 : at[i].chem_bonds_valence++;
1681 : 0 : at[n].chem_bonds_valence++;
1682 : 0 : at[i].bond_type[i1]++;
1683 : 0 : at[n].bond_type[i2]++;
1684 : 0 : num_changes++;
1685 : 0 : num_C_minus--;
1686 : 0 : num_O_plus--;
1687 : 0 : num_All -= 2;
1688 : 0 : continue;
1689 : : }
1690 : : }
1691 [ # # # # : 0 : if ((!type || 14 == type) && 0 < num_C_minus)
# # ]
1692 : : {
1693 : 0 : int num_neigh = 0, pos_neigh = -1;
1694 [ # # ]: 0 : for (i1 = 0; i1 < at[i].valence; i1++)
1695 : : {
1696 : 0 : n = at[i].neighbor[i1];
1697 [ # # # # : 0 : if (-1 == at[n].charge && 3 >= at[n].valence && 3 == at[n].chem_bonds_valence + NUMH(at, n) &&
# # ]
1698 : : /*0 == at[n].num_H &&*/
1699 [ # # # # ]: 0 : at[i].bond_type[i1] == BOND_TYPE_DOUBLE &&
1700 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1701 : : {
1702 : : /* found O(+)(III)=C(-)(III); prepare conversion to O(IV)#C(IV) */
1703 : 0 : num_neigh++;
1704 : 0 : pos_neigh = i1;
1705 : : }
1706 : : }
1707 [ # # # # ]: 0 : if (1 == num_neigh &&
1708 [ # # ]: 0 : !has_other_ion_neigh(at, i, n = at[i].neighbor[i1 = pos_neigh]) &&
1709 : 0 : !has_other_ion_neigh(at, n, i))
1710 : : {
1711 : : /*n = at[i].neighbor[i1=pos_neigh];*/
1712 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1713 : 0 : at[i].charge--;
1714 : 0 : at[n].charge++;
1715 : 0 : at[i].chem_bonds_valence++;
1716 : 0 : at[n].chem_bonds_valence++;
1717 : 0 : at[i].bond_type[i1]++;
1718 : 0 : at[n].bond_type[i2]++;
1719 : 0 : num_changes++;
1720 : 0 : num_C_minus--;
1721 : 0 : num_O_plus--;
1722 : 0 : num_All -= 2;
1723 : 0 : continue;
1724 : : }
1725 : : }
1726 [ # # # # : 0 : if ((!type || 15 == type) && 0 < num_N_minus)
# # ]
1727 : : {
1728 : 0 : int num_neigh = 0, pos_neigh = -1;
1729 [ # # ]: 0 : for (i1 = 0; i1 < at[i].valence; i1++)
1730 : : {
1731 : 0 : n = at[i].neighbor[i1];
1732 [ # # # # ]: 0 : if (-1 == at[n].charge && 2 >= nNoMetalNumBonds(at, n) &&
1733 [ # # ]: 0 : 2 == nNoMetalBondsValence(at, n) + NUMH(at, n) &&
1734 : : /*0 == num_of_H( at, n ) &&*/
1735 [ # # # # ]: 0 : at[i].bond_type[i1] == BOND_TYPE_SINGLE &&
1736 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_N)
1737 : : {
1738 : : /* found O(+)(III)=N(-)(II); prepare conversion to O(IV)#N(III) */
1739 : 0 : num_neigh++;
1740 : 0 : pos_neigh = i1;
1741 : : }
1742 : : }
1743 [ # # # # ]: 0 : if (1 == num_neigh &&
1744 [ # # ]: 0 : !has_other_ion_neigh(at, i, n = at[i].neighbor[i1 = pos_neigh]) &&
1745 : 0 : !has_other_ion_neigh(at, n, i))
1746 : : {
1747 : : /*n = at[i].neighbor[i1=pos_neigh];*/
1748 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1749 : 0 : at[i].charge--;
1750 : 0 : at[n].charge++;
1751 : 0 : at[i].chem_bonds_valence++;
1752 : 0 : at[n].chem_bonds_valence++;
1753 : 0 : at[i].bond_type[i1]++;
1754 : 0 : at[n].bond_type[i2]++;
1755 : 0 : num_changes++;
1756 : 0 : num_N_minus--;
1757 : 0 : num_O_plus--;
1758 : 0 : num_All -= 2;
1759 : 0 : continue;
1760 : : }
1761 : : }
1762 : : }
1763 : : }
1764 : : }
1765 : :
1766 : : /**************************************************************************/
1767 : : /*********************** NON-Terminal ion triples *************************/
1768 : : /**************************************************************************/
1769 : : /*-------------------------------------------------------------------------
1770 : : Non-Terminal triple types: 16, 17, 18 N=N,P,As,Sb; O=O,S,Se,Te; C=C,Si
1771 : : ========================================
1772 : : 16: C(+)(III)-O-N(-)(II) => C(IV)=O=N(III) (allow terminal H on N(-))
1773 : : | |
1774 : : 17: C(+)(III)-N-C(-)(III) => C(IV)=N=C(IV)
1775 : :
1776 : : 18: C(-)(III)-N=C(+)(III) => C(IV)=N#C(IV) (may have two or no charges)
1777 : : C(IV)=N-C(II) => C(IV)=N#C(IV)
1778 : :
1779 : : */
1780 : :
1781 [ + - + + : 342 : if ((!type || 16 == type) && 0 < num_C_plus && 0 < num_N_minus)
- + - - ]
1782 : : {
1783 : : int m[2], j[2], k;
1784 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1785 : : {
1786 [ # # # # : 0 : if (0 == at[i].charge && 2 == nNoMetalNumBonds(at, i) && 2 == nNoMetalBondsValence(at, i) &&
# # # # ]
1787 : 0 : 0 == num_of_H(at, i) &&
1788 [ # # ]: 0 : 0 <= (j[0] = nNoMetalNeighIndex(at, i)) &&
1789 [ # # ]: 0 : at[m[0] = at[i].neighbor[j[0]]].charge &&
1790 [ # # ]: 0 : 0 <= (j[1] = nNoMetalOtherNeighIndex(at, i, m[0])) &&
1791 [ # # ]: 0 : 0 == at[m[0]].charge + at[m[1] = at[i].neighbor[j[1]]].charge &&
1792 [ # # # # ]: 0 : 5 >= nNoMetalBondsValence(at, m[0]) + nNoMetalBondsValence(at, m[1]) &&
1793 : : /*5 >= at[m[0]].chem_bonds_valence + at[m[1]].chem_bonds_valence &&*/
1794 : 0 : ion_el_group(at[i].el_number) == EL_NUMBER_O)
1795 : : {
1796 : : /* found non-terminal A(+)-O-B(-); chem_bond_val of A+B <= 5 */
1797 : 0 : int n_N = -1, n_C = -1, i_C = -1;
1798 [ # # ]: 0 : for (k = 0; k < 2; k++)
1799 : : {
1800 : 0 : n = m[k];
1801 [ # # # # : 0 : if (-1 == at[n].charge && 2 == nNoMetalNumBonds(at, n) + NUMH(at, n) &&
# # ]
1802 : : /*0 == num_of_H( at, n ) &&*/
1803 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_N)
1804 : : {
1805 : 0 : n_N = n;
1806 : : }
1807 [ # # # # : 0 : else if (1 == at[n].charge && 3 == at[n].chem_bonds_valence + NUMH(at, n) &&
# # ]
1808 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1809 : : {
1810 : 0 : n_C = n;
1811 : 0 : i_C = k;
1812 : : }
1813 : : }
1814 [ # # # # : 0 : if (n_C < 0 || n_N < 0 ||
# # ]
1815 [ # # ]: 0 : has_other_ion_in_sphere_2(at, n_C, n_N) ||
1816 : 0 : has_other_ion_in_sphere_2(at, n_N, n_C))
1817 : : {
1818 : 0 : continue;
1819 : : }
1820 : :
1821 : : /* C(+)(III)-O-N(-)(II) => C(IV)=O=N(III) */
1822 [ # # ]: 0 : for (k = 0; k < 2; k++)
1823 : : {
1824 [ # # ]: 0 : n = k ? n_C : n_N;
1825 [ # # ]: 0 : i1 = k ? j[i_C] : j[1 - i_C];
1826 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1827 : 0 : at[i].bond_type[i1]++;
1828 : 0 : at[n].bond_type[i2]++;
1829 : 0 : at[i].chem_bonds_valence++;
1830 : 0 : at[n].chem_bonds_valence++;
1831 [ # # ]: 0 : at[n].charge += (k ? -1 : 1);
1832 : : }
1833 : 0 : num_changes++;
1834 : 0 : num_N_minus--;
1835 : 0 : num_C_plus--;
1836 : 0 : num_All -= 2;
1837 : : }
1838 : : }
1839 : : }
1840 : :
1841 [ + - + + : 342 : if ((!type || 17 == type) && 0 < num_C_plus && 0 < num_C_minus)
- + - - ]
1842 : : {
1843 : : int m[3], c[3], j[3], k;
1844 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1845 : : {
1846 [ # # # # : 0 : if (0 == at[i].charge && 3 == nNoMetalNumBonds(at, i) && 3 == nNoMetalBondsValence(at, i) &&
# # # # ]
1847 : 0 : 0 == num_of_H(at, i) &&
1848 [ # # ]: 0 : 0 <= (j[0] = nNoMetalNeighIndex(at, i)) &&
1849 [ # # ]: 0 : 0 <= (j[1] = nNoMetalOtherNeighIndex(at, i, m[0] = at[i].neighbor[j[0]])) &&
1850 [ # # ]: 0 : 0 <= (j[2] = nNoMetalOtherNeighIndex2(at, i, m[0], m[1] = at[i].neighbor[j[1]])) &&
1851 [ # # ]: 0 : 1 == !(c[0] = at[m[0]].charge) + !(c[1] = at[m[1]].charge) + !(c[2] = at[m[2] = at[i].neighbor[j[2]]].charge) &&
1852 [ # # ]: 0 : 0 == c[0] + c[1] + c[2] &&
1853 [ # # # # : 0 : 2 == (3 == (c[0] ? at[m[0]].chem_bonds_valence + NUMH(at, m[0]) : 0)) + (3 == (c[1] ? at[m[1]].chem_bonds_valence + NUMH(at, m[1]) : 0)) + (3 == (c[2] ? at[m[2]].chem_bonds_valence + NUMH(at, m[2]) : 0)) &&
# # # # #
# # # # #
# # ]
1854 : 0 : ion_el_group(at[i].el_number) == EL_NUMBER_N)
1855 : : {
1856 : : /* found non-terminal A(+)-O-B(-) */
1857 : 0 : int n_Cp = -1, n_Cm = -1, i_Cp = -1, i_Cm = -1; /* p = positive, m = negatice ion C */
1858 [ # # ]: 0 : for (k = 0; k < 3; k++)
1859 : : {
1860 [ # # ]: 0 : if (c[k])
1861 : : {
1862 : 0 : n = m[k];
1863 [ # # # # ]: 0 : if (-1 == at[n].charge &&
1864 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1865 : : {
1866 : 0 : n_Cm = n;
1867 : 0 : i_Cm = k;
1868 : : }
1869 [ # # # # ]: 0 : else if (1 == at[n].charge &&
1870 : 0 : ion_el_group(at[n].el_number) == EL_NUMBER_C)
1871 : : {
1872 : 0 : n_Cp = n;
1873 : 0 : i_Cp = k;
1874 : : }
1875 : : }
1876 : : }
1877 [ # # # # : 0 : if (n_Cp < 0 || n_Cm < 0 ||
# # ]
1878 [ # # ]: 0 : has_other_ion_in_sphere_2(at, n_Cp, n_Cm) ||
1879 : 0 : has_other_ion_in_sphere_2(at, n_Cm, n_Cp))
1880 : : {
1881 : 0 : continue;
1882 : : }
1883 : :
1884 : : /* | | */
1885 : : /* C(+)(III)-N-C(-)(III) => C(IV)=N=C(IV) */
1886 [ # # ]: 0 : for (k = 0; k < 2; k++)
1887 : : {
1888 [ # # ]: 0 : n = k ? n_Cp : n_Cm;
1889 [ # # ]: 0 : i1 = k ? j[i_Cp] : j[i_Cm];
1890 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1891 : 0 : at[i].bond_type[i1]++;
1892 : 0 : at[n].bond_type[i2]++;
1893 : 0 : at[i].chem_bonds_valence++;
1894 : 0 : at[n].chem_bonds_valence++;
1895 [ # # ]: 0 : at[n].charge += (k ? -1 : 1);
1896 : : }
1897 : 0 : num_changes++;
1898 : 0 : num_C_minus--;
1899 : 0 : num_C_plus--;
1900 : 0 : num_All -= 2;
1901 : : }
1902 : : }
1903 : : }
1904 : :
1905 [ + - + + : 342 : if ((!type || 18 == type) && ((0 < num_C_plus && 0 < num_C_minus) || 0 < num_C_II)) /* djb-rwth: addressing LLVM warning */
- + - - -
+ ]
1906 : : {
1907 : : int m[2], v[2], j[2], k;
1908 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
1909 : : {
1910 [ # # # # : 0 : if (0 == at[i].charge && 2 == nNoMetalNumBonds(at, i) && 3 == nNoMetalBondsValence(at, i) &&
# # # # ]
1911 : 0 : 0 == num_of_H(at, i) &&
1912 [ # # ]: 0 : 0 <= (j[0] = nNoMetalNeighIndex(at, i)) &&
1913 [ # # ]: 0 : 0 <= (j[1] = nNoMetalOtherNeighIndex(at, i, m[0] = at[i].neighbor[j[0]])) &&
1914 [ # # ]: 0 : 0 == at[m[0]].charge + at[m[1] = at[i].neighbor[j[1]]].charge &&
1915 [ # # ]: 0 : 6 == (v[0] = at[m[0]].chem_bonds_valence + NUMH(at, m[0])) + (v[1] = at[m[1]].chem_bonds_valence + NUMH(at, m[1])) &&
1916 [ # # # # : 0 : 2 >= abs(v[0] - v[1]) &&
# # ]
1917 [ # # ]: 0 : ion_el_group(at[i].el_number) == EL_NUMBER_N &&
1918 [ # # ]: 0 : ion_el_group(at[m[0]].el_number) == EL_NUMBER_C &&
1919 : 0 : ion_el_group(at[m[1]].el_number) == EL_NUMBER_C)
1920 : : {
1921 : : /* n_Cm i n_Cp */
1922 : : /* found non-terminal C(-)(III)-N=C(+)(III) or C(IV)=N-C(II): Cm-N-Cp */
1923 : : /* convert to C(IV)=N#C(IV) */
1924 : 0 : int n_Cp = -1, n_Cm = -1, i_Cp = -1, i_Cm = -1; /* p = positive, m = negatice ion C */
1925 [ # # ]: 0 : for (k = 0; k < 2; k++)
1926 : : {
1927 : 0 : n = m[k];
1928 [ # # # # : 0 : if (v[k] == 4 || (v[k] == 3 && at[i].bond_type[j[k]] == BOND_TYPE_SINGLE)) /* djb-rwth: addressing LLVM warning */
# # ]
1929 : : {
1930 : 0 : n_Cm = n;
1931 : 0 : i_Cm = k;
1932 : : }
1933 [ # # # # : 0 : else if (v[k] == 2 || (v[k] == 3 && at[i].bond_type[j[k]] == BOND_TYPE_DOUBLE)) /* djb-rwth: addressing LLVM warning */
# # ]
1934 : : {
1935 : 0 : n_Cp = n;
1936 : 0 : i_Cp = k;
1937 : : }
1938 : : }
1939 [ # # # # : 0 : if (n_Cp < 0 || n_Cm < 0 || at[n_Cp].valence + NUMH(at, n_Cp) != 2)
# # ]
1940 : : {
1941 : 0 : continue; /* guarantees at[n_Cp].valence <= 2 */
1942 : : }
1943 [ # # # # ]: 0 : if (v[i_Cp] == 2 || !at[n_Cp].charge)
1944 : : {
1945 [ # # ]: 0 : if (at[n_Cp].valence == 2)
1946 : : {
1947 : : /* neighbor of at[n_Cp] opposite to at[i] */
1948 : 0 : k = at[n_Cp].neighbor[at[n_Cp].neighbor[0] == i];
1949 [ # # ]: 0 : if (ion_el_group(at[k].el_number) == EL_NUMBER_N)
1950 : : {
1951 : 0 : continue;
1952 : : }
1953 : : }
1954 : : }
1955 [ # # ]: 0 : else if (at[n_Cp].charge)
1956 : : {
1957 [ # # # # ]: 0 : if (has_other_ion_in_sphere_2(at, n_Cp, n_Cm) ||
1958 : 0 : has_other_ion_in_sphere_2(at, n_Cm, n_Cp))
1959 : : {
1960 : 0 : continue;
1961 : : }
1962 : : }
1963 : : else
1964 : : {
1965 : 0 : continue; /* unknown case */
1966 : : }
1967 : :
1968 : : /* */
1969 : : /* C(-)(III)-N=C(+)(III) => C(IV)=N#C(IV) */
1970 : : /* C(IV)=N-C(II) => C(IV)=N#C(IV) */
1971 [ # # ]: 0 : if (at[n_Cp].charge)
1972 : : {
1973 : 0 : num_C_minus--;
1974 : 0 : num_C_plus--;
1975 : 0 : num_All -= 2;
1976 : : }
1977 : : else
1978 : : {
1979 : 0 : num_C_II--;
1980 : 0 : num_All--;
1981 : : }
1982 : :
1983 [ # # ]: 0 : for (k = 0; k < 2; k++)
1984 : : {
1985 [ # # ]: 0 : n = k ? n_Cp : n_Cm;
1986 [ # # ]: 0 : i3 = k ? i_Cp : i_Cm; /* added to fix the bug */
1987 : : /*i1 = k? j[i_Cp] : j[i_Cm];*/ /* replaced with next line */
1988 : 0 : i1 = j[i3];
1989 [ # # ]: 0 : if (v[i3 /*was i1*/] < 4)
1990 : : {
1991 : : /* WDI found a bug here: bounds violation */
1992 : 0 : int delta = 4 - v[i3 /*was i1*/];
1993 : 0 : i2 = (int)(is_in_the_list(at[n].neighbor, (AT_NUMB)i, at[n].valence) - at[n].neighbor);
1994 : 0 : at[i].bond_type[i1] += delta;
1995 : 0 : at[n].bond_type[i2] += delta;
1996 : 0 : at[i].chem_bonds_valence += delta;
1997 : 0 : at[n].chem_bonds_valence += delta;
1998 : 0 : at[n].charge = 0;
1999 : 0 : at[n].radical = 0;
2000 : : }
2001 : : }
2002 : 0 : at[i].charge = 0;
2003 : 0 : at[i].radical = 0;
2004 : 0 : num_changes++;
2005 : : }
2006 : : }
2007 : : }
2008 : : }
2009 : :
2010 : 19 : return num_changes;
2011 : : }
2012 : :
2013 : : /*#if ( DISCONNECT_SALTS == 1 )*/ /* { */
2014 : :
2015 : : /****************************************************************************/
2016 : 100 : int RemoveInpAtBond(inp_ATOM *atom, int iat, int k)
2017 : : {
2018 : : int i, j, m, m2; /* djb-rwth: removing redundant variables */
2019 : 100 : inp_ATOM *at = atom + iat;
2020 : 100 : inp_ATOM *at2 = NULL;
2021 : 100 : int val = at->valence - 1;
2022 : :
2023 [ + - ]: 100 : if (val >= 0)
2024 : : {
2025 : 100 : int bond = at->bond_type[k];
2026 [ + + ]: 100 : if (bond > BOND_TYPE_TRIPLE)
2027 : 14 : bond = BOND_TYPE_SINGLE; /* added 08-06-2003 */
2028 : :
2029 : : /* update CML tetrahedral atom parity. */
2030 [ - + ]: 100 : if (at->p_parity)
2031 : : {
2032 [ # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_ATOM_NEIGH; m++)
2033 : : {
2034 [ # # ]: 0 : if (at->p_orig_at_num[m] == at->orig_at_number)
2035 : : {
2036 : 0 : at->p_parity = 0;
2037 : 0 : break; /* only 3 bonds are present; removing one bond removes stereo */
2038 : : }
2039 : : }
2040 [ # # ]: 0 : if (at->p_parity /* at->valence == MAX_NUM_STEREO_ATOM_NEIGH*/)
2041 : : {
2042 : : /* p_orig_at_num is a fixed size array of MAX_NUM_STEREO_ATOM_NEIGH (4) elements */
2043 [ # # # # ]: 0 : for (m = 0; m < at->valence && m < MAX_NUM_STEREO_ATOM_NEIGH; m++) /* djb-rwth: fixing GH PR #72 */
2044 : : {
2045 [ # # ]: 0 : if (atom[(int)at->neighbor[k]].orig_at_number == at->p_orig_at_num[m])
2046 : : {
2047 : 0 : break;
2048 : : }
2049 : : }
2050 [ # # # # ]: 0 : if (m < at->valence && m < MAX_NUM_STEREO_ATOM_NEIGH) /* djb-rwth: fixing GH PR #72 */
2051 : : {
2052 : 0 : at->p_orig_at_num[m] = at->orig_at_number;
2053 : : }
2054 : : else
2055 : : {
2056 : 0 : at->p_parity = 0; /* wrong neighbors: at->neighbor[k] is not in the list of a stereo neighbors */
2057 : : }
2058 : : }
2059 : : }
2060 : :
2061 : : /* update CML stereogenic bond parities; at this point no removed explicit H exist yet */
2062 [ - + ]: 100 : if (at->sb_parity[0])
2063 : : {
2064 [ # # # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_BONDS && at->sb_parity[m];)
2065 : : {
2066 [ # # # # : 0 : if (k == at->sb_ord[m] || (k == at->sn_ord[m] && val < 2 && ATOM_PARITY_WELL_DEF(at->sb_parity[m]))) /* djb-rwth: addressing LLVM warning */
# # # # #
# ]
2067 : 0 : {
2068 : : /* !!! FLAW: does take into account removed H !!! */
2069 : : /* stereogenic bond is being removed OR */
2070 : : /* remove stereogenic bond because its only neighbor is being removed */
2071 : : int pnxt_atom, pinxt2cur, pinxt_sb_parity_ord;
2072 : 0 : int len = get_opposite_sb_atom(atom, iat, at->sb_ord[m], &pnxt_atom, &pinxt2cur, &pinxt_sb_parity_ord);
2073 [ # # ]: 0 : if (len)
2074 : : {
2075 : 0 : i = pinxt_sb_parity_ord;
2076 : 0 : at2 = atom + pnxt_atom;
2077 : : /* djb-rwth: removing redundant code */
2078 : : }
2079 : : else
2080 : : {
2081 : 0 : i = MAX_NUM_STEREO_BONDS;
2082 : : }
2083 : : /*
2084 : : at2 = atom + at->neighbor[ (int)at->sb_ord[m] ];
2085 : : for ( i = 0; i < MAX_NUM_STEREO_BONDS && at2->sb_parity[i]; i ++ )
2086 : : {
2087 : : if ( iat == at2->neighbor[ (int)at2->sb_ord[i] ] )
2088 : : break;
2089 : : }
2090 : : */
2091 [ # # # # ]: 0 : if (i < MAX_NUM_STEREO_BONDS && at2->sb_parity[i])
2092 : : {
2093 : 0 : m2 = i;
2094 : : /* remove bond parity from at */
2095 [ # # ]: 0 : if (m < MAX_NUM_STEREO_BONDS - 1)
2096 : : {
2097 : 0 : memmove(at->sb_parity + m, at->sb_parity + m + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m) * sizeof(at->sb_parity[0])); /* djb-rwth: cast operator added */
2098 : 0 : memmove(at->sb_ord + m, at->sb_ord + m + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m) * sizeof(at->sb_ord[0])); /* djb-rwth: cast operator added */
2099 : 0 : memmove(at->sn_ord + m, at->sn_ord + m + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m) * sizeof(at->sn_ord[0])); /* djb-rwth: cast operator added */
2100 : 0 : memmove(at->sn_orig_at_num + m, at->sn_orig_at_num + m + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m) * sizeof(at->sn_orig_at_num[0])); /* djb-rwth: cast operator added */
2101 : : }
2102 : 0 : at->sb_parity[MAX_NUM_STEREO_BONDS - 1] = 0;
2103 : 0 : at->sb_ord[MAX_NUM_STEREO_BONDS - 1] = 0;
2104 : 0 : at->sn_ord[MAX_NUM_STEREO_BONDS - 1] = 0;
2105 : 0 : at->sn_orig_at_num[MAX_NUM_STEREO_BONDS - 1] = 0;
2106 : : /* remove bond parity from at2 */
2107 [ # # ]: 0 : if (m2 < MAX_NUM_STEREO_BONDS - 1)
2108 : : {
2109 : 0 : memmove(at2->sb_parity + m2, at2->sb_parity + m2 + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m2) * sizeof(at2->sb_parity[0])); /* djb-rwth: cast operator added */
2110 : 0 : memmove(at2->sb_ord + m2, at2->sb_ord + m2 + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m2) * sizeof(at2->sb_ord[0])); /* djb-rwth: cast operator added */
2111 : 0 : memmove(at2->sn_ord + m2, at2->sn_ord + m2 + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m2) * sizeof(at2->sn_ord[0])); /* djb-rwth: cast operator added */
2112 : 0 : memmove(at2->sn_orig_at_num + m2, at2->sn_orig_at_num + m2 + 1, (MAX_NUM_STEREO_BONDS - 1 - (long long)m2) * sizeof(at2->sn_orig_at_num[0])); /* djb-rwth: cast operator added */
2113 : : }
2114 : 0 : at2->sb_parity[MAX_NUM_STEREO_BONDS - 1] = 0;
2115 : 0 : at2->sb_ord[MAX_NUM_STEREO_BONDS - 1] = 0;
2116 : 0 : at2->sn_ord[MAX_NUM_STEREO_BONDS - 1] = 0;
2117 : 0 : at2->sn_orig_at_num[MAX_NUM_STEREO_BONDS - 1] = 0;
2118 : : /* do not increment m here because the array elements have been shifted */
2119 : : }
2120 : : else
2121 : : {
2122 : 0 : m++; /* program error: inconsistent stereobond parity */
2123 : : }
2124 : : }
2125 [ # # ]: 0 : else if (k == at->sn_ord[m])
2126 : : {
2127 : : /* stereogenic bond neighbor is being removed; another neighbor remains */
2128 : : /* !!! FLAW: does take into account removed H !!! */
2129 [ # # ]: 0 : for (j = 0, i = -1; j < at->valence; j++)
2130 : : {
2131 [ # # # # ]: 0 : if (j != k && j != at->sb_ord[m])
2132 : : {
2133 : 0 : i = j;
2134 : 0 : break;
2135 : : }
2136 : : }
2137 : :
2138 : : /* i is the position of the neighbor that will become a new neighbor */
2139 : : /***************************************************************************
2140 : : * at->sb_parity[m] is the direction (EVEN=clockwise, ODD=counterclockwise)
2141 : : * from stereobond to the neighbor. If the neighbor is removed then
2142 : : * the parity should invert, otherwise it should be unchanged.
2143 : : ***************************************************************************/
2144 [ # # ]: 0 : if (i < 0)
2145 : : {
2146 : : /* no alternative neighbor is available */
2147 [ # # # # ]: 0 : if (ATOM_PARITY_WELL_DEF(at->sb_parity[m]))
2148 : : {
2149 : : /* parity cannot be not well-defined anymore */
2150 : : int pnxt_atom, pinxt2cur, pinxt_sb_parity_ord;
2151 : 0 : int len = get_opposite_sb_atom(atom, iat, at->sb_ord[m], &pnxt_atom, &pinxt2cur, &pinxt_sb_parity_ord);
2152 [ # # ]: 0 : if (len > 0)
2153 : : {
2154 : 0 : atom[pnxt_atom].sb_parity[pinxt_sb_parity_ord] = at->sb_parity[m] = AB_PARITY_UNDF;
2155 : : }
2156 : : }
2157 : 0 : at->sn_ord[m] = -99; /* sb neighbor has been disconnected */
2158 : 0 : at->sb_ord[m] -= (at->sb_ord[m] > k); /* same as above */
2159 : 0 : at->sn_orig_at_num[m] = 0;
2160 : : }
2161 [ # # ]: 0 : else if (i < at->valence)
2162 : : {
2163 : : /* choose another stereogenic bond neighbor, its ord. number is i before bond removal */
2164 [ # # # # ]: 0 : if (ATOM_PARITY_WELL_DEF(at->sb_parity[m]))
2165 : : {
2166 : : /* ALL WRONG: 'move' previous stereo bond neighbor to the last position (pos. 2 out of 0,1,2) */
2167 : : /* the parity of the transpositions is (2 - at->sn_ord[m])%2 = at->sn_ord[m] % 2 */
2168 : : /* and replace the neighbor with another; the contribution to the parity is 1 */
2169 : :
2170 : : /*at->sb_parity[m] = 2 - ( at->sb_parity[m] + at->sn_ord[m] + 1 ) % 2;*/
2171 : :
2172 : : /*at->sb_parity[m] = 2 - ( at->sb_parity[m] + k + i +
2173 : : (i > k) + (i > at->sb_ord[m]) ) % 2;*/
2174 : : /*=== parity should be INVERTED ===*/
2175 : 0 : at->sb_parity[m] = 3 - at->sb_parity[m];
2176 : : }
2177 : 0 : at->sn_ord[m] = i - (i > k); /* ord. number shifted because preceding bond is removed */
2178 : 0 : at->sb_ord[m] -= (at->sb_ord[m] > k); /* same as above */
2179 : 0 : at->sn_orig_at_num[m] = atom[(int)at->neighbor[i]].orig_at_number;
2180 : : /*at->sb_parity[m] = 2 - ( at->sb_parity[m] + 1 ) % 2;*/
2181 : : }
2182 : : else
2183 : : {
2184 : 0 : at->sb_parity[m] = 0; /* program error: inconsistent stereobond parity */
2185 : : }
2186 : 0 : m++;
2187 : : }
2188 : : else
2189 : : {
2190 : : /* removing another neighbor, k: first move it to the last position (pos. 2 out of 0,1,2) */
2191 [ # # # # ]: 0 : if (k < 2 && ATOM_PARITY_WELL_DEF(at->sb_parity[m]))
2192 : : {
2193 : : /*at->sb_parity[m] = 2 - ( at->sb_parity[m] + k ) % 2;*/
2194 : : /*at->sb_parity[m] = 2 - ( at->sb_parity[m] + (at->sn_ord[m] > k) + (at->sb_ord[m] > k) ) % 2;*/
2195 : : ; /*==== Parity should remain UNCHANGED ===*/
2196 : : }
2197 [ # # ]: 0 : if (at->sb_ord[m] > k)
2198 : : {
2199 : 0 : at->sb_ord[m]--;
2200 : : }
2201 [ # # ]: 0 : if (at->sn_ord[m] > k)
2202 : : {
2203 : 0 : at->sn_ord[m]--;
2204 : : }
2205 : 0 : m++;
2206 : : }
2207 : : }
2208 : : }
2209 : :
2210 [ + + ]: 100 : if (k < val)
2211 : : {
2212 : 24 : memmove(at->neighbor + k, at->neighbor + k + 1, sizeof(at->neighbor[0]) * ((long long)val - (long long)k)); /* djb-rwth: cast operators added */
2213 : 24 : memmove(at->bond_stereo + k, at->bond_stereo + k + 1, sizeof(at->bond_stereo[0]) * ((long long)val - (long long)k)); /* djb-rwth: cast operators added */
2214 : 24 : memmove(at->bond_type + k, at->bond_type + k + 1, sizeof(at->bond_type[0]) * ((long long)val - (long long)k)); /* djb-rwth: cast operators added */
2215 : : }
2216 : :
2217 : 100 : at->neighbor[val] = 0;
2218 : 100 : at->bond_stereo[val] = 0;
2219 : 100 : at->bond_type[val] = 0;
2220 : 100 : at->valence = val;
2221 : 100 : at->chem_bonds_valence -= bond;
2222 : 100 : return 1;
2223 : : }
2224 : :
2225 : 0 : return 0;
2226 : : }
2227 : :
2228 : : /****************************************************************************/
2229 : 50 : int DisconnectInpAtBond(inp_ATOM *at,
2230 : : AT_NUMB *nOldCompNumber,
2231 : : int iat,
2232 : : int neigh_ord)
2233 : : {
2234 : 50 : int neigh, i, ret = 0;
2235 : : int component;
2236 : 50 : neigh = at[iat].neighbor[neigh_ord];
2237 : :
2238 [ + - ]: 62 : for (i = 0; i < at[neigh].valence; i++)
2239 : : {
2240 [ + + ]: 62 : if (iat == (int)at[neigh].neighbor[i])
2241 : : {
2242 : 50 : break;
2243 : : }
2244 : : }
2245 : :
2246 [ + - ]: 50 : if (i < at[neigh].valence)
2247 : : {
2248 : 50 : ret += RemoveInpAtBond(at, iat, neigh_ord);
2249 : 50 : ret += RemoveInpAtBond(at, neigh, i);
2250 [ + + + - ]: 50 : if (nOldCompNumber && ret)
2251 : : {
2252 [ + - ]: 1 : if ((component = at[iat].component)) /* djb-rwth: addressing LLVM warning */
2253 : : {
2254 : 1 : nOldCompNumber[component - 1] = 0;
2255 : : }
2256 [ + - ]: 1 : if ((component = at[neigh].component)) /* djb-rwth: addressing LLVM warning */
2257 : : {
2258 : 1 : nOldCompNumber[component - 1] = 0;
2259 : : }
2260 : : }
2261 : : }
2262 : :
2263 : 50 : return (ret == 2);
2264 : : }
2265 : :
2266 : : /****************************************************************************/
2267 : 9261 : int bIsAmmoniumSalt(inp_ATOM *at,
2268 : : int i,
2269 : : int *piO,
2270 : : int *pk,
2271 : : S_CHAR *num_explicit_H)
2272 : : {
2273 : : /* NH4(+charge)-O(-charge)-C -> NH3 + HO-C; any charge including 0, */
2274 : : /* any C except charged or radical F, Cl, Br, I */
2275 : :
2276 : 9261 : int num_H, num_non_iso_H, num_impl_iso_H, bDisconnect = 1;
2277 : 9261 : int j, val, neigh, iO = -1, iC, k = -1;
2278 : :
2279 [ + + ]: 9261 : if (at[i].el_number != EL_NUMBER_N)
2280 : : {
2281 : 9236 : return 0;
2282 : : }
2283 : :
2284 : : /* check for NH4-O-C... -> NH3 + HO-C... */
2285 : 25 : val = at[i].valence;
2286 : 25 : num_impl_iso_H = NUM_ISO_H(at, i);
2287 : 25 : num_non_iso_H = at[i].num_H;
2288 : 25 : num_H = num_non_iso_H + num_impl_iso_H;
2289 [ + + ]: 25 : if (val + num_H == 5)
2290 : : {
2291 : 1 : int num_O = 0;
2292 : 1 : memset(num_explicit_H, 0, (NUM_H_ISOTOPES + 1) * sizeof(num_explicit_H[0])); /* djb-rwth: memset_s C11/Annex K variant? */
2293 [ + - ]: 2 : for (j = 0; j < val; j++)
2294 : : { /* looking for O: H4N-O-C... */
2295 : 2 : neigh = at[i].neighbor[j];
2296 [ + - ]: 2 : if (at[neigh].num_H ||
2297 [ - + - - : 2 : (at[neigh].charge && (at[neigh].el_number != EL_NUMBER_O || at[neigh].charge + at[i].charge)) ||
- - ]
2298 [ - + - - ]: 2 : (at[neigh].radical && at[neigh].radical != RADICAL_SINGLET)) /* djb-rwth: addressing LLVM warnings */
2299 : : {
2300 : 0 : bDisconnect = 0;
2301 : 0 : break; /* reject */
2302 : : }
2303 [ + + + - ]: 2 : if (at[neigh].el_number == EL_NUMBER_H && at[neigh].valence == 1 &&
2304 [ + - + - ]: 1 : !at[neigh].charge && !at[neigh].radical)
2305 : : {
2306 : : /* reject H isotopes beyond T: they would index past num_explicit_H (GH #289) */
2307 [ + - + - ]: 1 : if (at[neigh].iso_atw_diff < 0 || at[neigh].iso_atw_diff > NUM_H_ISOTOPES)
2308 : : {
2309 : 1 : bDisconnect = 0;
2310 : 1 : break;
2311 : : }
2312 : 0 : num_H++; /* at this point at[].num_H does not include explicit H count */
2313 : 0 : num_non_iso_H += (0 == at[neigh].iso_atw_diff);
2314 : 0 : num_explicit_H[at[neigh].iso_atw_diff]++; /* explicit H on N */
2315 : : }
2316 [ - + - - : 1 : else if (at[neigh].el_number == EL_NUMBER_O && at[neigh].valence == 2 && !num_O)
- - ]
2317 : : {
2318 : 0 : num_O++; /* found O: N-O- */
2319 : 0 : iO = neigh;
2320 : 0 : k = j;
2321 : 0 : iC = at[iO].neighbor[at[iO].neighbor[0] == i];
2322 [ # # ]: 0 : if (at[iC].el_number != EL_NUMBER_C || /*
2323 : : at[iC].num_H ||
2324 : : at[iC].chem_bonds_valence != 4 || */
2325 [ # # ]: 0 : at[iC].charge ||
2326 [ # # # # ]: 0 : (at[iC].radical && at[iC].radical != RADICAL_SINGLET) /*||
2327 : : at[iC].valence == at[iC].chem_bonds_valence*/
2328 : : ) /* djb-rwth: addressing LLVM warning */
2329 : : {
2330 : 0 : bDisconnect = 0;
2331 : 0 : break; /* reject */
2332 : : }
2333 : : }
2334 [ - + ]: 1 : else if ((at[neigh].el_number == EL_NUMBER_F ||
2335 [ # # ]: 0 : at[neigh].el_number == EL_NUMBER_CL ||
2336 [ # # ]: 0 : at[neigh].el_number == EL_NUMBER_BR ||
2337 [ # # ]: 0 : at[neigh].el_number == EL_NUMBER_I) &&
2338 [ + - + - ]: 1 : at[neigh].valence == 1 && at[neigh].chem_bonds_valence == 1 &&
2339 [ + - + - : 1 : !at[neigh].charge && !NUMH(at, neigh) && !num_O)
+ - ]
2340 : : {
2341 : 1 : num_O++; /* found O: N-O- */
2342 : 1 : iO = neigh;
2343 : 1 : k = j;
2344 : : /* djb-rwth: removing redundant code */
2345 : : }
2346 : : else
2347 : : {
2348 : 0 : bDisconnect = 0;
2349 : 0 : break; /* reject */
2350 : : }
2351 : : }
2352 [ - + - - : 1 : if (bDisconnect && (num_O != 1 || num_H != 4))
- - ]
2353 : : {
2354 : 0 : bDisconnect = 0; /* reject */
2355 : : }
2356 : : }
2357 : : else
2358 : : {
2359 : 24 : bDisconnect = 0;
2360 : : }
2361 [ - + ]: 25 : if (bDisconnect)
2362 : : {
2363 : 0 : *piO = iO;
2364 : 0 : *pk = k;
2365 : : }
2366 : :
2367 : 25 : return bDisconnect;
2368 : : }
2369 : :
2370 : : /****************************************************************************/
2371 : 0 : int DisconnectAmmoniumSalt(inp_ATOM *at,
2372 : : int iN,
2373 : : int iO,
2374 : : int k,
2375 : : S_CHAR *num_explicit_H)
2376 : : {
2377 : :
2378 : : /* disconnect NH4-O from O */
2379 : : /* Note: iO = at[iN].neighbor[k], at[iN] is N, at[iO].neighbor[0] is either N=at[iN] or C=at[iC] */
2380 : :
2381 : 0 : int nMove_H_iso_diff = -1; /* do not move explicit H */
2382 : : int j, neigh, iso_diff, neigh_pos;
2383 : 0 : int val = at[iN].valence;
2384 : :
2385 [ # # # # ]: 0 : if (at[iN].charge && !(at[iN].charge + at[iO].charge))
2386 : : {
2387 : 0 : at[iN].charge = at[iO].charge = 0; /* remove charges */
2388 : : }
2389 : :
2390 [ # # # # ]: 0 : neigh_pos = (at[iO].valence == 2) ? (at[iO].neighbor[1] == iN) : 0; /* position of at[iN] in the neigh list of iO */
2391 : : /* disconnect bond O-N */
2392 : 0 : RemoveInpAtBond(at, iO, neigh_pos);
2393 : 0 : RemoveInpAtBond(at, iN, k);
2394 : 0 : val--;
2395 : :
2396 : : /* move 1 H from NH4 to O- or Cl */
2397 : :
2398 : : /* find non-isotopic or the lightest isotopic H to move from N to O */
2399 [ # # ]: 0 : for (iso_diff = 0; iso_diff <= NUM_H_ISOTOPES; iso_diff++) /* djb-rwth: fixing GH PR #72 */
2400 : : {
2401 [ # # ]: 0 : if (!iso_diff)
2402 : : {
2403 : : /* find non-isotopic H */
2404 [ # # ]: 0 : if (at[iN].num_H)
2405 : : {
2406 : 0 : at[iN].num_H--; /* move non-isotopic implicit H */
2407 : 0 : at[iO].num_H++;
2408 : 0 : break;
2409 : : }
2410 [ # # ]: 0 : else if (num_explicit_H[0])
2411 : : {
2412 : 0 : nMove_H_iso_diff = 0; /* flag: move explicit non-isotopic H */
2413 : 0 : break;
2414 : : }
2415 : : }
2416 : : else
2417 : : {
2418 : : /* find isotopic H */
2419 : : /* num_iso_H has length NUM_H_ISOTOPES; do not access out-of-bounds */
2420 [ # # # # ]: 0 : if ((iso_diff < NUM_H_ISOTOPES) && at[iN].num_iso_H[iso_diff]) /* djb-rwth: fixing GH PR #72 */
2421 : : {
2422 : 0 : at[iN].num_iso_H[iso_diff]--; /* move implicit isotopic H, atw = 1 */
2423 : 0 : at[iO].num_iso_H[iso_diff]++;
2424 : 0 : break;
2425 : : }
2426 : : else
2427 : : {
2428 [ # # ]: 0 : if (num_explicit_H[iso_diff])
2429 : : {
2430 : 0 : nMove_H_iso_diff = iso_diff; /* flag: move explicit isotopic H, atw = 1 */
2431 : 0 : break;
2432 : : }
2433 : : }
2434 : : }
2435 : : }
2436 : :
2437 [ # # ]: 0 : if (nMove_H_iso_diff >= 0)
2438 : : {
2439 : : /* move explicit H, it is isotopic if nMove_H_iso_diff > 0 */
2440 : 0 : double dist2_H_O, min_dist2_H_O = -1.0;
2441 : 0 : int jH = -1, iH = -1;
2442 [ # # ]: 0 : for (j = 0; j < val; j++)
2443 : : { /* looking H in N-H such that H-O is shortest */
2444 : 0 : neigh = at[iN].neighbor[j];
2445 [ # # ]: 0 : if (at[neigh].el_number == EL_NUMBER_H &&
2446 [ # # ]: 0 : at[neigh].iso_atw_diff == nMove_H_iso_diff)
2447 : : {
2448 : 0 : dist2_H_O = (at[neigh].x - at[iO].x) * (at[neigh].x - at[iO].x) +
2449 : 0 : (at[neigh].y - at[iO].y) * (at[neigh].y - at[iO].y) +
2450 : 0 : (at[neigh].z - at[iO].z) * (at[neigh].z - at[iO].z);
2451 [ # # # # ]: 0 : if (min_dist2_H_O < 0.0 || min_dist2_H_O > dist2_H_O)
2452 : : {
2453 : 0 : min_dist2_H_O = dist2_H_O;
2454 : 0 : iH = neigh;
2455 : 0 : jH = j;
2456 : : }
2457 : : }
2458 : : }
2459 : :
2460 : : /* reconnect; bonds do not need changes except stereo */
2461 : 0 : neigh_pos = at[iO].valence;
2462 : 0 : at[iO].neighbor[neigh_pos] = iH;
2463 : 0 : at[iO].bond_stereo[neigh_pos] = 0;
2464 : 0 : at[iO].bond_type[neigh_pos] = at[iH].bond_type[0];
2465 : 0 : at[iO].chem_bonds_valence += at[iH].bond_type[0];
2466 : 0 : at[iO].valence++;
2467 : 0 : at[iH].neighbor[0] = iO;
2468 : 0 : at[iH].bond_stereo[0] = 0;
2469 : :
2470 : : /* disconnect H from N */
2471 : 0 : RemoveInpAtBond(at, iN, jH);
2472 : 0 : val--;
2473 [ # # ]: 0 : if (k > jH)
2474 : : {
2475 : 0 : k--;
2476 : : }
2477 : : }
2478 : :
2479 : 0 : return 1;
2480 : : }
2481 : :
2482 : : /****************************************************************************/
2483 : 12 : int bIsMetalSalt(inp_ATOM *at, int i)
2484 : : {
2485 : : int type, val, k, iO, iC, j, neigh;
2486 : 12 : int bDisconnect = 1;
2487 : :
2488 : : /* check for a metal atom:
2489 : : metal atom should be connected and be a metal */
2490 [ + - + + ]: 24 : if (!(val = at[i].valence) ||
2491 : 12 : !(type = get_el_type(at[i].el_number)) ||
2492 [ - + ]: 3 : !(type & IS_METAL))
2493 : : {
2494 : 9 : bDisconnect = 0; /* reject */
2495 : : }
2496 [ - + ]: 3 : else if (at[i].num_H)
2497 : : /* metal atom should not have adjacent H or multiple bonds or radical */
2498 : : {
2499 : 0 : bDisconnect = 0; /* reject */
2500 : : }
2501 : : else
2502 : : {
2503 : : /* check valence */
2504 [ + - ]: 3 : if ((at[i].charge == 0 &&
2505 [ + - - + ]: 3 : (((type & 1) && val == get_el_valence(at[i].el_number, 0, 0)) ||
2506 [ # # # # ]: 0 : ((type & 2) && val == get_el_valence(at[i].el_number, 0, 1)))) ||
2507 [ # # ]: 0 : (at[i].charge > 0 &&
2508 [ # # # # ]: 0 : (type & 1) && val == get_el_valence(at[i].el_number, at[i].charge, 0))) /* djb-rwth: addressing LLVM warnings */
2509 : : {
2510 : : ; /* accept */
2511 : : }
2512 : : else
2513 : : {
2514 : 0 : bDisconnect = 0; /* reject */
2515 : : }
2516 : : }
2517 : :
2518 [ + + ]: 12 : if (bDisconnect)
2519 : : {
2520 : : /*************************************************************************
2521 : : * | *
2522 : : * check M neighbors. Disconnect if all neighbors are M-O-C# or M-O-C= *
2523 : : * | *
2524 : : *************************************************************************/
2525 [ + - ]: 3 : for (k = 0; k < at[i].valence; k++)
2526 : : {
2527 : 3 : iO = at[i].neighbor[k];
2528 : : /* halogenide 2004-07-08 */
2529 [ + - ]: 3 : if ((at[iO].el_number == EL_NUMBER_F ||
2530 [ + - ]: 3 : at[iO].el_number == EL_NUMBER_CL ||
2531 [ + - ]: 3 : at[iO].el_number == EL_NUMBER_BR ||
2532 [ - + ]: 3 : at[iO].el_number == EL_NUMBER_I) &&
2533 [ # # # # ]: 0 : at[iO].valence == 1 && at[iO].chem_bonds_valence == 1 &&
2534 [ # # # # : 0 : !at[iO].charge && !(at[iO].radical && at[iO].radical != RADICAL_SINGLET) && !NUMH(at, iO))
# # # # ]
2535 : : {
2536 : : ; /* found */
2537 : : }
2538 : : else
2539 : : {
2540 : : /* -O-C= */
2541 [ - + ]: 3 : if (at[iO].el_number != EL_NUMBER_O ||
2542 [ # # ]: 0 : NUMH(at, iO) ||
2543 [ # # ]: 0 : at[iO].valence != 2 ||
2544 [ # # ]: 0 : at[iO].charge ||
2545 [ # # # # ]: 0 : (at[iO].radical && at[iO].radical != RADICAL_SINGLET) ||
2546 [ # # ]: 0 : at[iO].valence != at[iO].chem_bonds_valence) /* djb-rwth: addressing LLVM warning */
2547 : : {
2548 : 3 : bDisconnect = 0; /* reject */
2549 : 3 : break;
2550 : : }
2551 : 0 : iC = at[iO].neighbor[at[iO].neighbor[0] == i];
2552 [ # # ]: 0 : if (at[iC].el_number != EL_NUMBER_C ||
2553 [ # # ]: 0 : at[iC].num_H ||
2554 [ # # ]: 0 : at[iC].chem_bonds_valence != 4 ||
2555 [ # # ]: 0 : at[iC].charge ||
2556 [ # # # # ]: 0 : (at[iC].radical && at[iC].radical != RADICAL_SINGLET) ||
2557 [ # # ]: 0 : at[iC].valence == at[iC].chem_bonds_valence) /* djb-rwth: addressing LLVM warning */
2558 : : {
2559 : 0 : bDisconnect = 0; /* reject */
2560 : 0 : break;
2561 : : }
2562 [ # # ]: 0 : for (j = 0; j < at[iC].valence; j++)
2563 : : {
2564 : 0 : neigh = at[iC].neighbor[j];
2565 [ # # ]: 0 : if (at[neigh].el_number == EL_NUMBER_H)
2566 : : {
2567 : 0 : break;
2568 : : }
2569 : : }
2570 [ # # ]: 0 : if (j != at[iC].valence)
2571 : : {
2572 : 0 : bDisconnect = 0; /* reject */
2573 : 0 : break;
2574 : : }
2575 : : }
2576 : : }
2577 : : }
2578 : :
2579 : 12 : return bDisconnect;
2580 : : }
2581 : :
2582 : : /****************************************************************************/
2583 : 0 : int DisconnectMetalSalt(inp_ATOM *at, int i)
2584 : : {
2585 : : int k, iO;
2586 : : /* disconnect metal atom or ion at[i] */
2587 : :
2588 [ # # ]: 0 : for (k = 0; k < at[i].valence; k++)
2589 : : {
2590 : 0 : iO = at[i].neighbor[k];
2591 [ # # ]: 0 : if (at[iO].valence == 2)
2592 : : {
2593 [ # # ]: 0 : if (at[iO].neighbor[0] == i)
2594 : : {
2595 : : /* assuming atom O always has 2 bonds */
2596 : : /* copy the remaining neighbor to the 0 position */
2597 : 0 : at[iO].neighbor[0] = at[iO].neighbor[1];
2598 : 0 : at[iO].bond_stereo[0] = at[iO].bond_stereo[1];
2599 : 0 : at[iO].bond_type[0] = at[iO].bond_type[1];
2600 : : }
2601 : : /* clear neighbor at position 1 */
2602 : 0 : at[iO].neighbor[1] = 0;
2603 : 0 : at[iO].bond_stereo[1] = 0;
2604 : 0 : at[iO].bond_type[1] = 0;
2605 : : }
2606 : : else
2607 : : {
2608 : : /* clear neighbor at position 1 */
2609 : 0 : at[iO].neighbor[0] = 0;
2610 : 0 : at[iO].bond_stereo[0] = 0;
2611 : 0 : at[iO].bond_type[0] = 0;
2612 : : }
2613 : :
2614 : : /* make O negatively charged */
2615 : 0 : at[iO].charge = -1;
2616 : :
2617 : : /* reduce O valence to account for the removed single bond */
2618 : 0 : at[iO].valence--;
2619 : 0 : at[iO].chem_bonds_valence--;
2620 : :
2621 : : /* clear metal neighbor (O) */
2622 : 0 : at[i].neighbor[k] = 0;
2623 : 0 : at[i].bond_stereo[k] = 0;
2624 : 0 : at[i].bond_type[k] = 0;
2625 : :
2626 : : /* add a positive charge to the metal */
2627 : 0 : at[i].charge++;
2628 : : }
2629 : :
2630 : : /* set metal valence to zero because it has been disconnected */
2631 : 0 : at[i].valence = 0;
2632 : 0 : at[i].chem_bonds_valence = 0;
2633 : :
2634 : 0 : return k;
2635 : : }
2636 : :
2637 : : /****************************************************************************/
2638 : 1 : int DisconnectSalts(ORIG_ATOM_DATA *orig_inp_data, int bDisconnect)
2639 : : {
2640 : : int i, k, iO, num_changes, val;
2641 : : S_CHAR num_explicit_H[NUM_H_ISOTOPES + 1];
2642 : 1 : inp_ATOM *at = orig_inp_data->at;
2643 : 1 : int num_at = orig_inp_data->num_inp_atoms;
2644 : :
2645 : : /* check each atom */
2646 [ + + ]: 3 : for (i = 0, num_changes = 0; i < num_at; i++)
2647 : : {
2648 : :
2649 [ + - ]: 2 : if (!(val = at[i].valence) || /* disconnected atom */
2650 [ + - ]: 2 : val != at[i].chem_bonds_valence || /* a bond has higher multiplicity than 1 */
2651 [ - + - - ]: 2 : (at[i].radical && at[i].radical != RADICAL_SINGLET) /* radical */) /* djb-rwth: addressing LLVM warning */
2652 : : {
2653 : 0 : continue; /* reject */
2654 : : }
2655 : :
2656 [ - + ]: 2 : if (bIsAmmoniumSalt(at, i, &iO, &k, num_explicit_H))
2657 : : {
2658 [ # # ]: 0 : if (bDisconnect)
2659 : : {
2660 : 0 : DisconnectAmmoniumSalt(at, i, iO, k, num_explicit_H);
2661 : 0 : orig_inp_data->num_inp_bonds--;
2662 : : }
2663 : :
2664 : : /* count disconnected atoms */
2665 : 0 : num_changes++;
2666 : : }
2667 [ - + ]: 2 : else if (bIsMetalSalt(at, i))
2668 : : {
2669 [ # # ]: 0 : if (bDisconnect)
2670 : : {
2671 : 0 : k = DisconnectMetalSalt(at, i);
2672 : 0 : orig_inp_data->num_inp_bonds -= k;
2673 : : }
2674 : 0 : num_changes++;
2675 : : }
2676 : : }
2677 : :
2678 : 1 : return num_changes;
2679 : : }
2680 : :
2681 : : /*****************************************************************************/
2682 : : /* Important: Salt disconnection is independent from coord. disconnection: */
2683 : : /* because different atoms are disconnected. */
2684 : : /* However, sal disconnection may need to be rerun after metal disconnection */
2685 : : /* because metal disconnection may make certain atoms be eligible for salt */
2686 : : /* disconnection */
2687 : : /*****************************************************************************/
2688 : 10 : int bIsMetalToDisconnect(inp_ATOM *at, int i, int bCheckMetalValence)
2689 : : {
2690 : : int type, at_valence, num_H;
2691 : :
2692 : : /*
2693 : : if ( !at[i].valence )
2694 : : */
2695 : :
2696 [ + + ]: 10 : if (!(type = get_el_type(at[i].el_number)) ||
2697 [ - + ]: 2 : !(type & IS_METAL))
2698 : : {
2699 : 8 : return 0;
2700 : : }
2701 : :
2702 : 2 : num_H = NUMH(at, i);
2703 : 2 : at_valence = num_H + at[i].chem_bonds_valence;
2704 : :
2705 [ - + ]: 2 : if (!at_valence)
2706 : : {
2707 : 0 : return 0; /* nothing to disconnect */
2708 : : }
2709 : :
2710 [ - + ]: 2 : if (bCheckMetalValence)
2711 : : {
2712 [ # # ]: 0 : if (abs(at[i].charge) > 1)
2713 : : {
2714 : 0 : return 1; /* multiple charges */
2715 : : }
2716 : :
2717 [ # # # # ]: 0 : for (i = 0; i < 2 && (i & type); i++)
2718 : : {
2719 [ # # ]: 0 : if (at_valence == get_el_valence( at[i].el_number, at[i].charge, i )) /* djb-rwth: fixing coverity ID #499532 -- unresolved issue -- revision required */
2720 : : {
2721 : 0 : return 2; /* atom has normal valence */
2722 : : }
2723 : : }
2724 : : }
2725 : :
2726 : 2 : return 1;
2727 : : }
2728 : :
2729 : : /****************************************************************************/
2730 : 4 : int bMayDisconnectMetals(ORIG_ATOM_DATA *orig_inp_data,
2731 : : int bCheckMetalValence,
2732 : : INCHI_MODE *bTautFlagsDone)
2733 : : {
2734 : 4 : int i, j, k, iO, num_changes, val, bRadOrMultBonds, num_impl_H = 0;
2735 : : S_CHAR num_explicit_H[NUM_H_ISOTOPES + 1];
2736 : 4 : inp_ATOM *at = orig_inp_data->at;
2737 : 4 : int num_at = orig_inp_data->num_inp_atoms;
2738 : 4 : int *nNumImplH = &orig_inp_data->bDisconnectCoord;
2739 : :
2740 : : /* check each atom */
2741 [ + + ]: 14 : for (i = 0, num_changes = 0; i < num_at; i++)
2742 : : {
2743 : :
2744 [ + + + - ]: 10 : if (!(val = at[i].valence) && !NUMH(at, i))
2745 : : {
2746 : 2 : continue; /* disconnected atom */
2747 : : }
2748 : :
2749 : 16 : bRadOrMultBonds = (val == 0) ||
2750 [ + - + - ]: 16 : (val != at[i].chem_bonds_valence) || /* a bond has higher multiplicity than 1 */
2751 [ - + - - ]: 8 : (at[i].radical && at[i].radical != RADICAL_SINGLET); /* radical */
2752 : :
2753 [ + - - + ]: 8 : if (!bRadOrMultBonds && bIsAmmoniumSalt(at, i, &iO, &k, num_explicit_H))
2754 : : {
2755 : : ;
2756 : : }
2757 [ + - - + ]: 8 : else if (!bRadOrMultBonds && bIsMetalSalt(at, i))
2758 : : {
2759 : : ;
2760 : : }
2761 [ + + ]: 8 : else if (1 == (j = bIsMetalToDisconnect(at, i, bCheckMetalValence)))
2762 : : {
2763 : 1 : num_impl_H += NUMH(at, i);
2764 : 1 : num_changes++;
2765 : : }
2766 [ - + - - ]: 7 : else if (2 == j && bTautFlagsDone)
2767 : : {
2768 : 0 : *bTautFlagsDone |= TG_FLAG_CHECK_VALENCE_COORD_DONE;
2769 : : }
2770 : : }
2771 : :
2772 [ + - ]: 4 : if (nNumImplH)
2773 : : {
2774 [ + + ]: 4 : *nNumImplH = num_changes ? num_impl_H + 1 : 0;
2775 : : }
2776 : :
2777 : 4 : return num_changes;
2778 : : }
2779 : :
2780 : : /****************************************************************************/
2781 : :
2782 : : #if (bRELEASE_VERSION == 0 && (EXTR_HAS_METAL_ATOM & (EXTR_MASK | EXTR_FLAG)))
2783 : :
2784 : : /****************************************************************************/
2785 : : int bHasMetalAtom(ORIG_ATOM_DATA *orig_inp_data)
2786 : : {
2787 : : int i;
2788 : : inp_ATOM *at;
2789 : :
2790 : : if (orig_inp_data && (at = orig_inp_data->at))
2791 : : {
2792 : : int num_at = orig_inp_data->num_inp_atoms;
2793 : : /* check each atom */
2794 : : for (i = 0; i < num_at; i++)
2795 : : {
2796 : : if (IS_METAL & get_el_type(at[i].el_number))
2797 : : {
2798 : : return 1;
2799 : : }
2800 : : }
2801 : : }
2802 : :
2803 : : return 0;
2804 : : }
2805 : : #endif
2806 : :
2807 : : /*****************************************************************************
2808 : : { "F", 19, 19, 18.998403220, 0 , 0, {{0,}, {0,}, {1,}, {2,}, {3,5}, },},
2809 : : { "Cl", 35, 35, 34.968852730, 0 , 0, {{0,}, {0,}, {1,3,5,7}, {2,4,6}, {3,5,}, },},
2810 : : { "Br", 80, 79, 78.918336100, 0 , 0, {{0,}, {0,}, {1,3,5,7,}, {2,4,6,}, {3,5,}, },},
2811 : : { "I", 127, 127, 126.904500000, 0 , 0, {{0,}, {0,}, {1,3,5,7,}, {2,4,6}, {3,5,}, },},
2812 : : { "At", 210, 210, 209.987100000, 0 , 0, {{0,}, {0,}, {1,3,5,7,}, {2,4,6}, {3,5,}, },},
2813 : : { "N", 14, 14, 14.003074000, 0 , 0, {{1,}, {2,}, {3,5}, {4,}, {3,}, },},
2814 : : { "P", 31, 31, 30.973762000, 0 , 0, {{1,3,5,7,}, {2,4,6,}, {3,5,}, {4,}, {3,}, },},
2815 : : { "As", 75, 75, 74.921594200, 0 , 0, {{0,}, {2,4,6,}, {3,5,}, {4,}, {3,}, },},
2816 : : { "Sb", 122, 121, 120.903800000, 0 , 0, {{1,3,5,7,}, {2,4,6,}, {3,5,}, {2,4,}, {3,}, },},
2817 : : { "O", 16, 16, 15.994914630, 0 , 0, {{0,}, {1,}, {2,}, {3,5,}, {4,}, },},
2818 : : { "S", 32, 32, 31.972070700, 0 , 0, {{0,}, {1,3,5,7,}, {2,4,6}, {3,5,}, {4,}, },},
2819 : : { "Se", 79, 80, 79.916519600, 0 , 0, {{0,}, {1,3,5,7,}, {2,4,6,}, {3,5,}, {4,}, },},
2820 : : { "Te", 128, 130, 129.906200000, 0 , 0, {{0,}, {1,3,5,7,}, {2,4,6,}, {3,5,}, {2,4,}, },},
2821 : : { "Po", 209, 209, 208.982400000, 0 , 0, {{0,}, {1,3,5,7,}, {2,4,6,}, {3,5,}, {2,4,}, },},
2822 : : { "B", 11, 11, 11.009300000, 0 , 0, {{3,}, {4,}, {3,}, {2,}, {1,}, },},
2823 : : *****************************************************************************/
2824 : :
2825 : : /****************************************************************************/
2826 : 1 : int DisconnectMetals(ORIG_ATOM_DATA *orig_inp_data,
2827 : : int bCheckMetalValence,
2828 : : INCHI_MODE *bTautFlagsDone)
2829 : : {
2830 : : int i, j, k, n, iO, num_changes, val, bRadOrMultBonds;
2831 : : int num_impl_H, num_at, err, num_disconnected;
2832 : : S_CHAR num_explicit_H[NUM_H_ISOTOPES + 1];
2833 : : static char elnumber_Heteroat[16] = {
2834 : : '\0',
2835 : : };
2836 : : static int num_halogens = 0;
2837 : : int num_halogens2;
2838 : :
2839 : 1 : inp_ATOM *at = NULL;
2840 : 1 : S_CHAR *bMetal = NULL;
2841 : 1 : inp_ATOM *atom = orig_inp_data->at;
2842 : 1 : int num_atoms = orig_inp_data->num_inp_atoms;
2843 : 1 : int nNumExplH = (orig_inp_data->bDisconnectCoord > 0) ? orig_inp_data->bDisconnectCoord - 1 : 0;
2844 : 1 : AT_NUMB *nOldCompNumber = orig_inp_data->nOldCompNumber;
2845 : :
2846 : 1 : err = 0;
2847 : 1 : num_impl_H = 0;
2848 : 1 : num_at = num_atoms;
2849 : 1 : num_disconnected = 0;
2850 : :
2851 [ + - ]: 1 : if (!(at = (inp_ATOM *)inchi_calloc((long long)num_at + (long long)nNumExplH, sizeof(at[0]))) || /* djb-rwth: cast operators added */
2852 [ - + ]: 1 : !(bMetal = (S_CHAR *)inchi_calloc((long long)num_at + (long long)nNumExplH, sizeof(bMetal[0])))) /* djb-rwth: cast operators added */
2853 : : {
2854 : 0 : err = 1;
2855 : 0 : goto exit_function;
2856 : : }
2857 : :
2858 [ + - ]: 1 : if (!num_halogens) /* if (!elnumber_Heteroat[0] ) */
2859 : : {
2860 : 1 : i = 0;
2861 : : /* halogens */
2862 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_F; /* 0 */
2863 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_CL;
2864 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_BR;
2865 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_I;
2866 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_AT; /* 4 */
2867 : 1 : num_halogens2 = i;
2868 : : /* other non-metal */
2869 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_N;
2870 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_P;
2871 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_AS;
2872 : : /*elnumber_Heteroat[i++] = EL_NUMBER_SB;*/ /* metal 10-28-2003 */
2873 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_O;
2874 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_S;
2875 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_SE;
2876 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_TE;
2877 : : /*elnumber_Heteroat[i++] = EL_NUMBER_PO;*/ /* metal 10-28-2003 */
2878 : 1 : elnumber_Heteroat[i++] = (char)EL_NUMBER_B;
2879 : 1 : elnumber_Heteroat[i++] = 0;
2880 : 1 : num_halogens = num_halogens2;
2881 : : }
2882 : :
2883 : 1 : memcpy(at, atom, num_atoms * sizeof(at[0]));
2884 : :
2885 : : /* check each atom, mark metals */
2886 [ + + ]: 3 : for (i = 0, k = 0, num_changes = 0; i < num_atoms; i++)
2887 : : {
2888 [ - + - - ]: 2 : if (!(val = at[i].valence) && !NUMH(at, i))
2889 : : {
2890 : 0 : continue; /* disconnected atom */
2891 : : }
2892 : 4 : bRadOrMultBonds = (val == 0) ||
2893 [ + - + - ]: 4 : (val != at[i].chem_bonds_valence) || /* a bond has higher multiplicity than 1 */
2894 [ - + - - ]: 2 : (at[i].radical && at[i].radical != RADICAL_SINGLET); /* radical */
2895 : :
2896 [ + - - + ]: 2 : if (!bRadOrMultBonds && bIsAmmoniumSalt(at, i, &iO, &k, num_explicit_H))
2897 : : {
2898 : : ;
2899 : : }
2900 [ + - - + ]: 2 : else if (!bRadOrMultBonds && bIsMetalSalt(at, i))
2901 : : {
2902 : : ;
2903 : : }
2904 [ + + ]: 2 : else if (1 == (j = bIsMetalToDisconnect(at, i, bCheckMetalValence)))
2905 : : {
2906 : 1 : num_impl_H += (k = NUMH(at, i));
2907 : 1 : bMetal[i] = 1 + k;
2908 : 1 : num_changes++;
2909 : : }
2910 [ - + - - ]: 1 : else if (2 == j && bTautFlagsDone)
2911 : : {
2912 : 0 : *bTautFlagsDone |= TG_FLAG_CHECK_VALENCE_COORD_DONE;
2913 : : }
2914 : : }
2915 : :
2916 [ - + ]: 1 : if (num_impl_H != nNumExplH)
2917 : : {
2918 : 0 : err = 2;
2919 : 0 : goto exit_function;
2920 : : }
2921 : :
2922 : : /* replace implicit H atoms with explicit H atoms */
2923 [ + - - + ]: 1 : for (i = 0; i < num_atoms && 0 < num_impl_H; i++)
2924 : : {
2925 [ # # ]: 0 : if (bMetal[i] <= 1)
2926 : : {
2927 : 0 : continue;
2928 : : }
2929 [ # # ]: 0 : for (k = 0; k < NUM_H_ISOTOPES + 1; k++)
2930 : : {
2931 [ # # ]: 0 : n = k ? at[i].num_iso_H[k - 1] : at[i].num_H;
2932 [ # # ]: 0 : for (j = 0; j < n; j++)
2933 : : {
2934 [ # # ]: 0 : if (num_at >= num_atoms + nNumExplH)
2935 : : {
2936 : 0 : err = 3;
2937 : 0 : goto exit_function;
2938 : : }
2939 : 0 : at[num_at].elname[0] = 'H';
2940 : 0 : at[num_at].el_number = get_periodic_table_number(at[num_at].elname);
2941 : 0 : at[num_at].iso_atw_diff = k;
2942 : 0 : at[num_at].component = at[i].component;
2943 : 0 : move_explicit_Hcation(at, num_at + 1, i, num_at, 1);
2944 : 0 : at[num_at].orig_at_number = num_at + 1;
2945 : 0 : num_at++;
2946 : 0 : num_impl_H--;
2947 : 0 : bMetal[i]--;
2948 [ # # ]: 0 : if (k)
2949 : : {
2950 : 0 : at[i].num_iso_H[k - 1]--;
2951 : : }
2952 : : else
2953 : : {
2954 : 0 : at[i].num_H--;
2955 : : }
2956 : : }
2957 : : }
2958 : :
2959 [ # # ]: 0 : if (bMetal[i] != 1)
2960 : : {
2961 : 0 : err = 4;
2962 : 0 : goto exit_function;
2963 : : }
2964 : : }
2965 : :
2966 [ - + ]: 1 : if (num_at != num_atoms + nNumExplH)
2967 : : {
2968 : 0 : err = 5;
2969 : 0 : goto exit_function;
2970 : : }
2971 : :
2972 : : /* disconnect metal - ligand bonds */
2973 [ + + ]: 3 : for (i = 0; i < num_atoms; i++)
2974 : : {
2975 [ + + ]: 2 : if (!bMetal[i])
2976 : : {
2977 : 1 : continue;
2978 : : }
2979 : :
2980 : : /* disconnect metal atom M
2981 : :
2982 : : Note: Defect in case of bridging ligands:
2983 : :
2984 : : M M M M M M(+)
2985 : : \ / will be transformed to , not to
2986 : : N(+) N(+) N(-)
2987 : : / \ / \ / \
2988 : : R R R R R R
2989 : :
2990 : : Non-bridging are OK:
2991 : :
2992 : : M R M(+) R
2993 : : \ / /
2994 : : N(+) ---> N
2995 : : / \ / \
2996 : : R R R R
2997 : :
2998 : : */
2999 : :
3000 [ + + ]: 2 : for (j = at[i].valence - 1; 0 <= j; j--)
3001 : : {
3002 [ + - + - ]: 1 : if (j < at[i].valence && !bMetal[(int)at[i].neighbor[j]])
3003 : : {
3004 : : /* do not break metal-metal bond here */
3005 : :
3006 : 1 : num_disconnected += DisconnectOneLigand(at,
3007 : : nOldCompNumber,
3008 : : bMetal,
3009 : : elnumber_Heteroat,
3010 : : num_halogens,
3011 : : num_atoms,
3012 : : i,
3013 : : j,
3014 : : bTautFlagsDone);
3015 : : }
3016 : : }
3017 : : }
3018 : :
3019 : : /* disconnect metal-metal bonds */
3020 [ + + ]: 3 : for (i = 0; i < num_atoms; i++)
3021 : : {
3022 [ + + ]: 2 : if (!bMetal[i])
3023 : : {
3024 : 1 : continue;
3025 : : }
3026 [ - + ]: 1 : for (j = at[i].valence - 1; 0 <= j; j--)
3027 : : {
3028 [ # # # # ]: 0 : if (j < at[i].valence && bMetal[(int)at[i].neighbor[j]])
3029 : : {
3030 : : /* break metal-metal bond here */
3031 : :
3032 : 0 : num_disconnected += DisconnectOneLigand(at,
3033 : : nOldCompNumber,
3034 : : bMetal,
3035 : : elnumber_Heteroat,
3036 : : num_halogens,
3037 : : num_atoms,
3038 : : i,
3039 : : j,
3040 : : bTautFlagsDone);
3041 : : }
3042 : : }
3043 : : }
3044 : :
3045 : 1 : exit_function:
3046 : :
3047 [ - + ]: 1 : if (!num_disconnected)
3048 : : {
3049 : 0 : err = 6;
3050 : : }
3051 [ + - - + ]: 1 : if (at && err)
3052 : : {
3053 [ # # ]: 0 : inchi_free(at);
3054 : 0 : at = NULL;
3055 : : }
3056 [ + - + - ]: 1 : if (atom && at)
3057 : : { /* changed if ( at ) to if ( atom && at ) 2004-04-03 */
3058 [ + - ]: 1 : inchi_free(atom);
3059 : 1 : atom = NULL;
3060 : : }
3061 [ + - ]: 1 : if (bMetal)
3062 [ + - ]: 1 : inchi_free(bMetal);
3063 : :
3064 [ + - ]: 1 : if (at)
3065 : : {
3066 : 1 : orig_inp_data->at = at;
3067 : 1 : orig_inp_data->num_inp_atoms = num_at;
3068 : : }
3069 : :
3070 [ - + ]: 1 : return err ? -err : num_disconnected;
3071 : : }
3072 : :
3073 : : /****************************************************************************/
3074 : 1 : int DisconnectOneLigand(inp_ATOM *at,
3075 : : AT_NUMB *nOldCompNumber,
3076 : : S_CHAR *bMetal,
3077 : : char *elnumber_Heteroat,
3078 : : int num_halogens,
3079 : : int num_atoms,
3080 : : int iMetal,
3081 : : int jLigand,
3082 : : INCHI_MODE *bTautFlagsDone)
3083 : : {
3084 : : int i, j, iLigand, neigh, val;
3085 : : int metal_neigh_ord[MAXVAL], num_neigh_arom_bonds[MAXVAL];
3086 : : int num_metal_neigh, num_disconnections;
3087 : : int num_del_arom_bonds, num_tot_arom_bonds, new_charge;
3088 : : char *p;
3089 : :
3090 : 1 : iLigand = at[iMetal].neighbor[jLigand];
3091 : 1 : num_metal_neigh = 0;
3092 : 1 : num_disconnections = 0;
3093 : 1 : num_del_arom_bonds = num_tot_arom_bonds = 0;
3094 : :
3095 : : /* find bonds to disconnect */
3096 [ + + ]: 2 : for (i = 0; i < at[iLigand].valence; i++)
3097 : : {
3098 : 1 : num_neigh_arom_bonds[i] = 0;
3099 : 1 : neigh = (int)at[iLigand].neighbor[i];
3100 [ + - + - ]: 1 : if (neigh < num_atoms && bMetal[neigh])
3101 : : {
3102 : 1 : metal_neigh_ord[num_metal_neigh++] = i;
3103 [ - + ]: 1 : if (at[iLigand].bond_type[i] > BOND_TYPE_TRIPLE)
3104 : : {
3105 : : /* aromatic bond */
3106 [ # # ]: 0 : for (j = 0; j < at[neigh].valence; j++)
3107 : : {
3108 : 0 : num_neigh_arom_bonds[i] += (at[neigh].bond_type[j] > BOND_TYPE_TRIPLE);
3109 : : }
3110 : 0 : num_del_arom_bonds++;
3111 : : }
3112 : : }
3113 : 1 : num_tot_arom_bonds += (at[iLigand].bond_type[i] > BOND_TYPE_TRIPLE);
3114 : : }
3115 : :
3116 : : /* Disconnect */
3117 [ - + ]: 1 : if (num_del_arom_bonds)
3118 : : {
3119 : : /* fix chem_valence of the ligand and its neighbors in case of disconnecting arom. bonds */
3120 : : /* because in this case special care should be taken of updating at[].chem_bonds_valence */
3121 [ # # ]: 0 : for (i = 0; i < num_metal_neigh; i++)
3122 : : {
3123 : 0 : j = metal_neigh_ord[i];
3124 [ # # ]: 0 : if (num_neigh_arom_bonds[j])
3125 : : {
3126 : 0 : neigh = at[iLigand].neighbor[j];
3127 : 0 : at[neigh].chem_bonds_valence -= num_neigh_arom_bonds[j] / 2 - (num_neigh_arom_bonds[j] - 1) / 2;
3128 : : }
3129 : : }
3130 : 0 : at[iLigand].chem_bonds_valence -= num_tot_arom_bonds / 2 - (num_tot_arom_bonds - num_del_arom_bonds) / 2;
3131 : : }
3132 : :
3133 : : /* disconnect in reverse order, otherwise the metal_neigh_ord[i]
3134 : : becomes invalid after the first disconnection
3135 : : */
3136 [ + + ]: 2 : for (i = num_metal_neigh - 1; 0 <= i; i--)
3137 : : {
3138 : 1 : num_disconnections += DisconnectInpAtBond(at,
3139 : : nOldCompNumber,
3140 : : iLigand,
3141 : : metal_neigh_ord[i]);
3142 : : }
3143 : :
3144 : : /* attempt to change ligand charge to make its valence 'natural' */
3145 : 1 : i = num_tot_arom_bonds - num_del_arom_bonds;
3146 [ - + - - : 1 : if ((i && i != 2 && i != 3) ||
- - ]
3147 [ - + - - ]: 1 : (at[iLigand].radical && at[iLigand].radical != RADICAL_SINGLET) ||
3148 [ + - ]: 1 : !(p = strchr(elnumber_Heteroat, at[iLigand].el_number))) /* djb-rwth: addressing LLVM warnings */
3149 : : {
3150 : 1 : goto exit_function; /* non-standard atom */
3151 : : }
3152 : :
3153 : 0 : val = at[iLigand].chem_bonds_valence + NUMH(at, iLigand);
3154 : 0 : new_charge = MAX_ATOMS; /* impossible value */
3155 : :
3156 [ # # ]: 0 : if (!val)
3157 : : {
3158 [ # # ]: 0 : if (p - elnumber_Heteroat < num_halogens)
3159 : : {
3160 : 0 : new_charge = -1;
3161 : : }
3162 : : }
3163 : : else
3164 : : {
3165 [ # # ]: 0 : for (i = -1; i <= 1; i++)
3166 : : {
3167 [ # # ]: 0 : if (val == get_el_valence(at[iLigand].el_number, i, 0))
3168 : : {
3169 : 0 : new_charge = i; /* found charge that fits chem. valence */
3170 : 0 : break;
3171 : : }
3172 : : }
3173 : : }
3174 : :
3175 [ # # ]: 0 : if (new_charge != MAX_ATOMS)
3176 : : {
3177 [ # # ]: 0 : if ((new_charge != at[iLigand].charge ||
3178 [ # # # # : 0 : (at[iLigand].radical && at[iLigand].radical != RADICAL_SINGLET)) &&
# # ]
3179 : : 1 == num_metal_neigh)
3180 : : {
3181 [ # # # # : 0 : if (1 == new_charge && 4 == val && 2 == at[iLigand].valence &&
# # ]
3182 [ # # ]: 0 : 4 == at[iLigand].chem_bonds_valence &&
3183 [ # # ]: 0 : at[iLigand].bond_type[0] == at[iLigand].bond_type[1])
3184 : : {
3185 : : ; /* do not add +1 charge to disconnected =N=, etc. 2004-10-27 */
3186 : : }
3187 : : else
3188 : : {
3189 [ # # # # ]: 0 : if (bTautFlagsDone && new_charge != at[iLigand].charge)
3190 : : {
3191 : 0 : *bTautFlagsDone |= TG_FLAG_MOVE_CHARGE_COORD_DONE;
3192 : : }
3193 : 0 : at[iMetal].charge -= new_charge - at[iLigand].charge;
3194 : 0 : at[iLigand].charge = new_charge;
3195 : : /*at[iLigand].radical = 0;*/
3196 : : }
3197 : : }
3198 : : }
3199 : :
3200 : 0 : exit_function:
3201 : :
3202 : 1 : return num_disconnections; /* ret;*/
3203 : : }
3204 : :
3205 : : /****************************************************************************/
3206 : 0 : double dist3D(inp_ATOM *at1, inp_ATOM *at2)
3207 : : {
3208 : 0 : double dx = at1->x - at2->x;
3209 : 0 : double dy = at1->y - at2->y;
3210 : 0 : double dz = at1->z - at2->z;
3211 : :
3212 : 0 : return sqrt(dx * dx + dy * dy + dz * dz);
3213 : : }
3214 : :
3215 : : /****************************************************************************/
3216 : : #define MIN_BOND_LENGTH (1.0e-6)
3217 : : #define MIN_COS (1.0e-6)
3218 : : #define MIN_BOND_LENGTH2 (MIN_BOND_LENGTH * MIN_BOND_LENGTH)
3219 : : #define MAX_BOND_LENGTH (1.0e30)
3220 : : /****************************************************************************/
3221 : :
3222 : : /****************************************************************************/
3223 : 0 : double GetMinDistDistribution(inp_ATOM *at,
3224 : : int num_at,
3225 : : int iat,
3226 : : int iat_H,
3227 : : int bInAllComponents,
3228 : : double min_dist[],
3229 : : int num_segm)
3230 : : {
3231 : : /* const double one_pi = 2.0*atan2(1.0 , 0.0 ); */
3232 : 0 : const double one_pi = 3.14159265358979323846; /* M_PI */
3233 : 0 : const double two_pi = 2.0 * one_pi;
3234 : 0 : const double f_step = two_pi / num_segm;
3235 : 0 : const double h_step = f_step / 2.0;
3236 : : int i, j, k, kk, ki, kn, n, num_bonds;
3237 : : double xi, yi, xn, yn, cross_prod_in, dot_prod_in, xni, yni, rni, tni, rmin;
3238 : 0 : double fi, fk, fn, ft = 0, rt = 0, rk, ri, rn, c, ave_bond_len;
3239 : :
3240 [ # # ]: 0 : for (i = 0; i < num_segm; i++)
3241 : : {
3242 : 0 : min_dist[i] = MAX_BOND_LENGTH; /* more than any distance */
3243 : : }
3244 : 0 : num_bonds = 0;
3245 : 0 : ave_bond_len = 0.0;
3246 : :
3247 [ # # ]: 0 : for (i = 0; i < num_at; i++)
3248 : : {
3249 [ # # # # : 0 : if (i != iat && i != iat_H &&
# # ]
3250 [ # # ]: 0 : (bInAllComponents || at[i].component == at[iat].component))
3251 : : {
3252 [ # # ]: 0 : for (j = 0; j < at[i].valence; j++)
3253 : : {
3254 : 0 : n = at[i].neighbor[j];
3255 [ # # # # : 0 : if ((n > i && n != iat) || n == iat_H)
# # ]
3256 : : {
3257 : 0 : continue;
3258 : : }
3259 : : #if (bRELEASE_VERSION != 1 && defined(_DEBUG))
3260 : : if (n == iat)
3261 : : {
3262 : : int stop = 1; /* <BRKPT> */
3263 : : }
3264 : : #endif
3265 : 0 : xi = at[i].x - at[iat].x; /* ri; i != iat */
3266 : 0 : yi = at[i].y - at[iat].y;
3267 : 0 : xn = at[n].x - at[iat].x; /* rn; possibly n == iat */
3268 : 0 : yn = at[n].y - at[iat].y;
3269 : 0 : cross_prod_in = xi * yn - xn * yi; /* ((r(i)-r(iat)) x (r(n)-r(iat)) */
3270 [ # # ]: 0 : if (cross_prod_in < -0.01 * MIN_BOND_LENGTH2)
3271 : : {
3272 : : /* make sure the r(i)->r(n) vector is counterclockwise around at[iat] */
3273 : 0 : inchi_swap((char *)&xi, (char *)&xn, sizeof(xi));
3274 : 0 : inchi_swap((char *)&yi, (char *)&yn, sizeof(yi));
3275 : : /* djb-rwth: removing redundant code */
3276 : : }
3277 : :
3278 : 0 : xni = xn - xi; /* r(n)->r(i) */
3279 : 0 : yni = yn - yi;
3280 : 0 : rni = xni * xni + yni * yni;
3281 [ # # ]: 0 : if (rni > 0.01 * MIN_BOND_LENGTH2)
3282 : : {
3283 : : /* vector length |ri->rn| is not too small */
3284 : : /* arrowhead of the vector r(t) = ri + (rn-ri)*t; 0 <= t <= 1 points to the bond ri->rn */
3285 : : /* r(tni) is perpendicular to the bond ri->rn so that min|r(t)| = r(tni) = |tni|*rni */
3286 : 0 : tni = -(xni * xi + yni * yi) / rni;
3287 : : /* find min. distance from n-i bond to at[iat] */
3288 [ # # ]: 0 : if (tni < 0.0)
3289 : : {
3290 : 0 : rmin = sqrt(xi * xi + yi * yi);
3291 : : }
3292 [ # # ]: 0 : else if (tni > 1.0)
3293 : : {
3294 : 0 : rmin = sqrt(xn * xn + yn * yn);
3295 : : }
3296 : : else
3297 : : {
3298 : 0 : rmin = sqrt(tni * tni * rni);
3299 : : }
3300 : 0 : ave_bond_len += sqrt(rni);
3301 : 0 : num_bonds++;
3302 : : }
3303 : : else
3304 : : {
3305 : : /* zero length i-n bond */
3306 : 0 : tni = 0.5; /* fake */
3307 : 0 : rmin = sqrt(xi * xi + yi * yi); /* arbitrarily choose one */
3308 : : }
3309 [ # # ]: 0 : if (rmin >= 0.1 * MIN_BOND_LENGTH)
3310 : : {
3311 : : /* at[iat] does not belong to at[i]-at[n] bond */
3312 : 0 : int bCalc_rt = 1;
3313 : 0 : fi = atan2(yi, xi);
3314 [ # # ]: 0 : fn = (n == iat) ? fi : atan2(yn, xn);
3315 [ # # ]: 0 : if (fi > fn)
3316 : : {
3317 : : /* make sure fn - fi >= 0 */
3318 : 0 : fn += two_pi;
3319 : : }
3320 [ # # ]: 0 : if (fi < 0.0)
3321 : : {
3322 : 0 : fi += two_pi;
3323 : 0 : fn += two_pi;
3324 : : }
3325 : 0 : ki = (int)floor((fi + h_step) / f_step); /* cast does not match function type */
3326 : 0 : kn = (int)floor((fn + h_step) / f_step);
3327 : :
3328 : : /* the bond may affect several segments */
3329 [ # # ]: 0 : for (k = ki; k <= kn; k++)
3330 : : {
3331 : 0 : kk = k % num_segm;
3332 [ # # ]: 0 : if (min_dist[kk] < rmin)
3333 : : {
3334 : 0 : continue;
3335 : : }
3336 [ # # ]: 0 : if (bCalc_rt)
3337 : : {
3338 [ # # ]: 0 : if (n == iat)
3339 : : {
3340 : 0 : ft = fi;
3341 : 0 : rt = rmin;
3342 : : }
3343 : : else
3344 : : {
3345 : : double xt, yt;
3346 : 0 : xt = xi + xni * tni;
3347 : 0 : yt = yi + yni * tni;
3348 : 0 : ft = atan2(yt, xt);
3349 : 0 : rt = sqrt(xt * xt + yt * yt);
3350 : : }
3351 : 0 : bCalc_rt = 0;
3352 : : }
3353 : 0 : fk = f_step * kk;
3354 : 0 : c = fabs(cos(fk - ft));
3355 [ # # ]: 0 : if (c < MIN_COS)
3356 : 0 : c = MIN_COS;
3357 : 0 : rk = rt / c;
3358 [ # # ]: 0 : if (min_dist[kk] > rk)
3359 : : {
3360 : 0 : min_dist[kk] = rk;
3361 : : }
3362 : : }
3363 : : }
3364 : : else
3365 : : {
3366 : : /* rmin < 0.1*MIN_BOND_LENGTH */
3367 : 0 : ri = xi * xi + yi * yi;
3368 : 0 : rn = xn * xn + yn * yn;
3369 [ # # # # ]: 0 : if (ri > MIN_BOND_LENGTH2 && rn > MIN_BOND_LENGTH2)
3370 : : {
3371 : 0 : dot_prod_in = xn * xi + yn * yi;
3372 : : /* a very short bond */
3373 [ # # ]: 0 : if (dot_prod_in > 0.01 * MIN_BOND_LENGTH2)
3374 : : {
3375 : : /* bond does not cross at[iat] */
3376 : 0 : double fyixi = atan2(yi, xi);
3377 [ # # ]: 0 : if (fyixi < 0.0)
3378 : 0 : fyixi += two_pi;
3379 : 0 : kk = (int)floor((fyixi + h_step) / f_step) % num_segm;
3380 [ # # ]: 0 : if (min_dist[kk] > rmin)
3381 : : {
3382 : 0 : min_dist[kk] = rmin;
3383 : : }
3384 : : }
3385 [ # # ]: 0 : else if (dot_prod_in < -0.01 * MIN_BOND_LENGTH2)
3386 : : {
3387 : : /* bond does cross at[iat] */
3388 : 0 : double fyixi = atan2(yi, xi);
3389 [ # # ]: 0 : if (fyixi < 0.0)
3390 : 0 : fyixi += two_pi;
3391 : 0 : kk = (int)floor((fyixi + h_step) / f_step) % num_segm;
3392 [ # # ]: 0 : if (min_dist[kk] > rmin)
3393 : : {
3394 : 0 : min_dist[kk] = rmin;
3395 : : }
3396 : 0 : fyixi += one_pi;
3397 : 0 : kk = (int)floor((fyixi + h_step) / f_step) % num_segm;
3398 [ # # ]: 0 : if (min_dist[kk] > rmin)
3399 : : {
3400 : 0 : min_dist[kk] = rmin;
3401 : : }
3402 : : }
3403 : : else
3404 : : {
3405 : : ; /* error, should not happen */
3406 : : }
3407 : : }
3408 [ # # # # ]: 0 : else if (ri <= MIN_BOND_LENGTH2 && rn <= MIN_BOND_LENGTH2)
3409 : : {
3410 : : /* a very short bond coincides with at[iat]; ignore */
3411 : : ;
3412 : : }
3413 : : else
3414 : : {
3415 : : /* one end of the bond coincides with at[iat] */
3416 [ # # ]: 0 : fi = ri > rn ? atan2(yi, xi) : atan2(yn, xn);
3417 [ # # ]: 0 : if (fi < 0.0)
3418 : 0 : fi += two_pi;
3419 : 0 : kk = (int)floor((fi + h_step) / f_step) % num_segm;
3420 [ # # ]: 0 : if (min_dist[kk] > rmin)
3421 : : {
3422 : 0 : min_dist[kk] = rmin;
3423 : : }
3424 : : }
3425 : : }
3426 : : }
3427 : : }
3428 : : }
3429 : :
3430 [ # # ]: 0 : if (num_bonds)
3431 : : {
3432 : 0 : return ave_bond_len / (double)num_bonds;
3433 : : }
3434 : : else
3435 : : {
3436 : 0 : return 0.0;
3437 : : }
3438 : : }
3439 : :
3440 : : /****************************************************************************/
3441 : 0 : int move_explicit_Hcation(inp_ATOM *at,
3442 : : int num_at,
3443 : : int iat,
3444 : : int iat_H,
3445 : : int bInAllComponents)
3446 : : {
3447 : :
3448 : : #define NUM_SEGM 20
3449 : :
3450 : : /* const double one_pi = 2.0*atan2(1.0 , 0.0 ); */
3451 : 0 : const double one_pi = 3.14159265358979323846; /* M_PI */
3452 : 0 : const double two_pi = 2.0 * one_pi;
3453 : 0 : const double f_step = two_pi / NUM_SEGM;
3454 : 0 : const double h_step = f_step / 2.0;
3455 : : double min_dist[NUM_SEGM];
3456 : 0 : int nB, i, k, kk, next, val = 0;
3457 : : double r, r0, xd, yd, zd, xr, yr, zr, ave_bond_len;
3458 : : /*double step = 4.0*atan(1.0)/NUM_SEGM;*/
3459 : : /* find at[iat] neighbors coordinates */
3460 : :
3461 : 0 : xd = yd = zd = 0.0;
3462 : :
3463 [ # # ]: 0 : if (at[iat].valence)
3464 : : {
3465 [ # # ]: 0 : for (i = 0, nB = 0, r = 0.0; i < at[iat].valence; i++)
3466 : : {
3467 : 0 : next = at[iat].neighbor[i];
3468 : 0 : xd += at[next].x;
3469 : 0 : yd += at[next].y;
3470 : 0 : zd += at[next].z;
3471 : 0 : r += dist3D(at + iat, at + next);
3472 : 0 : nB++;
3473 : : }
3474 : 0 : xd /= (double)nB;
3475 : 0 : yd /= (double)nB;
3476 : 0 : zd /= (double)nB;
3477 : 0 : r /= (double)nB;
3478 : 0 : r0 = sqrt((double)(xd - at[iat].x) * (xd - at[iat].x) + (double)(yd - at[iat].y) * (yd - at[iat].y));
3479 : : }
3480 : : else
3481 : : {
3482 [ # # ]: 0 : if (at[iat_H].valence)
3483 : : {
3484 : 0 : r = dist3D(at + iat_H, at + (int)at[iat_H].neighbor[0]);
3485 : : }
3486 : : else
3487 : : {
3488 : 0 : r = 0.0;
3489 : : }
3490 : 0 : r0 = 0.0;
3491 : : }
3492 : :
3493 : 0 : ave_bond_len = GetMinDistDistribution(at, num_at, iat, iat_H,
3494 : : bInAllComponents, min_dist,
3495 : : NUM_SEGM);
3496 : :
3497 [ # # # # ]: 0 : if (r < MIN_BOND_LENGTH && ave_bond_len > MIN_BOND_LENGTH)
3498 : : {
3499 : 0 : r = ave_bond_len; /* ave_bond_len = 0.0 may mean that it is 0D structure */
3500 : : }
3501 : :
3502 [ # # ]: 0 : if (r > MIN_BOND_LENGTH)
3503 : : {
3504 : : /* process non-zero bond lengths */
3505 : : double f;
3506 [ # # ]: 0 : if (10.0 * r0 < r)
3507 : : {
3508 : 0 : xr = -r; /* arbitrary */
3509 : 0 : yr = 0.0;
3510 : 0 : zr = 0.0;
3511 : : }
3512 : : else
3513 : : {
3514 : : /*
3515 : : if ( r0 < MIN_BOND_LENGTH ) {
3516 : : r0 = 1.0;
3517 : : }
3518 : : */
3519 : 0 : xr = r * (at[iat].x - xd) / r0;
3520 : 0 : yr = r * (at[iat].y - yd) / r0; /* length = r */
3521 : 0 : zr = r * (at[iat].z - zd) / r0;
3522 : :
3523 : : /* -- test: opposire direction --
3524 : : xr = -r * ( at[iat].x - xd )/r0;
3525 : : yr = -r * ( at[iat].y - yd )/r0;
3526 : : zr = -r * ( at[iat].z - zd )/r0;
3527 : : */
3528 [ # # ]: 0 : if (xr * xr + yr * yr < 0.04 * r * r)
3529 : : {
3530 : 0 : xr = -r;
3531 : 0 : yr = 0.0;
3532 : : }
3533 : : }
3534 : :
3535 : 0 : r = sqrt(xr * xr + yr * yr);
3536 : 0 : f = atan2(yr, xr);
3537 : :
3538 [ # # ]: 0 : if (f < 0.0)
3539 : : {
3540 : 0 : f += two_pi;
3541 : : }
3542 : :
3543 : 0 : kk = (int)floor((f + h_step) / f_step) % NUM_SEGM;
3544 : : /* cast does not match function type by design */
3545 : :
3546 [ # # ]: 0 : if (min_dist[kk] < 1.5 * r)
3547 : : {
3548 : 0 : double dist = 1.5 * r;
3549 : 0 : int start = -1, len = 0, start_max = -1, len_max = 0;
3550 : :
3551 : 0 : again:
3552 : : /* look for longest kk interval with min_dist[kk] >= dist */
3553 [ # # ]: 0 : for (k = 0, start = 0, len = 0, len_max = 0; k < 2 * NUM_SEGM; k++)
3554 : : {
3555 : 0 : kk = k % NUM_SEGM;
3556 [ # # ]: 0 : if (min_dist[kk] >= dist)
3557 : : {
3558 [ # # ]: 0 : if (!len++)
3559 : : {
3560 : 0 : start = k;
3561 : : }
3562 : : }
3563 : : else
3564 : : {
3565 [ # # ]: 0 : if (len > len_max)
3566 : : {
3567 : 0 : len_max = len;
3568 : 0 : start_max = start;
3569 : : }
3570 : 0 : len = 0;
3571 : : }
3572 : : }
3573 [ # # ]: 0 : if (!len_max)
3574 : : {
3575 [ # # ]: 0 : if (dist > 0.1 * r)
3576 : : {
3577 : 0 : dist *= 0.75;
3578 : 0 : goto again;
3579 : : }
3580 : : else
3581 : : {
3582 : 0 : goto done; /* do it anyway */
3583 : : }
3584 : : }
3585 : : else
3586 : : {
3587 : : /* found a good sector */
3588 : 0 : f = f_step * ((double)start_max + ((double)len_max - 1.0) / 2.0); /* djb-rwth: cast operators added */
3589 : 0 : r0 = dist / 1.5;
3590 : 0 : xr = r0 * cos(f);
3591 : 0 : yr = r0 * sin(f);
3592 : 0 : zr = zr / r * r0;
3593 : : }
3594 : : }
3595 : : }
3596 : : else
3597 : : {
3598 : 0 : xr = yr = zr = 0;
3599 : : }
3600 : :
3601 : 0 : done:
3602 [ # # ]: 0 : if (at[iat_H].valence)
3603 : : {
3604 : : /* disconnect H */
3605 : 0 : next = at[iat_H].neighbor[0];
3606 [ # # ]: 0 : for (i = 0; i < at[next].valence; i++)
3607 : : {
3608 [ # # ]: 0 : if (at[next].neighbor[i] == iat_H)
3609 : : {
3610 : 0 : RemoveInpAtBond(at, next, i);
3611 : 0 : i = 0; /* success */
3612 : 0 : break;
3613 : : }
3614 : : }
3615 : : }
3616 : : else
3617 : : {
3618 : : /* isolated H+ cation */
3619 : 0 : next = iat_H;
3620 : 0 : i = 0;
3621 : 0 : at[iat_H].valence = 1;
3622 : 0 : at[iat_H].chem_bonds_valence = 1;
3623 : 0 : at[iat_H].bond_type[0] = BOND_TYPE_SINGLE;
3624 : : }
3625 : :
3626 [ # # ]: 0 : if (0 == i /*i < at[next].valence*/)
3627 : : {
3628 : : /* move charge */
3629 [ # # # # ]: 0 : if (at[next].charge > 0 && at[iat].charge < 0)
3630 : : {
3631 : 0 : at[next].charge--;
3632 : 0 : at[iat].charge++;
3633 : : }
3634 : :
3635 : : /* connect H to at[iat] */
3636 : 0 : val = at[iat].valence;
3637 : :
3638 : : #pragma warning (push)
3639 : : #pragma warning (disable: 6386)
3640 [ # # ]: 0 : if (val < MAXVAL)
3641 : : {
3642 : 0 : at[iat].neighbor[val] = iat_H;
3643 : 0 : at[iat].bond_type[val] = at[iat_H].bond_type[0];
3644 : 0 : at[iat].bond_stereo[val] = 0;
3645 : 0 : at[iat].chem_bonds_valence += at[iat_H].bond_type[0];
3646 : 0 : at[iat].valence = val + 1;
3647 : : };
3648 : : #pragma warning(pop)
3649 : :
3650 : 0 : at[iat_H].component = at[iat].component;
3651 : 0 : at[iat_H].neighbor[0] = iat;
3652 : 0 : at[iat_H].bond_stereo[0] = 0; /* possible loss of stereo info */
3653 : 0 : at[iat_H].x = at[iat].x + xr;
3654 : 0 : at[iat_H].y = at[iat].y + yr;
3655 : 0 : at[iat_H].z = at[iat].z + zr;
3656 : :
3657 : 0 : return 1; /* success */
3658 : : }
3659 : :
3660 : 0 : return 0; /* failed */
3661 : : }
3662 : :
3663 : : /****************************************************************************/
3664 : 1264 : int add_DT_to_num_H(int num_atoms, inp_ATOM *at)
3665 : : /* assume num_1H, num_D and num_T are not included in num_H */
3666 : : {
3667 : : int i, j;
3668 [ + + ]: 10463 : for (i = 0; i < num_atoms; i++)
3669 : : {
3670 [ + + ]: 36796 : for (j = 0; j < NUM_H_ISOTOPES; j++)
3671 : : {
3672 : 27597 : at[i].num_H += at[i].num_iso_H[j];
3673 : : }
3674 : : }
3675 : 1264 : return 0;
3676 : : }
3677 : :
3678 : : /****************************************************************************
3679 : : **@nnuk
3680 : : * @param at input atom array
3681 : : * @param num_atoms number of atoms
3682 : : * @param metal_idx index of the metal atom
3683 : : * @return 1 all neighbors are H/D/T
3684 : : * @return 0 at least one non-hydrogen neighbor is present
3685 : : ****************************************************************************/
3686 : 5 : static int is_only_HDT_neighbors(const inp_ATOM* at, int num_atoms, int metal_idx)
3687 : : {
3688 : : int v;
3689 : :
3690 [ + - + - : 5 : if (!at || metal_idx < 0 || metal_idx >= num_atoms)
- + ]
3691 : : {
3692 : 0 : return 0;
3693 : : }
3694 : :
3695 [ + + ]: 22 : for (v = 0; v < at[metal_idx].valence; v++)
3696 : : {
3697 : 18 : int nb = at[metal_idx].neighbor[v];
3698 : :
3699 [ + - - + ]: 18 : if (nb < 0 || nb >= num_atoms)
3700 : : {
3701 : 0 : continue;
3702 : : }
3703 : :
3704 [ + + - + ]: 18 : if (!(at[nb].elname[0] == 'H' && at[nb].elname[1] == '\0'))
3705 : : {
3706 : 1 : return 0;
3707 : : }
3708 : : }
3709 : :
3710 : 4 : return 1;
3711 : : }
3712 : :
3713 : : /****************************************************************************
3714 : : Return value: new number of atoms > 0 or -1=out of RAM
3715 : : ****************************************************************************/
3716 : 1264 : int remove_terminal_HDT(int num_atoms, inp_ATOM *at, int bFixTermHChrg, const unsigned char* keep_explicit_HDT)
3717 : : {
3718 : : AT_NUMB *new_ord;
3719 : : inp_ATOM *new_at;
3720 : : char *p;
3721 : : static const char szHDT[] = "HDT";
3722 : : static const int kMax = sizeof(szHDT); /* = 4 */
3723 : 1264 : int ret = -1;
3724 : 1264 : int num_hydrogens = 0, num_H = 0; /* number of terminal H, D, T */
3725 : : int i, j, k, n, m;
3726 : : int val;
3727 : : AT_RANK new_HydrogenAt_order[NUM_H_ISOTOPES + 1];
3728 : : AT_RANK new_OtherNeigh_order[MAXVAL];
3729 : : S_CHAR old_trans[MAX_NUM_STEREO_BONDS];
3730 : :
3731 : : int num_OtherNeigh, num_HydrogenAt;
3732 : :
3733 : 1264 : new_ord = (AT_NUMB *)inchi_calloc(num_atoms, sizeof(new_ord[0])); /* changed malloc to calloc 9-11-2003 */
3734 : 1264 : new_at = (inp_ATOM *)inchi_malloc(sizeof(new_at[0]) * num_atoms);
3735 [ + - - + ]: 1264 : if (!new_ord || !new_at)
3736 : : {
3737 : 0 : goto exit_function;
3738 : : }
3739 : :
3740 : : /* move H. D, T to the end of the list of atoms */
3741 [ + + ]: 10511 : for (i = 0; i < num_atoms; i++)
3742 : : {
3743 : 9247 : at[i].component = i; /* temporarily save original numbering */
3744 : : /* get k = temp. hydrogen isotope/non-hydrogen atom type: */
3745 : : /* k=0:H, k=2:D, k=3:T, k=4=kMax: not a hydrogen */
3746 [ + + ]: 18247 : k = at[i].elname[1] ? kMax : (p = (char *)strchr(szHDT, at[i].elname[0])) ? (int)(p - szHDT)
3747 [ + + ]: 9000 : : kMax;
3748 : : /* set hydrogen isotope atw differences */
3749 : : /* Notes: k-value of isotopic H is incremented to correct iso_atw_diff value later. */
3750 : : /* 1H isotope cannot be detected here. */
3751 [ + - - + ]: 9247 : if (k == ATW_H || k == ATW_H + 1)
3752 : : {
3753 : : /* D or T, k = 1 or 2 */
3754 : 0 : at[i].elname[0] = 'H'; /* hydrogen isotope */
3755 : 0 : at[i].iso_atw_diff = ++k; /* increment k to make k = iso_atw_diff ( 2 for D, 3 for T ) */
3756 : : }
3757 [ + + + + : 9247 : num_H += (k != kMax && at[i].valence == 1 && at[i].chem_bonds_valence == 1 && !NUMH(at, i));
+ - + - ]
3758 : : }
3759 : :
3760 : : /* special case: HD, HT, DT, HH: the only non-isotopic H or
3761 : : * the lightest isotopic H out of two is removed
3762 : : * to become implicit (make the heavier H the "central atom").
3763 : : * Note: This must be consistent with MOL_FMT_to_atom()
3764 : : * treatment of isotopic Hn aliases.
3765 : : */
3766 [ - + - - ]: 1264 : if (2 == num_H && 2 == num_atoms &&
3767 [ # # # # : 0 : !NUMH(at, 0) && !NUMH(at, 1) &&
# # ]
3768 : 0 : (!keep_explicit_HDT ||
3769 [ # # ]: 0 : (!keep_explicit_HDT[0] &&
3770 [ # # ]: 0 : !keep_explicit_HDT[1])))
3771 : : {
3772 : :
3773 [ # # ]: 0 : if (at[0].iso_atw_diff >= at[1].iso_atw_diff)
3774 : : {
3775 : 0 : new_ord[0] = 0;
3776 : 0 : new_ord[1] = 1;
3777 : : }
3778 : : else
3779 : : {
3780 : 0 : new_ord[0] = 1;
3781 : 0 : new_ord[1] = 0;
3782 : : }
3783 [ # # ]: 0 : if (at[new_ord[1]].charge)
3784 : : {
3785 : 0 : at[new_ord[0]].charge += at[new_ord[1]].charge;
3786 : 0 : at[new_ord[1]].charge = 0;
3787 : : }
3788 : 0 : new_at[new_ord[0]] = at[0];
3789 : 0 : new_at[new_ord[1]] = at[1];
3790 : 0 : num_hydrogens = 1;
3791 : : }
3792 : : else
3793 : : {
3794 : : /* general case except H-H */
3795 [ + + ]: 10511 : for (i = 0; i < num_atoms; i++)
3796 : : {
3797 [ + + + + ]: 9514 : k = (at[i].elname[1] || NUMH(at, i)) ? kMax : (at[i].elname[0] == 'H') ? at[i].iso_atw_diff
3798 [ + + ]: 267 : : kMax;
3799 : 9247 : n = (int)at[i].neighbor[0];
3800 [ + + + + : 9247 : if (k < kMax && at[i].valence == 1 && at[i].chem_bonds_valence == 1 &&
+ - + + ]
3801 [ - + + - ]: 51 : (!keep_explicit_HDT || !keep_explicit_HDT[i]) &&
3802 : : /* the order of comparison is important */
3803 : 49 : ((n > i) /* at[n] has not been encountered yet*/ ||
3804 [ + - + + ]: 98 : (int)new_ord[n] < num_atoms - num_hydrogens) /* at[n] might have been encountered; it has not been moved */ &&
3805 [ + + ]: 54 : (!is_el_a_metal(at[n].el_number) || is_only_HDT_neighbors(at, num_atoms, n))/*@nnuk*/)
3806 : : {
3807 : : /* found an explicit terminal hydrogen */
3808 : 48 : num_hydrogens++;
3809 [ + + - + : 48 : if (k == 0 && ATW_H <= at[i].iso_atw_diff && at[i].iso_atw_diff < ATW_H + NUM_H_ISOTOPES)
- - ]
3810 : : {
3811 : 0 : k = at[i].iso_atw_diff; /* H isotope has already been marked above or elsewhere */ /* djb-rwth: ignoring LLVM warning: variable used */
3812 : : }
3813 [ - + ]: 48 : if (at[i].charge)
3814 : : {
3815 : : /* transfer charge from the hydrogen */
3816 : 0 : at[n].charge += at[i].charge;
3817 : 0 : at[i].charge = 0;
3818 [ # # ]: 0 : if (bFixTermHChrg)
3819 : : {
3820 : : /* Fixed bug (July 6, 2008 IPl) :
3821 : : if terminal H was charged (not neutralized before call of remove_terminal_HDT)
3822 : : and had an ordering number > than that of heavy-atom neighbour, then
3823 : : charge on neighbour atom was not adjusted (though charge on H was removed). */
3824 [ # # ]: 0 : if (i > n)
3825 : : /* new_at[new_ord[n]] has been created and filled already */
3826 : 0 : new_at[new_ord[n]].charge = at[n].charge;
3827 : : }
3828 : : }
3829 : 48 : new_ord[i] = num_atoms - num_hydrogens; /* move hydrogens to the end of the list */
3830 : : }
3831 : : else
3832 : : {
3833 : : /* atom is not an explicit terminal hydrogen */
3834 : 9199 : new_ord[i] = i - num_hydrogens; /* adjust non-hydrogens positions */
3835 : : }
3836 : :
3837 : : /* copy atom to the new position */
3838 : 9247 : new_at[new_ord[i]] = at[i];
3839 : : } /* i */
3840 : : } /* general case except H-H */
3841 : :
3842 [ + + ]: 1264 : if (num_hydrogens)
3843 : : {
3844 : 8 : int num_others = num_atoms - num_hydrogens; /* atoms which are not terminal H, D, T */
3845 [ + + ]: 8 : if (num_hydrogens > 1)
3846 : : {
3847 : : /* sort hydrogen isotopes in ascending order, */
3848 : : /* orig, numbers being the secondary sorting key */
3849 : 2 : qsort(new_at + num_others, num_hydrogens, sizeof(new_at[0]), cmp_iso_atw_diff_component_no);
3850 : : }
3851 : : /* save new numbering of hydrogen atoms using temporarily saved orig numbering */
3852 [ + + ]: 56 : for (i = num_others; i < num_atoms; i++)
3853 : : {
3854 : 48 : new_ord[(int)new_at[i].component] = i;
3855 : : }
3856 : :
3857 : : /* renumber neighbors according to new_ord[] and detach terminal hydrogens */
3858 [ + + ]: 5050 : for (i = 0; i < num_others; i++)
3859 : : {
3860 : 5042 : memset(new_HydrogenAt_order, 0, sizeof(new_HydrogenAt_order)); /* djb-rwth: memset_s C11/Annex K variant? */
3861 : 5042 : memset(new_OtherNeigh_order, 0, sizeof(new_OtherNeigh_order)); /* djb-rwth: memset_s C11/Annex K variant? */
3862 : 5042 : num_OtherNeigh = 0;
3863 : 5042 : num_HydrogenAt = 0;
3864 : 5042 : num_H = 0;
3865 : :
3866 [ + - - + ]: 5042 : for (m = 0; m < MAX_NUM_STEREO_BONDS && new_at[i].sb_parity[m]; m++)
3867 : : {
3868 : 0 : old_trans[m] = 2 - (new_at[i].sn_ord[m] + new_at[i].sb_ord[m] + (new_at[i].sn_ord[m] > new_at[i].sb_ord[m])) % 2;
3869 : : }
3870 : :
3871 [ + + ]: 15170 : for (k = val = 0; k < new_at[i].valence; k++) /* djb-rwth: removing redundant variables/code */
3872 : : {
3873 [ + + ]: 10128 : if (num_others <= (n = new_ord[new_at[i].neighbor[k]]))
3874 : : {
3875 : : /* discovered neighbor = disconnected explicit hydrogen
3876 : : * i = new atom new_at[i] ordering number
3877 : : * n = new number of the explicit H
3878 : : * k = ordering number of the explicit H in new_at[i] adjacency list
3879 : : */
3880 [ + + + - ]: 48 : if (0 < new_at[n].iso_atw_diff && new_at[n].iso_atw_diff < ATW_H + NUM_H_ISOTOPES)
3881 : : {
3882 : : /* make explicit isotopic H implicit */
3883 : 5 : new_at[i].num_iso_H[new_at[n].iso_atw_diff - 1]++; /* isotopic H */
3884 : 5 : num_HydrogenAt += !new_HydrogenAt_order[new_at[n].iso_atw_diff];
3885 : 5 : new_HydrogenAt_order[new_at[n].iso_atw_diff] = k + 1;
3886 : : }
3887 : : else
3888 : : {
3889 : : /* make explicit non-isotopic H implicit */
3890 : 43 : new_at[i].num_H++; /* non-isotopic H */
3891 : 43 : num_HydrogenAt += !num_H;
3892 : 43 : num_H++;
3893 : 43 : new_HydrogenAt_order[0] = k + 1;
3894 : : }
3895 : : /* decrement chem. bonds valence because one bond is removed */
3896 [ + - ]: 48 : new_at[i].chem_bonds_valence = inchi_max(0, new_at[i].chem_bonds_valence - 1);
3897 : 48 : new_at[n].neighbor[0] = i; /* update removed hydrogen neighbor number */
3898 [ - + ]: 48 : if (new_at[i].sb_parity[0])
3899 : : {
3900 : : /* if the removed H is an SB neighbor then mark it as removed */
3901 [ # # # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_BONDS && new_at[i].sb_parity[m]; m++)
3902 : : {
3903 [ # # ]: 0 : if (k == (int)new_at[i].sn_ord[m])
3904 : : {
3905 : 0 : new_at[i].sn_ord[m] = -(new_at[n].iso_atw_diff + 1);
3906 : : /* means the SB neighbor has been removed; (-4)=H, (-3)=1H, (-2)=D, (-1)=T */
3907 : : }
3908 : : }
3909 : : }
3910 : : }
3911 : : else
3912 : : {
3913 : : /* discovered a regular (not an explicit H) neighbor */
3914 [ - + ]: 10080 : if (new_at[i].sb_parity[0])
3915 : : {
3916 [ # # ]: 0 : if (num_OtherNeigh < MAX_NUM_STEREO_BONDS)
3917 : : {
3918 : 0 : new_OtherNeigh_order[num_OtherNeigh] = k + 1;
3919 : : }
3920 : 0 : num_OtherNeigh++; /* increment outside of if() to detect overflow */
3921 [ # # ]: 0 : if (val != k)
3922 : : {
3923 : : /* store new stereobond and sb-neighbor ordering numbers */
3924 [ # # # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_BONDS && new_at[i].sb_parity[m]; m++)
3925 : : {
3926 [ # # ]: 0 : if (k == (int)new_at[i].sb_ord[m])
3927 : 0 : new_at[i].sb_ord[m] = val;
3928 [ # # ]: 0 : else if (k == (int)new_at[i].sn_ord[m])
3929 : 0 : new_at[i].sn_ord[m] = val;
3930 : : }
3931 : : }
3932 : : }
3933 : 10080 : new_at[i].neighbor[val] = new_ord[new_at[i].neighbor[k]];
3934 : 10080 : new_at[i].bond_type[val] = new_at[i].bond_type[k];
3935 : 10080 : new_at[i].bond_stereo[val] = new_at[i].bond_stereo[k];
3936 : 10080 : val++;
3937 : : }
3938 : : }
3939 [ + + - + ]: 5042 : if (new_at[i].valence > val && new_at[i].sb_parity[0])
3940 : : {
3941 [ # # # # ]: 0 : if (num_HydrogenAt == new_at[i].valence - val && num_HydrogenAt + num_OtherNeigh <= MAXVAL)
3942 : : {
3943 : : /* recalculate parity so that it would describe neighbor sequence H,1H,D,T,neigh[0],neigh[1]... */
3944 : 0 : memmove(new_OtherNeigh_order + num_HydrogenAt, new_OtherNeigh_order, num_OtherNeigh * sizeof(new_OtherNeigh_order[0]));
3945 [ # # ]: 0 : for (k = 0, j = 1; k <= NUM_H_ISOTOPES; k++)
3946 : : {
3947 [ # # # # : 0 : if (new_HydrogenAt_order[k] && (num_HydrogenAt - j < MAXVAL) && (num_HydrogenAt - j >= 0)) /* djb-rwth: fixing buffer overruns */
# # ]
3948 : : {
3949 : 0 : new_OtherNeigh_order[num_HydrogenAt - j] = new_HydrogenAt_order[k];
3950 [ # # # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_BONDS && new_at[i].sb_parity[m]; m++)
3951 : : {
3952 [ # # ]: 0 : if ((int)new_at[i].sn_ord[m] == -(k + 1))
3953 : : {
3954 : 0 : new_at[i].sn_ord[m] = -j;
3955 : : /* negative means explicit H isotope ord are
3956 : : (contiguously) in front of the adjacency list */
3957 : : }
3958 : : }
3959 : 0 : j++;
3960 : : }
3961 : : }
3962 : : /* at this point new_OtherNeigh_order[] contains
3963 : : incremented old ordering numbers in new order */
3964 : 0 : k = insertions_sort_AT_RANK(new_OtherNeigh_order, num_HydrogenAt + num_OtherNeigh); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
3965 : : /* djb-rwth: removing redundant code */
3966 : : /*if ( k ) {*/
3967 : : /*
3968 : : for ( m = 0; m < MAX_NUM_STEREO_BONDS && new_at[i].sb_parity[m]; m ++ ) {
3969 : : if ( PARITY_WELL_DEF(new_at[i].sb_parity[m]) ) {
3970 : : if ( old_trans[m] != 2 - (4 + new_at[i].sn_ord[m] + new_at[i].sb_ord[m] + (new_at[i].sn_ord[m] > new_at[i].sb_ord[m]))%2 ) {
3971 : : new_at[i].sb_parity[m] = 3 - new_at[i].sb_parity[m];
3972 : : }
3973 : : }
3974 : : }
3975 : : */
3976 : : /*}*/
3977 : : }
3978 : : }
3979 : 5042 : new_at[i].valence = val;
3980 : : }
3981 : 8 : memcpy(at, new_at, sizeof(at[0]) * num_atoms);
3982 : 8 : ret = num_others;
3983 : : }
3984 : : else
3985 : : {
3986 : 1256 : ret = num_atoms;
3987 : : }
3988 : :
3989 : 1264 : exit_function:
3990 : :
3991 [ + - ]: 1264 : if (new_ord)
3992 : : {
3993 [ + - ]: 1264 : inchi_free(new_ord);
3994 : : }
3995 [ + - ]: 1264 : if (new_at)
3996 : : {
3997 [ + - ]: 1264 : inchi_free(new_at);
3998 : : }
3999 : :
4000 : 1264 : return ret;
4001 : : }
4002 : :
4003 : : /*#endif*/ /* } DISCONNECT_SALTS */
4004 : :
4005 : : typedef enum tagIonAtomType
4006 : : {
4007 : : IAT_H = 0,
4008 : : IAT_C,
4009 : : IAT_N,
4010 : : IAT_P,
4011 : : IAT_O,
4012 : : IAT_S,
4013 : : IAT_Se,
4014 : : IAT_Te,
4015 : : IAT_F,
4016 : : IAT_Cl,
4017 : : IAT_Br,
4018 : : IAT_I,
4019 : : IAT_MAX = 12
4020 : : } ION_ATOM_TYPE;
4021 : :
4022 : : /****************************************************************************/
4023 : 9255 : int get_iat_number(int el_number)
4024 : : {
4025 [ + + + + : 9255 : switch (el_number)
+ + + + +
+ + + + ]
4026 : : {
4027 : 56 : case EL_NUMBER_H:
4028 : 56 : return IAT_H;
4029 : 8699 : case EL_NUMBER_C:
4030 : 8699 : return IAT_C;
4031 : 25 : case EL_NUMBER_N:
4032 : 25 : return IAT_N;
4033 : 1 : case EL_NUMBER_P:
4034 : 1 : return IAT_P;
4035 : 201 : case EL_NUMBER_O:
4036 : 201 : return IAT_O;
4037 : 3 : case EL_NUMBER_S:
4038 : 3 : return IAT_S;
4039 : 1 : case EL_NUMBER_SE:
4040 : 1 : return IAT_Se;
4041 : 1 : case EL_NUMBER_TE:
4042 : 1 : return IAT_Te;
4043 : 14 : case EL_NUMBER_F:
4044 : 14 : return IAT_F;
4045 : 102 : case EL_NUMBER_CL:
4046 : 102 : return IAT_Cl;
4047 : 18 : case EL_NUMBER_BR:
4048 : 18 : return IAT_Br;
4049 : 1 : case EL_NUMBER_I:
4050 : 1 : return IAT_I;
4051 : 133 : default:
4052 : 133 : return -1;
4053 : : }
4054 : : }
4055 : :
4056 : : /*****************************************************************************
4057 : : * (@nnuk : Nauman Ullah Khan)
4058 : : * Array only to be used for molecular inorganics. It is seperate from the ElData
4059 : : * array in util.c file. The need for duplicate array arrives as in molecular inorganics
4060 : : * we need a change in metal type. Some changes include an upgrade from lower metal
4061 : : * type to higher metal type (meaning : more valences of an atom can be used).
4062 : : * Some changes include a degradation from higher metal type to a lower metal type
4063 : : * where fewer number of valences for an atom are used. Due to these changes there
4064 : : * is an urge to have a duplicate array so that the current functionality does
4065 : : * not get affected.
4066 : : *
4067 : : * (Summary) : Type of the element has changed from METAL -> MolInOrg.
4068 : : ******************************************************************************/
4069 : :
4070 : : /****************************************************************************
4071 : : * (@nnuk : Nauman Ullah Khan)
4072 : : * Function retrieves the valence values from the MolecularInoragnicsArray
4073 : : ****************************************************************************/
4074 : 58 : int getElValenceforMolecularInorganics(int nPeriodicNum, int charge, int val_num)
4075 : : {
4076 [ + - + - : 58 : if (charge < MIN_ATOM_CHARGE || charge > MAX_ATOM_CHARGE || val_num >= MAX_NUM_VALENCES)
- + ]
4077 : : {
4078 : 0 : return 0;
4079 : : }
4080 : :
4081 [ + - ]: 58 : return (int) MolecularInorganicsArray[nPeriodicNum > 1 ? nPeriodicNum + 1 : 0].cValence[NEUTRAL_STATE + charge][val_num];
4082 : : }
4083 : :
4084 : : /****************************************************************************
4085 : : * (@nnuk : Nauman Ullah Khan)
4086 : : * Function retrieves the element type value from the MolecularInorganicsArray
4087 : : ****************************************************************************/
4088 : 9009 : int getElTypeforMolecularInorganics(int nPeriodicNum)
4089 : : {
4090 : 9009 : return MolecularInorganicsArray[nPeriodicNum + 1].nType;
4091 : : }
4092 : :
4093 : : /*****************************************************************************
4094 : : * (@nnuk : Nauman Ullah Khan)
4095 : : * Function check for molecular inorganics if a metal atom has to be disconnected
4096 : : * or not.
4097 : : ******************************************************************************/
4098 : 43 : int MolecularInorganicsIsMetalToDisconnect(inp_ATOM *at, int atom_idx)
4099 : : {
4100 : :
4101 : : int at_valence, num_H;
4102 : 43 : int type = getElTypeforMolecularInorganics(at[atom_idx].el_number);
4103 : : int j;
4104 : :
4105 : : /* Calculate the actual valence of the atom
4106 : : * It means that the total bounding capacity
4107 : : * of the atom has to be taken into account
4108 : : * which will further get equated within the
4109 : : * elements array to a metal atom(neutral or an ion) */
4110 : 43 : num_H = NUMH(at, atom_idx);
4111 : 43 : at_valence = num_H + at[atom_idx].chem_bonds_valence;
4112 : :
4113 [ + + + + ]: 43 : if (!at_valence || (abs(at[atom_idx].charge) > 1))
4114 : : {
4115 : 4 : return 0; /* Isolated metal atom or Atom has multiple charges */
4116 : : }
4117 : :
4118 : : /* Check if the valence matches a known normal valence */
4119 [ + + ]: 79 : for (j = 0; j < 4; j++)
4120 : : {
4121 : 73 : const int bitmask = (1 << j); /* Create bitmask for the j-th bit */
4122 : 73 : const bool is_valence_state_valid = (bitmask & type) != 0; /* Check if the j-th valence state is allowed */
4123 : :
4124 [ + + + + ]: 73 : if (is_valence_state_valid && at_valence == getElValenceforMolecularInorganics(at[atom_idx].el_number, at[atom_idx].charge, j))
4125 : : {
4126 : 33 : return 1; /* Atom has normal valence follows disconnection logic if applicable */
4127 : : }
4128 : : }
4129 : :
4130 : : /* Default: Valence greater than usual or the atom is isolated hence no disconnection */
4131 : 6 : return 0;
4132 : : }
4133 : :
4134 : : /*****************************************************************************
4135 : : * (@nnuk : Nauman Ullah Khan)
4136 : : * Function to retrieve binary values for MolecularInorganicsPreprocessing
4137 : : * Binary values are retrieved from the disconnection array "binaryArrayMolecularInorganics"
4138 : : * "1" means that the bond between atom and the neighbouring atom has to be
4139 : : * disconnected whereas, "0" means the bond has to be kept.
4140 : : ******************************************************************************/
4141 : :
4142 : 65 : int shouldBondBeCut(int atom1, int atom2)
4143 : : {
4144 : : int index1, index2, binaryValue;
4145 : :
4146 : : /* Bounds-check the 1-based periodic numbers before indexing the
4147 : : * NUM_ELEMENTS x NUM_ELEMENTS table. An out-of-range value (e.g. a
4148 : : * pseudo-atom with el_number 0, or a number beyond the table) would
4149 : : * otherwise read out of bounds. Default to 0 = keep the bond. */
4150 [ + - + - : 65 : if (atom1 < 1 || atom1 > NUM_ELEMENTS || atom2 < 1 || atom2 > NUM_ELEMENTS)
+ - - + ]
4151 : : {
4152 : 0 : return 0;
4153 : : }
4154 : :
4155 : : /* Get the indices corresponding to the atomic numbers */
4156 : 65 : index1 = atom1 - 1;
4157 : 65 : index2 = atom2 - 1;
4158 : :
4159 : : /* Retrieve the binary value from the binary array */
4160 : 65 : binaryValue = binaryArrayMolecularInorganics[index1][index2];
4161 : :
4162 : : /* Output for debugging purposes */
4163 : : /*printf("Electronegativity binary value between the atom and it's neighbour is: %d\n", binaryValue); */
4164 : :
4165 : 65 : return binaryValue;
4166 : : }
4167 : :
4168 : : /*****************************************************************************
4169 : : * (@nnuk : Nauman Ullah Khan)
4170 : : * Function to update the neighboring list of the atom for MolecularInorganicsPreprocessing.
4171 : : ******************************************************************************/
4172 : :
4173 : 78 : void updateNeighborListMolecularInorganics(inp_ATOM *at, int atom_idx, int neighbor_idx)
4174 : : {
4175 : :
4176 : 78 : int valence = at[atom_idx].valence;
4177 : : int i, j;
4178 : :
4179 [ + + ]: 142 : for (i = 0; i < valence; i++)
4180 : : {
4181 [ - + ]: 64 : if (at[atom_idx].neighbor[i] == neighbor_idx)
4182 : : {
4183 : : /* Shift remaining neighbors to fill the gap */
4184 [ # # ]: 0 : for (j = i; j < valence - 1; j++)
4185 : : {
4186 : 0 : at[atom_idx].neighbor[j] = at[atom_idx].neighbor[j + 1];
4187 : : }
4188 : 0 : at[atom_idx].valence--; /* Reduce the valence (number of neighbors) */
4189 : :
4190 : 0 : break;
4191 : : }
4192 : : }
4193 : 78 : }
4194 : :
4195 : : /*****************************************************************************
4196 : : * Convert coordinative (type 9) bonds into normal single bonds.
4197 : : *
4198 : : * A coordinative bond is the zero-order, charge-separated equivalent of a
4199 : : * single bond: the two bonded atoms carry equal and opposite formal charges
4200 : : * (e.g. M(+) ... L(-)). Realizing it as a single bond turns the donated lone
4201 : : * pair into the shared bonding pair, so the +/- charges cancel. This routine
4202 : : * performs that change in place: every COORDINATIVE_BOND becomes
4203 : : * BOND_TYPE_SINGLE and, when the two atoms carry opposite-sign charges, each is
4204 : : * moved one step toward neutral (one charge pair neutralized per bond).
4205 : : *
4206 : : * Only type-9 bonds are touched. A plain single bond is left alone even when
4207 : : * its endpoints carry opposite formal charges: there the charges are an
4208 : : * intrinsic part of the single-bond Lewis structure, not the zero-order
4209 : : * artifact of a dative bond, so cancelling them would corrupt a self-consistent
4210 : : * depiction (and leave the atoms at unusual valences).
4211 : : *
4212 : : * Each undirected bond is processed exactly once, from its lower-indexed
4213 : : * endpoint (the j > i guard), so a multiply-charged pair (e.g. M(2+)-L(2-))
4214 : : * cannot have its charges cancelled once from each stored half-bond.
4215 : : *****************************************************************************/
4216 : 187 : static void ConvertCoordinativeBondsToSingle(inp_ATOM *at, int num_atoms)
4217 : : {
4218 : : int i, k, k2, j;
4219 : :
4220 [ + + ]: 9432 : for (i = 0; i < num_atoms; i++)
4221 : : {
4222 [ + + ]: 25405 : for (k = 0; k < at[i].valence; k++)
4223 : : {
4224 : 16160 : j = at[i].neighbor[k];
4225 [ + - - + ]: 16160 : if (j < 0 || j >= num_atoms)
4226 : : {
4227 : 0 : continue;
4228 : : }
4229 : :
4230 : : /* Visit each undirected bond once, from the lower-indexed atom, so
4231 : : * a multiply-charged pair cannot be neutralized once from each end. */
4232 [ + + ]: 16160 : if (j <= i)
4233 : : {
4234 : 8080 : continue;
4235 : : }
4236 : :
4237 : : /* Only coordinative (type 9) bonds are converted; plain single
4238 : : * bonds keep their intrinsic formal charges untouched. */
4239 [ + + ]: 8080 : if (at[i].bond_type[k] != COORDINATIVE_BOND)
4240 : : {
4241 : 8064 : continue;
4242 : : }
4243 : :
4244 : : /* Realize the bond as single on both endpoints so the rest of the
4245 : : * pipeline sees a plain single bond from either atom. */
4246 : 16 : at[i].bond_type[k] = BOND_TYPE_SINGLE;
4247 : :
4248 [ + - ]: 40 : for (k2 = 0; k2 < at[j].valence; k2++)
4249 : : {
4250 [ + + ]: 40 : if (at[j].neighbor[k2] == i)
4251 : : {
4252 : 16 : at[j].bond_type[k2] = BOND_TYPE_SINGLE;
4253 : 16 : break;
4254 : : }
4255 : : }
4256 : :
4257 : : /* Cancel the paired +/- charges: the lone pair becomes the bonding
4258 : : * pair, so each atom moves one unit toward neutral. */
4259 [ - + - - ]: 16 : if (at[i].charge > 0 && at[j].charge < 0)
4260 : : {
4261 : 0 : at[i].charge--;
4262 : 0 : at[j].charge++;
4263 : : }
4264 [ + + - + ]: 16 : else if (at[i].charge < 0 && at[j].charge > 0)
4265 : : {
4266 : 0 : at[i].charge++;
4267 : 0 : at[j].charge--;
4268 : : }
4269 : : }
4270 : : }
4271 : 187 : }
4272 : :
4273 : : /*****************************************************************************
4274 : : * @nnuk
4275 : : * Determine whether an element is a Group 1 or Group 2 metal.
4276 : : *
4277 : : * Hydrogen is excluded because it is not treated as a metal by the
4278 : : * Molecular Inorganics preprocessing.
4279 : : *
4280 : : * @param el_number number in the periodic table
4281 : : *
4282 : : * @return 1 or 0
4283 : : *****************************************************************************/
4284 : 109 : static int MolecularInorganicsIsGroup1or2Metal(int el_number)
4285 : : {
4286 [ + + ]: 109 : switch (el_number)
4287 : : {
4288 : : /* Group 1 */
4289 : 40 : case 3: /* Li */
4290 : : case 11: /* Na */
4291 : : case 19: /* K */
4292 : : case 37: /* Rb */
4293 : : case 55: /* Cs */
4294 : : case 87: /* Fr */
4295 : :
4296 : : /* Group 2 */
4297 : : case 4: /* Be */
4298 : : case 12: /* Mg */
4299 : : case 20: /* Ca */
4300 : : case 38: /* Sr */
4301 : : case 56: /* Ba */
4302 : : case 88: /* Ra */
4303 : 40 : return 1;
4304 : :
4305 : 69 : default:
4306 : 69 : return 0;
4307 : : }
4308 : : }
4309 : :
4310 : : /*****************************************************************************
4311 : : * @brief Label every atom with its fragment (connected component) and
4312 : : * perceive ring systems in every fragment that contains a metal.
4313 : : *
4314 : : * MarkRingSystemsInp() walks only the fragment that contains its start atom,
4315 : : * numbers ring systems from 1 on every call, and allocates work arrays for
4316 : : * all num_at atoms. It is therefore called once per fragment, and only for
4317 : : * fragments with a metal: the chelate check compares a metal's ring system
4318 : : * with atoms of its own fragment only. Atoms of metal-free fragments keep
4319 : : * nRingSystem 0, which never equals a perceived ID (those start at 1). The
4320 : : * ring-system IDs of each perceived fragment are shifted so that they stay
4321 : : * unique across the input (issue #305). nBlockSystem is not shifted;
4322 : : * Molecular Inorganics does not read it.
4323 : : *
4324 : : * @param at input atom array
4325 : : * @param num_at number of atoms
4326 : : * @param fragment output: fragment index of each atom (num_at elements)
4327 : : * @param queue work array (num_at elements)
4328 : : *
4329 : : * @return Number of fragments, or a negative value on error.
4330 : : *****************************************************************************/
4331 : 187 : static int MolecularInorganicsMarkFragments(inp_ATOM* at, int num_at, int* fragment, int* queue)
4332 : : {
4333 : : int start, head, tail, cur, n, nb, ret, has_metal;
4334 : 187 : int num_fragments = 0;
4335 : 187 : int ring_offset = 0;
4336 : :
4337 : 187 : UnMarkRingSystemsInp(at, num_at);
4338 : :
4339 [ + + ]: 9432 : for (start = 0; start < num_at; start++)
4340 : : {
4341 : 9245 : fragment[start] = -1;
4342 : : }
4343 : :
4344 [ + + ]: 9432 : for (start = 0; start < num_at; start++)
4345 : : {
4346 [ + + ]: 9245 : if (fragment[start] >= 0)
4347 : : {
4348 : 8018 : continue;
4349 : : }
4350 : :
4351 : : /* Collect the atoms of this fragment in queue[0..tail). */
4352 : 1227 : head = tail = 0;
4353 : 1227 : queue[tail++] = start;
4354 : 1227 : fragment[start] = num_fragments;
4355 : 1227 : has_metal = 0;
4356 : :
4357 [ + + ]: 10472 : while (head < tail)
4358 : : {
4359 : 9245 : cur = queue[head++];
4360 [ + + ]: 9245 : if (is_el_a_metal(at[cur].el_number))
4361 : : {
4362 : 78 : has_metal = 1;
4363 : : }
4364 : :
4365 [ + + ]: 25405 : for (n = 0; n < at[cur].valence; n++)
4366 : : {
4367 : 16160 : nb = at[cur].neighbor[n];
4368 : :
4369 [ + - + - : 16160 : if (nb < 0 || nb >= num_at || fragment[nb] >= 0)
+ + ]
4370 : : {
4371 : 8142 : continue;
4372 : : }
4373 : :
4374 : 8018 : fragment[nb] = num_fragments;
4375 : 8018 : queue[tail++] = nb;
4376 : : }
4377 : : }
4378 : :
4379 [ + + ]: 1227 : if (has_metal)
4380 : : {
4381 : 74 : ret = MarkRingSystemsInp(at, num_at, start);
4382 [ - + ]: 74 : if (ret < 0)
4383 : : {
4384 : 0 : return ret;
4385 : : }
4386 : :
4387 [ + + ]: 384 : for (head = 0; head < tail; head++)
4388 : : {
4389 : 310 : at[queue[head]].nRingSystem += (AT_NUMB)ring_offset;
4390 : : }
4391 : :
4392 : 74 : ring_offset += ret;
4393 : : }
4394 : :
4395 : 1227 : num_fragments++;
4396 : : }
4397 : :
4398 : 187 : return num_fragments;
4399 : : }
4400 : :
4401 : : /*****************************************************************************
4402 : : * @nnuk
4403 : : * @brief Count C, N, and O atoms in the metal's perceived ring system.
4404 : : *
4405 : : * Implements the chelate-ring criterion from the 2026-07-16 Molecular
4406 : : * Inorganics decision tree. Group 1 and Group 2 metals are excluded and
4407 : : * therefore return zero.
4408 : : *
4409 : : * @param at Input atom array
4410 : : * @param num_at atom number
4411 : : * @param metal_idx index of the metal atom
4412 : : *
4413 : : * @return Number of C, N, and O atoms in the same ring system, or zero when
4414 : : * the metal is in Group 1 or Group 2 or is not part of a ring system.
4415 : : *****************************************************************************/
4416 : 43 : static int MolecularInorganicsCountRingCNOAtoms(const inp_ATOM* at, int num_at, int metal_idx)
4417 : : {
4418 : : int atom_idx;
4419 : 43 : int ring_cno_count = 0;
4420 : : AT_NUMB ring_id;
4421 : :
4422 [ + + + - ]: 43 : if (!MolecularInorganicsIsGroup1or2Metal(at[metal_idx].el_number) && at[metal_idx].nRingSystem)
4423 : : {
4424 : 28 : ring_id = at[metal_idx].nRingSystem;
4425 : :
4426 [ + + ]: 310 : for (atom_idx = 0; atom_idx < num_at; atom_idx++)
4427 : : {
4428 [ + + ]: 282 : if (at[atom_idx].nRingSystem == ring_id &&
4429 [ + + ]: 84 : (at[atom_idx].el_number == 6 || /* C */
4430 [ + - ]: 44 : at[atom_idx].el_number == 7 || /* N */
4431 [ + + ]: 44 : at[atom_idx].el_number == 8)) /* O */
4432 : : {
4433 : 56 : ring_cno_count++;
4434 : : }
4435 : : }
4436 : : }
4437 : :
4438 : 43 : return ring_cno_count;
4439 : : }
4440 : :
4441 : :
4442 : : /*****************************************************************************
4443 : : * @nnuk
4444 : : * Determine whether an input bond has stereochemical direction information
4445 : : * stored on either endpoint of the bond.
4446 : : *
4447 : : * @param at input atom array
4448 : : * @param atom_idx index of the atom
4449 : : * @param bond_pos bond position
4450 : : *
4451 : : * @return if bond has stereo or not
4452 : : *****************************************************************************/
4453 : 62 : static int MolecularInorganicsBondHasStereo(const inp_ATOM* at, int atom_idx, int bond_pos)
4454 : : {
4455 : : int neigh_pos;
4456 : : int neighbor_idx;
4457 : : int bond_has_stereo;
4458 : :
4459 : 62 : neighbor_idx = at[atom_idx].neighbor[bond_pos];
4460 : 62 : bond_has_stereo = (at[atom_idx].bond_stereo[bond_pos] != 0);
4461 : :
4462 [ + + + - ]: 80 : for (neigh_pos = 0; !bond_has_stereo && neigh_pos < at[neighbor_idx].valence; neigh_pos++)
4463 : : {
4464 [ + + ]: 79 : if (at[neighbor_idx].neighbor[neigh_pos] == atom_idx)
4465 : : {
4466 : 61 : bond_has_stereo = (at[neighbor_idx].bond_stereo[neigh_pos] != 0);
4467 : 61 : break;
4468 : : }
4469 : : }
4470 : :
4471 : 62 : return bond_has_stereo;
4472 : : }
4473 : :
4474 : : /*****************************************************************************
4475 : : * (@nnuk :: Nauman Ullah Khan)
4476 : : * @brief Function to preprocess molecular inorganics structures by disconnecting metal bonds and
4477 : : * handling salts + ammonium salts.
4478 : : *
4479 : : * This function processes a given molecular structure, identifies metal atoms,
4480 : : * and performs bond disconnections based on electronegativity differences,
4481 : : * coordination number and valence. After metal disconnections,
4482 : : * it also checks for ammonium salt patterns and disconnects them where applicable.
4483 : : *
4484 : : * @param orig_at_data Pointer to the original atom data structure.
4485 : : * @param ip Pointer to the input parameters structure.
4486 : : * @return Number of successful disconnections or -1 on error.
4487 : : *****************************************************************************/
4488 : :
4489 : 189 : int MolecularInorganicsPreprocessing(ORIG_ATOM_DATA *orig_at_data, INPUT_PARMS *ip)
4490 : : {
4491 : 189 : int ret_code = 0;
4492 : :
4493 [ + - + - : 189 : if (!ip || !orig_at_data || !orig_at_data->at)
- + ]
4494 : : {
4495 : 0 : return -1;
4496 : : }
4497 : :
4498 [ - + ]: 189 : if (!ip->bMolecularInorganics)
4499 : : {
4500 : 0 : return -1;
4501 : : }
4502 : :
4503 : 189 : ip->bMolecularInorganicsReconnectedInChI = 0; /*@nnuk: flag reset*/
4504 : :
4505 : : /* Pointer to the array of input atoms from the original atom data structure */
4506 : 189 : inp_ATOM *at = orig_at_data->at;
4507 : :
4508 : : /* Total number of input atoms present in the molecular structure */
4509 : 189 : int num_at = orig_at_data->num_inp_atoms;
4510 : :
4511 : : /* Pointer to the array storing the old component numbers for each atom, used for tracking connectivity */
4512 : 189 : AT_NUMB *nOldCompNumber = orig_at_data->nOldCompNumber;
4513 : :
4514 : : /* Track number of disconnections */
4515 : 189 : int num_disconnected = 0;
4516 : :
4517 : : /* variables declared */
4518 : : int i, j, n, k;
4519 : : int binaryValue;
4520 : : int disconnectDecision;
4521 : : int neigh_pos;
4522 : : int neighbor_idx;
4523 : : int num_metals;
4524 : : int has_single_bond_to_metal;
4525 : : int fragment_idx;
4526 : :
4527 : : /* memory allocation */
4528 : 189 : S_CHAR* dfs_visited = (S_CHAR*)inchi_calloc(num_at, sizeof(S_CHAR));
4529 : 189 : int* dfs_stack = (int*)inchi_malloc(num_at * sizeof(int));
4530 : : /* Per-fragment state: fragment index of each atom, whether the fragment
4531 : : * keeps its metal bonds, and whether it contains a non-terminal metal. */
4532 : 189 : int* fragment = (int*)inchi_malloc(num_at * sizeof(int));
4533 : 189 : S_CHAR* keep_fragment = (S_CHAR*)inchi_calloc(num_at, sizeof(S_CHAR));
4534 : 189 : S_CHAR* fragment_has_nonterminal_metal = (S_CHAR*)inchi_calloc(num_at, sizeof(S_CHAR));
4535 : :
4536 [ + - + - : 189 : if (!dfs_visited || !dfs_stack || !fragment || !keep_fragment || !fragment_has_nonterminal_metal)
+ - + - -
+ ]
4537 : : {
4538 : 0 : ret_code = -1;
4539 : 0 : goto cleanup;
4540 : : }
4541 : :
4542 : : /* Before disconnection, invoke ammonium salt functions */
4543 [ + + ]: 9438 : for (j = 0; j < num_at; j++)
4544 : : {
4545 : : int piO, pk;
4546 : : S_CHAR num_explicit_H[NUM_H_ISOTOPES + 1];
4547 : :
4548 [ - + ]: 9249 : if (bIsAmmoniumSalt(at, j, &piO, &pk, num_explicit_H))
4549 : : {
4550 : 0 : DisconnectAmmoniumSalt(at, j, piO, pk, num_explicit_H);
4551 : : }
4552 : : }
4553 : :
4554 : : /* Disconnect charge-separated metal-ligand bonds, preserving the drawn
4555 : : * formal charges. A metal and ligand carrying opposite formal charges
4556 : : * across a single (type 1) or coordinative (type 9) bond depict an ionic
4557 : : * interaction (e.g. Na(+) ... O(-)) and must split into the drawn ions
4558 : : * regardless of the metal's nominal valence or the presence of other
4559 : : * metals in the same component - the heuristics in the disconnection loop
4560 : : * below would otherwise keep these bonds connected. DisconnectInpAtBond
4561 : : * also decrements valence and chem_bonds_valence on both atoms (a type-9
4562 : : * bond counts as single), so no charge is added or removed here: each ion
4563 : : * keeps exactly the charge drawn in the input. Higher-order bonds (e.g. a
4564 : : * drawn M=O double bond) are genuine covalent bonds and are left intact.
4565 : : *
4566 : : * The pass is restricted to Group 1 and Group 2 metals. On any other metal
4567 : : * opposite formal charges usually depict oxidation states on a covalent
4568 : : * complex (e.g. Pt(4+) with Cl(-) in H2PtCl6, Al(3+) with H(-) in AlH4(-)),
4569 : : * and those bonds are left to the decision tree below. If the bond table
4570 : : * cuts that element pair (e.g. Ag(+)-F(-), likewise Cu/Hg/Tl-F), charged
4571 : : * and neutral drawings cannot be reconciled by either keeping or cutting
4572 : : * the bond, so the structure is rejected with an error (issue #304). */
4573 [ + + ]: 9434 : for (i = 0; i < num_at; i++)
4574 : : {
4575 [ + + ]: 25417 : for (k = 0; k < at[i].valence; )
4576 : : {
4577 : : int metal_idx, ligand_idx;
4578 : :
4579 : 16172 : j = at[i].neighbor[k];
4580 : :
4581 : : /* Process each undirected bond once, from its lower-indexed atom,
4582 : : * and only charge-separated metal/non-metal single or coordinative
4583 : : * bonds. */
4584 [ + + + - ]: 16172 : if (j <= i || j >= num_at ||
4585 [ + + ]: 8092 : (at[i].bond_type[k] != COORDINATIVE_BOND &&
4586 [ + + + + ]: 16102 : at[i].bond_type[k] != BOND_TYPE_SINGLE) ||
4587 : 8033 : is_el_a_metal(at[i].el_number) ==
4588 : 8033 : is_el_a_metal(at[j].el_number) ||
4589 [ + + + + ]: 123 : !((at[i].charge > 0 && at[j].charge < 0) ||
4590 [ + + + + ]: 102 : (at[i].charge < 0 && at[j].charge > 0)))
4591 : : {
4592 : 16149 : k++;
4593 : 16149 : continue;
4594 : : }
4595 : :
4596 [ + + ]: 23 : metal_idx = is_el_a_metal(at[i].el_number) ? i : j;
4597 [ + + ]: 23 : ligand_idx = (metal_idx == i) ? j : i;
4598 : :
4599 [ + + ]: 23 : if (!MolecularInorganicsIsGroup1or2Metal(at[metal_idx].el_number))
4600 : : {
4601 [ + + ]: 13 : if (shouldBondBeCut(at[metal_idx].el_number, at[ligand_idx].el_number) == 1)
4602 : : {
4603 : 2 : ret_code = MI_ERR_CHARGE_SEPARATED_NON_GROUP12;
4604 : 2 : goto cleanup;
4605 : : }
4606 : 11 : k++;
4607 : 11 : continue;
4608 : : }
4609 : :
4610 : 10 : DisconnectInpAtBond(at, nOldCompNumber, i, k);
4611 : 10 : num_disconnected++;
4612 : 10 : ip->bMolecularInorganicsReconnectedInChI = 1;
4613 : : }
4614 : : }
4615 : :
4616 : : /* Realize the remaining (uncharged) coordinative (type 9) bonds as single
4617 : : * bonds, so the rest of the pipeline treats them like the equivalent
4618 : : * single-bonded structure.
4619 : : */
4620 : 187 : ConvertCoordinativeBondsToSingle(at, num_at);
4621 : :
4622 : : /*
4623 : : * Find the fragments and perceive ring systems in every fragment with a
4624 : : * metal for the decision-tree chelate check, after the charge-separated bond
4625 : : * disconnections, because those graph changes may alter both.
4626 : : */
4627 [ - + ]: 187 : if (MolecularInorganicsMarkFragments(at, num_at, fragment, dfs_stack) < 0)
4628 : : {
4629 : 0 : ret_code = -1;
4630 : 0 : goto cleanup;
4631 : : }
4632 : :
4633 : : /* Compute the number of metals and, per fragment, whether every metal in
4634 : : * it is terminal. */
4635 : 187 : num_metals = 0;
4636 : :
4637 [ + + ]: 9432 : for (i = 0; i < num_at; i++)
4638 : : {
4639 [ + + ]: 9245 : if (is_el_a_metal(at[i].el_number))
4640 : : {
4641 : 78 : num_metals++;
4642 : :
4643 [ + + - + ]: 78 : if (at[i].valence != 1 || at[i].bond_type[0] != BOND_TYPE_SINGLE)
4644 : : {
4645 : 62 : fragment_has_nonterminal_metal[fragment[i]] = 1;
4646 : : }
4647 : : }
4648 : : }
4649 : :
4650 [ + + ]: 187 : if (!num_metals)
4651 : : {
4652 : 121 : goto cleanup;
4653 : : }
4654 : :
4655 : : /* Check whether at least one single bond remains attached to a metal. */
4656 : 66 : has_single_bond_to_metal = 0;
4657 : :
4658 [ + + + + ]: 216 : for (i = 0; i < num_at && !has_single_bond_to_metal; i++)
4659 : : {
4660 [ + + ]: 150 : if (!is_el_a_metal(at[i].el_number))
4661 : : {
4662 : 83 : continue;
4663 : : }
4664 : :
4665 [ + + ]: 67 : for (n = 0; n < at[i].valence; n++)
4666 : : {
4667 [ + - ]: 32 : if (at[i].bond_type[n] == BOND_TYPE_SINGLE)
4668 : : {
4669 : 32 : has_single_bond_to_metal = 1;
4670 : 32 : break;
4671 : : }
4672 : : }
4673 : : }
4674 : :
4675 [ + + ]: 66 : if (!has_single_bond_to_metal)
4676 : : {
4677 : 34 : goto cleanup;
4678 : : }
4679 : :
4680 : : /* Evaluate the complete decision tree before changing any remaining
4681 : : * metal-ligand bond. Each fragment is decided on its own: if one bond in a
4682 : : * fragment must be retained, all metal bonds in that fragment are
4683 : : * retained, and other fragments are not affected (issue #305). */
4684 [ + + ]: 321 : for (i = 0; i < num_at; i++)
4685 : : {
4686 : 289 : int ring_cno_count = 0;
4687 : : int current_metal_is_group_1_or_2;
4688 : :
4689 [ + + ]: 289 : if (!is_el_a_metal(at[i].el_number))
4690 : : {
4691 : 246 : continue;
4692 : : }
4693 : :
4694 : 43 : fragment_idx = fragment[i];
4695 : :
4696 [ - + ]: 43 : if (keep_fragment[fragment_idx])
4697 : : {
4698 : 0 : continue;
4699 : : }
4700 : :
4701 : 43 : current_metal_is_group_1_or_2 = MolecularInorganicsIsGroup1or2Metal(at[i].el_number);
4702 : :
4703 : : /*
4704 : : * Count C, N, and O atoms in the same ring system as the metal.
4705 : : * Group 1 and Group 2 metals are excluded from chelate protection.
4706 : : */
4707 : 43 : ring_cno_count = MolecularInorganicsCountRingCNOAtoms(at, num_at, i);
4708 : :
4709 : 43 : disconnectDecision = MolecularInorganicsIsMetalToDisconnect(at, i);
4710 : :
4711 [ + + ]: 83 : for (n = 0; n < at[i].valence; n++)
4712 : : {
4713 : 62 : int bond_has_stereo = 0;
4714 : 62 : int bond_links_to_other_metal = 0;
4715 : : int dfs_stack_size;
4716 : :
4717 : 62 : neighbor_idx = at[i].neighbor[n];
4718 : :
4719 [ + - - + ]: 62 : if (neighbor_idx < 0 || neighbor_idx >= num_at)
4720 : : {
4721 : 0 : continue;
4722 : : }
4723 : :
4724 : 62 : bond_has_stereo = MolecularInorganicsBondHasStereo(at, i, n);
4725 : :
4726 : : /* The actual bond must be a non-stereogenic single bond. */
4727 [ + + + + ]: 62 : if (at[i].bond_type[n] != BOND_TYPE_SINGLE || bond_has_stereo)
4728 : : {
4729 : 2 : keep_fragment[fragment_idx] = 1;
4730 : 2 : break;
4731 : : }
4732 : :
4733 [ + + ]: 60 : if (disconnectDecision != 1)
4734 : : {
4735 : 8 : keep_fragment[fragment_idx] = 1;
4736 : 8 : break;
4737 : : }
4738 : :
4739 : : /* The bonded atom pair must satisfy the binary disconnection table. */
4740 : 52 : binaryValue = shouldBondBeCut(at[i].el_number, at[neighbor_idx].el_number);
4741 : :
4742 [ + + ]: 52 : if (binaryValue != 1)
4743 : : {
4744 : 6 : keep_fragment[fragment_idx] = 1;
4745 : 6 : break;
4746 : : }
4747 : :
4748 : : /* Determine whether the actual bond is part of a path from this
4749 : : * metal atom to another metal atom. The current metal is excluded
4750 : : * from the traversal so that only paths starting through this bond
4751 : : * are considered. */
4752 : 46 : memset(dfs_visited, 0, num_at * sizeof(*dfs_visited));
4753 : 46 : dfs_stack_size = 0;
4754 : 46 : dfs_visited[i] = 1;
4755 : 46 : dfs_visited[neighbor_idx] = 1;
4756 : 46 : dfs_stack[dfs_stack_size++] = neighbor_idx;
4757 : :
4758 [ + + + - ]: 200 : while (dfs_stack_size && !bond_links_to_other_metal)
4759 : : {
4760 : 164 : int current_atom = dfs_stack[--dfs_stack_size];
4761 : :
4762 [ + + ]: 164 : if (is_el_a_metal(at[current_atom].el_number))
4763 : : {
4764 : 10 : bond_links_to_other_metal = 1;
4765 : 10 : break;
4766 : : }
4767 : :
4768 [ + + ]: 460 : for (neigh_pos = 0; neigh_pos < at[current_atom].valence; neigh_pos++)
4769 : : {
4770 : 306 : int neighbor_atom = at[current_atom].neighbor[neigh_pos];
4771 : :
4772 [ + - + - : 306 : if (neighbor_atom < 0 || neighbor_atom >= num_at || dfs_visited[neighbor_atom])
+ + ]
4773 : : {
4774 : 171 : continue;
4775 : : }
4776 : :
4777 : 135 : dfs_visited[neighbor_atom] = 1;
4778 : 135 : dfs_stack[dfs_stack_size++] = neighbor_atom;
4779 : : }
4780 : : }
4781 : :
4782 : : /*
4783 : : * Retain a linkage to another metal unless:
4784 : : * 1) all metal atoms in the fragment are terminal, or
4785 : : * 2) the current metal belongs to Group 1 or Group 2.
4786 : : */
4787 [ + + + + : 46 : if (bond_links_to_other_metal && fragment_has_nonterminal_metal[fragment_idx] && !current_metal_is_group_1_or_2)
- + ]
4788 : : {
4789 : 0 : keep_fragment[fragment_idx] = 1;
4790 : 0 : break;
4791 : : }
4792 : :
4793 : : /*
4794 : : * Protect a qualifying chelate ring. Group 1 and Group 2 metals return
4795 : : * zero from MolecularInorganicsCountRingCNOAtoms().
4796 : : */
4797 [ + + ]: 46 : if (ring_cno_count >= 3)
4798 : : {
4799 : 6 : keep_fragment[fragment_idx] = 1;
4800 : 6 : break;
4801 : : }
4802 : : }
4803 : : }
4804 : :
4805 : : /* All remaining metal bonds in a fragment that is not kept satisfy the
4806 : : * complete decision tree and may be disconnected.
4807 : : **/
4808 [ + + ]: 321 : for (i = 0; i < num_at; i++)
4809 : : {
4810 [ + + ]: 289 : if (!is_el_a_metal(at[i].el_number))
4811 : : {
4812 : 246 : continue;
4813 : : }
4814 : :
4815 [ + + ]: 43 : if (keep_fragment[fragment[i]])
4816 : : {
4817 : 22 : ip->bMolecularInorganicsReconnectedInChI = 1;
4818 : 22 : continue;
4819 : : }
4820 : :
4821 [ + + ]: 60 : for (n = at[i].valence - 1; n >= 0; n--)
4822 : : {
4823 : 39 : neighbor_idx = at[i].neighbor[n];
4824 : :
4825 [ + - - + ]: 39 : if (neighbor_idx < 0 || neighbor_idx >= num_at)
4826 : : {
4827 : 0 : continue;
4828 : : }
4829 : :
4830 : 39 : DisconnectInpAtBond(at, nOldCompNumber, i, n);
4831 : :
4832 : : /* Updating the metal as well as neighbor list */
4833 : 39 : updateNeighborListMolecularInorganics(at, i, neighbor_idx);
4834 : 39 : updateNeighborListMolecularInorganics(at, neighbor_idx, i);
4835 : :
4836 : 39 : at[i].charge += 1; /* Metal atom loses an electron -> +1 charge */
4837 : 39 : at[neighbor_idx].charge -= 1; /* Neighbor atom gains an electron -> -1 charge */
4838 : :
4839 : 39 : num_disconnected++;
4840 : : }
4841 : : }
4842 : :
4843 : 32 : cleanup:
4844 : : /* Leave the ring-system fields as they were found; later stages mark
4845 : : * ring systems themselves. */
4846 : 189 : UnMarkRingSystemsInp(at, num_at);
4847 : :
4848 [ + - + - ]: 189 : if (dfs_visited) inchi_free(dfs_visited);
4849 [ + - + - ]: 189 : if (dfs_stack) inchi_free(dfs_stack);
4850 [ + - + - ]: 189 : if (fragment) inchi_free(fragment);
4851 [ + - + - ]: 189 : if (keep_fragment) inchi_free(keep_fragment);
4852 [ + - + - ]: 189 : if (fragment_has_nonterminal_metal) inchi_free(fragment_has_nonterminal_metal);
4853 : :
4854 [ + + ]: 189 : return (ret_code < 0) ? ret_code : num_disconnected;
4855 : : }
4856 : :
4857 : : #if (READ_INCHI_STRING == 1)
4858 : :
4859 : : /****************************************************************************/
4860 : 0 : int bHeteroAtomMayHaveXchgIsoH(inp_ATOM *atom, int iat)
4861 : : {
4862 : 0 : inp_ATOM *at = atom + iat, *at2;
4863 : 0 : int j, val, is_H = 0, num_H, iat_numb, bAccept; /* djb-rwth: removing redundant variables */
4864 : :
4865 [ # # ]: 0 : if (0 > (iat_numb = get_iat_number(at->el_number)))
4866 : : {
4867 : 0 : return 0;
4868 : : }
4869 : :
4870 [ # # # # : 0 : if (abs(at->charge) > 1 || (at->radical && RADICAL_SINGLET != at->radical)) /* djb-rwth: addressing LLVM warning */
# # ]
4871 : : {
4872 : 0 : return 0;
4873 : : }
4874 : :
4875 : 0 : val = -1;
4876 [ # # # # : 0 : switch (iat_numb)
# ]
4877 : : {
4878 : 0 : case IAT_N:
4879 : : case IAT_P:
4880 : : /* djb-rwth: removing redundant code */
4881 : 0 : val = 3 + at->charge;
4882 : 0 : break;
4883 : :
4884 : 0 : case IAT_O:
4885 : : case IAT_S:
4886 : : case IAT_Se:
4887 : : case IAT_Te:
4888 : : /* djb-rwth: removing redundant code */
4889 : 0 : val = 2 + at->charge;
4890 : 0 : break;
4891 : :
4892 : 0 : case IAT_F:
4893 : : case IAT_Cl:
4894 : : case IAT_Br:
4895 : : case IAT_I:
4896 [ # # ]: 0 : if (at->charge == 0)
4897 : : {
4898 : : /* djb-rwth: removing redundant code */
4899 : 0 : val = 1;
4900 : : }
4901 : 0 : break;
4902 : :
4903 : 0 : case IAT_H:
4904 [ # # ]: 0 : if (at->valence == 0 &&
4905 [ # # ]: 0 : at->charge == 1)
4906 : : {
4907 : 0 : is_H = 1; /* isolated proton */
4908 : 0 : val = 0;
4909 : : }
4910 : : }
4911 [ # # ]: 0 : if (val < 0)
4912 : : {
4913 : 0 : return 0;
4914 : : }
4915 : 0 : num_H = NUMH(at, 0);
4916 [ # # ]: 0 : if (val != at->chem_bonds_valence + num_H)
4917 : : {
4918 : 0 : return 0;
4919 : : }
4920 [ # # ]: 0 : if (is_H)
4921 : : {
4922 : 0 : return 2; /* H atom */
4923 : : }
4924 : : else
4925 : : {
4926 : : /* djb-rwth: removing redundant code */
4927 [ # # # # ]: 0 : for (j = 0, bAccept = 1; j < at->valence && bAccept; j++)
4928 : : {
4929 : 0 : at2 = atom + (int)at->neighbor[j];
4930 [ # # # # ]: 0 : if ((at2->charge && at->charge) ||
4931 [ # # # # ]: 0 : (at2->radical && RADICAL_SINGLET != at2->radical)) /* djb-rwth: addressing LLVM warning */
4932 : : {
4933 : 0 : return 0; /* adjacent charged/radical atoms: do not neutralizate */
4934 : : }
4935 : : }
4936 : : }
4937 : :
4938 : 0 : return 1;
4939 : : }
4940 : :
4941 : : #endif
4942 : :
4943 : : /****************************************************************************/
4944 : 191 : int bNumHeterAtomHasIsotopicH(inp_ATOM *atom, int num_atoms)
4945 : : {
4946 : 191 : int i, j, val, is_H = 0, num_H, iat_numb, bAccept, num_iso_H, cur_num_iso_H, num_iso_atoms; /* djb-rwth: removing redundant variables */
4947 : : inp_ATOM *at, *at2;
4948 : :
4949 : 191 : num_iso_H = 0;
4950 : 191 : num_iso_atoms = 0;
4951 : :
4952 [ + + ]: 9446 : for (i = 0, at = atom; i < num_atoms; i++, at++)
4953 : : {
4954 : :
4955 [ + + - + ]: 9255 : num_iso_atoms += (at->iso_atw_diff != 0 || NUM_ISO_H(at, 0));
4956 : : /* isotopic atoms and implicit isotopic H */
4957 : :
4958 [ + + ]: 9255 : if (0 > (iat_numb = get_iat_number(at->el_number)))
4959 : : {
4960 : 133 : continue;
4961 : : }
4962 : :
4963 [ + + - + : 9122 : if (abs(at->charge) > 1 || (at->radical && RADICAL_SINGLET != at->radical)) /* djb-rwth: addressing LLVM warning */
- - ]
4964 : : {
4965 : 5 : continue;
4966 : : }
4967 : :
4968 : 9117 : val = -1;
4969 [ + + + + : 9117 : switch (iat_numb)
+ ]
4970 : : {
4971 : 26 : case IAT_N:
4972 : : case IAT_P:
4973 : : /* djb-rwth: removing redundant code */
4974 : 26 : val = 3 + at->charge;
4975 : 26 : break;
4976 : :
4977 : 201 : case IAT_O:
4978 : : case IAT_S:
4979 : : case IAT_Se:
4980 : : case IAT_Te:
4981 : : /* djb-rwth: removing redundant code */
4982 : 201 : val = 2 + at->charge;
4983 : 201 : break;
4984 : :
4985 : 135 : case IAT_F:
4986 : : case IAT_Cl:
4987 : : case IAT_Br:
4988 : : case IAT_I:
4989 [ + + ]: 135 : if (at->charge == 0)
4990 : : {
4991 : : /* djb-rwth: removing redundant code */
4992 : 123 : val = 1;
4993 : : }
4994 : 135 : break;
4995 : :
4996 : 56 : case IAT_H:
4997 [ + + ]: 56 : if (at->valence == 0 &&
4998 [ + - ]: 4 : at->charge == 1)
4999 : : {
5000 : 4 : is_H = 1; /* isolated proton */
5001 : 4 : val = 0;
5002 : : }
5003 : : }
5004 [ + + ]: 9117 : if (val < 0)
5005 : : {
5006 : 8763 : continue;
5007 : : }
5008 : :
5009 : 354 : num_H = NUMH(at, 0);
5010 [ + + ]: 354 : if (val != at->chem_bonds_valence + num_H)
5011 : : {
5012 : 5 : continue;
5013 : : }
5014 : :
5015 [ + + ]: 349 : if (is_H)
5016 : : {
5017 : 4 : bAccept = 1;
5018 : 4 : cur_num_iso_H = (at->iso_atw_diff != 0);
5019 : : }
5020 : : else
5021 : : {
5022 : 345 : cur_num_iso_H = 0;
5023 [ + + + - ]: 736 : for (j = 0, bAccept = 1; j < at->valence && bAccept; j++)
5024 : : {
5025 : 391 : at2 = atom + (int)at->neighbor[j];
5026 [ + + + - ]: 391 : if ((at2->charge && at->charge) ||
5027 [ - + - - ]: 391 : (at2->radical && RADICAL_SINGLET != at2->radical)) /* djb-rwth: addressing LLVM warning */
5028 : : {
5029 : 0 : bAccept = 0; /* adjacent charged/radical atoms: do not neutralizate */
5030 : 0 : break;
5031 : : }
5032 [ + + ]: 391 : else if (at2->el_number == EL_NUMBER_H &&
5033 [ + - - + ]: 2 : at2->valence == 1 && at2->iso_atw_diff)
5034 : : {
5035 : 0 : cur_num_iso_H++; /* isotopic explicit H */
5036 : : }
5037 : : }
5038 : :
5039 [ + - ]: 345 : if (bAccept)
5040 : : {
5041 : 345 : num_iso_atoms -= cur_num_iso_H; /* avoid counting explicit H as isotopic atom */
5042 : 345 : cur_num_iso_H += NUM_ISO_H(at, 0);
5043 : : }
5044 : : }
5045 : :
5046 [ + - + + ]: 349 : num_iso_H += (bAccept && cur_num_iso_H); /* number of acceptable heteroatoms that have isotopic H */
5047 : : }
5048 : :
5049 [ + + ]: 191 : return ((num_iso_H ? 1 : 0) | (num_iso_atoms ? 2 : 0));
5050 : : }
5051 : :
5052 : : /****************************************************/
5053 : : /* Mark and count disconnected structure components */
5054 : : /* by Depth-first searching each component */
5055 : : /****************************************************/
5056 : 5040 : int cmp_components(const void *a1, const void *a2)
5057 : : {
5058 : : int ret;
5059 : : AT_NUMB n1;
5060 : : AT_NUMB n2;
5061 : :
5062 : 5040 : n1 = ((const AT_NUMB *)a1)[0];
5063 : : /* number of atoms in the component -- descending order */
5064 : 5040 : n2 = ((const AT_NUMB *)a2)[0];
5065 : :
5066 [ + + ]: 5040 : if ((ret = (int)n2 - (int)n1)) /* djb-rwth: addressing LLVM warning */
5067 : : {
5068 : 59 : return ret;
5069 : : }
5070 : :
5071 : : /* stable sort */
5072 : 4981 : n1 = ((const AT_NUMB *)a1)[1];
5073 : : /* component ordering number -- ascending order */
5074 : 4981 : n2 = ((const AT_NUMB *)a2)[1];
5075 : :
5076 : 4981 : ret = (int)n1 - (int)n2;
5077 : :
5078 : 4981 : return ret;
5079 : : }
5080 : :
5081 : : /**
5082 : : * @brief Get the canonical atom number from original atom number
5083 : : *
5084 : : * @param aux Pointer to INChI auxiliary data
5085 : : * @param orig_atom_num Original atom number
5086 : : * @return Returns the canonical atom number, or -1 if not found
5087 : : */
5088 : 670 : int get_canonical_atom_number( const INChI_Aux *aux,
5089 : : int orig_atom_num)
5090 : : {
5091 [ - + ]: 670 : if (aux == NULL) {
5092 : 0 : return -1;
5093 : : }
5094 : :
5095 [ - + ]: 670 : if (aux->nOrigAtNosInCanonOrd == NULL) {
5096 : 0 : return -1;
5097 : : }
5098 : :
5099 [ + + ]: 7290 : for (int canon_num = 1; canon_num <= aux->nNumberOfAtoms; canon_num++) {
5100 [ + + ]: 6964 : if (aux->nOrigAtNosInCanonOrd[canon_num - 1] == orig_atom_num) {
5101 : 344 : return canon_num;
5102 : : }
5103 : : }
5104 : 326 : return -1;
5105 : : }
5106 : :
5107 : : /**
5108 : : * @brief Get the parity idx from canonical atom number object
5109 : : *
5110 : : * @param canon_atom_num Canonical atom number
5111 : : * @param nNumber Pointer to array of canonical atom numbers
5112 : : * @param nof_atoms Number of atoms
5113 : : * @return Returns the parity index, or -1 if not found
5114 : : */
5115 : 137 : int get_parity_idx_from_canonical_atom_number( int canon_atom_num,
5116 : : const AT_NUMB *nNumber,
5117 : : int nof_atoms)
5118 : : {
5119 [ - + ]: 137 : if (nNumber == NULL) {
5120 : 0 : return -1;
5121 : : }
5122 : :
5123 [ - + ]: 137 : if (nof_atoms <= 0) {
5124 : 0 : return -1;
5125 : : }
5126 : :
5127 [ + + ]: 425 : for (int i = 0; i < nof_atoms; i++) {
5128 [ + + ]: 418 : if (nNumber[i] == canon_atom_num) {
5129 : 130 : return i;
5130 : : }
5131 : : }
5132 : 7 : return -1;
5133 : : }
5134 : :
5135 : : /**
5136 : : * @brief Invert the parities for enhanced stereochemistry t- and m-layers
5137 : : *
5138 : : * @param inchi Pointer to INChI structure
5139 : : * @param aux Pointer to INChI auxiliary data
5140 : : * @param list_atoms Pointer to list of atom lists for abs, rel or rac information
5141 : : * @param nof_lists Number of lists
5142 : : * @param is_absolute Flag indicating if processing absolute stereochemistry
5143 : : * @return Returns 0 on success, 1 if list_atoms is NULL
5144 : : */
5145 : 153 : int invert_parities(const INChI *inchi,
5146 : : const INChI_Aux *aux,
5147 : : int **list_atoms,
5148 : : int nof_lists,
5149 : : int is_absolute)
5150 : : {
5151 : :
5152 : : // t - layer: parities
5153 : : // 2 = +
5154 : : // 1 = -
5155 : :
5156 : : // m - layer
5157 : : // -1 = m0
5158 : : // 1 = m1
5159 : :
5160 [ + + ]: 153 : if (list_atoms == NULL) {
5161 : 64 : return 1;
5162 : : }
5163 : :
5164 [ - + ]: 89 : if (nof_lists == 0)
5165 : : {
5166 : 0 : return 1;
5167 : : }
5168 : :
5169 : 89 : S_CHAR *t_parity = inchi->Stereo->t_parity;
5170 : :
5171 [ + + ]: 254 : for (int i = 0; i < nof_lists; i++) {
5172 : 165 : int nof_atoms = list_atoms[i][1];
5173 : :
5174 : 165 : AT_NUMB min_c_atom_num = (AT_NUMB)INT_MAX;
5175 [ + + ]: 478 : for (int j = 0; j < nof_atoms; j++) {
5176 : 313 : int orig_atom_num = list_atoms[i][2 + j];
5177 : 313 : AT_NUMB canon_atom_num = (AT_NUMB)get_canonical_atom_number(aux, orig_atom_num);
5178 [ + + ]: 313 : if (canon_atom_num < min_c_atom_num) {
5179 : 153 : min_c_atom_num = canon_atom_num;
5180 : : }
5181 : : }
5182 [ + + ]: 165 : if (min_c_atom_num == (AT_NUMB)INT_MAX) {
5183 : 53 : continue;
5184 : : }
5185 : 112 : int min_c_parity_idx = get_parity_idx_from_canonical_atom_number(min_c_atom_num,
5186 : 112 : inchi->Stereo->nNumber,
5187 : 112 : inchi->Stereo->nNumberOfStereoCenters);
5188 : :
5189 [ + + ]: 112 : if (min_c_parity_idx == -1) {
5190 : 3 : continue;
5191 : : }
5192 : :
5193 : 109 : int min_c_atom_parity = t_parity[min_c_parity_idx];
5194 [ + + ]: 109 : if (min_c_atom_parity == 2) {
5195 : :
5196 [ + + ]: 38 : for (int j = 0; j < nof_atoms; j++) {
5197 : 25 : int orig_atom_num = list_atoms[i][2 + j];
5198 : 25 : AT_NUMB canon_atom_num = (AT_NUMB)get_canonical_atom_number(aux, orig_atom_num);
5199 : 25 : int parity_idx = get_parity_idx_from_canonical_atom_number(canon_atom_num,
5200 : 25 : inchi->Stereo->nNumber,
5201 : 25 : inchi->Stereo->nNumberOfStereoCenters);
5202 : :
5203 [ + + ]: 25 : if (parity_idx == -1) {
5204 : 4 : continue;
5205 : : }
5206 : :
5207 [ + + ]: 21 : if (t_parity[parity_idx] == 2) {
5208 : 19 : t_parity[parity_idx] = 1;
5209 [ + - ]: 2 : } else if(t_parity[parity_idx] == 1) {
5210 : 2 : t_parity[parity_idx] = 2;
5211 : : }
5212 : : }
5213 : :
5214 [ + + ]: 13 : if (is_absolute) {
5215 : 2 : inchi->Stereo->nCompInv2Abs = -1; //m1
5216 : : }
5217 : : }
5218 : : }
5219 : 89 : return 0;
5220 : : }
5221 : :
5222 : : /**
5223 : : * @brief Set the enhanced stereochemistry information for t- and m-layers
5224 : : *
5225 : : * @param orig_inp_data Pointer to original input atom data
5226 : : * @param inchi Pointer to INChI structure
5227 : : * @param aux Pointer to INChI auxiliary data
5228 : : * @return Retruns 1 if not V3000, otherwise 0
5229 : : */
5230 : 57 : int set_EnhancedStereo_t_m_layers( const ORIG_ATOM_DATA *orig_inp_data,
5231 : : const INChI *inchi,
5232 : : const INChI_Aux *aux)
5233 : : {
5234 : 57 : int ret = 0;
5235 : :
5236 : : /* no input structure (-InChI2InChI) or no V3000 collections */
5237 [ + - + + ]: 57 : if (!orig_inp_data || !orig_inp_data->v3000)
5238 : : {
5239 : 1 : return 1;
5240 : : }
5241 : :
5242 [ + - - + ]: 56 : if (inchi == NULL || aux == NULL)
5243 : : {
5244 : 0 : return 1;
5245 : : }
5246 : :
5247 [ + - ]: 56 : if (inchi->Stereo == NULL ||
5248 [ + - ]: 56 : inchi->Stereo->t_parity == NULL ||
5249 [ + - ]: 56 : inchi->Stereo->nNumber == NULL ||
5250 [ + + ]: 56 : inchi->Stereo->nNumberOfStereoCenters <= 0) {
5251 : 5 : return 1;
5252 : : }
5253 : :
5254 [ + - ]: 51 : if (aux->nOrigAtNosInCanonOrd == NULL ||
5255 [ - + ]: 51 : aux->nNumberOfAtoms <= 0) {
5256 : 0 : return 1;
5257 : : }
5258 : :
5259 : 51 : int ret_abs = invert_parities(inchi, aux, orig_inp_data->v3000->lists_steabs, orig_inp_data->v3000->n_steabs, 1);
5260 : 51 : int ret_rac = invert_parities(inchi, aux, orig_inp_data->v3000->lists_sterac, orig_inp_data->v3000->n_sterac, 0);
5261 : 51 : int ret_rel = invert_parities(inchi, aux, orig_inp_data->v3000->lists_sterel, orig_inp_data->v3000->n_sterel, 0);
5262 : :
5263 [ + + ]: 51 : if ((orig_inp_data->v3000->n_steabs == 0) &&
5264 [ + + ]: 21 : (orig_inp_data->v3000->n_sterel > 0 ||
5265 [ + + ]: 17 : orig_inp_data->v3000->n_sterac)) {
5266 : 15 : inchi->Stereo->nCompInv2Abs = 1; //m0
5267 : : }
5268 : :
5269 : 51 : return ret;
5270 : : }
5271 : :
5272 : : /****************************************************************************
5273 : : Set the (disconnected) component numbers in ORIG_ATOM_DATA 'at[*].component'
5274 : : NB: components are (stable) sorted by number of heavy atoms
5275 : : ****************************************************************************/
5276 : 190 : int MarkDisconnectedComponents(ORIG_ATOM_DATA *orig_at_data,
5277 : : int bProcessOldCompNumbers)
5278 : : {
5279 : : typedef AT_NUMB AT_TRIPLE[3];
5280 : :
5281 : 190 : inp_ATOM *at = orig_at_data->at;
5282 : 190 : int num_at = orig_at_data->num_inp_atoms;
5283 : 190 : AT_NUMB *nCurAtLen = NULL;
5284 : :
5285 : 190 : AT_NUMB *nNewCompNumber = NULL;
5286 : 190 : AT_NUMB *nPrevAtom = NULL;
5287 : 190 : S_CHAR *iNeigh = NULL;
5288 : :
5289 : 190 : AT_NUMB *nOldCompNumber = NULL;
5290 : : int i, j, num_components, ret;
5291 : : int new_comp_no;
5292 : : AT_NUMB old_comp_no, another_comp_no, no_component;
5293 : :
5294 : : /* component_nbr[i][0] = number of atoms in the component i-1
5295 : : * component_nbr[i][1] = original component number (id-1) = i
5296 : : * after sorting:
5297 : : * component_nbr[j][2] = new number of component #(component_nbr[i][1]+1)
5298 : : */
5299 : 190 : AT_TRIPLE *component_nbr = NULL;
5300 : : int fst_at, nxt_at, cur_at, cur_neq_fst; /* moved from below 2024-09-01 DT */
5301 : :
5302 : : /* initialize */
5303 [ + + - + ]: 190 : if (bProcessOldCompNumbers && !orig_at_data->nOldCompNumber)
5304 : : {
5305 : 0 : bProcessOldCompNumbers = 0;
5306 : : }
5307 : 190 : num_components = 0;
5308 : :
5309 : : /*
5310 : : for ( j = 0; j < num_at; j ++ )
5311 : : {
5312 : : at[j].component = 0;
5313 : : }
5314 : : */
5315 : :
5316 : 190 : ret = -1;
5317 [ - + ]: 190 : if (!num_at)
5318 : : {
5319 : 0 : return 0;
5320 : : }
5321 : :
5322 : 190 : nNewCompNumber = (AT_NUMB *)inchi_calloc(num_at, sizeof(nNewCompNumber[0]));
5323 : 190 : nPrevAtom = (AT_NUMB *)inchi_calloc(num_at, sizeof(nPrevAtom[0]));
5324 : 190 : iNeigh = (S_CHAR *)inchi_calloc(num_at, sizeof(iNeigh[0]));
5325 : :
5326 [ + - + - : 190 : if (!nNewCompNumber || !nPrevAtom || !iNeigh) /* nNewCompNumber: for non-recursive DFS only: */
- + ]
5327 : : {
5328 : 0 : goto exit_function;
5329 : : }
5330 : :
5331 : : /*printf("\nnum_at = %d\n", num_at);*/
5332 : :
5333 : : /* Mark and count; avoid deep DFS recursion: it may make verifying software unhappy */
5334 : : /* nNewCompNumber[i] will contain new component number for atoms at[i], i=0..num_at-1 */
5335 : :
5336 [ + + ]: 9441 : for (j = 0; j < num_at; j++)
5337 : : {
5338 [ + + ]: 9251 : if (!nNewCompNumber[j])
5339 : : {
5340 : : /* mark starting with at[j] */
5341 : 1266 : fst_at = 0;
5342 : 1266 : nxt_at = 0;
5343 : 1266 : cur_at = j;
5344 : 1266 : cur_neq_fst = 1;
5345 : 1266 : num_components++;
5346 : :
5347 : : /* first time at at[j] */
5348 : 1266 : fst_at = cur_at;
5349 : 1266 : nNewCompNumber[fst_at] = (AT_NUMB)num_components;
5350 : :
5351 : : /* find next neighbor */
5352 : : do
5353 : : {
5354 [ + + ]: 25337 : if (iNeigh[cur_at] < at[cur_at].valence)
5355 : : {
5356 : 16086 : int ineigh_incr = (int)iNeigh[cur_at];
5357 : 16086 : nxt_at = at[cur_at].neighbor[ineigh_incr];
5358 : 16086 : iNeigh[cur_at]++;
5359 : :
5360 [ + + ]: 16086 : if (!nNewCompNumber[nxt_at])
5361 : : {
5362 : : /* forward edge: found new atom */
5363 : 7985 : nNewCompNumber[nxt_at] = (AT_NUMB)num_components;
5364 : 7985 : nPrevAtom[nxt_at] = (AT_NUMB)cur_at;
5365 : 7985 : cur_at = nxt_at;
5366 : : }
5367 : : }
5368 [ + + ]: 9251 : else if (cur_at == fst_at)
5369 : : {
5370 : 1266 : cur_neq_fst = 0;
5371 : : /* break; done */
5372 : : }
5373 : : else
5374 : : {
5375 : 7985 : cur_at = nPrevAtom[cur_at]; /* retract */
5376 : : }
5377 [ + + ]: 25337 : } while (cur_neq_fst);
5378 : : }
5379 : : }
5380 : :
5381 [ + - ]: 190 : inchi_free(nPrevAtom);
5382 : 190 : nPrevAtom = NULL;
5383 [ + - ]: 190 : inchi_free(iNeigh);
5384 : 190 : iNeigh = NULL;
5385 : :
5386 : : /* Allocate more memory */
5387 : 190 : i = inchi_max(num_components, orig_at_data->num_components);
5388 : :
5389 : 190 : nCurAtLen = (AT_NUMB *)inchi_calloc((long long)num_components + 1, sizeof(nCurAtLen[0])); /* djb-rwth: cast operator added */
5390 : 190 : nOldCompNumber = (AT_NUMB *)inchi_calloc((long long)i + 1, sizeof(nOldCompNumber[0])); /* djb-rwth: cast operator added */
5391 : 190 : component_nbr = (AT_TRIPLE *)inchi_calloc((long long)num_components + 1, sizeof(component_nbr[0])); /* djb-rwth: cast operator added */
5392 : :
5393 [ + - + - : 190 : if (!nCurAtLen || !nOldCompNumber || !component_nbr)
- + ]
5394 : : {
5395 : 0 : goto exit_function;
5396 : : }
5397 : :
5398 : : /* Count atoms per component and renumber the components */
5399 [ + + ]: 1456 : for (i = 0; i < num_components; i++)
5400 : : {
5401 : 1266 : component_nbr[i][0] = 0; /* number of atoms in the component */
5402 : 1266 : component_nbr[i][1] = i; /* component ordering number */
5403 : : }
5404 : :
5405 [ + + ]: 9441 : for (j = 0; j < num_at; j++)
5406 : : {
5407 : 9251 : component_nbr[(int)nNewCompNumber[j] - 1][0]++; /* count atoms in each component */
5408 : : }
5409 : :
5410 : : /* Sort settings
5411 : : key: number of atoms
5412 : : order: descending
5413 : : stable sort
5414 : : */
5415 : :
5416 : 190 : qsort( (void*) component_nbr[0], num_components, sizeof( component_nbr[0] ), cmp_components ); /* djb-rwth: fixed buffer overrun */
5417 : :
5418 : : /* Invert the transposition */
5419 [ + + ]: 1456 : for (i = 0; i < num_components; i++)
5420 : : {
5421 : 1266 : nCurAtLen[i] = component_nbr[i][0];
5422 : 1266 : component_nbr[component_nbr[i][1]][2] = i + 1;
5423 : : }
5424 : :
5425 : : /* Renumber the components so that the component with the greatest number of atoms is the first */
5426 : 190 : no_component = num_at + 1;
5427 : :
5428 [ + + ]: 9441 : for (j = 0; j < num_at; j++)
5429 : : {
5430 : : /* new component number for at[j] */
5431 : 9251 : new_comp_no = component_nbr[(int)nNewCompNumber[j] - 1][2] - 1; /* starts from 0 */
5432 [ + + ]: 9251 : if (bProcessOldCompNumbers)
5433 : : {
5434 : : /* old component number for at[j] */
5435 : 2 : old_comp_no = at[j].component;
5436 : : /* fill out nOldCompNumber[]; initially it contains zeroes */
5437 [ - + ]: 2 : if (!old_comp_no)
5438 : : {
5439 : 0 : nOldCompNumber[new_comp_no] = no_component; /* atom did not have component number */
5440 : : }
5441 [ + - ]: 2 : else if (nOldCompNumber[new_comp_no] != old_comp_no)
5442 : : {
5443 [ + - ]: 2 : if (!nOldCompNumber[new_comp_no])
5444 : : {
5445 : 2 : nOldCompNumber[new_comp_no] = old_comp_no;
5446 : : }
5447 : : else
5448 : : {
5449 : : /* at[j] moved from old comp #old_comp_no to old comp #nOldCompNumber[new_comp_no]
5450 : : Both components cannot be equal to any current component */
5451 : 0 : another_comp_no = nOldCompNumber[new_comp_no];
5452 [ # # ]: 0 : for (i = 0; i < num_components; i++)
5453 : : {
5454 [ # # ]: 0 : if (nOldCompNumber[i] == old_comp_no ||
5455 [ # # ]: 0 : nOldCompNumber[i] == another_comp_no)
5456 : : {
5457 : 0 : nOldCompNumber[i] = no_component;
5458 : : }
5459 : : }
5460 : : /* nOldCompNumber[new_comp_no] = num_at+1; */
5461 : : }
5462 : : }
5463 : : }
5464 : :
5465 : : /* orig_at_data->nOldCompNumber */
5466 : :
5467 : : /* Finally, set the new component number for atom j (NB: starts from 1 ) */
5468 : 9251 : at[j].component = new_comp_no + 1;
5469 : : }
5470 : :
5471 [ + + ]: 190 : if (bProcessOldCompNumbers)
5472 : : {
5473 [ + + ]: 3 : for (j = 0; j < num_components; j++)
5474 : : {
5475 [ - + ]: 2 : if (nOldCompNumber[j] == no_component)
5476 : : {
5477 : : /* the component has atom from another component */
5478 : 0 : nOldCompNumber[j] = 0;
5479 : : }
5480 [ + - ]: 2 : else if (nOldCompNumber[j] &&
5481 [ + - ]: 2 : !orig_at_data->nOldCompNumber[nOldCompNumber[j] - 1])
5482 : : {
5483 : : /* the component has changed in the previous processing */
5484 : 2 : nOldCompNumber[j] = 0;
5485 : : }
5486 : : }
5487 : : }
5488 : : else
5489 : : {
5490 [ + + ]: 1453 : for (j = 0; j < num_components; j++)
5491 : : {
5492 : 1264 : nOldCompNumber[j] = j + 1;
5493 : : }
5494 : : }
5495 : :
5496 : 190 : ret = num_components;
5497 : :
5498 : 190 : exit_function:
5499 : :
5500 [ + - ]: 190 : if (nNewCompNumber)
5501 : : {
5502 [ + - ]: 190 : inchi_free(nNewCompNumber);
5503 : : }
5504 [ + - ]: 190 : if (component_nbr)
5505 : : {
5506 [ + - ]: 190 : inchi_free(component_nbr);
5507 : : }
5508 : :
5509 [ - + ]: 190 : if (ret < 0)
5510 : : {
5511 [ # # ]: 0 : if (nPrevAtom)
5512 : : {
5513 [ # # ]: 0 : inchi_free(nPrevAtom);
5514 : 0 : nPrevAtom = NULL;
5515 : : }
5516 [ # # ]: 0 : if (iNeigh)
5517 : : {
5518 [ # # ]: 0 : inchi_free(iNeigh);
5519 : 0 : iNeigh = NULL;
5520 : : }
5521 [ # # ]: 0 : if (nCurAtLen)
5522 : : {
5523 [ # # ]: 0 : inchi_free(nCurAtLen);
5524 : 0 : nCurAtLen = NULL;
5525 : : }
5526 [ # # ]: 0 : if (nOldCompNumber)
5527 : : {
5528 [ # # ]: 0 : inchi_free(nOldCompNumber);
5529 : 0 : nOldCompNumber = NULL;
5530 : : }
5531 : 0 : num_components = ret;
5532 : : }
5533 : :
5534 : : /* avoid memory leaks */
5535 [ + - ]: 190 : if (orig_at_data->nCurAtLen)
5536 : : {
5537 [ + - ]: 190 : inchi_free(orig_at_data->nCurAtLen);
5538 : : }
5539 [ + - ]: 190 : if (orig_at_data->nOldCompNumber)
5540 : : {
5541 [ + - ]: 190 : inchi_free(orig_at_data->nOldCompNumber);
5542 : : }
5543 : :
5544 : 190 : orig_at_data->nCurAtLen = nCurAtLen;
5545 : 190 : orig_at_data->nOldCompNumber = nOldCompNumber;
5546 : :
5547 : 190 : orig_at_data->num_components = num_components;
5548 : :
5549 : 190 : return ret; /* number of disconnected components;
5550 : : 1=>single connected structure */
5551 : : }
5552 : :
5553 : : /****************************************************************************
5554 : : Extract one (connected) component
5555 : : ****************************************************************************/
5556 : 1266 : int ExtractConnectedComponent(inp_ATOM *at,
5557 : : int num_at,
5558 : : int component_number,
5559 : : inp_ATOM *component_at)
5560 : : {
5561 : : int i, j, num_component_at;
5562 : : AT_NUMB *number;
5563 : :
5564 [ - + ]: 1266 : if (NULL == (number = (AT_NUMB *)inchi_calloc(num_at, sizeof(AT_NUMB))))
5565 : : {
5566 : 0 : return CT_OUT_OF_RAM; /* out of memory */ /* <BRKPT> */
5567 : : }
5568 : :
5569 : : /* copy atoms */
5570 [ + + ]: 1010629 : for (i = 0, num_component_at = 0; i < num_at; i++)
5571 : : {
5572 [ + + ]: 1009363 : if (at[i].component == component_number)
5573 : : {
5574 : 9250 : number[i] = num_component_at;
5575 : 9250 : component_at[num_component_at++] = at[i];
5576 : : }
5577 : : }
5578 : :
5579 : : /* renumber neighbors */
5580 [ + + ]: 10516 : for (i = 0; i < num_component_at; i++)
5581 : : {
5582 : 9250 : component_at[i].orig_compt_at_numb = (AT_NUMB)(i + 1);
5583 [ + + ]: 25334 : for (j = 0; j < component_at[i].valence; j++)
5584 : : {
5585 : 16084 : component_at[i].neighbor[j] = number[(int)component_at[i].neighbor[j]];
5586 : : }
5587 : : }
5588 : :
5589 [ + - ]: 1266 : inchi_free(number);
5590 : :
5591 : 1266 : return num_component_at;
5592 : : }
5593 : :
5594 : : /****************************************************************************/
5595 : 1269 : int SetConnectedComponentNumber(inp_ATOM *at, int num_at, int component_number)
5596 : : {
5597 : : int i;
5598 [ + + ]: 10527 : for (i = 0; i < num_at; i++)
5599 : : {
5600 : 9258 : at[i].component = (AT_NUMB)component_number;
5601 : : }
5602 : :
5603 : 1269 : return 0;
5604 : : }
5605 : :
5606 : : /****************************************************************************/
5607 : 2606 : int Free_INChI_Stereo(INChI_Stereo *pINChI_Stereo)
5608 : : {
5609 [ + + ]: 2606 : if (pINChI_Stereo)
5610 : : {
5611 [ + + + - ]: 1289 : qzfree(pINChI_Stereo->nNumber);
5612 [ + + + - ]: 1289 : qzfree(pINChI_Stereo->t_parity);
5613 [ + + + - ]: 1289 : qzfree(pINChI_Stereo->nNumberInv);
5614 [ + + + - ]: 1289 : qzfree(pINChI_Stereo->t_parityInv);
5615 [ + + + - ]: 1289 : qzfree(pINChI_Stereo->nBondAtom1);
5616 [ + + + - ]: 1289 : qzfree(pINChI_Stereo->nBondAtom2);
5617 [ + + + - ]: 1289 : qzfree(pINChI_Stereo->b_parity);
5618 : : }
5619 : :
5620 : 2606 : return 0;
5621 : : }
5622 : :
5623 : : /****************************************************************************/
5624 : 1280 : INChI_Stereo *Alloc_INChI_Stereo(int num_at, int num_bonds)
5625 : : {
5626 : :
5627 : : INChI_Stereo *pINChI_Stereo = (INChI_Stereo *)
5628 : 1280 : inchi_calloc(1, sizeof(INChI_Stereo));
5629 : :
5630 [ + - ]: 1280 : if (pINChI_Stereo)
5631 : : {
5632 [ + - ]: 1280 : if (num_at &&
5633 [ + - ]: 1280 : (pINChI_Stereo->nNumber = (AT_NUMB *)inchi_calloc(num_at, sizeof(pINChI_Stereo->nNumber[0]))) &&
5634 [ + - ]: 1280 : (pINChI_Stereo->t_parity = (S_CHAR *)inchi_calloc(num_at, sizeof(pINChI_Stereo->t_parity[0]))) &&
5635 [ + - ]: 1280 : (pINChI_Stereo->nNumberInv = (AT_NUMB *)inchi_calloc(num_at, sizeof(pINChI_Stereo->nNumberInv[0]))) &&
5636 [ + - ]: 1280 : (pINChI_Stereo->t_parityInv = (S_CHAR *)inchi_calloc(num_at, sizeof(pINChI_Stereo->t_parityInv[0]))))
5637 : : {
5638 : : ;
5639 : : }
5640 [ # # ]: 0 : else if (num_at)
5641 : : {
5642 : 0 : goto out_of_RAM;
5643 : : }
5644 : :
5645 [ + + ]: 1280 : if (num_bonds &&
5646 [ + - ]: 160 : (pINChI_Stereo->nBondAtom1 = (AT_NUMB *)inchi_calloc(num_bonds, sizeof(pINChI_Stereo->nBondAtom1[0]))) &&
5647 [ + - ]: 160 : (pINChI_Stereo->nBondAtom2 = (AT_NUMB *)inchi_calloc(num_bonds, sizeof(pINChI_Stereo->nBondAtom2[0]))) &&
5648 [ + - ]: 160 : (pINChI_Stereo->b_parity = (S_CHAR *)inchi_calloc(num_bonds, sizeof(pINChI_Stereo->b_parity[0]))))
5649 : : {
5650 : : ;
5651 : : }
5652 [ - + ]: 1120 : else if (num_bonds)
5653 : : {
5654 : 0 : goto out_of_RAM;
5655 : : }
5656 : :
5657 : 1280 : return pINChI_Stereo;
5658 : :
5659 : 0 : out_of_RAM:
5660 : :
5661 : 0 : Free_INChI_Stereo(pINChI_Stereo);
5662 [ # # # # ]: 0 : qzfree(pINChI_Stereo);
5663 : : } /* if ( pINChI_Stereo ) */
5664 : :
5665 : 0 : return NULL;
5666 : : }
5667 : :
5668 : : /****************************************************************************/
5669 : 2578 : int Free_INChI(INChI **ppINChI)
5670 : : {
5671 : :
5672 : : INChI *pINChI;
5673 : :
5674 [ + + ]: 2578 : if ((pINChI = *ppINChI)) /* djb-rwth: addressing LLVM warning */
5675 : : {
5676 : :
5677 : : #if (bREUSE_INCHI == 1)
5678 [ - + ]: 1285 : if (pINChI->nRefCount-- > 0)
5679 : 0 : return 1;
5680 : : #endif
5681 : :
5682 : 1285 : Free_INChI_Members(pINChI);
5683 [ + - + - ]: 1285 : qzfree(pINChI);
5684 : 1285 : *ppINChI = NULL;
5685 : : }
5686 : :
5687 : 2578 : return 0;
5688 : : }
5689 : :
5690 : : /****************************************************************************/
5691 : 1303 : int Free_INChI_Members(INChI *pINChI)
5692 : : {
5693 [ + - ]: 1303 : if (pINChI)
5694 : : {
5695 : 1303 : Free_INChI_Stereo(pINChI->Stereo);
5696 : 1303 : Free_INChI_Stereo(pINChI->StereoIsotopic);
5697 [ + + + - ]: 1303 : qzfree(pINChI->nAtom);
5698 [ + + + - ]: 1303 : qzfree(pINChI->nConnTable);
5699 [ + + + - ]: 1303 : qzfree(pINChI->nTautomer);
5700 [ + + + - ]: 1303 : qzfree(pINChI->nNum_H);
5701 [ + + + - ]: 1303 : qzfree(pINChI->nNum_H_fixed);
5702 [ + + + - ]: 1303 : qzfree(pINChI->IsotopicAtom);
5703 [ + + + - ]: 1303 : qzfree(pINChI->IsotopicTGroup);
5704 [ + + + - ]: 1303 : qzfree(pINChI->nPossibleLocationsOfIsotopicH);
5705 [ + + + - ]: 1303 : qzfree(pINChI->Stereo);
5706 [ + + + - ]: 1303 : qzfree(pINChI->StereoIsotopic);
5707 [ + + + - ]: 1303 : qzfree(pINChI->szHillFormula);
5708 : : }
5709 : :
5710 : 1303 : return 0;
5711 : : }
5712 : :
5713 : : /****************************************************************************/
5714 : 1267 : INChI *Alloc_INChI(inp_ATOM *at,
5715 : : int num_at,
5716 : : int *found_num_bonds,
5717 : : int *found_num_isotopic,
5718 : : int nAllocMode)
5719 : : {
5720 : : int i, num_bonds, num_isotopic_atoms;
5721 : : INChI *pINChI;
5722 : 1267 : int bIsotopic = (nAllocMode & REQ_MODE_ISO);
5723 : : /* int bTautomeric = (nAllocMode & REQ_MODE_TAUT); */
5724 : :
5725 [ + - ]: 1267 : if (num_at <= 0 ||
5726 [ - + ]: 1267 : NULL == (pINChI = (INChI *)inchi_calloc(1, sizeof(INChI))))
5727 : : {
5728 : 0 : return NULL;
5729 : : }
5730 : :
5731 [ + + ]: 10522 : for (i = 0, num_bonds = 0, num_isotopic_atoms = 0; i < num_at; i++)
5732 : : {
5733 : 9255 : num_bonds += at[i].valence;
5734 : : /* if ( bIsotopic ) { */
5735 : 9255 : num_isotopic_atoms += (0 != at[i].iso_atw_diff ||
5736 [ + - ]: 9245 : !strcmp(at[i].elname, "D") ||
5737 [ + - ]: 9245 : !strcmp(at[i].elname, "T") ||
5738 [ + - ]: 9245 : at[i].num_iso_H[0] ||
5739 [ + + + - ]: 27745 : at[i].num_iso_H[1] ||
5740 [ - + ]: 9245 : at[i].num_iso_H[2]);
5741 : : /* } */
5742 : : }
5743 : 1267 : num_bonds /= 2;
5744 : :
5745 : 1267 : *found_num_bonds = num_bonds;
5746 : 1267 : *found_num_isotopic = num_isotopic_atoms;
5747 : :
5748 [ + - ]: 1267 : if ((pINChI->nAtom = (U_CHAR *)inchi_calloc(num_at, sizeof(pINChI->nAtom[0]))) &&
5749 [ + - ]: 1267 : (pINChI->nConnTable = (AT_NUMB *)inchi_calloc((long long)num_at + (long long)num_bonds, sizeof(pINChI->nConnTable[0]))) && /* djb-rwth: cast operator added */
5750 [ + - ]: 1267 : (pINChI->nTautomer = (AT_NUMB *)inchi_calloc(((3 + INCHI_T_NUM_MOVABLE) * (long long)num_at) / 2 + 1, sizeof(pINChI->nTautomer[0]))) && /* djb-rwth: cast operator added */
5751 [ + - ]: 1267 : (pINChI->nNum_H = (S_CHAR *)inchi_calloc(num_at, sizeof(pINChI->nNum_H[0]))) &&
5752 [ + - ]: 1267 : (pINChI->nNum_H_fixed = (S_CHAR *)inchi_calloc(num_at, sizeof(pINChI->nNum_H_fixed[0]))))
5753 : : {
5754 : : ;
5755 : : /* nTautomer length: max. number of tautomeric groups is num_at/2
5756 : :
5757 : : 1 word -> number of t-groups
5758 : :
5759 : : each group has:
5760 : :
5761 : : 1 word -> number of endpoints+INCHI_T_NUM_MOVABLE
5762 : : INCHI_T_NUM_MOVABLE words -> number(s) of moveable attachments
5763 : : numbers of endpoints words -> canon. numbers
5764 : :
5765 : : max. occurs if each t-group has 2 atoms (num_at/2 t-groups) and all atoms
5766 : : belong to t-groups (num_at endpoints)
5767 : :
5768 : : Total: 1 + (number of t-groups)*(1+INCHI_T_NUM_MOVABLE) + (number of endpoints) <=
5769 : : 1 + (num_at/2) * (1+INCHI_T_NUM_MOVABLE) + num_at <=
5770 : : 1 + (3+INCHI_T_NUM_MOVABLE)*num_at/2 words.
5771 : : */
5772 : : }
5773 : : else
5774 : : {
5775 : 0 : goto out_of_RAM;
5776 : : }
5777 : :
5778 : 1267 : pINChI->szHillFormula = NULL; /* the length is unknown */
5779 : :
5780 [ + + ]: 1267 : if (bIsotopic)
5781 : : {
5782 [ + + ]: 13 : if (num_isotopic_atoms &&
5783 [ + - ]: 10 : (pINChI->IsotopicAtom = (INChI_IsotopicAtom *)inchi_calloc(num_isotopic_atoms, sizeof(INChI_IsotopicAtom))) &&
5784 [ + - ]: 10 : (pINChI->IsotopicTGroup = (INChI_IsotopicTGroup *)inchi_calloc(num_isotopic_atoms, sizeof(INChI_IsotopicTGroup))))
5785 : : {
5786 : : ;
5787 : : }
5788 [ - + ]: 3 : else if (num_isotopic_atoms)
5789 : : {
5790 : 0 : goto out_of_RAM;
5791 : : }
5792 [ - + ]: 13 : if (!(pINChI->nPossibleLocationsOfIsotopicH = (AT_NUMB *)inchi_calloc((long long)num_at + 1, sizeof(pINChI->nPossibleLocationsOfIsotopicH[0])))) /* djb-rwth: cast operator added */
5793 : : {
5794 : 0 : goto out_of_RAM;
5795 : : }
5796 : : }
5797 : :
5798 [ - + ]: 1267 : if ((pINChI->Stereo = Alloc_INChI_Stereo(num_at, num_bonds)))
5799 : : {
5800 : : ;
5801 : : }
5802 : : else
5803 : : {
5804 : 0 : goto out_of_RAM;
5805 : : }
5806 : :
5807 [ + + ]: 1267 : if (bIsotopic)
5808 : : {
5809 [ - + ]: 13 : if ((pINChI->StereoIsotopic = Alloc_INChI_Stereo(num_at, num_bonds)))
5810 : : {
5811 : : ;
5812 : : }
5813 : : else
5814 : : {
5815 : 0 : goto out_of_RAM;
5816 : : }
5817 : : }
5818 : :
5819 : 1267 : return pINChI;
5820 : :
5821 : 0 : out_of_RAM:
5822 [ # # ]: 0 : if (pINChI)
5823 : : {
5824 : 0 : Free_INChI(&pINChI);
5825 : : /*
5826 : : inchi_free(pINChI);
5827 : : */
5828 : : }
5829 : :
5830 : 0 : return NULL;
5831 : : }
5832 : :
5833 : : /****************************************************************************/
5834 : 2578 : int Free_INChI_Aux(INChI_Aux **ppINChI_Aux)
5835 : : {
5836 : 2578 : INChI_Aux *pINChI_Aux = *ppINChI_Aux;
5837 [ + + ]: 2578 : if (pINChI_Aux)
5838 : : {
5839 : :
5840 : : #if (bREUSE_INCHI == 1)
5841 [ - + ]: 1285 : if (pINChI_Aux->nRefCount-- > 0)
5842 : 0 : return 1;
5843 : : #endif
5844 : :
5845 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nOrigAtNosInCanonOrd);
5846 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nIsotopicOrigAtNosInCanonOrd);
5847 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nOrigAtNosInCanonOrdInv);
5848 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nIsotopicOrigAtNosInCanonOrdInv);
5849 [ + + + - ]: 1285 : qzfree(pINChI_Aux->szOrigCoord);
5850 [ + + + - ]: 1285 : qzfree(pINChI_Aux->OrigInfo);
5851 : : /*
5852 : : qzfree( pINChI_Aux->nOriginalAtomNumber );
5853 : : qzfree( pINChI_Aux->nCanonicalTGroupNumbers );
5854 : : qzfree( pINChI_Aux->nIsotopicCanonicalTGroupNumbers);
5855 : : qzfree( pINChI_Aux->nTautomer );
5856 : : qzfree( pINChI_Aux->nNontautomericCanonicalNumbers );
5857 : : qzfree( pINChI_Aux->nIsotopicCanonicalNumbers );
5858 : : qzfree( pINChI_Aux->nNontautomericIsotopicCanonicalNumbers );
5859 : : qzfree( pINChI_Aux->nNontautomericEquNumbers );
5860 : : qzfree( pINChI_Aux->nNontautomericIsotopicEquNumbers );
5861 : : */
5862 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nConstitEquNumbers);
5863 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nConstitEquTGroupNumbers);
5864 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nConstitEquIsotopicNumbers);
5865 [ + + + - ]: 1285 : qzfree(pINChI_Aux->nConstitEquIsotopicTGroupNumbers);
5866 [ + - + - ]: 1285 : qzfree(pINChI_Aux);
5867 : 1285 : *ppINChI_Aux = NULL;
5868 : : }
5869 : :
5870 : 2578 : return 0;
5871 : : }
5872 : :
5873 : : /****************************************************************************/
5874 : 1267 : INChI_Aux *Alloc_INChI_Aux(int num_at,
5875 : : int num_isotopic_atoms,
5876 : : int nAllocMode,
5877 : : int bOrigCoord)
5878 : : {
5879 : : INChI_Aux *pINChI_Aux;
5880 : 1267 : int bIsotopic = (nAllocMode & REQ_MODE_ISO);
5881 : 1267 : int num_at_tg = num_at + num_at / 2;
5882 : : /* int bTautomeric = (nAllocMode & REQ_MODE_TAUT); */
5883 : :
5884 [ + - ]: 1267 : if (num_at <= 0 ||
5885 [ - + ]: 1267 : NULL == (pINChI_Aux = (INChI_Aux *)inchi_calloc(1, sizeof(INChI_Aux))))
5886 : : {
5887 : 0 : return NULL;
5888 : : }
5889 : :
5890 : 1267 : if ((pINChI_Aux->nOrigAtNosInCanonOrd = (AT_NUMB *)
5891 [ + - ]: 1267 : inchi_calloc(num_at_tg, sizeof(pINChI_Aux->nOrigAtNosInCanonOrd[0]))) &&
5892 : 1267 : (pINChI_Aux->nOrigAtNosInCanonOrdInv = (AT_NUMB *)
5893 [ + - ]: 1267 : inchi_calloc(num_at_tg, sizeof(pINChI_Aux->nOrigAtNosInCanonOrd[0]))) &&
5894 : 1267 : (pINChI_Aux->nConstitEquNumbers = (AT_NUMB *)
5895 [ + - ]: 1267 : inchi_calloc(num_at_tg, sizeof(pINChI_Aux->nConstitEquNumbers[0]))))
5896 : : {
5897 : : ;
5898 : : }
5899 : : else
5900 : : {
5901 : 0 : goto out_of_RAM;
5902 : : }
5903 : :
5904 [ + + ]: 1420 : if (num_at > 1 &&
5905 [ + - ]: 153 : (pINChI_Aux->nConstitEquTGroupNumbers = (AT_NUMB *)inchi_calloc((long long)num_at / 2 + 1, sizeof(pINChI_Aux->nConstitEquTGroupNumbers[0])))) /* djb-rwth: cast operator added */
5906 : : {
5907 : : ;
5908 : : }
5909 : : else
5910 : : {
5911 [ - + ]: 1114 : if (num_at > 1)
5912 : : {
5913 : 0 : goto out_of_RAM;
5914 : : }
5915 : : }
5916 : :
5917 [ + - ]: 1267 : if (num_at > 0)
5918 : : {
5919 : 1267 : pINChI_Aux->OrigInfo = (ORIG_INFO *)inchi_calloc(num_at, sizeof(pINChI_Aux->OrigInfo[0]));
5920 [ - + ]: 1267 : if (!pINChI_Aux->OrigInfo)
5921 : 0 : goto out_of_RAM;
5922 : : }
5923 : :
5924 [ + + + - ]: 1267 : if (bOrigCoord && num_at > 0)
5925 : : {
5926 : 1261 : pINChI_Aux->szOrigCoord = (MOL_COORD *)inchi_calloc(num_at, sizeof(pINChI_Aux->szOrigCoord[0]));
5927 [ - + ]: 1261 : if (!pINChI_Aux->szOrigCoord)
5928 : 0 : goto out_of_RAM;
5929 : : }
5930 : :
5931 [ + + ]: 1267 : if (bIsotopic)
5932 : : {
5933 : 13 : if (/*num_isotopic_atoms &&*/
5934 [ + - ]: 13 : (pINChI_Aux->nIsotopicOrigAtNosInCanonOrd = (AT_NUMB *)inchi_calloc(num_at_tg, sizeof(pINChI_Aux->nIsotopicOrigAtNosInCanonOrd[0]))) &&
5935 [ + - ]: 13 : (pINChI_Aux->nIsotopicOrigAtNosInCanonOrdInv = (AT_NUMB *)inchi_calloc(num_at_tg, sizeof(pINChI_Aux->nIsotopicOrigAtNosInCanonOrd[0]))) &&
5936 [ + - ]: 13 : (pINChI_Aux->nConstitEquIsotopicNumbers = (AT_NUMB *)inchi_calloc(num_at_tg, sizeof(pINChI_Aux->nConstitEquIsotopicNumbers[0]))))
5937 : : {
5938 : : ;
5939 : : }
5940 [ # # ]: 0 : else if (num_isotopic_atoms)
5941 : : {
5942 : 0 : goto out_of_RAM;
5943 : : }
5944 : :
5945 : 13 : if ( /*num_isotopic_atoms && num_at > 1 &&*/
5946 [ - + ]: 13 : (pINChI_Aux->nConstitEquIsotopicTGroupNumbers = (AT_NUMB *)inchi_calloc((long long)num_at / 2 + 1, sizeof(pINChI_Aux->nConstitEquIsotopicTGroupNumbers[0])))) /* djb-rwth: cast operator added */
5947 : : {
5948 : : ;
5949 : : }
5950 [ # # # # ]: 0 : else if (num_isotopic_atoms && num_at > 1)
5951 : : {
5952 : 0 : goto out_of_RAM;
5953 : : }
5954 : : }
5955 : :
5956 : 1267 : return pINChI_Aux;
5957 : :
5958 : 0 : out_of_RAM:
5959 : :
5960 [ # # ]: 0 : if (pINChI_Aux)
5961 : : {
5962 : 0 : Free_INChI_Aux(&pINChI_Aux);
5963 : : /*
5964 : : inchi_free(pINChI_Aux);
5965 : : */
5966 : : }
5967 : :
5968 : 0 : return NULL;
5969 : : }
5970 : :
5971 : : /****************************************************************************
5972 : : Note that orig_num, curr_num are allocd by caller as (n+1)-long lists
5973 : : ****************************************************************************/
5974 : 0 : void CompAtomData_GetNumMapping(COMP_ATOM_DATA *adata, int *orig_num, int *curr_num)
5975 : : {
5976 : : int i;
5977 [ # # # # ]: 0 : if (!orig_num || !curr_num)
5978 : : {
5979 : 0 : return;
5980 : : }
5981 [ # # ]: 0 : for (i = 0; i < adata->num_at; i++)
5982 : : {
5983 : 0 : int orig = adata->at[i].orig_at_number;
5984 : 0 : orig_num[i] = orig; /* orig's are from 1 */
5985 : 0 : curr_num[orig] = i; /* curr's are from 0 */
5986 : : }
5987 : : }
5988 : :
5989 : : /****************************************************************************
5990 : : Allocate integer matrix [mxn]
5991 : : ****************************************************************************/
5992 : 85 : int imat_new(int m, int n, int ***a)
5993 : : {
5994 : : int i;
5995 [ + + - + ]: 85 : if (m == 0 || n == 0)
5996 : : {
5997 : 19 : return 0;
5998 : : }
5999 [ - + ]: 66 : if (*a)
6000 : : {
6001 : 0 : imat_free(m, *a);
6002 : 0 : *a = NULL;
6003 : : }
6004 : 66 : *a = (int **)inchi_calloc(m, sizeof(int *));
6005 [ - + ]: 66 : if (NULL == *a)
6006 : : {
6007 : 0 : return 1;
6008 : : }
6009 [ + + ]: 528 : for (i = 0; i < m; i++)
6010 : : {
6011 : 462 : (*a)[i] = (int *)inchi_calloc(n, sizeof(int));
6012 [ - + ]: 462 : if (NULL == (*a)[i])
6013 : : {
6014 : 0 : return 1;
6015 : : }
6016 : : }
6017 : 66 : return 0;
6018 : : }
6019 : :
6020 : : /****************************************************************************
6021 : : Free integer matrix [mxn]
6022 : : ****************************************************************************/
6023 : 66 : void imat_free(int m, int **a)
6024 : : {
6025 : : int i;
6026 [ + - ]: 66 : if (NULL != a)
6027 : : {
6028 [ + + ]: 528 : for (i = 0; i < m; i++)
6029 : : {
6030 [ + - ]: 462 : if (NULL != a[i]) /* djb-rwth: unresolved issue -- revision required? -- false positive as this function just does the clean-up job */
6031 : : {
6032 [ + - ]: 462 : inchi_free(a[i]);
6033 : : }
6034 : : }
6035 [ + - ]: 66 : inchi_free(a);
6036 : 66 : a = NULL;
6037 : : }
6038 : :
6039 : 66 : return;
6040 : : }
6041 : :
6042 : : /*
6043 : : Lt-wt subgraph
6044 : : */
6045 : :
6046 : : /****************************************************************************
6047 : : Establish light-weight subgraph representing (part of) orig_inp_data
6048 : : ****************************************************************************/
6049 : 0 : subgraf *subgraf_new(ORIG_ATOM_DATA *orig_inp_data,
6050 : : int nnodes,
6051 : : int *nodes)
6052 : : {
6053 : 0 : int i, j, iat, nbr, jat, nj, degree, nat, err = 0;
6054 : :
6055 : 0 : subgraf *sg = (subgraf *)inchi_calloc(1, sizeof(subgraf));
6056 [ # # ]: 0 : if (!sg)
6057 : : {
6058 : 0 : return NULL;
6059 : : }
6060 : :
6061 : 0 : nat = orig_inp_data->num_inp_atoms;
6062 : :
6063 : : /* orig2node is mapping of original at numbers --> subgraph node numbers */
6064 : 0 : err = 1;
6065 [ # # ]: 0 : if (!(sg->orig2node = (int *)inchi_calloc((long long)nat + 1, sizeof(int)))) /* djb-rwth: cast operator added */
6066 : : {
6067 : 0 : goto exit_function;
6068 : : }
6069 [ # # ]: 0 : if (!(sg->nodes = (int *)inchi_calloc(nnodes, sizeof(int))))
6070 : : {
6071 : 0 : goto exit_function;
6072 : : }
6073 [ # # ]: 0 : if (!(sg->degrees = (int *)inchi_calloc(nnodes, sizeof(int))))
6074 : : {
6075 : 0 : goto exit_function;
6076 : : }
6077 : : /* NB: input list of 'nodes' is assumed to be in 'original_atom_numbering domain' which starts from 1.
6078 : : Now it is mapped to current atom numbers which starts from 0/connections using at[j].orig_at_number */
6079 : 0 : sg->nnodes = 0;
6080 [ # # ]: 0 : for (i = 0; i < nnodes; i++)
6081 : : {
6082 : 0 : sg->nodes[sg->nnodes++] = nodes[i];
6083 : : }
6084 : :
6085 [ # # ]: 0 : for (i = 0; i <= nat; i++)
6086 : : {
6087 : 0 : sg->orig2node[i] = -1;
6088 : : }
6089 [ # # ]: 0 : for (i = 0; i < nnodes; i++)
6090 : : {
6091 : 0 : sg->orig2node[sg->nodes[i]] = i;
6092 : : }
6093 : :
6094 : : /* Create and fill subgraph adjacency matrix based on nodes/orig atom numbers
6095 : : and connections stored in orig_inp_data */
6096 : 0 : sg->adj = (subgraf_edge **)inchi_calloc(nnodes, sizeof(subgraf_edge *));
6097 [ # # ]: 0 : if (!sg->adj)
6098 : : {
6099 : 0 : goto exit_function;
6100 : : }
6101 : :
6102 [ # # ]: 0 : for (i = 0; i < sg->nnodes; i++)
6103 : : {
6104 : 0 : iat = nodes[i] - 1; /* current atom number for this node */
6105 : 0 : degree = orig_inp_data->at[iat].valence;
6106 : 0 : nj = -1;
6107 : 0 : sg->adj[i] = (subgraf_edge *)inchi_calloc(degree, sizeof(subgraf_edge));
6108 [ # # ]: 0 : if (!sg->adj[i])
6109 : : {
6110 : 0 : goto exit_function;
6111 : : }
6112 [ # # ]: 0 : for (j = 0; j < degree; j++)
6113 : : {
6114 : 0 : jat = orig_inp_data->at[iat].neighbor[j]; /* for curr num jat, a (jat+1) would be an orig num */
6115 : 0 : nbr = sg->orig2node[jat + 1];
6116 [ # # ]: 0 : if (nbr < 0)
6117 : 0 : continue;
6118 : 0 : nj++;
6119 : 0 : sg->adj[i][nj].nbr = nbr;
6120 : 0 : sg->adj[i][nj].etype = orig_inp_data->at[iat].bond_type[j];
6121 : : }
6122 : 0 : sg->degrees[i] = nj + 1;
6123 : : }
6124 : 0 : err = 0;
6125 : :
6126 : : /* subgraf_debug_trace( sg ); */
6127 : :
6128 : 0 : exit_function:
6129 [ # # ]: 0 : if (err)
6130 : : {
6131 : 0 : subgraf_free(sg);
6132 : 0 : return NULL; /* djb-rwth: avoiding reading from freed memory */
6133 : : }
6134 : :
6135 : 0 : return sg;
6136 : : }
6137 : :
6138 : : /****************************************************************************/
6139 : 0 : void subgraf_free(subgraf *sg)
6140 : : {
6141 : : int i;
6142 [ # # ]: 0 : if (!sg)
6143 : : {
6144 : 0 : return;
6145 : : }
6146 [ # # ]: 0 : if (sg->nodes)
6147 : : {
6148 [ # # ]: 0 : inchi_free(sg->nodes);
6149 : : }
6150 [ # # ]: 0 : if (sg->degrees)
6151 : : {
6152 [ # # ]: 0 : inchi_free(sg->degrees);
6153 : : }
6154 [ # # ]: 0 : if (sg->orig2node)
6155 : : {
6156 [ # # ]: 0 : inchi_free(sg->orig2node);
6157 : : }
6158 [ # # ]: 0 : if (sg->adj)
6159 : : {
6160 [ # # ]: 0 : for (i = 0; i < sg->nnodes; i++)
6161 : : {
6162 [ # # ]: 0 : if (sg->adj[i]) /* djb-rwth: unresolved issue -- revision required? -- false positive as this function just does the clean-up job */
6163 : : {
6164 [ # # ]: 0 : inchi_free(sg->adj[i]);
6165 : : }
6166 : : }
6167 [ # # ]: 0 : inchi_free(sg->adj);
6168 : : }
6169 [ # # ]: 0 : inchi_free(sg);
6170 : 0 : sg = NULL;
6171 : :
6172 : 0 : return;
6173 : : }
6174 : :
6175 : : /****************************************************************************/
6176 : 0 : void subgraf_debug_trace(subgraf *sg)
6177 : : {
6178 : : int p, q;
6179 : :
6180 : : ITRACE_("\n\n*********************************************************************\n* Subgraf:");
6181 : : ITRACE_("\n\tNodes: %-d ( ", sg->nnodes);
6182 [ # # ]: 0 : for (p = 0; p < sg->nnodes; p++)
6183 : : {
6184 : : ITRACE_("%-d ", sg->nodes[p]);
6185 : : }
6186 : : ITRACE_(")\n\tAdj lists:\n");
6187 [ # # ]: 0 : for (p = 0; p < sg->nnodes; p++)
6188 : : {
6189 : : ITRACE_("\tNode #%-d (orig# %-d) ::: Neighbors (node#, orig#) : ",
6190 : : p, sg->nodes[p]);
6191 [ # # ]: 0 : for (q = 0; q < sg->degrees[p]; q++)
6192 : : {
6193 : 0 : int nbr = sg->adj[p][q].nbr; /* djb-rwth: ignoring LLVM warning: variable used */
6194 : : ITRACE_("(%-d/%-d/%-d) ", nbr, sg->nodes[nbr]);
6195 : : }
6196 : : ITRACE_("\n");
6197 : : }
6198 : : ITRACE_("\n* End Subgraf\n*********************************************************************\n");
6199 : :
6200 : 0 : return;
6201 : : }
6202 : :
6203 : : /****************************************************************************/
6204 : 0 : subgraf_pathfinder *subgraf_pathfinder_new(subgraf *sg,
6205 : : ORIG_ATOM_DATA *orig_inp_data,
6206 : : int start,
6207 : : int end)
6208 : : {
6209 : 0 : subgraf_pathfinder *spf = NULL;
6210 : :
6211 : 0 : spf = (subgraf_pathfinder *)inchi_calloc(1, sizeof(subgraf_pathfinder));
6212 [ # # ]: 0 : if (!spf)
6213 : : {
6214 : 0 : goto exit_function;
6215 : : }
6216 : :
6217 : 0 : spf->sg = sg;
6218 : 0 : spf->start = start;
6219 : 0 : spf->end = end;
6220 : 0 : spf->nbonds = 0;
6221 : 0 : spf->nseen = 0;
6222 : :
6223 : 0 : spf->seen = (int *)inchi_calloc(spf->sg->nnodes, sizeof(int));
6224 [ # # ]: 0 : if (!spf->seen)
6225 : : {
6226 [ # # ]: 0 : inchi_free(spf);
6227 : 0 : spf = NULL;
6228 : : }
6229 : :
6230 : 0 : exit_function:
6231 : 0 : return spf;
6232 : : }
6233 : :
6234 : : /****************************************************************************/
6235 : 0 : void subgraf_pathfinder_free(subgraf_pathfinder *spf)
6236 : : {
6237 [ # # ]: 0 : if (!spf)
6238 : : {
6239 : 0 : return;
6240 : : }
6241 [ # # ]: 0 : if (spf->seen)
6242 : : {
6243 [ # # ]: 0 : inchi_free(spf->seen);
6244 : : }
6245 [ # # ]: 0 : inchi_free(spf);
6246 : 0 : spf = NULL;
6247 : 0 : return;
6248 : : }
6249 : :
6250 : : /****************************************************************************
6251 : : Find path(s) from subgraf node spf->start to spf->end
6252 : : and fill bonds[nbonds] and atoms[natoms]
6253 : : Do not traverse through supplied forbidden edges (if not zero/NULL)
6254 : : ****************************************************************************/
6255 : 0 : void subgraf_pathfinder_run(subgraf_pathfinder *spf,
6256 : : int nforbidden, /* number of edges forbidden for traversal */
6257 : : int *forbidden, /* nodes of forbidden edges: [edge1node1,edge1node2, edge2node1, edge2node2, ... ] */
6258 : : int *nbonds,
6259 : : int **bonds, /* collect subgraf bonds here */
6260 : : int *natoms,
6261 : : int *atoms /* if not NULL, collect subgraf atoms here */
6262 : : )
6263 : : {
6264 : : int j, k, node, node0;
6265 : : int f, skip;
6266 : :
6267 [ # # ]: 0 : if (spf->nseen < 1)
6268 : : {
6269 : : /* Even at very beginning, push start node to seen and set nseen = 1
6270 : : and put end node into subgraf_pathfinder's end */
6271 : 0 : return;
6272 : : }
6273 : :
6274 : 0 : node0 = spf->seen[spf->nseen - 1];
6275 [ # # ]: 0 : for (j = 0; j < spf->sg->degrees[node0]; j++)
6276 : : {
6277 : 0 : node = spf->sg->adj[node0][j].nbr;
6278 [ # # ]: 0 : if (is_in_the_ilist(spf->seen, node, spf->nseen))
6279 : : {
6280 : 0 : continue;
6281 : : }
6282 [ # # # # ]: 0 : if (nforbidden && forbidden)
6283 : : {
6284 : 0 : skip = 0;
6285 [ # # ]: 0 : for (f = 0; f < nforbidden; f++)
6286 : : {
6287 [ # # ]: 0 : if (bIsSameBond(node0, node, forbidden[2 * f], forbidden[2 * f + 1]))
6288 : : {
6289 : 0 : skip = 1;
6290 : 0 : break;
6291 : : }
6292 : : }
6293 [ # # ]: 0 : if (skip)
6294 : : {
6295 : 0 : continue;
6296 : : }
6297 : : }
6298 [ # # ]: 0 : if (node == spf->end)
6299 : : {
6300 : 0 : spf->seen[spf->nseen++] = node;
6301 : :
6302 : : ITRACE_("\n\tFound path (in orig atom numbers):\t");
6303 [ # # ]: 0 : for (k = 0; k < spf->nseen; k++)
6304 : : {
6305 : 0 : int orig_atnum = spf->sg->nodes[spf->seen[k]];
6306 : : ITRACE_("%-d ", orig_atnum);
6307 [ # # # # ]: 0 : if (atoms && !is_in_the_ilist(atoms, orig_atnum, *natoms))
6308 : : {
6309 : 0 : atoms[(*natoms)++] = orig_atnum;
6310 : : }
6311 : : }
6312 : : ITRACE_("\t( In node nums: ");
6313 [ # # ]: 0 : for (k = 1; k < spf->nseen; k++)
6314 : : {
6315 : 0 : int at1 = spf->seen[k - 1];
6316 : 0 : int at2 = spf->seen[k];
6317 : 0 : add_bond_if_unseen(spf, at1, at2, nbonds, bonds);
6318 : :
6319 : : ITRACE_("%-d ", spf->seen[k]);
6320 : : }
6321 : : ITRACE_(")");
6322 : :
6323 : 0 : spf->seen[spf->nseen - 1] = 0;
6324 : 0 : spf->nseen--; /* pop_back */
6325 : 0 : break;
6326 : : }
6327 : : }
6328 [ # # ]: 0 : for (j = 0; j < spf->sg->degrees[node0]; j++)
6329 : : {
6330 : 0 : node = spf->sg->adj[node0][j].nbr;
6331 [ # # # # ]: 0 : if (node == spf->end || is_in_the_ilist(spf->seen, node, spf->nseen))
6332 : : {
6333 : 0 : continue;
6334 : : }
6335 [ # # # # ]: 0 : if (nforbidden && forbidden)
6336 : : {
6337 : 0 : skip = 0;
6338 [ # # ]: 0 : for (f = 0; f < nforbidden; f++)
6339 : : {
6340 [ # # ]: 0 : if (bIsSameBond(node0, node, forbidden[2 * f], forbidden[2 * f + 1]))
6341 : : {
6342 : 0 : skip = 1;
6343 : 0 : break;
6344 : : }
6345 : : }
6346 [ # # ]: 0 : if (skip)
6347 : : {
6348 : 0 : continue;
6349 : : }
6350 : : }
6351 : 0 : spf->seen[spf->nseen++] = node;
6352 : 0 : subgraf_pathfinder_run(spf, 0, NULL, nbonds, bonds, natoms, atoms);
6353 : 0 : spf->seen[spf->nseen - 1] = 0;
6354 : 0 : spf->nseen--;
6355 : : }
6356 : :
6357 : 0 : return;
6358 : : }
6359 : :
6360 : : /****************************************************************************/
6361 : 0 : void add_bond_if_unseen(subgraf_pathfinder *spf,
6362 : : int node0,
6363 : : int node,
6364 : : int *nbonds,
6365 : : int **bonds)
6366 : : {
6367 : : int seen, p, at1, at2;
6368 : :
6369 : 0 : at1 = spf->sg->nodes[node0];
6370 : 0 : at2 = spf->sg->nodes[node];
6371 : : #if 0
6372 : : if ( at1 > at2 )
6373 : : {
6374 : : int tmp = at1;
6375 : : at1 = at2;
6376 : : at2 = tmp;
6377 : : }
6378 : : #endif
6379 : 0 : seen = 0;
6380 [ # # ]: 0 : for (p = 0; p < *nbonds; p++)
6381 : : {
6382 : : /*if (bonds[p][0] == at1 && bonds[p][1] == at2)*/
6383 [ # # ]: 0 : if (bIsSameBond(at1, at2, bonds[p][0], bonds[p][1]))
6384 : : {
6385 : 0 : seen = 1;
6386 : 0 : break;
6387 : : }
6388 : : }
6389 [ # # # # ]: 0 : if (!seen && bonds)
6390 : : {
6391 : 0 : bonds[*nbonds][0] = at1;
6392 : 0 : bonds[*nbonds][1] = at2;
6393 : 0 : (*nbonds)++;
6394 : : }
6395 : :
6396 : 0 : return;
6397 : : }
6398 : :
6399 : : /****************************************************************************
6400 : : At the first call, push start node to spf->start and set spf->nseen = 0
6401 : : ****************************************************************************/
6402 : 0 : int subgraf_pathfinder_collect_all(subgraf_pathfinder *spf,
6403 : : int nforbidden, /* number of edges forbidden for traversal */
6404 : : int *forbidden, /* nodes of forbidden edges: [edge1node1,edge1node2, edge2node1, edge2node2, ... ] */
6405 : : int *atnums /* 1-based origs# */
6406 : : )
6407 : : {
6408 : : int j, f, node, next_node, skip;
6409 : :
6410 : 0 : node = spf->start;
6411 : 0 : spf->seen[spf->nseen] = node;
6412 : 0 : atnums[spf->nseen] = spf->sg->nodes[node];
6413 : 0 : spf->nseen++;
6414 : :
6415 [ # # ]: 0 : for (j = 0; j < spf->sg->degrees[node]; j++)
6416 : : {
6417 : 0 : next_node = spf->sg->adj[node][j].nbr;
6418 [ # # ]: 0 : if (is_in_the_ilist(spf->seen, next_node, spf->nseen))
6419 : : {
6420 : 0 : continue;
6421 : : }
6422 [ # # # # ]: 0 : if (nforbidden && forbidden)
6423 : : {
6424 : 0 : skip = 0;
6425 [ # # ]: 0 : for (f = 0; f < nforbidden; f++)
6426 : : {
6427 [ # # ]: 0 : if (bIsSameBond(node, next_node, forbidden[2 * f], forbidden[2 * f + 1]))
6428 : : {
6429 : 0 : skip = 1;
6430 : 0 : break;
6431 : : }
6432 : : }
6433 [ # # ]: 0 : if (skip)
6434 : : {
6435 : 0 : continue;
6436 : : }
6437 : : }
6438 : 0 : spf->start = next_node;
6439 : 0 : subgraf_pathfinder_collect_all(spf, nforbidden, forbidden, atnums);
6440 : : }
6441 : :
6442 : 0 : return spf->nseen;
6443 : : }
6444 : :
6445 : : #if (FIX_ADJ_RAD == 1)
6446 : :
6447 : : /****************************************************************************/
6448 : : int FixNextRadicals(int cur_at, inp_ATOM *at);
6449 : : int FixNextRadicals(int cur_at, inp_ATOM *at)
6450 : : {
6451 : : int j, neigh, num_found = 0;
6452 : :
6453 : : for (j = 0; j < at[cur_at].valence; j++)
6454 : : {
6455 : : neigh = at[cur_at].neighbor[j];
6456 : : if (at[neigh].radical == RADICAL_DOUBLET)
6457 : : {
6458 : : at[neigh].radical = 0;
6459 : : num_found++;
6460 : : num_found += FixNextRadicals(neigh, at);
6461 : : }
6462 : : }
6463 : :
6464 : : return num_found;
6465 : : }
6466 : :
6467 : : /****************************************************************************/
6468 : : int FixAdjacentRadicals(int num_inp_atoms, inp_ATOM *at)
6469 : : {
6470 : : int i, j;
6471 : : char *bVisited = NULL;
6472 : : int nNumFound = 0, neigh, cur_found;
6473 : :
6474 : : for (i = 0; i < num_inp_atoms; i++)
6475 : : {
6476 : : if (at[i].radical == RADICAL_DOUBLET)
6477 : : {
6478 : : cur_found = 1;
6479 : : for (j = 0; j < at[i].valence; j++)
6480 : : {
6481 : : neigh = at[i].neighbor[j];
6482 : : if (at[neigh].radical == RADICAL_DOUBLET)
6483 : : {
6484 : : cur_found++;
6485 : : }
6486 : : }
6487 : : if (cur_found >= 3)
6488 : : {
6489 : : nNumFound++;
6490 : : at[i].radical = 0;
6491 : : nNumFound += FixNextRadicals(i, at);
6492 : : }
6493 : : }
6494 : : }
6495 : :
6496 : : return nNumFound;
6497 : : }
6498 : :
6499 : : #endif
6500 : :
6501 : : #ifdef COMPILE_ANSI_ONLY
6502 : :
6503 : : #ifndef TARGET_API_LIB
6504 : : /*
6505 : : #include <stdio.h>
6506 : : #include "inpdef.h"
6507 : : */
6508 : :
6509 : : /****************************************************************************/
6510 : : void PrintFileName(const char *fmt,
6511 : : FILE *out_file,
6512 : : /* INCHI_IOSTREAM *out_file, */
6513 : : const char *szFname)
6514 : : {
6515 : : inchi_print_nodisplay(out_file, fmt, szFname);
6516 : : }
6517 : : #endif
6518 : :
6519 : : #endif
|