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 : :
9 : : /* */
10 : : /*#define CHECK_WIN32_VC_HEAP*/
11 : :
12 : : #include "mode.h"
13 : :
14 : : #if ( READ_INCHI_STRING == 1 )
15 : :
16 : : #include "ichitime.h"
17 : : #include "ichirvrs.h"
18 : : #include "ichicant.h"
19 : : #include "ichi_io.h"
20 : : /* djb-rwth: these two headers have to be included as well */
21 : : #include "ichimain.h"
22 : : #include "inpdef.h"
23 : :
24 : : #include "bcf_s.h"
25 : :
26 : : /**************************************************************************************************
27 : :
28 : : ChargeStruct fictitios structures MY_CONST CN_LIST cnList[*]
29 : : ============================================================
30 : :
31 : :
32 : : bond flow (+) => Positive charge c-group
33 : : ----------------- (-) => Negative charge c-group
34 : : Single 0 (+C) => Positive charge group for C, Si, Ge, Sn, Pb
35 : : Double 1 (-C) => Negative charge group for C, Si, Ge, Sn, Pb
36 : : Triple 2 (.) => additional one unit of st_cap
37 : :
38 : : A) Interpretation:
39 : :
40 : : X-(-) or X=(+) => zero charge
41 : : X=(-) or X-(+) => charge = -1 or +1, respectively
42 : :
43 : : B) Information to keep:
44 : :
45 : : ordering zero-based number of the edge
46 : : to (+) or (-) from the Interpretation (A) section
47 : :
48 : : vCap = vertex cap
49 : : vFlow = vertex flow
50 : : val = number of edges incident to the vertex
51 : : neigh = 1-based ordering number of the adjacent vertex; 0 => no more adjacent vertices
52 : : cap = cap of the edge to the adjacent vertex
53 : : flow = flow of the edge to the adjacent vertex
54 : :
55 : : atom (c-point) always has number 1
56 : : c-group(s) always are the last vertices
57 : : always adjacent_neigh_number > vertex_number, that is, neigh > vertex
58 : :
59 : : Contribution to the Total Charge:
60 : : ----------------------------------
61 : : edge_cap(+) - edge_flow(+) - edge_flow(-) - Delta(+) - Delta(-)
62 : :
63 : : where edge_cap(+) is edge capacity to c-group (+);
64 : : edge_flow(+) is edge capacity to c-group (?), (?)= (+) or (-);
65 : : Delta(?) = st_cap(?) - st_floe(?) of the c-group vertex (?), (?)= (+) or (-);
66 : :
67 : : ***************************************************************************************************/
68 : : /**************************************************************************************************
69 : :
70 : : Important:
71 : : vCap and vFlow Note: since metal charge group (vert. 2-4) MUST be registered before
72 : : marked with empty the "metal flower" (5-8) all charge group vertex numbers are
73 : : comments for vertices less than metal flower vertices: (2,3,4) < (5,6,7,8)
74 : : 1 and 5(M) should This MAY be neded for c-group enumeration. The order is:
75 : : be set separately t-groups, c-groups, M-flower. All types BNS_VT_M_GROUP allows to avoid duplications.
76 : :
77 : : 3(+)
78 : : || (Metal)
79 : : || \|/ init charge=0; MAX_METAL_CHARGE = 16
80 : : 4(-) 5(M) 2 -Fe- CAP(BOND_TO_BNS_VT_M_GROUP) = NUM_BONDS*CAP
81 : : \ | / |
82 : : \ | /
83 : : 1 X(V), V=valence
84 : : */
85 : : MY_CONST C_NODE cnMe[5] = {
86 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
87 : : { {BNS_VERT_TYPE_ATOM,0/**/ ,0/**/,3}, {{ 2, 16,0, 0 },{ 4, 16,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
88 : : { {BNS_VT_CHRG_STRUCT,16, 16, 2}, {{ 3, 16,0,16 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
89 : : { {BNS_VT_C_POS_M, 16, 16, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 2 }, { 0, 0,0, 0 }} }, /* 3 */
90 : : { {BNS_VT_C_NEG_M, 0 + 16, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
91 : : { {BNS_VT_M_GROUP, 0/**/ ,0/**/,3}, {{ 1, 3,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
92 : : };
93 : : /*
94 : : #define cn_bits_Me (-1)
95 : : */
96 : :
97 : : /**************************************************************************************************
98 : : c=2 5(+.)
99 : : _____ / (PNPN)
100 : : 4=====3 |||| init charge=0
101 : : c=2 \ / c=1 -N-
102 : : 2 |
103 : : ||||
104 : : 1 X+(V), X(V+1), X+(V+2), X(V+3); V=valence
105 : : */
106 : : MY_CONST C_NODE cnPNPN[5] = {
107 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
108 : : { {BNS_VERT_TYPE_ATOM, 3, 3, 1}, {{ 2, 3,0, 3 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
109 : : { {BNS_VT_CHRG_STRUCT, 3, 3, 3}, {{ 3, 1,0, 0 },{ 4, 2,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
110 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 5, 1,0, 0 },{ 4, 2,0, 2 }, { 0, 0,0, 0 }} }, /* 3 */
111 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 2}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
112 : : { {BNS_VT_C_POS, 1 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
113 : : };
114 : : /*
115 : : #define cn_bits_PNPN MAKE_CN_BITS(cn_bits_P, cn_bits_N, cn_bits_P, cn_bits_N)
116 : : */
117 : : /**************************************************************************************************
118 : : 5(+)
119 : : c=1 // (NPNP)
120 : : 4=====3 |||| init charge=0
121 : : c=1 \ / c=2 -N-
122 : : 2 |
123 : : ||||
124 : : 1 X(V), X+(V+1), X(V+2), X+(V+3); V=valence
125 : : */
126 : : MY_CONST C_NODE cnNPNP[5] = {
127 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
128 : : { {BNS_VERT_TYPE_ATOM, 3, 3, 1}, {{ 2, 3,0, 3 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
129 : : { {BNS_VT_CHRG_STRUCT, 3, 3, 3}, {{ 3, 2,0, 0 },{ 4, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
130 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 5, 1,0, 1 },{ 4, 1,0, 1 }, { 0, 0,0, 0 }} }, /* 3 */
131 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
132 : : { {BNS_VT_C_POS, 0 + 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
133 : : };
134 : : /*
135 : : #define cn_bits_NPNP MAKE_CN_BITS(cn_bits_N, cn_bits_P, cn_bits_N, cn_bits_P)
136 : : */
137 : : /********************* end new ********************************************************************/
138 : :
139 : :
140 : : /**************************************************************************************************
141 : : 5(+)
142 : : // (NPN)
143 : : 4=====3 ||| init charge=0
144 : : \ / -N-
145 : : 2 |
146 : : |||
147 : : 1 X(V), X+(V+1), X(V+2); V=valence
148 : : */
149 : : MY_CONST C_NODE cnNPN[5] = {
150 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
151 : : { {BNS_VERT_TYPE_ATOM, 2, 2, 1}, {{ 2, 2,0, 2 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
152 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 3, 1,0, 0 },{ 4, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
153 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 5, 1,0, 1 },{ 4, 1,0, 1 }, { 0, 0,0, 0 }} }, /* 3 */
154 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
155 : : { {BNS_VT_C_POS, 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
156 : : };
157 : : /*
158 : : #define cn_bits_NPN MAKE_CN_BITS(cn_bits_N, cn_bits_P, cn_bits_N, 0)
159 : : */
160 : : /**************************************************************************************************
161 : : 5(+.)
162 : : / (PNP)
163 : : 4=====3 ||| init charge=0
164 : : \ / -Cl-
165 : : 2 /\
166 : : |||
167 : : 1 X+(V), X(V+1), X+(V+2); V=valence
168 : : */
169 : : MY_CONST C_NODE cnPNP[5] = {
170 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
171 : : { {BNS_VERT_TYPE_ATOM, 2, 2, 1}, {{ 2, 2,0, 2 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
172 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 3, 1,0, 0 },{ 4, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
173 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 3}, {{ 5, 1,0, 0 },{ 4, 1,0, 1 }, { 0, 0,0, 0 }} }, /* 3 */
174 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
175 : : { {BNS_VT_C_POS, 1 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
176 : : };
177 : : /*
178 : : #define cn_bits_PNP MAKE_CN_BITS(cn_bits_P, cn_bits_N, cn_bits_P, 0)
179 : : */
180 : : /**************************************************************************************************
181 : :
182 : : (MNP)
183 : : \ / init charge=0
184 : : N(.)
185 : : 3(-) 2(+) / \
186 : : \ //
187 : : 1(.) X-(V), X(V+1), X+(V+2); V=valence
188 : : */
189 : : MY_CONST C_NODE cnMNP[3] = {
190 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
191 : : { {BNS_VERT_TYPE_ATOM, 2, 1, 2}, {{ 2, 1,0, 1 },{ 3, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
192 : : { {BNS_VT_C_POS, 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
193 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} } /* 3 */
194 : : };
195 : : /*
196 : : #define cn_bits_MNP MAKE_CN_BITS(cn_bits_M, cn_bits_N, cn_bits_P, 0)
197 : : */
198 : : #ifdef NEVER
199 : : /************************** not used **************************************************************
200 : : (PNM)
201 : : 5(-) 4(+) \\ /
202 : : \ // B(.) init charge=0
203 : : 3 2 / \
204 : : \\ /
205 : : 1(.) X+(V), X(V+1), X+(V+2); V=valence
206 : : */
207 : : MY_CONST C_NODE cnPNM[5] = {
208 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
209 : : { {BNS_VERT_TYPE_ATOM, 2, 1, 2}, {{ 2, 1,0, 0 },{ 3, 1,0, 1 }, { 0, 0,0, 0 }} }, /* 1 */
210 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 4, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
211 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 5, 1,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 3 */
212 : : { {BNS_VT_C_POS, 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
213 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
214 : : };
215 : :
216 : : #define cn_bits_PNM MAKE_CN_BITS(cn_bits_P, cn_bits_N, cn_bits_M, 0)
217 : :
218 : : #endif
219 : :
220 : : /**************************************************************************************************
221 : : 4(-) 3(+.) (PNM)
222 : : \ / ||| init charge=0
223 : : 2 --P--
224 : : ||| / \
225 : : 1 X-(V), X(V+1), X+(V+2); V=valence
226 : : */
227 : : MY_CONST C_NODE cnPNM[4] = {
228 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
229 : : { {BNS_VERT_TYPE_ATOM, 2, 2, 1}, {{ 2, 2,0, 2 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
230 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 3, 1,0, 0 },{ 4, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
231 : : { {BNS_VT_C_POS, 1 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 3 */
232 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
233 : : }; /* explanaton of vCap: ^ ^ */
234 : : /* additional dot:/ \ capacity of the edge to (+) or (-) vertex */
235 : : /*
236 : : #define cn_bits_PNM MAKE_CN_BITS(cn_bits_P, cn_bits_N, cn_bits_M, 0)
237 : : */
238 : : /**************************************************************************************************
239 : : 5(+C)
240 : : // init charge=0
241 : : 6(-C) 4
242 : : \ / (EN) E=either +1 or -1
243 : : 3 |
244 : : || -C(.)-
245 : : 2 |
246 : : |
247 : : 1(.) X-(V), X+(V), X(V+1); V=valence
248 : : */
249 : : MY_CONST C_NODE cnEN[6] = {
250 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
251 : : { {BNS_VERT_TYPE_ATOM, 1, 0, 1}, {{ 2, 1,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
252 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 3, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
253 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 3}, {{ 4, 1,0, 0 },{ 6, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 3 */
254 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 5, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
255 : : { {BNS_VT_C_POS_C, 0 + 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
256 : : { {BNS_VT_C_NEG_C, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} } /* 6 */
257 : : };
258 : : /*
259 : : #define cn_bits_EN MAKE_CN_BITS(cn_bits_P | cn_bits_M, cn_bits_N, 0, 0)
260 : : */
261 : : /**************************************************************************************************
262 : : 5(-)
263 : : / (NMN) init charge=0
264 : : 4=====3 |||
265 : : \ / -X-
266 : : 2 /\
267 : : |||
268 : : 1 X(V), X-(V+1), X(V+2); V=valence
269 : : */
270 : : MY_CONST C_NODE cnNMN[5] = {
271 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
272 : : { {BNS_VERT_TYPE_ATOM, 2, 2, 1}, {{ 2, 2,0, 2 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
273 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 3, 1,0, 0 },{ 4, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
274 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 3}, {{ 5, 1,0, 0 },{ 4, 1,0, 1 }, { 0, 0,0, 0 }} }, /* 3 */
275 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
276 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} } /* 5 */
277 : : };
278 : : /*
279 : : #define cn_bits_NMN MAKE_CN_BITS(cn_bits_N, cn_bits_M, cn_bits_N, 0)
280 : : */
281 : : /**************************************************************************************************
282 : : 4(+)
283 : : // (NE) E=either +1 or -1
284 : : 5(-) 3 ||
285 : : \ / -X- init charge=0
286 : : 2 |
287 : : ||
288 : : 1 X(V), X+(V+1), X-(V+1); V=valence
289 : : */
290 : : MY_CONST C_NODE cnNE[5] = {
291 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
292 : : { {BNS_VERT_TYPE_ATOM, 1, 1, 1}, {{ 2, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
293 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 3}, {{ 3, 1,0, 0 },{ 5, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
294 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 4, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 3 */
295 : : { {BNS_VT_C_POS, 0 + 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
296 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} } /* 5 */
297 : : };
298 : : /*
299 : : #define cn_bits_NE MAKE_CN_BITS(cn_bits_N, cn_bits_P | cn_bits_M, 0, 0)
300 : : */
301 : : /**************************************************************************************************
302 : : 6(-) 5(+)
303 : : \ // (NEN)
304 : : 4=====3 ||| init charge=0
305 : : \ / -X-
306 : : 2 |
307 : : |||
308 : : 1 X(V), X+(V+1), X-(V+1), X(V+2); V=valence
309 : : */
310 : : MY_CONST C_NODE cnNEN[6] = {
311 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
312 : : { {BNS_VERT_TYPE_ATOM, 2, 2, 1}, {{ 2, 2,0, 2 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
313 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 3, 1,0, 0 },{ 4, 1,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
314 : : { {BNS_VT_CHRG_STRUCT, 2, 2, 3}, {{ 5, 1,0, 1 },{ 4, 1,0, 1 }, { 0, 0,0, 0 }} }, /* 3 */
315 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 3}, {{ 6, 1,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 4 */
316 : : { {BNS_VT_C_POS, 0 + 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 5 */
317 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 6 */
318 : : };
319 : : /*
320 : : #define cn_bits_NEN MAKE_CN_BITS(cn_bits_N, cn_bits_M | cn_bits_N, cn_bits_N, 0)
321 : : */
322 : : /*=======================================================*/
323 : : /**************************************************************************************************
324 : : (NP)
325 : : ||
326 : : -X- init charge=0
327 : : 2(+) |
328 : : ||
329 : : 1 X(V), X+(V+1); V=valence
330 : : */
331 : : MY_CONST C_NODE cnNP[2] = {
332 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
333 : : { {BNS_VERT_TYPE_ATOM, 1, 1, 1}, {{ 2, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
334 : : { {BNS_VT_C_POS, 0 + 1, 1, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
335 : : };
336 : : /*
337 : : #define cn_bits_NP MAKE_CN_BITS(cn_bits_N, cn_bits_P, 0, 0)
338 : : */
339 : : /**************************************************************************************************
340 : : (PN)
341 : : 3(+.) || init charge=0 [because cap(+)-flow(+)-Delta=1-0-1=0]
342 : : | -X-
343 : : 2 |
344 : : ||
345 : : 1 X+(V), X(V+1); V=valence
346 : : */
347 : : MY_CONST C_NODE cnPN[3] = {
348 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
349 : : { {BNS_VERT_TYPE_ATOM, 1, 1, 1}, {{ 2, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
350 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 3, 1,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
351 : : { {BNS_VT_C_POS, 1 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 3 */
352 : : };
353 : : /*
354 : : #define cn_bits_PN MAKE_CN_BITS(cn_bits_P, cn_bits_N, 0, 0)
355 : : */
356 : : /**************************************************************************************************
357 : : (NM)
358 : : 3(-) || init charge=0
359 : : | -X-
360 : : 2 |
361 : : ||
362 : : 1 X(V), X-(V+1); V=valence
363 : : */
364 : : MY_CONST C_NODE cnNM[3] = {
365 : : /* vertex type vCap vFlow; val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
366 : : { {BNS_VERT_TYPE_ATOM, 1, 1, 1}, {{ 2, 1,0, 1 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
367 : : { {BNS_VT_CHRG_STRUCT, 1, 1, 2}, {{ 3, 1,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
368 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 3 */
369 : : };
370 : : /*
371 : : #define cn_bits_NM MAKE_CN_BITS(cn_bits_N, cn_bits_M, 0, 0)
372 : : */
373 : : /**************************************************************************************************
374 : : (MN)
375 : : |
376 : : -X- init charge=0
377 : : 2(-) |
378 : : |
379 : : 1(.) X-(V), X(V+1); V=valence
380 : : */
381 : : MY_CONST C_NODE cnMN[2] = {
382 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
383 : : { {BNS_VERT_TYPE_ATOM, 1, 0, 1}, {{ 2, 1,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
384 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
385 : : };
386 : : /*
387 : : #define cn_bits_MN MAKE_CN_BITS(cn_bits_M, cn_bits_N, 0, 0)
388 : : */
389 : : /**************************************************************************************************
390 : : (P)
391 : : |
392 : : -X- init charge=0
393 : : 2(+.) |
394 : : |
395 : : 1 X+(V); V=valence; all chemical (real) bonds to X have cap=0
396 : : */
397 : : MY_CONST C_NODE cnP_[2] = {
398 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
399 : : { {BNS_VERT_TYPE_ATOM, 0, 0, 1}, {{ 2, 1,1, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
400 : : { {BNS_VT_C_POS, 1 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
401 : : };
402 : : /*
403 : : #define cn_bits_P_ MAKE_CN_BITS(cn_bits_P, 0, 0, 0)
404 : : */
405 : : #ifdef NEVER
406 : : /**************************************************************************************************
407 : : (M)
408 : : |
409 : : -X- init charge=-1 on atom
410 : : 2(-) |
411 : : |
412 : : 1(.) X+(V); V=valence; all chemical (real) bonds to X have cap=0
413 : : */
414 : : MY_CONST C_NODE cnM_[2] = {
415 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
416 : : { {BNS_VERT_TYPE_ATOM, 1, 0, 1}, {{ 2, 1,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
417 : : { {BNS_VT_C_NEG, 0 + 1, 0, 1}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 2 */
418 : : };
419 : : #endif
420 : :
421 : : MY_CONST C_NODE cnM_[1] = {
422 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
423 : : { {BNS_VERT_TYPE_ATOM, 0, 0, 0}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
424 : : };
425 : : /*
426 : : #define cn_bits_M_ MAKE_CN_BITS(cn_bits_M, 0, 0, 0)
427 : : */
428 : : /**************************************************************************************************
429 : :
430 : :
431 : : -X- init charge=0
432 : : |
433 : :
434 : : 1 X(V); V=valence;
435 : : */
436 : : MY_CONST C_NODE cnN_[1] = {
437 : : /* vertex type vCap vFlow val; neigh cap flow; neigh cap flow; neigh cap flow vertex */
438 : : { {BNS_VERT_TYPE_ATOM, 0, 0, 0}, {{ 0, 0,0, 0 },{ 0, 0,0, 0 }, { 0, 0,0, 0 }} }, /* 1 */
439 : : };
440 : : /*
441 : : #define cn_bits_N_ MAKE_CN_BITS(cn_bits_N, 0, 0, 0)
442 : : */
443 : : /**************************************************************************************************/
444 : :
445 : :
446 : : MY_CONST CN_LIST cnList[] = {
447 : : {cnPNPN, cn_bits_PNPN, 0, sizeof( cnPNPN ) / sizeof( cnPNPN[0] )}, /* 0 */
448 : : {cnNPNP, cn_bits_NPNP, 0, sizeof( cnNPNP ) / sizeof( cnNPNP[0] )}, /* 1 */
449 : : {cnNPN, cn_bits_NPN, 0, sizeof( cnNPN ) / sizeof( cnNPN[0] )}, /* 2 */
450 : : {cnPNP, cn_bits_PNP, 0, sizeof( cnPNP ) / sizeof( cnPNP[0] )}, /* 3 */
451 : : {cnMNP, cn_bits_MNP, 0, sizeof( cnMNP ) / sizeof( cnMNP[0] )}, /* 4 */
452 : : {cnPNM, cn_bits_PNM, 0, sizeof( cnPNM ) / sizeof( cnPNM[0] )}, /* 5 */
453 : : {cnEN, cn_bits_EN , 0, sizeof( cnEN ) / sizeof( cnEN[0] )}, /* 6 */
454 : : {cnNMN, cn_bits_NMN, 0, sizeof( cnNMN ) / sizeof( cnNMN[0] )}, /* 7 */
455 : : {cnNE, cn_bits_NE , 0, sizeof( cnNE ) / sizeof( cnNE[0] )}, /* 8 */
456 : : {cnNEN, cn_bits_NEN, 0, sizeof( cnNEN ) / sizeof( cnNEN[0] )}, /* 9 */
457 : : {cnNP, cn_bits_NP, 0, sizeof( cnNP ) / sizeof( cnNP[0] )}, /* 10 */
458 : : {cnPN, cn_bits_PN, 0, sizeof( cnPN ) / sizeof( cnPN[0] )}, /* 11 */
459 : : {cnNM, cn_bits_NM, 0, sizeof( cnNM ) / sizeof( cnNM[0] )}, /* 12 */
460 : : {cnMN, cn_bits_MN, 0, sizeof( cnMN ) / sizeof( cnMN[0] )}, /* 13 */
461 : : {cnP_, cn_bits_P_, 0, sizeof( cnP_ ) / sizeof( cnP_[0] )}, /* 14 */
462 : : {cnM_, cn_bits_M_, -1, sizeof( cnM_ ) / sizeof( cnM_[0] )}, /* 15 */
463 : : {cnN_, cn_bits_N_, 0, sizeof( cnN_ ) / sizeof( cnN_[0] )}, /* 16 */
464 : : {cnMe, cn_bits_Me, 0, sizeof( cnMe ) / sizeof( cnMe[0] )} /* 17 */
465 : : };
466 : :
467 : : #define cnListIndexMe (17) /* index of {cnMe, cn_bits_Me,... } element of cnList[] */
468 : :
469 : : const int cnListNumEl = (int) ( sizeof( cnList ) / sizeof( cnList[0] ) );
470 : :
471 : :
472 : :
473 : : /****************************************************************************/
474 : 0 : void clear_t_group_info( T_GROUP_INFO *ti )
475 : : {
476 [ # # ]: 0 : if (!ti)
477 : : {
478 : 0 : return;
479 : : }
480 : : else
481 : : {
482 : 0 : T_GROUP *t_group = ti->t_group;
483 : 0 : int max_num_t_groups = ti->max_num_t_groups;
484 : 0 : AT_NUMB *tGroupNumber = ti->tGroupNumber;
485 : 0 : int num_t_groups = ti->num_t_groups;
486 : 0 : AT_NUMB *nEndpointAtomNumber = ti->nEndpointAtomNumber;
487 : 0 : int nNumEndpoints = ti->nNumEndpoints;
488 : 0 : AT_NUMB *nIsotopicEndpointAtomNumber = ti->nIsotopicEndpointAtomNumber;
489 : 0 : int nNumIsotopicEndpoints = ti->nNumIsotopicEndpoints;
490 : 0 : memset( ti, 0, sizeof( *ti ) ); /* djb-rwth: memset_s C11/Annex K variant? */
491 [ # # ]: 0 : if (t_group)
492 : : {
493 : 0 : memset( t_group, 0, sizeof( t_group[0] )*max_num_t_groups ); /* djb-rwth: memset_s C11/Annex K variant? */
494 : : }
495 : : else
496 : : {
497 : 0 : max_num_t_groups = 0;
498 : : }
499 [ # # ]: 0 : if (tGroupNumber)
500 : : {
501 : 0 : memset( tGroupNumber, 0, sizeof( tGroupNumber[0] )*num_t_groups ); /* djb-rwth: memset_s C11/Annex K variant? */
502 : : }
503 : : else
504 : : {
505 : 0 : num_t_groups = 0;
506 : : }
507 [ # # ]: 0 : if (nEndpointAtomNumber)
508 : : {
509 : 0 : memset( nEndpointAtomNumber, 0, sizeof( nEndpointAtomNumber[0] )*nNumEndpoints ); /* djb-rwth: memset_s C11/Annex K variant? */
510 : : }
511 : : else
512 : : {
513 : 0 : nNumEndpoints = 0;
514 : : }
515 [ # # ]: 0 : if (nIsotopicEndpointAtomNumber)
516 : : {
517 : 0 : memset( nIsotopicEndpointAtomNumber, 0, sizeof( nIsotopicEndpointAtomNumber[0] )*nNumIsotopicEndpoints ); /* djb-rwth: memset_s C11/Annex K variant? */
518 : : }
519 : : else
520 : : {
521 : 0 : nNumIsotopicEndpoints = 0;
522 : : }
523 : :
524 : 0 : ti->t_group = t_group;
525 : 0 : ti->max_num_t_groups = max_num_t_groups;
526 : 0 : ti->tGroupNumber = tGroupNumber;
527 : 0 : ti->num_t_groups = num_t_groups;
528 : 0 : ti->nEndpointAtomNumber = nEndpointAtomNumber;
529 : 0 : ti->nNumEndpoints = nNumEndpoints;
530 : 0 : ti->nIsotopicEndpointAtomNumber = nIsotopicEndpointAtomNumber;
531 : :
532 : 0 : ti->nNumIsotopicEndpoints = nNumIsotopicEndpoints;
533 : : }
534 : :
535 : 0 : return;
536 : : }
537 : :
538 : :
539 : : /****************************************************************************/
540 : 0 : int GetTgroupInfoFromInChI( T_GROUP_INFO *ti,
541 : : inp_ATOM *at,
542 : : AT_NUMB *endpoint,
543 : : INChI *pInChI )
544 : : {
545 : : int ret, i, j, k, itg, num_atoms, len_tg, num_t_groups; /* djb-rwth: removing redundant variables */
546 : 0 : AT_NUMB *tGroupNumber = NULL;
547 : : /* djb-rwth: removing redundant variables */
548 : 0 : AT_NUMB *tiGroupNumber = NULL;
549 : :
550 : 0 : ret = 0;
551 : :
552 : 0 : clear_t_group_info( ti );
553 [ # # # # ]: 0 : if (pInChI && pInChI->lenTautomer > 1 &&
554 [ # # # # ]: 0 : pInChI->nTautomer && pInChI->nTautomer[0] > 0)
555 : : {
556 : 0 : num_atoms = pInChI->nNumberOfAtoms;
557 : : /* djb-rwth: removing redundant code */
558 : 0 : num_t_groups = pInChI->nTautomer[0];
559 : 0 : len_tg = pInChI->lenTautomer - T_GROUP_HDR_LEN*pInChI->nTautomer[0] - 1; /* number of endpoints */
560 : :
561 : : /* allocation ti->t_group */
562 [ # # # # ]: 0 : if (ti->max_num_t_groups != num_atoms / 2 + 1 || !ti->t_group)
563 : : {
564 : 0 : ti->max_num_t_groups = num_atoms / 2 + 1;
565 [ # # ]: 0 : if (ti->t_group)
566 [ # # ]: 0 : inchi_free( ti->t_group );
567 : 0 : ti->t_group = (T_GROUP *) inchi_calloc( (long long)ti->max_num_t_groups + 1, sizeof( ti->t_group[0] ) ); /* djb-rwth: correcting 0 bytes allocation */
568 : : }
569 : :
570 : : /* allocation ti->tGroupNumber */
571 [ # # # # ]: 0 : if (ti->num_t_groups != num_t_groups || !ti->tGroupNumber)
572 : : {
573 : 0 : ti->num_t_groups = num_t_groups;
574 [ # # ]: 0 : if (ti->tGroupNumber)
575 [ # # ]: 0 : inchi_free( ti->tGroupNumber );
576 : 0 : ti->tGroupNumber = (AT_NUMB *) inchi_calloc( ( (long long)ti->num_t_groups + 1 )*TGSO_TOTAL_LEN, sizeof( ti->tGroupNumber[0] ) ); /* djb-rwth: cast operator added */
577 : : }
578 : :
579 : : /* allocation ti->tGroupNumber */
580 [ # # # # ]: 0 : if (len_tg != ti->nNumEndpoints || !ti->nEndpointAtomNumber)
581 : : {
582 : 0 : ti->nNumEndpoints = len_tg;
583 [ # # ]: 0 : if (ti->nEndpointAtomNumber)
584 [ # # ]: 0 : inchi_free( ti->nEndpointAtomNumber );
585 : 0 : ti->nEndpointAtomNumber = (AT_NUMB *) inchi_calloc( (long long)len_tg + 1, sizeof( ti->nEndpointAtomNumber[0] ) ); /* djb-rwth: cast operator added */
586 : : }
587 : :
588 : : /* check */
589 [ # # # # : 0 : if (!ti->t_group || !ti->tGroupNumber || !ti->nEndpointAtomNumber)
# # ]
590 : : {
591 : 0 : ret = RI_ERR_ALLOC;
592 : 0 : goto exit_function;
593 : : }
594 : :
595 : 0 : tGroupNumber = ti->tGroupNumber;
596 : : /* djb-rwth: removing redundant code */
597 : 0 : tiGroupNumber = tGroupNumber + TGSO_SYMM_IORDER * (long long)ti->num_t_groups; /* djb-rwth: cast operator added */
598 : :
599 : :
600 : : INCHI_HEAPCHK
601 : :
602 : 0 : j = 1; /* index in pInChI->nTautomer[] */
603 : 0 : i = 0; /* index in ti->nEndpointAtomNumber[] */
604 : :
605 : : /* djb-rwth: fixing oss-fuzz issues #67681, #67641 */
606 [ # # # # ]: 0 : for (itg = 0; itg < pInChI->nTautomer[0] && itg <= ti->max_num_t_groups; itg++)
607 : : {
608 : 0 : len_tg = pInChI->nTautomer[j]; /* t-group length not including pInChI->nTautomer[j] */
609 : 0 : ti->t_group[itg].num[0] = pInChI->nTautomer[j + 1] + pInChI->nTautomer[j + 2]; /* num mobile H & (-) */
610 : 0 : ti->t_group[itg].num[1] = pInChI->nTautomer[j + 2]; /* num mobile (-) */
611 : 0 : tGroupNumber[itg] = tiGroupNumber[itg] = itg; /* index */
612 : 0 : ti->t_group[itg].nGroupNumber = /*tSymmRank[itg] = tiSymmRank[itg] =*/ itg + 1; /* t-group number */
613 : 0 : j += T_GROUP_HDR_LEN; /* skip t-group header */
614 : 0 : len_tg -= T_GROUP_HDR_LEN - 1;
615 : :
616 : 0 : ti->t_group[itg].nNumEndpoints = len_tg;
617 : 0 : ti->t_group[itg].nFirstEndpointAtNoPos = i;
618 : :
619 [ # # ]: 0 : while (len_tg > 0)
620 : : {
621 : 0 : k = ti->nEndpointAtomNumber[i] = pInChI->nTautomer[j] - 1; /* djb-rwth: buffer overrun avoided implicitly in loop condition */
622 : : #if ( FIX_GAF_2019_1==1 )
623 [ # # # # ]: 0 : if (k<0 || k>num_atoms)
624 : : {
625 : 0 : ret = RI_ERR_PROGR;
626 : 0 : goto exit_function;
627 : : }
628 : : #endif
629 [ # # ]: 0 : if (at)
630 : : {
631 : 0 : at[k].endpoint = itg + 1;
632 : : }
633 [ # # ]: 0 : if (endpoint)
634 : : {
635 : 0 : endpoint[k] = itg + 1;
636 : : }
637 : 0 : len_tg--;
638 : 0 : j++;
639 : 0 : i++;
640 : : }
641 : : }
642 : :
643 [ # # ]: 0 : if (i != ti->nNumEndpoints)
644 : : {
645 : 0 : ret = RI_ERR_PROGR;
646 : : }
647 : :
648 : : INCHI_HEAPCHK
649 : : }
650 : :
651 : 0 : exit_function:
652 : 0 : return ret;
653 : : }
654 : :
655 : :
656 : : /****************************************************************************/
657 : 0 : int FillOutpStructEndpointFromInChI( INChI *pInChI, AT_NUMB **pEndpoint )
658 : : {
659 : 0 : int num_at = pInChI->nNumberOfAtoms;
660 : 0 : AT_NUMB *endpoint = *pEndpoint;
661 : : int itg, i, j, k, len_tg;
662 : :
663 [ # # # # ]: 0 : if (!endpoint && !( endpoint = (AT_NUMB*) inchi_malloc( num_at * sizeof( endpoint[0] ) ) ))
664 : : {
665 : 0 : return RI_ERR_ALLOC;
666 : : }
667 : :
668 : 0 : memset( endpoint, 0, num_at * sizeof( endpoint[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
669 [ # # # # ]: 0 : if (pInChI->lenTautomer <= 1 || !pInChI->nTautomer)
670 : : {
671 : 0 : goto exit_function;
672 : : }
673 : :
674 : 0 : j = 1; /* index in pInChI->nTautomer[] */
675 : 0 : i = 0; /* index in ti->nEndpointAtomNumber[] */
676 [ # # ]: 0 : for (itg = 0; itg < pInChI->nTautomer[0]; itg++)
677 : : {
678 : 0 : len_tg = pInChI->nTautomer[j]; /* t-group length not including pInChI->nTautomer[j] */
679 : 0 : j += T_GROUP_HDR_LEN; /* skip t-group header */
680 : 0 : len_tg -= T_GROUP_HDR_LEN - 1;
681 : : /* ti->t_group[itg].nNumEndpoints = len_tg; */
682 [ # # ]: 0 : for (; 0 < len_tg--; j++, i++)
683 : : {
684 : 0 : k = pInChI->nTautomer[j] - 1;
685 : 0 : endpoint[k] = itg + 1;
686 : : }
687 : : }
688 : :
689 : 0 : exit_function:
690 : 0 : *pEndpoint = endpoint;
691 : :
692 : 0 : return 0;
693 : : }
694 : :
695 : :
696 : : /************************************************************************************/
697 : 0 : int cmp_charge_val( const void *a1, const void *a2, void *p )
698 : : {
699 : 0 : const CHARGE_VAL *p1 = (const CHARGE_VAL *) a1;
700 : 0 : const CHARGE_VAL *p2 = (const CHARGE_VAL *) a2;
701 : : int diff;
702 : :
703 [ # # ]: 0 : if ((diff = (int) p1->nValence - (int) p2->nValence)) /* smaller valence first */ /* djb-rwth: addressing LLVM warning */
704 : 0 : return diff;
705 [ # # ]: 0 : if ((diff = abs( (int) p1->nCharge ) - abs( (int) p2->nCharge ))) /* smaller abs charge first */ /* djb-rwth: addressing LLVM warning */
706 : 0 : return diff;
707 [ # # ]: 0 : if ((diff = (int) p2->nCharge - (int) p1->nCharge)) /* (+) first, (-) second */ /* djb-rwth: addressing LLVM warning */
708 : 0 : return diff;
709 : 0 : return (int) p1->nValenceOrderingNumber - (int) p2->nValenceOrderingNumber;
710 : : }
711 : :
712 : :
713 : : /************************************************************************************/
714 : 0 : int bMayBeACationInMobileHLayer( inp_ATOM *at, VAL_AT *pVA, int iat, int bMobileH )
715 : : {
716 : : int j, neigh;
717 : : U_CHAR cVal; /* moved from below 2024-09-01 DT */
718 [ # # # # ]: 0 : if (!bMobileH || !at[iat].num_H)
719 : : {
720 : 0 : return 1;
721 : : }
722 : :
723 : : /* cVal, cation valence */
724 [ # # # ]: 0 : switch ( at[iat].el_number ) {
725 : 0 : case EL_NUMBER_N: /* fallthrough */
726 : : case EL_NUMBER_P:
727 : 0 : cVal = 4;
728 : 0 : break;
729 : 0 : case EL_NUMBER_O: /* fallthrough */
730 : : case EL_NUMBER_S:
731 : : case EL_NUMBER_SE:
732 : : case EL_NUMBER_TE:
733 : 0 : cVal = 3;
734 : 0 : break;
735 : 0 : default:
736 : 0 : return 1;
737 : : }
738 : :
739 [ # # ]: 0 : if (at[iat].valence + at[iat].num_H <= cVal)
740 : : {
741 [ # # ]: 0 : for (j = 0; j < at[iat].valence; j++)
742 : : {
743 : 0 : neigh = at[iat].neighbor[j];
744 [ # # # # : 0 : if (at[neigh].valence == 4 && at[neigh].chem_bonds_valence == 4 && !at[neigh].num_H &&
# # ]
745 [ # # # # ]: 0 : pVA[neigh].cNumValenceElectrons == 3 && pVA[neigh].cPeriodicRowNumber == 1)
746 : : {
747 : 0 : return 1;
748 : : }
749 : : }
750 : 0 : return 0;
751 : : }
752 : 0 : return 1;
753 : : }
754 : :
755 : :
756 : : /************************************************************************************/
757 : 0 : int clean_charge_val( struct tagCANON_GLOBALS *pCG, CHARGE_VAL *pChargeVal, int len, inp_ATOM *atom, VAL_AT *pVA,
758 : : int iat, int bIsMetal, int bMobileH, AT_NUMB *endpoint )
759 : : {
760 : 0 : inp_ATOM *at = atom + iat;
761 : 0 : int nPeriodicNum = at->el_number;
762 : 0 : int num_bonds = at->valence;
763 : 0 : int min_valence = at->valence + at->num_H;
764 : : /* in fixed-H case treat tautomeric -O as tautomeric to avoid #O(+) */
765 : 0 : int bTautomeric = ( at->endpoint != 0 );
766 [ # # # # : 0 : int bFixedHTautomeric = !bMobileH && ( endpoint && endpoint[iat] &&
# # ]
767 [ # # # # ]: 0 : pVA[iat].cNumValenceElectrons == 6 && 1 == num_bonds &&
768 [ # # # # ]: 0 : !at->num_H && !bIsMetal );
769 : : /* int bIsMetal = is_el_a_metal( nPeriodicNum );*/
770 : 0 : int bDoNotAddH = if_skip_add_H( nPeriodicNum );
771 : : int nPeriod, nNumEqAbsCharges;
772 : 0 : int nNumValenceEl = get_sp_element_type( nPeriodicNum, &nPeriod ) - 1;
773 : :
774 : : int i, j;
775 : :
776 [ # # ]: 0 : if (!len)
777 : 0 : return len;
778 : :
779 : 0 : insertions_sort( pCG, pChargeVal, len, sizeof( pChargeVal[0] ), cmp_charge_val );
780 : :
781 : : /* metals -- very preliminary code */
782 [ # # # # ]: 0 : if (bIsMetal && bDoNotAddH)
783 : : {
784 : : /* keep the 1st found */
785 : 0 : return inchi_min( 1, len );
786 : : }
787 : : /* Mobile-H layer cannot have H on positively charged N, P (all IV), O, S, Se, Te (all III) */
788 : :
789 : : /*
790 : : if ( abs( pChargeVal[0].nCharge ) > 1 && pChargeVal[0].nValence >= min_valence ) {
791 : : return inchi_min( 1, len );
792 : : }
793 : : */
794 : 0 : nNumEqAbsCharges = 0;
795 [ # # # # : 0 : for (i = j = 0; i < len && j < ( nNumEqAbsCharges ? 3 + nNumEqAbsCharges : 4 ); i++)
# # ]
796 : : {
797 : : /* for now accept only charge = 0, -1, +1 */
798 [ # # ]: 0 : if (abs( pChargeVal[i].nCharge ) > 1)
799 : : {
800 : 0 : continue;
801 : : }
802 [ # # ]: 0 : if (BOND_TYPE_TRIPLE + BOND_TYPE_DOUBLE * ( min_valence - 1 ) < pChargeVal[i].nValence)
803 : : {
804 : 0 : continue; /* not more than one triple and the rest - double bonds per atom */
805 : : }
806 [ # # # # : 0 : if (( bTautomeric || (j && bFixedHTautomeric) ) && pChargeVal[i].nCharge < 0) /* djb-rwth: addressing LLVM warning */
# # # # ]
807 : : {
808 : 0 : continue; /* negative charge must be included in the tautomeric group */
809 : : }
810 [ # # # # : 0 : if (( bTautomeric || bFixedHTautomeric ) && pChargeVal[i].nCharge > 0)
# # ]
811 : : {
812 : 0 : continue; /* positive charge for now cannot reach a tautomeric group */
813 : : }
814 [ # # # # : 0 : if (j && !bMayBeACationInMobileHLayer( atom, pVA, iat, bMobileH ) && pChargeVal[i].nCharge > 0)
# # ]
815 : : {
816 [ # # ]: 0 : if (i + 1 < len &&
817 [ # # ]: 0 : pChargeVal[i].nValence == pChargeVal[i + 1].nValence &&
818 [ # # ]: 0 : pChargeVal[i].nCharge == -pChargeVal[i + 1].nCharge)
819 : : {
820 : : /* (-) if exists is always after (+) */
821 : 0 : i += 1; /* also skip the next element */
822 : : }
823 : 0 : continue; /* in case of Mobile-H, a hydrogen cannot be on a (+)-charged heteroatom */
824 : : }
825 : :
826 : : /* accept same valence opposite charges only for C and its group in Periodic Table */
827 [ # # # # ]: 0 : if (j && !bTautomeric &&
828 [ # # ]: 0 : pChargeVal[i].nValence == pChargeVal[j - 1].nValence &&
829 [ # # ]: 0 : pChargeVal[i].nCharge == -pChargeVal[j - 1].nCharge)
830 : : {
831 [ # # # # : 0 : if (nNumValenceEl == VALUE_OCTET / 2 && pChargeVal[i].nCharge && !nNumEqAbsCharges)
# # ]
832 : : {
833 : 0 : pChargeVal[j++] = pChargeVal[i];
834 : 0 : nNumEqAbsCharges++;
835 : : }
836 : 0 : continue;
837 : : }
838 : : /* do not accept valence=5 for neutral NHn in case of not Mobile-H 2005-01-26 ???? */
839 [ # # # # : 0 : if (nNumValenceEl == 5 && nPeriod == 1 && at->num_H &&
# # # # ]
840 [ # # ]: 0 : j && !bMobileH &&
841 [ # # # # ]: 0 : pChargeVal[i].nValence == 5 && !pChargeVal[i].nCharge)
842 : : {
843 : 0 : continue;
844 : : }
845 : : /* do not accept gaps in allowed valences */
846 [ # # # # ]: 0 : if (j && pChargeVal[i].nValence > pChargeVal[j - 1].nValence + 1)
847 : : {
848 : 0 : break;
849 : : }
850 : 0 : pChargeVal[j++] = pChargeVal[i];
851 : : }
852 : 0 : len = j;
853 [ # # # # : 0 : if (!nNumEqAbsCharges && num_bonds < 3 && len == 4)
# # ]
854 : : {
855 : 0 : len--; /* prohibit =S# where # is a triple bond */
856 : : }
857 : 0 : return len;
858 : : }
859 : :
860 : :
861 : : /****************************************************************************
862 : : int GetAtomRestoreInfo( CANON_GLOBALS *pCG, inp_ATOM *atom, int iat, VAL_AT *pVArray )
863 : :
864 : :
865 : : pVA->cDoNotAddH
866 : : pVA->cMetal
867 : : pVA->cNumValenceElectrons
868 : : pVA->cPeriodicRowNumber
869 : : pVA->cInitFreeValences
870 : : pVA->cnListIndex = index+1
871 : :
872 : : return value:
873 : : -1 => error
874 : : 0 => do not know what to do; leave the atom unchanged
875 : : 1 => success
876 : : ****************************************************************************/
877 : 0 : int GetAtomRestoreInfo(struct tagCANON_GLOBALS* pCG,
878 : : inp_ATOM* atom, int iat, VAL_AT* pVArray,
879 : : ICHICONST SRM* pSrm, int bMobileH, AT_NUMB* endpoint)
880 : : {
881 : : /* #defines from util.c */
882 : : #define MIN_ATOM_CHARGE (-2)
883 : : #define MAX_ATOM_CHARGE 2
884 : : #define NEUTRAL_STATE (-MIN_ATOM_CHARGE)
885 : : #define NUM_ATOM_CHARGES (MAX_ATOM_CHARGE - MIN_ATOM_CHARGE + 1)
886 : : #define MAX_NUM_VALENCES 5 /* max. number + 1 to provide zero termination */
887 : :
888 : : int i, j, j2, k, k2, charge, cur_charge, num_non_bonding_electrons;
889 : : int nNumStates, nNumSelectedStates, num_H, num_bonds;
890 : : int nOctetNeutralValenceExcess, nFirstNeutralValenceExcess;
891 : : int nFoundNeutralValenceExcess, nFoundNeutralValenceOrdNumber;
892 : : int nLastFoundValenceOrdNumber, nLastFoundValenceState;
893 : : int cn_bits, cn_bits_array[5], len_cn_bits_array;
894 : 0 : inp_ATOM* at = atom + iat;
895 : 0 : VAL_AT* pVA = pVArray + iat;
896 : 0 : int nPeriodicNum = at->el_number;
897 : : int cur_chem_valence, cur_chem_valence_fixed, min_chem_valence, known_chem_valence;
898 : : int metal_bonds_chem_valence, not_metal_bonds_chem_valence, alt_bonds_delta_valence, bonds_chem_valence, bond_type;
899 : : CHARGE_VAL ChargeVal[NUM_ATOM_CHARGES * MAX_NUM_VALENCES];
900 : :
901 : 0 : memset(ChargeVal, 0, sizeof(ChargeVal));
902 : :
903 : 0 : pVA->cDoNotAddH = if_skip_add_H(nPeriodicNum); /* InChI never adds H to this atom */
904 : : /*pVA->cMetal = is_el_a_metal( nPeriodicNum );*/ /* the atom is a metal */
905 : :
906 : : /* count bonds to metal atoms; metals have already been marked */
907 : 0 : metal_bonds_chem_valence = not_metal_bonds_chem_valence = alt_bonds_delta_valence = 0;
908 : :
909 [ # # ]: 0 : if (pVA->cMetal)
910 : : {
911 : 0 : j = at->valence; /* all bonds to metal */
912 [ # # ]: 0 : for (i = k = j2 = k2 = 0; i < at->valence; i++)
913 : : {
914 : 0 : bond_type = (at->bond_type[i] & BOND_TYPE_MASK);
915 [ # # ]: 0 : if (bond_type <= BOND_TYPE_TRIPLE)
916 : : {
917 : 0 : metal_bonds_chem_valence += inchi_max(BOND_TYPE_SINGLE, bond_type);
918 : : }
919 : : else
920 : : {
921 : 0 : metal_bonds_chem_valence += BOND_TYPE_SINGLE;
922 : 0 : k++; /* count alternating bonds */
923 : : }
924 : : }
925 : : }
926 : : else
927 : : {
928 [ # # ]: 0 : for (i = j = j2 = k = k2 = 0; i < at->valence; i++)
929 : : {
930 : 0 : bond_type = (at->bond_type[i] & BOND_TYPE_MASK);
931 [ # # ]: 0 : if (pVArray[(int)at->neighbor[i]].cMetal)
932 : : {
933 : 0 : j++; /* number of bonds to metal atoms */
934 [ # # ]: 0 : if (bond_type <= BOND_TYPE_TRIPLE)
935 : : {
936 : 0 : metal_bonds_chem_valence += inchi_max(BOND_TYPE_SINGLE, bond_type);
937 : : }
938 : : else
939 : : {
940 : 0 : metal_bonds_chem_valence += BOND_TYPE_SINGLE;
941 : 0 : k++; /* count alternating bonds */
942 : : }
943 : : }
944 : : else
945 : : {
946 : 0 : j2++;
947 [ # # ]: 0 : if (bond_type <= BOND_TYPE_TRIPLE)
948 : : {
949 : 0 : not_metal_bonds_chem_valence += inchi_max(BOND_TYPE_SINGLE, bond_type);
950 : : }
951 : : else
952 : : {
953 : 0 : not_metal_bonds_chem_valence += BOND_TYPE_SINGLE;
954 : 0 : k2++; /* count alternating bonds */
955 : : }
956 : : }
957 : : }
958 : : }
959 : :
960 : 0 : bonds_chem_valence = metal_bonds_chem_valence + not_metal_bonds_chem_valence;
961 : :
962 [ # # ]: 0 : if (at->chem_bonds_valence > bonds_chem_valence)
963 : : {
964 [ # # ]: 0 : if (at->chem_bonds_valence - bonds_chem_valence > 1)
965 : : {
966 : 0 : at->chem_bonds_valence = bonds_chem_valence + 1; /* should not happen */
967 : : }
968 : 0 : alt_bonds_delta_valence = at->chem_bonds_valence - bonds_chem_valence;
969 : : }
970 : :
971 : 0 : pVA->cNumBondsToMetal = j;
972 : :
973 [ # # ]: 0 : if (nPeriodicNum == EL_NUMBER_H)
974 : : {
975 : : /* ignore bridging H; ??? later add ??? */
976 : 0 : return 0;
977 : : }
978 : :
979 : 0 : num_H = at->num_H;
980 : 0 : num_bonds = at->valence;
981 : :
982 [ # # # # ]: 0 : if (!num_bonds && !num_H)
983 : : {
984 : 0 : return 0; /* do not know the answer: isolated atom */
985 : : }
986 : : /* at the beginning all bonds are single */
987 : 0 : min_chem_valence = num_bonds + num_H;
988 : 0 : cur_chem_valence = bonds_chem_valence + alt_bonds_delta_valence + num_H; /* includes double & alternating bond contribution */
989 : :
990 : : /* number of non-bonding electrons in case of all single bonds */
991 : 0 : num_non_bonding_electrons = (int)pVA->cNumValenceElectrons - min_chem_valence;
992 : : /* Octet rule: charge = bonds_valence + NumValenceElectrons - 8 */
993 : 0 : charge = min_chem_valence + (int)pVA->cNumValenceElectrons - VALUE_OCTET; /* wrong */
994 : :
995 : : /* typical (ad hoc) minimal neutral valence */
996 : 0 : known_chem_valence = (pVA->cNumValenceElectrons > VALUE_OCTET / 2) ?
997 [ # # ]: 0 : VALUE_OCTET - pVA->cNumValenceElectrons :
998 : 0 : pVA->cNumValenceElectrons;
999 : : /* excess of typical valence over all-single-bonds valence */
1000 : 0 : nOctetNeutralValenceExcess = known_chem_valence - min_chem_valence;
1001 : : /* (NB=num.bonds, NV=neutral valence, NVX=neutral valence excess, LFVS=last found valence state, val.=valence)
1002 : :
1003 : : element NB knownFst octet Last octetNVX firstNVX foundNVX chargeLFVS LFVS
1004 : : valence val. NV>=
1005 : :
1006 : : -B 1 3 3 3 2 2 = 2 +2
1007 : : >B 2 3 3 3 1 1 = 1 +1
1008 : : >B- 3 3 3 3 0 0 = 0 0
1009 : : >B< 4 3 3 3 -1 -1 <> N/A -1
1010 : :
1011 : : -C 1 4 4 4 3 3 = 3 N/A
1012 : : >C 2 4 4 4 2 2 = 2 +2 (-2)
1013 : : >C- 3 4 4 4 1 1 = 1 +1 (-1)
1014 : : >C< 4 4 4 4 0 0 = 0 0
1015 : : C(V) 5 4 4 N/A -1 -1 <> N/A N/A
1016 : :
1017 : : -Si 1 4 4 4 3 3 = 3 N/A
1018 : : >Si 2 4 4 4 2 2 = 2 +2 (-2)
1019 : : >Si- 3 4 4 4 1 1 = 1 +1 (-1)
1020 : : >Si- 4 4 4 4 0 0 = 0 0
1021 : : Si(V) 5 4 4 N/A -1 -1 <> N/A -1
1022 : :
1023 : : -N 1 3 3 3 2 2 = 2 -2
1024 : : >N 2 3 3 3 1 1 = 1 -1
1025 : : >N- 3 3 3 3 0 0 = 0 0 (+2)
1026 : : >N< 4 3 3 5 -1 -1 <> 1 +1
1027 : : N(V) 5 3 3 5 -2 -2 <> 0 0
1028 : : N(VI) 6 3 3 N/A -3 -3 <> N/A N/A
1029 : : N(VII) 7 3 3 N/A -4 -4 <> N/A N/A
1030 : :
1031 : : -P 1 3 3 3 2 2 = 2 -2
1032 : : >P 2 3 3 3 1 1 = 1 -1
1033 : : >P- 3 3 3 3 0 0 = 0 0 (-2, +2)
1034 : : >P< 4 3 3 5 -1 -1 <> 1 +1 (-1)
1035 : : P(V) 5 3 3 5 -2 -2 <> 0 0 (-2)
1036 : : P(VI) 6 3 3 N/A -3 -3 <> N/A -1
1037 : : P(VII) 7 3 3 N/A -4 -4 <> N/A -2
1038 : : P(VIII) 8 3 3 N/A -5 -5 <> N/A N/A
1039 : :
1040 : : -O 1 2 2 2 1 1 = 1 -1
1041 : : >O 2 2 2 2 0 0 = 0 0
1042 : : >O- 3 2 2 N/A -1 -1 <> N/A +1
1043 : : >O< 4 2 2 N/A -2 -2 <> N/A +2
1044 : : O(V) 5 2 2 N/A -3 -3 <> N/A +1
1045 : : O(VI) 6 2 2 N/A -4 -4 <> N/A N/A
1046 : :
1047 : : -S 1 2 2 2 1 1 = 1 -1
1048 : : >S 2 2 2 2 0 0 = 0 0 NPNP - prohibit
1049 : : >S- 3 2 2 4 -1 -1 <> 1 +1 (-1) PNPN
1050 : : >S< 4 2 2 4 -2 -2 <> 0 0 (+2)
1051 : : S(V) 5 2 2 6 -3 -3 <> 1 +1 (-1)
1052 : : S(VI) 6 2 2 6 -4 -4 <> 0 0
1053 : : S(VII) 7 2 2 N/A -5 -5 <> 0 -1
1054 : : S(VIII) 8 2 2 N/A -6 -6 <> N/A N/A
1055 : :
1056 : : -F 1 1 1 1 0 0 = 0 0
1057 : : >F 2 1 1 1 -1 -1 <> N/A +1
1058 : : >F- 3 1 1 1 -2 -2 <> N/A +2
1059 : : >F< 4 1 1 1 -3 -3 <> N/A N/A
1060 : : F(V) 5 1 1 1 -4 -4 <> N/A +2
1061 : : F(VI) 6 1 1 1 -5 -5 <> N/A N/A
1062 : :
1063 : : -Cl 1 1 1 1 0 0 = 0 0 NPNP - prohibit
1064 : : >Cl 2 1 1 3 -1 -1 <> 1 +1 PNPN - prohibit
1065 : : >Cl- 3 1 1 3 -2 -2 <> 0 0 (+2) NPNP
1066 : : >Cl< 4 1 1 5 -3 -3 <> 1 +1 PNPN
1067 : : Cl(V) 5 1 1 5 -4 -4 <> 0 0
1068 : : Cl(VI) 6 1 1 7 -5 -5 <> 1 +1
1069 : : Cl(VII) 7 1 1 7 -6 -6 <> 0 0
1070 : : Cl(VIII) 8 1 1 N/A -7 -7 <> N/A N/A
1071 : :
1072 : :
1073 : : NB = num_bonds+num_H
1074 : :
1075 : : knownFst valence = nFirstNeutralValenceExcess + min_chem_valence
1076 : : octet val. = nOctetNeutralValenceExcess + min_chem_valence
1077 : : Last NV>= = nFoundNeutralValenceExcess + min_chem_valence
1078 : :
1079 : : octetNVX = nOctetNeutralValenceExcess
1080 : : firstNVX = nFirstNeutralValenceExcess
1081 : : foundNVX = nFoundNeutralValenceExcess
1082 : :
1083 : : chargeLFVS = ChargeVal[nLastFoundValenceState].nCharge
1084 : :
1085 : : */
1086 : : /* minimal known neutral atom valence; different for Sn(2/4), Tl(1/3), Pb(2/4): (known/typical ad hoc) */
1087 : 0 : known_chem_valence = get_el_valence( nPeriodicNum, 0, 0 );
1088 : :
1089 [ # # ]: 0 : if (pSrm->bMetalAddFlower)
1090 : : {
1091 : : /* bond orders of bonds to metal may be as they are (pSrm->nMetalInitBondOrder==1)
1092 : : or decreased by one (pSrm->nMetalInitBondOrder==0)
1093 : : nMetalInitBondOrder == nMetalMinBondOrder + nMetalInitEdgeFlow
1094 : : */
1095 : 0 : cur_chem_valence_fixed = cur_chem_valence - pVA->cNumBondsToMetal * ( 1 - pSrm->nMetalInitBondOrder );
1096 : 0 : pVA->cInitOrigValenceToMetal = metal_bonds_chem_valence;
1097 : 0 : pVA->cInitValenceToMetal = metal_bonds_chem_valence - pVA->cNumBondsToMetal * ( 1 - pSrm->nMetalInitBondOrder );
1098 : 0 : pVA->cInitFlowToMetal = pVA->cInitValenceToMetal - pVA->cNumBondsToMetal * pSrm->nMetalMinBondOrder;
1099 [ # # ]: 0 : if (pVA->cMetal)
1100 : : {
1101 : 0 : pVA->cInitFreeValences += alt_bonds_delta_valence;
1102 : : }
1103 [ # # ]: 0 : if (pSrm->nMetalInitEdgeFlow < pSrm->nMetalInitBondOrder - pSrm->nMetalMinBondOrder)
1104 : : {
1105 : : /* single bond has zero initial flow + 2 radicals at incident atoms */
1106 [ # # ]: 0 : if (pVA->cInitFlowToMetal <= pVA->cNumBondsToMetal)
1107 : : {
1108 [ # # ]: 0 : if (pVA->cMetal)
1109 : : {
1110 : 0 : pVA->cInitFreeValences += pVA->cInitFlowToMetal;
1111 : : }
1112 : 0 : pVA->cInitFlowToMetal = 0;
1113 : : }
1114 : : else
1115 : : {
1116 [ # # ]: 0 : if (pVA->cMetal)
1117 : : {
1118 : 0 : pVA->cInitFreeValences += pVA->cNumBondsToMetal * ( 1 - pSrm->nMetalInitEdgeFlow );
1119 : : }
1120 : 0 : pVA->cInitFlowToMetal -= pVA->cNumBondsToMetal * ( 1 - pSrm->nMetalInitEdgeFlow );
1121 : : }
1122 : : }
1123 : : }
1124 : : else
1125 : : {
1126 : : /* treat metal atoms as ordinary non-metal atoms */
1127 : 0 : cur_chem_valence_fixed = cur_chem_valence;
1128 : 0 : pVA->cInitFlowToMetal = metal_bonds_chem_valence - pVA->cNumBondsToMetal;
1129 : 0 : pVA->cInitValenceToMetal = metal_bonds_chem_valence;
1130 : 0 : pVA->cInitOrigValenceToMetal = metal_bonds_chem_valence;
1131 : : }
1132 : :
1133 : :
1134 [ # # # # ]: 0 : if (pVA->cMetal && pSrm->bMetalAddFlower)
1135 : : {
1136 : 0 : pVA->cnListIndex = cnListIndexMe + 1;
1137 : : /*
1138 : : pVA->cInitOrigValenceToMetal += alt_bonds_delta_valence;
1139 : : pVA->cInitValenceToMetal += alt_bonds_delta_valence;
1140 : : pVA->cInitFreeValences = (pSrm->nMetalInitBondOrder + alt_bonds_delta_valence
1141 : : - (pSrm->nMetalMinBondOrder + pSrm->nMetalInitEdgeFlow)) * pVA->cNumBondsToMetal;
1142 : : */
1143 : 0 : return 0; /* metal */
1144 : : }
1145 : :
1146 [ # # ]: 0 : if (!known_chem_valence)
1147 : : {
1148 : : /* a noble gas like He, Ne, ... */
1149 : 0 : pVA->cInitFreeValences = at->chem_bonds_valence - at->valence;
1150 : 0 : return TREAT_ATOM_AS_METAL; /* do not know anything about this atom; needs 2nd pass */
1151 : : }
1152 : :
1153 : 0 : nFirstNeutralValenceExcess = known_chem_valence - min_chem_valence;
1154 : :
1155 : 0 : nFoundNeutralValenceExcess = NO_VALUE_INT;
1156 : 0 : nFoundNeutralValenceOrdNumber = NO_VALUE_INT;
1157 : 0 : nLastFoundValenceOrdNumber = NO_VALUE_INT;
1158 : 0 : nLastFoundValenceState = NO_VALUE_INT;
1159 : :
1160 : : /* find the lowest known valence >= all-single-bonds valence */
1161 [ # # ]: 0 : for (cur_charge = MIN_ATOM_CHARGE, nNumStates = 0; cur_charge <= MAX_ATOM_CHARGE; cur_charge++)
1162 : : {
1163 [ # # ]: 0 : for (i = 0; i < MAX_NUM_VALENCES; i++)
1164 : : {
1165 : 0 : known_chem_valence = get_el_valence(nPeriodicNum, cur_charge, i);
1166 [ # # # # ]: 0 : if (cur_chem_valence_fixed > known_chem_valence || !known_chem_valence)
1167 : : {
1168 : 0 : continue; /* known valence < all-single-bonds valence */
1169 : : }
1170 [ # # ]: 0 : if (BOND_TYPE_TRIPLE + BOND_TYPE_DOUBLE * (num_bonds - 1) + num_H < known_chem_valence)
1171 : : {
1172 : 0 : continue; /* not more than one triple and the rest - double bonds per atom */
1173 : : }
1174 : :
1175 : : /* keep all found */
1176 : 0 : ChargeVal[nNumStates].nValence = known_chem_valence;
1177 : 0 : ChargeVal[nNumStates].nCharge = cur_charge;
1178 : 0 : ChargeVal[nNumStates].nValenceOrderingNumber = i;
1179 [ # # # # ]: 0 : if (!cur_charge && nFoundNeutralValenceExcess == NO_VALUE_INT)
1180 : : {
1181 : : /* neutral state; compare to the lowest typical valence */
1182 : 0 : nFoundNeutralValenceExcess = known_chem_valence - min_chem_valence;
1183 : 0 : nFoundNeutralValenceOrdNumber = i;
1184 : : }
1185 [ # # ]: 0 : if (min_chem_valence == known_chem_valence)
1186 : : {
1187 [ # # ]: 0 : if (nLastFoundValenceState == NO_VALUE_INT)
1188 : : {
1189 : : /* accept the first found */
1190 : 0 : nLastFoundValenceState = nNumStates;
1191 : : }
1192 : : else
1193 [ # # ]: 0 : if (abs(ChargeVal[nLastFoundValenceState].nCharge) >= abs(cur_charge))
1194 : : {
1195 : : /* accept smaller abs(charge); if abs(charges) are same, accept (+) */
1196 : 0 : nLastFoundValenceState = nNumStates;
1197 : : }
1198 : : }
1199 : :
1200 : 0 : nNumStates++;
1201 : : }
1202 : : }
1203 : :
1204 : : /***********************************************************************************/
1205 : : /* select only appropriate charge & valence so that a suitable ChargeStruct exists */
1206 : : /***********************************************************************************/
1207 : :
1208 : 0 : nNumSelectedStates = clean_charge_val( pCG, ChargeVal, nNumStates, atom,
1209 : 0 : pVArray, iat, pVA->cMetal,
1210 : : bMobileH, endpoint );
1211 : :
1212 [ # # ]: 0 : if (!nNumSelectedStates)
1213 : : {
1214 : 0 : return TREAT_ATOM_AS_METAL; /* nothing to do */
1215 : : }
1216 : :
1217 : : /***********************************************************************************/
1218 : : /* Find an appropriate ChargeStruct index for the ChargeVal found */
1219 : : /***********************************************************************************/
1220 : 0 : cn_bits = 0;
1221 : 0 : memset( cn_bits_array, 0, sizeof( cn_bits_array ) ); /* djb-rwth: memset_s C11/Annex K variant? */
1222 : : /***** set bits identifying a suitable ChargeStruct ******/
1223 [ # # # # ]: 0 : for (i = len_cn_bits_array = 0; i < nNumSelectedStates && len_cn_bits_array < 4; i++)
1224 : : {
1225 [ # # # # ]: 0 : switch (ChargeVal[i].nCharge)
1226 : : {
1227 : 0 : case -1:
1228 : 0 : cn_bits_array[len_cn_bits_array] |= cn_bits_M; /* Minus 1 */
1229 : 0 : break;
1230 : 0 : case 0:
1231 : 0 : cn_bits_array[len_cn_bits_array] |= cn_bits_N; /* Neutral */
1232 : 0 : break;
1233 : 0 : case 1:
1234 : 0 : cn_bits_array[len_cn_bits_array] |= cn_bits_P; /* Plus 1 */
1235 : 0 : break;
1236 : 0 : default:
1237 : 0 : return RI_ERR_PROGR; /* program error */
1238 : : }
1239 [ # # ]: 0 : if (i + 1 < nNumSelectedStates &&
1240 [ # # ]: 0 : ChargeVal[i].nValence == ChargeVal[i + 1].nValence &&
1241 [ # # ]: 0 : ChargeVal[i].nCharge &&
1242 [ # # ]: 0 : ChargeVal[i].nCharge == -ChargeVal[i + 1].nCharge)
1243 : : {
1244 : : ; /* add opposite charge to the same element of cn_bits_array[] */
1245 : : }
1246 : : else
1247 : : {
1248 : 0 : len_cn_bits_array++;
1249 : : }
1250 : : }
1251 [ # # # # ]: 0 : if (!len_cn_bits_array || len_cn_bits_array > 4)
1252 : : {
1253 : 0 : return RI_ERR_PROGR; /* program error */
1254 : : }
1255 : : /* accommodate added 4-state ChargeStruct: +/- cannot be in case of 4 states */
1256 [ # # # # ]: 0 : if (len_cn_bits_array + 1 == nNumSelectedStates && nNumSelectedStates == 4)
1257 : : {
1258 : 0 : len_cn_bits_array--;
1259 : 0 : nNumSelectedStates--;
1260 : 0 : cn_bits_array[len_cn_bits_array] = 0;
1261 : : }
1262 : : /* fix for terminal hydrogenless -C as in isocyano or CO: there is no just cnE_[] ChargeStruct */
1263 [ # # ]: 0 : if (len_cn_bits_array == 1 &&
1264 [ # # ]: 0 : cn_bits_array[0] == ( cn_bits_P | cn_bits_M ) &&
1265 [ # # ]: 0 : ChargeVal[0].nValence + 1 > BOND_TYPE_TRIPLE + BOND_TYPE_DOUBLE * ( num_bonds - 1 ) + num_H)
1266 : : {
1267 : 0 : cn_bits_array[len_cn_bits_array++] = cn_bits_N;
1268 : 0 : ChargeVal[nNumSelectedStates].nValence = ChargeVal[nNumSelectedStates - 1].nValence;
1269 : 0 : ChargeVal[nNumSelectedStates].nCharge = 0;
1270 : 0 : ChargeVal[nNumSelectedStates].nValenceOrderingNumber = 0;
1271 : : }
1272 : :
1273 : 0 : make_cn_bits:
1274 : 0 : cn_bits = MAKE_CN_BITS( cn_bits_array[0], cn_bits_array[1], cn_bits_array[2], cn_bits_array[3] );
1275 : :
1276 : : /*********** find ChargeStructure **************/
1277 [ # # ]: 0 : for (i = 0, j = -1; i < cnListNumEl; i++)
1278 : : {
1279 [ # # ]: 0 : if (cnList[i].bits == cn_bits)
1280 : : {
1281 : 0 : j = i;
1282 : 0 : break; /* found */
1283 : : }
1284 : : }
1285 : :
1286 [ # # ]: 0 : if (j < 0)
1287 : : {
1288 : : /* ChargeStructure was not found */
1289 [ # # # # ]: 0 : if (1 < len_cn_bits_array && len_cn_bits_array + 1 == nNumSelectedStates)
1290 : : {
1291 : : /* a pair of opposite charges was combined */
1292 : 0 : len_cn_bits_array--;
1293 : 0 : cn_bits_array[len_cn_bits_array] = 0;
1294 : 0 : goto make_cn_bits;
1295 : : }
1296 [ # # ]: 0 : else if (nNumSelectedStates == 4)
1297 : : {
1298 : : /* reduce number of states */
1299 : 0 : len_cn_bits_array--;
1300 : 0 : cn_bits_array[len_cn_bits_array] = 0;
1301 : 0 : nNumSelectedStates--;
1302 : 0 : goto make_cn_bits;
1303 : : }
1304 : 0 : return RI_ERR_PROGR; /* charge structure not found */
1305 : : }
1306 : :
1307 : : /********** ChargeStructure has been found **********/
1308 : 0 : pVA->cnListIndex = j + 1; /* charge structure index + 1 */
1309 : 0 : pVA->cInitCharge = cnList[j].nInitialCharge;
1310 : : /********** Calculate "Free Valence" ****************/
1311 : : #if ( ALLOW_METAL_BOND_ZERO == 1 )
1312 : :
1313 : : #if ( INIT_METAL_BOND_ZERO == 1 )
1314 : : if (pVA->cMetal)
1315 : : {
1316 : : j = 0;
1317 : : }
1318 : : else
1319 : : {
1320 : : j = ChargeVal[0].nValence - cur_chem_valence_fixed;
1321 : : }
1322 : : #else
1323 : 0 : j = ChargeVal[0].nValence - cur_chem_valence_fixed;
1324 : : #endif
1325 : :
1326 : : #else
1327 : : j = ChargeVal[0].nValence - cur_chem_valence_fixed;
1328 : : #endif
1329 [ # # ]: 0 : if (j < 0)
1330 : : {
1331 : 0 : return RI_ERR_PROGR; /* program error */
1332 : : }
1333 : 0 : pVA->cInitFreeValences = j; /* number of initial unsatisfied valences; should be combined with */
1334 : : /* (cap - flow) of vertex=0 in the charge structure[pVA->cnListIndex-1] */
1335 : 0 : return 1; /* success */
1336 : :
1337 : : #undef MIN_ATOM_CHARGE
1338 : : #undef MAX_ATOM_CHARGE
1339 : : #undef NEUTRAL_STATE
1340 : : #undef NUM_ATOM_CHARGES
1341 : : #undef MAX_NUM_VALENCES
1342 : : }
1343 : :
1344 : :
1345 : : #ifdef NEVER
1346 : : /****************************************************************************/
1347 : : int get_bonds_valences( int nPeriodicNum, int bonds_valence, int num_H, VAL_AT *pVA )
1348 : : {
1349 : : int i, j, charge, chem_valence, known_chem_valence;
1350 : : #define MAX_NUM_VALENCES 5 /* defined in util.c */
1351 : :
1352 : : memset( pVA, 0, sizeof( pVA[0] ) );
1353 : :
1354 : : if (!bonds_valence && !num_H)
1355 : : return 0; /* do not know the answer */
1356 : :
1357 : : chem_valence = bonds_valence + num_H;
1358 : : for (charge = VAL_MIN_CHARGE; charge <= VAL_MAX_CHARGE; charge++)
1359 : : {
1360 : : for (i = 0, j = 0; i < MAX_NUM_VALENCES, j < VAL_NUMBER; i++)
1361 : : {
1362 : : if (chem_valence <= ( known_chem_valence = get_el_valence( nPeriodicNum, charge, i ) ))
1363 : : {
1364 : : if (!charge)
1365 : : {
1366 : : pVA->cValence[j][VAL_NEUTR_ORDER] = i + 1;
1367 : : }
1368 : : pVA->cValence[j++][charge + VAL_BASE] = known_chem_valence - num_H;
1369 : : }
1370 : : }
1371 : : }
1372 : : pVA->cDoNotAddH = if_skip_add_H( nPeriodicNum );
1373 : : pVA->cMetal = is_el_a_metal( nPeriodicNum );
1374 : : return pVA->cValence[0][VAL_BASE]; /* 0 means do not know the answer */
1375 : : #undef MAX_NUM_VALENCES
1376 : : }
1377 : : #endif
1378 : : /*********** calculate s or p-element type ************/
1379 : 0 : int get_sp_element_type( int nPeriodicNumber, int *nRow )
1380 : : /*
1381 : : num el
1382 : : el neg
1383 : : 1 => H ATYPE_H 1 1 21
1384 : : 2 => Li, Na, K, Rb, Cs, Fr ATYPE_Na 2 1 10 09 08 08 07
1385 : : 3 => Be, Mg, Ca, Sr, Ba, Ra ATYPE_Mg 3 2 15 12 10 10 09
1386 : : 4 => B, Al, Ga, In, Tl ATYPE_B 4 3 20 15 18 17 18
1387 : : 5 => C, Si, Ge, Sn, Pb ATYPE_C 5 4 25 18 18 18 18
1388 : : 6 => N, P, As, Sb, Bi ATYPE_N 6 5 30 21 20 19 19
1389 : : 7 => O, S, Se, Te, Po ATYPE_O 7 6 35 25 24 21 20
1390 : : 8 => F, Cl, Br, I, At ATYPE_Cl 8 7 40 30 28 25 22
1391 : :
1392 : : number of valence electrons = (type>1)? type-1: type
1393 : :
1394 : : */
1395 : : {
1396 : 0 : int row = 0, type = 0;
1397 [ # # ]: 0 : if (nPeriodicNumber == 1)
1398 : : {
1399 : 0 : type = 1; /* H: 1 */
1400 : 0 : row = 0;
1401 : : }
1402 [ # # ]: 0 : else if (nPeriodicNumber == 2)
1403 : : {
1404 : 0 : type = 0; row = 0;
1405 : : }
1406 [ # # ]: 0 : else if (nPeriodicNumber <= 10)
1407 : : {
1408 : : /* Li: 2, Be: 3, B: 4, C: 5, N: 6, O: 7, F: 8, Ne: 9; later subtract 1 */
1409 : 0 : type = nPeriodicNumber - 1; row = 1;
1410 : : }
1411 [ # # ]: 0 : else if (nPeriodicNumber <= 18)
1412 : : {
1413 : 0 : type = nPeriodicNumber - 9; row = 2;
1414 : : }
1415 [ # # ]: 0 : else if (nPeriodicNumber <= 20)
1416 : : {
1417 : 0 : type = nPeriodicNumber - 17; row = 3;
1418 : : }
1419 [ # # ]: 0 : else if (nPeriodicNumber <= 30)
1420 : : {
1421 : 0 : type = 0; row = 3;
1422 : : }
1423 [ # # ]: 0 : else if (nPeriodicNumber <= 36)
1424 : : {
1425 : 0 : type = nPeriodicNumber - 27; row = 3;
1426 : : }
1427 [ # # ]: 0 : else if (nPeriodicNumber <= 38)
1428 : : {
1429 : 0 : type = nPeriodicNumber - 35; row = 4;
1430 : : }
1431 [ # # ]: 0 : else if (nPeriodicNumber <= 48)
1432 : : {
1433 : 0 : type = 0; row = 4;
1434 : : }
1435 [ # # ]: 0 : else if (nPeriodicNumber <= 54)
1436 : : {
1437 : 0 : type = nPeriodicNumber - 45; row = 4;
1438 : : }
1439 [ # # ]: 0 : else if (nPeriodicNumber <= 56)
1440 : : {
1441 : 0 : type = nPeriodicNumber - 53; row = 5;
1442 : : }
1443 [ # # ]: 0 : else if (nPeriodicNumber <= 80)
1444 : : {
1445 : 0 : type = 0; row = 5;
1446 : : }
1447 [ # # ]: 0 : else if (nPeriodicNumber <= 86)
1448 : : {
1449 : 0 : type = nPeriodicNumber - 77; row = 5;
1450 : : }
1451 [ # # ]: 0 : else if (nPeriodicNumber <= 88)
1452 : : {
1453 : 0 : type = nPeriodicNumber - 85; row = 6;
1454 : : }
1455 : : else
1456 : : {
1457 : 0 : type = 0; row = 6;
1458 : : }
1459 : :
1460 : 0 : *nRow = row;
1461 : :
1462 [ # # ]: 0 : return type == 9 ? 0 : type;
1463 : : }
1464 : :
1465 : :
1466 : : /****************************************************************************/
1467 : 0 : int ReallocTCGroups( ALL_TC_GROUPS *pTCGroups, int nAdd )
1468 : : {
1469 : 0 : TC_GROUP *pTCGroup = (TC_GROUP *) inchi_malloc( sizeof( pTCGroup[0] )*( (long long)pTCGroups->max_tc_groups + nAdd ) ); /* djb-rwth: cast operator added */
1470 : :
1471 [ # # ]: 0 : if (pTCGroup)
1472 : : {
1473 [ # # ]: 0 : if (pTCGroups->num_tc_groups)
1474 : : {
1475 : 0 : memcpy(pTCGroup, pTCGroups->pTCG, sizeof(pTCGroup[0]) * pTCGroups->num_tc_groups);
1476 : : }
1477 : 0 : memset( pTCGroup + pTCGroups->max_tc_groups, 0, sizeof( pTCGroup[0] )*nAdd ); /* djb-rwth: memset_s C11/Annex K variant? */
1478 [ # # ]: 0 : if (pTCGroups->pTCG)
1479 : : {
1480 [ # # ]: 0 : inchi_free( pTCGroups->pTCG );
1481 : : }
1482 : 0 : pTCGroups->pTCG = pTCGroup;
1483 : 0 : pTCGroups->max_tc_groups += nAdd;
1484 : 0 : return 0;
1485 : : }
1486 : :
1487 : 0 : return RI_ERR_ALLOC;
1488 : : }
1489 : :
1490 : :
1491 : : /****************************************************************************/
1492 : 0 : int RegisterTCGroup( ALL_TC_GROUPS *pTCGroups, int nGroupType, int nGroupOrdNum,
1493 : : int nVertexCap, int nVertexFlow, int nEdgeCap, int nEdgeFlow, int nNumEdges )
1494 : : {
1495 : 0 : int i, ret = 0;
1496 : :
1497 : : /* search */
1498 [ # # ]: 0 : for (i = 0; i < pTCGroups->num_tc_groups; i++)
1499 : : {
1500 [ # # ]: 0 : if (pTCGroups->pTCG[i].type == nGroupType &&
1501 [ # # ]: 0 : pTCGroups->pTCG[i].ord_num == nGroupOrdNum)
1502 : : {
1503 : 0 : break;
1504 : : }
1505 : : }
1506 : :
1507 [ # # ]: 0 : if (i == pTCGroups->num_tc_groups)
1508 : : {
1509 : : /* add one more group */
1510 [ # # ]: 0 : if (pTCGroups->num_tc_groups == pTCGroups->max_tc_groups)
1511 : : {
1512 : 0 : ret = ReallocTCGroups( pTCGroups, INC_NUM_TCGROUPS );
1513 [ # # ]: 0 : if (ret)
1514 : : {
1515 : 0 : goto exit_function;
1516 : : }
1517 : : }
1518 : :
1519 : 0 : ret = i + 1; /* added new group */
1520 : 0 : pTCGroups->num_tc_groups++;
1521 : 0 : pTCGroups->pTCG[i].type = nGroupType;
1522 : 0 : pTCGroups->pTCG[i].ord_num = nGroupOrdNum;
1523 : : }
1524 : :
1525 : 0 : pTCGroups->pTCG[i].num_edges += nNumEdges;
1526 : :
1527 : 0 : pTCGroups->pTCG[i].st_cap += nVertexCap;
1528 : 0 : pTCGroups->pTCG[i].st_flow += nVertexFlow;
1529 : :
1530 : 0 : pTCGroups->pTCG[i].edges_cap += nEdgeCap;
1531 : 0 : pTCGroups->pTCG[i].edges_flow += nEdgeFlow;
1532 : :
1533 : 0 : exit_function:
1534 : 0 : return ret;
1535 : : }
1536 : :
1537 : :
1538 : : /****************************************************************************/
1539 : 0 : int nTautEndpointEdgeCap( inp_ATOM *at, VAL_AT *pVA, int i )
1540 : : {
1541 : : /* There are 3 sources of cap-flow = number of unsatisfied valences:
1542 : : -----------------------------------------------------------------
1543 : : 1. pVA[i].cInitFreeValences
1544 : : 2. pCN[0].v.cap - pCN[0].v.flow
1545 : : 3. st[i].chem_bonds_valence - SUM(SINGLE, DOUBLE, TRIPLE bond orders)
1546 : : Reasons: (a) This sum will not include 'ALTERN' bonds
1547 : : (b) until now at[i].chem_bonds_valence was used as a
1548 : : number of satisfied valences. In case of adjacent
1549 : : stereobonds marked as BOND_TYPE_ALTERN the value of
1550 : : at[i].chem_bonds_valence may be = at[i].valence+1.
1551 : : 4. Since tautomerism is defined for a neutral atom, do not add
1552 : : initial flows from the atom to the ChargeStruct
1553 : : CORRECTION: tautomeric endpoints do not have ChargeStruct.
1554 : :
1555 : : */
1556 : : int j, k, nEdgeCap, bonds_valence, stereo_bond_excess_valence;
1557 [ # # ]: 0 : MY_CONST C_NODE *pCN = pVA[i].cnListIndex > 0 ? cnList[pVA[i].cnListIndex - 1].pCN : NULL;
1558 : :
1559 : :
1560 : : /* 1: free valences to reach the minimum known atom valence */
1561 : 0 : nEdgeCap = pVA[i].cInitFreeValences;
1562 : :
1563 : : /* 2: atom free valence in the ChargeStruct */
1564 [ # # ]: 0 : if (pCN)
1565 : : {
1566 : 0 : nEdgeCap += pCN[0].v.cap - pCN[0].v.flow; /* normally should not happen */
1567 : : }
1568 : :
1569 : : /* 3: atom free valence due to known from stereochemistry stereogenic bond types */
1570 : : /*
1571 : : for ( j = 0, bonds_valence = 0; j < at[i].valence; j ++ )
1572 : : {
1573 : : if ( at[i].bond_type[j] <= BOND_TYPE_TRIPLE )
1574 : : {
1575 : : bonds_valence += at[i].bond_type[j];
1576 : : }
1577 : : }
1578 : : */
1579 : :
1580 : : /* bonds > SINGLE are assumed fixed stereobonds; fixed bond cannot increase t-group edge flow */
1581 [ # # # # ]: 0 : for (stereo_bond_excess_valence = 0, j = 0; j < MAX_NUM_STEREO_BONDS && at[i].sb_parity[j]; j++)
1582 : : {
1583 : 0 : k = at[i].sb_ord[j];
1584 [ # # ]: 0 : if (at[i].bond_type[k] < BOND_TYPE_TRIPLE)
1585 : : {
1586 : 0 : stereo_bond_excess_valence += at[i].bond_type[k] - BOND_TYPE_SINGLE;
1587 : : }
1588 : : }
1589 : :
1590 : : /*
1591 : : bonds_valence = (at[i].chem_bonds_valence - bonds_valence) + (bonds_valence -at[i].valence - stereo_bond_excess_valence);
1592 : : */
1593 : 0 : bonds_valence = ( at[i].chem_bonds_valence - at[i].valence ) - stereo_bond_excess_valence;
1594 : :
1595 : :
1596 : : /*---- add 1, 2, 3 ----*/
1597 [ # # ]: 0 : if (bonds_valence >= 0)
1598 : : {
1599 : 0 : nEdgeCap += bonds_valence;
1600 : : }
1601 : : else
1602 : : {
1603 : 0 : nEdgeCap = RI_ERR_PROGR;
1604 : : }
1605 : :
1606 : 0 : return nEdgeCap;
1607 : : }
1608 : :
1609 : :
1610 : : /****************************************************************************/
1611 : : /* If Metal flowers are allowed ( pSrm->bMetalAddFlower != 0), then: */
1612 : : /* */
1613 : : /* bond to a metal atom min_bond_order[i] = pSrm->nMetalMinBondOrder */
1614 : : /* taut endpoint - metal min_bond_order[i] = pSrm->nMetal2EndpointMinBondOrder */
1615 : : /* single bond to metal atom: initial_bond_order[i] = pSrm->nMetalInitBondOrder */
1616 : : /* n-order bond to metal atom initial_bond_order[i] = pSrm->nMetalInitBondOrder + n-1 */
1617 : : /* = bond_order[i]-BOND_TYPE_SINGLE+pSrm->nMetalInitBondOrder */
1618 : : /* single t-endpoint--atom bond initial_bond_order[i] = pSrm->nMetal2EndpointInitBondOrder */
1619 : : /* n-order t-endpoint--metal bond initial_bond_order[i] = pSrm->nMetal2EndpointInitBondOrder+n-1 */
1620 : : /* = bond_order[i]-BOND_TYPE_SINGLE+pSrm->nMetal2EndpointInitBondOrder*/
1621 : : /* */
1622 : : /* Exceptions from simple atom-metal conditions: */
1623 : : /* 1. Atom is a tautomeric endpoint: use pSrm->nMetal2Endpoint* instead of pSrm->nMetal* */
1624 : : /* 2. Atom is sp3-stereogenic and pSrm->bFixStereoBonds != 0: use atom-atom rules */
1625 : : /* 3. Atom has a sp2-stereo and pSrm->bFixStereoBonds != 0: use atom-atom rules */
1626 : : /* */
1627 : : /* Atom-atom rules (applies to all atoms if pSrm->bMetalAddFlower=0) */
1628 : : /* */
1629 : : /* min_bond_order[i] = BOND_TYPE_SINGLE (BOND_TYPE_SINGLE = 1) */
1630 : : /* initial_bond_order[i] = bond_type[i] */
1631 : : /* */
1632 : : /* General rules: */
1633 : : /* initial_bond_flow[i] = initial_bond_order[i]-min_bond_order[i] */
1634 : : /* atom[k] initial_st_cap = at[k].chem_bonds_valence - SUM{i; initial_bond_order[i]} */
1635 : : /* bond_cap[i] = BOND_TYPE_TRIPLE - min_bond_order[i] */
1636 : : /* (reason: quadruple and higher order bonds are not allowed) */
1637 : : /* Exception: in case of metal-atom bond, if pSrm->nMetal2EndpointInitEdgeFlow = 0 AND */
1638 : : /* pSrm->nMetalInitBondOrder - pSrm->nMetalMinBondOrder = 1 then */
1639 : : /* reduce bond to metal order by 1 and increase st_cap of both neighbors by 1: */
1640 : : /* initial_bond_flow[i] --; metal_initial_st_cap += num_bonds; */
1641 : : /* ==== Note: ONLY the INCREASE is already included in pVA->cInitFreeValences of both atoms */
1642 : : /* */
1643 : : /* Notes: initial_st_cap does not include: */
1644 : : /* 1. atom[k] additional st_cap from ChargeStruct pCN[0].v.cap */
1645 : : /* 2. pVA[k].cInitFreeValences due to a difference between the smallest known valence and st_cap */
1646 : : /* */
1647 : : /* here k=atom at[k] index, */
1648 : : /* i=bond index; i = 0..at[k].valence; */
1649 : : /* SUM{i; M[i]} is a sum of M[i] over all i */
1650 : : /* bond_order[i] = at[k].bond_type[i] >= BOND_TYPE_SINGLE - input bond order */
1651 : : /****************************************************************************/
1652 : :
1653 : : /***************** new *************************************************************************************/
1654 : :
1655 : 0 : int BondFlowMaxcapMinorder( inp_ATOM *atom,
1656 : : VAL_AT *pVA,
1657 : : ICHICONST SRM *pSrm,
1658 : : int iat,
1659 : : int ineigh,
1660 : : int *pnMaxcap,
1661 : : int *pnMinorder,
1662 : : int *pbNeedsFlower )
1663 : : {
1664 : :
1665 : 0 : int nFlow, nMaxcap, nMinorder, nInitorder, bNeedsFlower = 0;
1666 : 0 : inp_ATOM *at = atom + iat;
1667 : 0 : int neigh = at->neighbor[ineigh];
1668 : 0 : int bond_type = at->bond_type[ineigh] & BOND_TYPE_MASK;
1669 : 0 : int nMetal = ( 0 != pVA[iat].cMetal ) + ( 0 != pVA[neigh].cMetal );
1670 : 0 : int nEndpoint = ( 0 != at->endpoint ) + ( 0 != atom[neigh].endpoint );
1671 [ # # # # : 0 : int nStereo = ( at->p_parity || at->sb_parity[0] ) + ( atom[neigh].p_parity || atom[neigh].sb_parity[0] );
# # # # ]
1672 : :
1673 [ # # ]: 0 : if (bond_type > BOND_TYPE_TRIPLE)
1674 : : {
1675 : 0 : bond_type = BOND_TYPE_SINGLE;
1676 : : }
1677 : :
1678 : : /* M=metal, A=non-metal atom, e=endpoint */
1679 [ # # # # : 0 : if ((nStereo && pSrm->bFixStereoBonds) || !nMetal || !pSrm->bMetalAddFlower) /* djb-rwth: addressing LLVM warning */
# # # # ]
1680 : : {
1681 : : /* atom-atom rules, no metal atoms involved (1: A-A, A-Ae, Ae-Ae) */
1682 : 0 : nMinorder = BOND_TYPE_SINGLE;
1683 : 0 : nInitorder = bond_type;
1684 : 0 : nFlow = nInitorder - nMinorder;
1685 : : }
1686 [ # # # # ]: 0 : else if (nMetal && !nEndpoint)
1687 : : {
1688 : : /* M-a, M-M */
1689 : : /* atom - metal or metal-metal, none of them is an endpoint (2: M-M, M-A) */
1690 : 0 : nMinorder = pSrm->nMetalMinBondOrder;
1691 : 0 : nInitorder = pSrm->nMetalInitBondOrder + bond_type - BOND_TYPE_SINGLE;
1692 : 0 : nFlow = nInitorder - nMinorder;
1693 [ # # ]: 0 : if (!pSrm->nMetalInitEdgeFlow &&
1694 [ # # # # ]: 0 : pSrm->nMetalInitBondOrder > pSrm->nMetalMinBondOrder &&
1695 : : nFlow > 0)
1696 : : {
1697 : : /* reduce initial flow by 1 and increase st_cap on metal by 1 */
1698 : 0 : nFlow--;
1699 : : }
1700 : 0 : bNeedsFlower = ( 0 != pVA[iat].cMetal );
1701 : : }
1702 : : else
1703 [ # # # # : 0 : if ((pVA[iat].cMetal && !at->endpoint && !pVA[neigh].cMetal && atom[neigh].endpoint) ||
# # # # ]
1704 [ # # # # : 0 : (pVA[neigh].cMetal && !atom[neigh].endpoint && !pVA[iat].cMetal && at->endpoint)) /* djb-rwth: addressing LLVM warnings */
# # # # ]
1705 : : {
1706 : : /* M-ae */
1707 : : /* metal connected to a non-metal endpoint (3: M-Ae) */
1708 : 0 : nMinorder = pSrm->nMetal2EndpointMinBondOrder;
1709 : 0 : nInitorder = pSrm->nMetal2EndpointInitBondOrder + bond_type - BOND_TYPE_SINGLE;
1710 : 0 : nFlow = nInitorder - nMinorder;
1711 [ # # ]: 0 : if (!pSrm->nMetal2EndpointInitEdgeFlow &&
1712 [ # # # # ]: 0 : pSrm->nMetal2EndpointInitBondOrder > pSrm->nMetal2EndpointMinBondOrder &&
1713 : : nFlow > 0)
1714 : : {
1715 : : /* reduce initial flow by 1 and increase st_cap on metal by 1 */
1716 : 0 : nFlow--;
1717 : : }
1718 : 0 : bNeedsFlower = ( 0 != pVA[iat].cMetal );
1719 : : }
1720 : : else
1721 : : {
1722 : : /* endpoint is metal => no flower (4: M-Me, Me-Me, Me-A, Me-Ae) */
1723 : 0 : nMinorder = pSrm->nMetal2EndpointMinBondOrder;
1724 : 0 : nInitorder = pSrm->nMetal2EndpointInitBondOrder + bond_type - BOND_TYPE_SINGLE;
1725 : 0 : nFlow = nInitorder - nMinorder;
1726 [ # # ]: 0 : if (!pSrm->nMetal2EndpointInitEdgeFlow &&
1727 [ # # # # ]: 0 : pSrm->nMetal2EndpointInitBondOrder > pSrm->nMetal2EndpointMinBondOrder &&
1728 : : nFlow > 0)
1729 : : {
1730 : : /* reduce initial flow by 1 and increase st_cap on metal by 1 */
1731 : 0 : nFlow--;
1732 : : }
1733 [ # # # # ]: 0 : bNeedsFlower = ( pVA[iat].cMetal && !at->endpoint );
1734 : : }
1735 : :
1736 : 0 : nMaxcap = BOND_TYPE_TRIPLE - nMinorder;
1737 [ # # ]: 0 : if (pnMaxcap)
1738 : : {
1739 : 0 : *pnMaxcap = nMaxcap;
1740 : : }
1741 [ # # ]: 0 : if (pnMinorder)
1742 : : {
1743 : 0 : *pnMinorder = nMinorder;
1744 : : }
1745 [ # # ]: 0 : if (pbNeedsFlower)
1746 : : {
1747 : 0 : *pbNeedsFlower = bNeedsFlower;
1748 : : }
1749 : :
1750 : 0 : return nFlow;
1751 : : }
1752 : :
1753 : :
1754 : : /*********** new *******************************************************************************************/
1755 : 0 : int AtomStcapStflow( inp_ATOM *atom, VAL_AT *pVA, ICHICONST SRM *pSrm, int iat, int *pnStcap, int *pnStflow,
1756 : : EdgeFlow *pnMGroupEdgeCap, EdgeFlow *pnMGroupEdgeFlow )
1757 : : {
1758 : : int ineigh, bFlower;
1759 : 0 : int nStflow = 0, nMaxBondCap, nMinBondOrder, bNeedsFlower = 0;
1760 : 0 : int valence = atom[iat].valence;
1761 : 0 : int nStcap = atom[iat].chem_bonds_valence;
1762 : 0 : int nMGroupEdgeCap = 0, nMGroupEdgeFlow = 0, nFlow;
1763 : :
1764 [ # # ]: 0 : if (pSrm->bMetalAddFlower)
1765 : : {
1766 : 0 : nStcap -= pVA[iat].cInitOrigValenceToMetal - pVA[iat].cInitValenceToMetal;
1767 : : }
1768 : :
1769 [ # # ]: 0 : for (ineigh = 0; ineigh < valence; ineigh++)
1770 : : {
1771 : 0 : nFlow = BondFlowMaxcapMinorder( atom, pVA, pSrm, iat, ineigh, &nMaxBondCap, &nMinBondOrder, &bFlower );
1772 : 0 : nStflow += nFlow;
1773 : 0 : nStcap -= nMinBondOrder;
1774 [ # # ]: 0 : if (bFlower)
1775 : : {
1776 : 0 : bNeedsFlower++;
1777 : 0 : nMGroupEdgeFlow += nFlow;
1778 : 0 : nMGroupEdgeCap += BOND_TYPE_TRIPLE - nMinBondOrder + pSrm->nMetalMaxCharge_D;
1779 : : }
1780 : : }
1781 : :
1782 [ # # ]: 0 : if (pnStcap)
1783 : : {
1784 [ # # ]: 0 : *pnStcap = bNeedsFlower ? nStflow : nStcap; /* initially, metal atoms are not radicals */
1785 : : }
1786 [ # # ]: 0 : if (pnStflow)
1787 : : {
1788 : 0 : *pnStflow = nStflow;
1789 : : }
1790 [ # # ]: 0 : if (pnMGroupEdgeFlow)
1791 : : {
1792 : 0 : *pnMGroupEdgeFlow = nMGroupEdgeCap - nMGroupEdgeFlow;
1793 : : }
1794 [ # # ]: 0 : if (pnMGroupEdgeCap)
1795 : : {
1796 : 0 : *pnMGroupEdgeCap = nMGroupEdgeCap;
1797 : : }
1798 : :
1799 : 0 : return bNeedsFlower; /* number of variable bonds to metal */
1800 : : }
1801 : :
1802 : :
1803 : : /**************************************************************************************
1804 : : int nCountBnsSizes( inp_ATOM *at, int num_at, int nAddEdges2eachAtom, int nAddVertices,
1805 : : T_GROUP_INFO *ti, VAL_AT *pVA, ALL_TC_GROUPS *pTCGroups )
1806 : :
1807 : : fills out totals:
1808 : :
1809 : : pTCGroups->num_atoms = number of atoms
1810 : : pTCGroups->num_bonds = number of bonds between atoms
1811 : : pTCGroups->num_tgroups = number of tautomeric groups
1812 : : pTCGroups->num_tgroup_edges = number of edges to tautomeric groups
1813 : : pTCGroups->tgroup_charge = total charge on thautomeric atoms (negative)
1814 : : pTCGroups->num_tc_groups = total number of all groups
1815 : : pTCGroups->nVertices = total number of vertices excluding groups interconnections
1816 : : pTCGroups->nEdges = total number of edges excluding groups interconnections
1817 : :
1818 : : creates entries for the groups and adds to each group:
1819 : :
1820 : : TC_GROUP::type = BNS_VERT_TYPE_TGROUP, BNS_VT_C_POS, BNS_VT_C_NEG, BNS_VT_C_POS_C, BNS_VT_C_NEG_C
1821 : : TC_GROUP::ord_num = ordering number within the type, e.g. t-group number
1822 : : TC_GROUP::st_cap = all from the atoms in ChargeStruct or tautomeric group info.
1823 : : TC_GROUP::st_flow = all from the atoms in ChargeStruct (0 for t-groups).
1824 : : TC_GROUP::num_edges = number of edges to the atoms or ChargeStruct vertices.
1825 : : TC_GROUP::edges_cap = sum of all incoming edge caps; see also nTautEndpointEdgeCap(..).
1826 : : TC_GROUP::edges_flow = sum of all incoming edge flows; 0 for t-groups.
1827 : :
1828 : : TC_GROUP::nVertexNumber - NO FILLED WITH ANYTHING
1829 : :
1830 : : Note: the nDelta = st_cap - st_flow needs to be preserved when adding more vertices
1831 : :
1832 : : Return value: =0 => success
1833 : : <0 => error
1834 : : **************************************************************************************/
1835 : 0 : int nCountBnsSizes( inp_ATOM *at, int num_at, int nAddEdges2eachAtom, int nAddVertices,
1836 : : T_GROUP_INFO *ti, VAL_AT *pVA, ICHICONST SRM *pSrm, ALL_TC_GROUPS *pTCGroups )
1837 : : {
1838 : 0 : int i, j, n, k, ret = 0, nBonds, nOtherEdges, nVertices, bMetalAtoms, bNeedsFlower;
1839 : : int nTgroupEdges, nTgroupEdgesFromTg, nTotNegChargInTgroups, cap, flow;
1840 : 0 : MY_CONST C_NODE *pCN = NULL;
1841 : 0 : nVertices = nBonds = nOtherEdges = nTgroupEdges = nTgroupEdgesFromTg = nTotNegChargInTgroups = 0;
1842 : :
1843 : : /* count metal atoms and electrons */
1844 [ # # ]: 0 : for (i = 0; i < num_at; i++)
1845 : : {
1846 : 0 : pTCGroups->num_metal_atoms += ( pVA[i].cMetal != 0 );
1847 : 0 : pTCGroups->num_metal_bonds += pVA[i].cNumBondsToMetal;
1848 : 0 : pTCGroups->total_electrons += at[i].el_number;
1849 [ # # ]: 0 : pTCGroups->total_electrons_metals += pVA[i].cMetal ? at[i].el_number : 0;
1850 : : }
1851 : 0 : pTCGroups->total_electrons -= pTCGroups->total_charge;
1852 : 0 : pTCGroups->num_metal_bonds /= 2;
1853 : :
1854 : : /* register tautomeric groups */
1855 [ # # ]: 0 : for (i = 0; i < ti->num_t_groups; i++)
1856 : : {
1857 : 0 : ret = RegisterTCGroup( pTCGroups,
1858 : : BNS_VERT_TYPE_TGROUP,
1859 : 0 : ti->t_group[i].nGroupNumber,
1860 : 0 : ti->t_group[i].num[0] /* st_cap */,
1861 : : 0 /* st_flow */,
1862 : : 0 /* edge cap */,
1863 : : 0 /* edge flow */,
1864 : 0 : ti->t_group[i].nNumEndpoints /* num Edges */
1865 : : );
1866 [ # # ]: 0 : if (ret < 0)
1867 : : {
1868 : 0 : goto exit_function;
1869 : : }
1870 : :
1871 : : /* edges to tautomeric groups */
1872 : 0 : nOtherEdges += ti->t_group[i].nNumEndpoints;
1873 : 0 : nTgroupEdgesFromTg += ti->t_group[i].nNumEndpoints;
1874 : : /* total negative charge in t-groups */
1875 : 0 : nTotNegChargInTgroups += ti->t_group[i].num[1];
1876 [ # # ]: 0 : if (ret > 0)
1877 : : {
1878 : : /* should always happen since this is the first time this t-group is added */
1879 : 0 : j = ret - 1;
1880 : 0 : pTCGroups->pTCG[j].tg_num_H = ti->t_group[i].num[0] - ti->t_group[i].num[1];
1881 : 0 : pTCGroups->pTCG[j].tg_num_Minus = ti->t_group[i].num[1];
1882 : : }
1883 : : }
1884 : :
1885 : 0 : bMetalAtoms = 0;
1886 : :
1887 : 0 : repeat_for_metals:
1888 : :
1889 : : /* count vertices and register ChargeValence groups */
1890 : : /* for now an atom may belong either to a t-group or to a ChargeValence group, but not to both */
1891 [ # # ]: 0 : for (i = 0; i < num_at; i++)
1892 : : {
1893 : : /* number of bonds */
1894 : 0 : nBonds += at[i].valence;
1895 : : /* Process ChargeStruct vertices and edges */
1896 [ # # ]: 0 : if (pVA[i].cnListIndex)
1897 : : {
1898 : : /* count vertices & edges in the ChargeValence Substructure attached to an atom */
1899 : : /* Important: unlike inp_ATOM, each edge e appears in pCN[*].e[*] only ONE time */
1900 : 0 : int len = cnList[j = pVA[i].cnListIndex - 1].len;
1901 : 0 : int bits = cnList[j].bits;
1902 : : int type, neigh_type; /* djb-rwth: removing redundant variables */
1903 : 0 : pCN = cnList[j].pCN;
1904 : :
1905 : : /* first process all non-metals, after that -- all metals */
1906 [ # # ]: 0 : if (( bits != cn_bits_Me ) != !bMetalAtoms)
1907 : : {
1908 : 0 : continue;
1909 : : }
1910 : : /* djb-rwth: removing redundant code */
1911 [ # # ]: 0 : for (j = 0; j < len; j++)
1912 : : {
1913 : 0 : type = pCN[j].v.type; /* ChargeStruct vertex type: atom is the first, c-groups are last */
1914 : :
1915 : : /* process all pCN[j] neighbors */
1916 [ # # # # ]: 0 : for (k = 0; k < MAX_CN_VAL && ( n = pCN[j].e[k].neigh ); k++)
1917 : : {
1918 : 0 : nOtherEdges++; /* edges inside ChargeStruct */
1919 : 0 : n--; /* neighbor vertex position inside cnList[j].pCN */
1920 : 0 : neigh_type = pCN[n].v.type; /* type of the neighboring atom */
1921 : :
1922 [ # # ]: 0 : if (IS_BNS_VT_C_GR( neigh_type ))
1923 : : {
1924 : : /* register this edge to a CN-group vertex */
1925 [ # # # # ]: 0 : cap = !bMetalAtoms ? pCN[j].e[k].cap : pCN[j].e[k].cap ? pSrm->nMetalMaxCharge_D : 0;
1926 [ # # # # ]: 0 : flow = !bMetalAtoms ? pCN[j].e[k].flow : pCN[j].e[k].flow ? pSrm->nMetalMaxCharge_D : 0;
1927 : :
1928 : 0 : ret = RegisterTCGroup( pTCGroups,
1929 : : neigh_type,
1930 : : 0 /* ord_num*/,
1931 : : 0 /* st_cap */,
1932 : : 0 /* st_flow */,
1933 : : cap /* edge cap*/,
1934 : : flow /* edge flow */,
1935 : : 1 /* nNumEdges*/ );
1936 [ # # ]: 0 : if (ret < 0)
1937 : : {
1938 : 0 : goto exit_function;
1939 : : }
1940 [ # # ]: 0 : if (ret > 0)
1941 : : {
1942 : : /* the group has just been created; add one more edge to (+/-) or supergroup */
1943 : 0 : ret = RegisterTCGroup( pTCGroups,
1944 : : neigh_type,
1945 : : 0 /* ord_num*/,
1946 : : 0 /* st_cap */,
1947 : : 0 /* st_flow */,
1948 : : 0 /* edge cap*/,
1949 : : 0/* edge flow*/,
1950 : : 1 /* nNumEdges*/ );
1951 [ # # ]: 0 : if (ret < 0)
1952 : : {
1953 : 0 : goto exit_function;
1954 : : }
1955 : 0 : nOtherEdges++;
1956 : : }
1957 : : }
1958 : :
1959 [ # # ]: 0 : if (IS_BNS_VT_C_GR( type ))
1960 : : {
1961 : : /* register this edge to a CN-group vertex; normally this does not happen */
1962 : :
1963 [ # # # # ]: 0 : cap = !bMetalAtoms ? pCN[j].e[k].cap : pCN[j].e[k].cap ? pSrm->nMetalMaxCharge_D : 0;
1964 [ # # # # ]: 0 : flow = !bMetalAtoms ? pCN[j].e[k].flow : pCN[j].e[k].flow ? pSrm->nMetalMaxCharge_D : 0;
1965 : :
1966 : 0 : ret = RegisterTCGroup( pTCGroups, type, 0 /* ord_num*/,
1967 : : 0 /* st_cap */, 0 /* st_flow */,
1968 : : cap /* edge cap*/, flow /* edge flow */, 1 /* nNumEdges*/ );
1969 [ # # ]: 0 : if (ret < 0)
1970 : : {
1971 : 0 : goto exit_function;
1972 : : }
1973 [ # # ]: 0 : if (ret > 0)
1974 : : {
1975 : : /* the group has just been created; add one more edge to (+/-) or supergroup */
1976 : 0 : ret = RegisterTCGroup( pTCGroups, type, 0 /* ord_num*/,
1977 : : 0 /* st_cap */, 0 /* st_flow */,
1978 : : 0 /* edge cap*/, 0/* edge flow*/, 1 /* nNumEdges*/ );
1979 [ # # ]: 0 : if (ret < 0)
1980 : : {
1981 : 0 : goto exit_function;
1982 : : }
1983 : 0 : nOtherEdges++;
1984 : : }
1985 : : }
1986 : : } /* end of the current vertex pCN[j] neighbors */
1987 : :
1988 : : /* process pCN[j] vertex */
1989 : :
1990 [ # # ]: 0 : if (type & BNS_VERT_TYPE_ATOM)
1991 : : {
1992 : 0 : continue; /* do not count regular atoms here */
1993 : : }
1994 [ # # # # ]: 0 : if (IS_BNS_VT_CHRG_STRUCT( type ))
1995 : : {
1996 : 0 : nVertices++;
1997 : 0 : continue;
1998 : : }
1999 : :
2000 [ # # # # ]: 0 : if (pSrm->bMetalAddFlower && IS_BNS_VT_M_GR( type ))
2001 : 0 : {
2002 : : /* special treatment: flow and cap are known as well as structure */
2003 : : /* initial bond valence to metal is either 0 or 1 */
2004 : : EdgeFlow nEdgeFlow, nEdgeCap;
2005 : :
2006 : 0 : bNeedsFlower = AtomStcapStflow( at, pVA, pSrm, i, NULL /*pnStcap*/, NULL /*pnStflow*/,
2007 : : &nEdgeCap, &nEdgeFlow );
2008 [ # # ]: 0 : if (!bNeedsFlower)
2009 : : {
2010 : 0 : ret = RI_ERR_PROGR;
2011 : 0 : goto exit_function;
2012 : : }
2013 : : /*
2014 : : GetAtomToMCGroupInitEdgeCapFlow( &nEdgeCap, &nEdgeFlow, pSrm, at, pVA, i );
2015 : : GetAtomToMCGroupInitEdgeCapFlow( &nEdgeCap, &nEdgeFlow, pSrm );
2016 : : */
2017 : : /* the 1st is the flower base */
2018 : : /* atom - G0 edge and G0 vertex */
2019 : 0 : ret = RegisterTCGroup( pTCGroups,
2020 : : type,
2021 : : 0 /* ord_num*/,
2022 : : /*pVA[i].cInitFreeValences*/
2023 : : 0 /* st_cap */,
2024 : : 0 /* st_flow */,
2025 : : (int) nEdgeCap,
2026 : : (int) nEdgeFlow,
2027 : : 1 /* nNumEdges*/ );
2028 [ # # ]: 0 : if (ret < 0)
2029 : : {
2030 : 0 : goto exit_function;
2031 : : }
2032 : : /* count edge atom-G0 */
2033 : 0 : nOtherEdges++;
2034 [ # # ]: 0 : if (ret > 0)
2035 : : {
2036 : : /* first time registration: add G0-G1 and G0-G2 edges to G0 */
2037 : :
2038 : 0 : ret = RegisterTCGroup( pTCGroups,
2039 : : type,
2040 : : 0 /* ord_num*/,
2041 : : 0 /* st_cap */,
2042 : : 0 /* st_flow */,
2043 : : 0,/* edge cap*/
2044 : : 0 /*edge flow*/,
2045 : : 2 /* nNumEdges*/ );
2046 : :
2047 [ # # ]: 0 : if (ret < 0)
2048 : : {
2049 : 0 : goto exit_function;
2050 : : }
2051 : :
2052 : : /* first time registration: add G1; it has 3 edges */
2053 : :
2054 : 0 : ret = RegisterTCGroup( pTCGroups,
2055 : : type,
2056 : : 1 /* ord_num*/,
2057 : : 0 /* st_cap */,
2058 : : 0 /* st_flow */,
2059 : : 0,/* edge cap*/
2060 : : 0 /*edge flow*/,
2061 : : 3 /* nNumEdges*/ );
2062 : :
2063 [ # # ]: 0 : if (ret <= 0)
2064 : : {
2065 [ # # ]: 0 : ret = !ret ? RI_ERR_PROGR : ret;
2066 : 0 : goto exit_function;
2067 : : }
2068 : :
2069 : : /* first time registration: add G2; it has 3 edges */
2070 : :
2071 : 0 : ret = RegisterTCGroup( pTCGroups,
2072 : : type,
2073 : : 2 /* ord_num*/,
2074 : : 0 /* st_cap */,
2075 : : 0 /* st_flow */,
2076 : : 0,/* edge cap*/
2077 : : 0 /*edge flow*/,
2078 : : 3 /* nNumEdges*/ );
2079 : :
2080 [ # # ]: 0 : if (ret <= 0)
2081 : : {
2082 [ # # ]: 0 : ret = !ret ? RI_ERR_PROGR : ret;
2083 : 0 : goto exit_function;
2084 : : }
2085 : :
2086 : : /* first time registration: add G3; it has 2 edges */
2087 : :
2088 : 0 : ret = RegisterTCGroup( pTCGroups,
2089 : : type,
2090 : : 3 /* ord_num*/,
2091 : : 0 /* st_cap */,
2092 : : 0 /* st_flow */,
2093 : : 0,/* edge cap*/
2094 : : 0 /*edge flow*/,
2095 : : 2 /* nNumEdges*/ );
2096 : :
2097 [ # # ]: 0 : if (ret <= 0)
2098 : : {
2099 [ # # ]: 0 : ret = !ret ? RI_ERR_PROGR : ret;
2100 : 0 : goto exit_function;
2101 : : }
2102 : : /* count added metal flower vertices: G0, G1, G2, G3 */
2103 : 0 : nVertices += 4;
2104 : : /* count added metal flower edges: C0-C1, C0-C2, C1-C2, C1-C3, C2-C3 */
2105 : 0 : nOtherEdges += 5;
2106 : : /* add connections of G0 to G1 and G2 */
2107 : : }
2108 : 0 : continue;
2109 : : }
2110 : :
2111 : 0 : nVertices++; /* count BNS_VT_C_POS* types; all contain BNS_VERT_TYPE_C_GROUP bit */
2112 [ # # ]: 0 : if (!IS_BNS_VT_C_GR( type ))
2113 : : { /* check */
2114 : 0 : ret = RI_ERR_PROGR;
2115 : 0 : goto exit_function;
2116 : : }
2117 : : /* add st_cap and st_flow for a charge group */
2118 [ # # # # ]: 0 : cap = !bMetalAtoms ? pCN[j].v.cap : pCN[j].v.cap ? pSrm->nMetalMaxCharge_D : 0;
2119 [ # # # # ]: 0 : flow = !bMetalAtoms ? pCN[j].v.flow : pCN[j].v.flow ? pSrm->nMetalMaxCharge_D : 0;
2120 : :
2121 : 0 : ret = RegisterTCGroup( pTCGroups, type, 0 /* ord_num*/,
2122 : : cap /* st-cap*/, flow /* st-flow */,
2123 : : 0 /* edge cap */, 0 /* edge flow */, 0 /* edges already counted */ );
2124 [ # # ]: 0 : if (ret < 0)
2125 : : {
2126 : 0 : goto exit_function;
2127 : : }
2128 : : }
2129 : : }
2130 : : else
2131 : : {
2132 : 0 : pCN = NULL;
2133 : : }
2134 : :
2135 : : /* count edge caps to t-groups */
2136 [ # # ]: 0 : if (at[i].endpoint)
2137 : : {
2138 : 0 : int nEdgeCap = nTautEndpointEdgeCap( at, pVA, i );
2139 : 0 : nTgroupEdges++;
2140 [ # # ]: 0 : if (nEdgeCap < 0)
2141 : : {
2142 : 0 : ret = nEdgeCap;
2143 : 0 : goto exit_function;
2144 : : }
2145 : :
2146 : : /* add number of unsatisfied valences for a t-group; the unknown flow = 0 */
2147 : 0 : ret = RegisterTCGroup( pTCGroups,
2148 : : BNS_VERT_TYPE_TGROUP,
2149 : 0 : at[i].endpoint,
2150 : : 0 /* st_cap */,
2151 : : 0 /* st_flow */,
2152 : : nEdgeCap /* edge cap */,
2153 : : 0 /* edge flow */,
2154 : : 0 /* t-group edges have already been counted */ );
2155 [ # # ]: 0 : if (ret < 0)
2156 : : {
2157 : 0 : goto exit_function;
2158 : : }
2159 : : }
2160 : : }
2161 : :
2162 [ # # # # ]: 0 : if (!bMetalAtoms && pTCGroups->num_metal_atoms)
2163 : : {
2164 : 0 : bMetalAtoms = 1;
2165 : 0 : nBonds = 0; /* added 2006-05-15 */
2166 : 0 : goto repeat_for_metals;
2167 : : }
2168 : :
2169 : : /* count real atoms and bonds */
2170 : 0 : nBonds /= 2;
2171 : 0 : pTCGroups->num_atoms = num_at;
2172 : 0 : pTCGroups->num_bonds = nBonds;
2173 : :
2174 : 0 : pTCGroups->num_tgroups = ti->num_t_groups;
2175 : 0 : pTCGroups->num_tgroup_edges = nTgroupEdges;
2176 : 0 : pTCGroups->tgroup_charge = -nTotNegChargInTgroups;
2177 : :
2178 [ # # # # ]: 0 : if (0 <= ret && nTgroupEdgesFromTg != nTgroupEdges)
2179 : : {
2180 : 0 : ret = BNS_PROGRAM_ERR;
2181 : : }
2182 : :
2183 : 0 : nVertices += num_at;
2184 : :
2185 : :
2186 : : /* count other vertices */
2187 : 0 : nVertices += ti->num_t_groups;
2188 : 0 : nBonds += nOtherEdges;
2189 : :
2190 : : /* return edges and vertices */
2191 : 0 : pTCGroups->nVertices = nVertices;
2192 : 0 : pTCGroups->nEdges = nBonds;
2193 : :
2194 : :
2195 : 0 : exit_function:
2196 : 0 : return ret;
2197 : : }
2198 : :
2199 : :
2200 : : /****************************************************************
2201 : : int nAddSuperCGroups( ALL_TC_GROUPS *pTCGroups )
2202 : :
2203 : : 1. adds BNS_VT_C_POS_ALL and BNS_VT_C_NEG_ALL ONLY if both
2204 : : {TCG_Plus0 and TCG_Plus_C0} and/or
2205 : : {TCG_Minus0 and TCG_Minus_C0} are present, respectively
2206 : :
2207 : : 2. fills pTCGroups->nGroup[]:
2208 : :
2209 : : pTCGroups->nGroup[k] < 0 => does not exist
2210 : : pTCGroups->nGroup[k] = i => the group is pTCGroups->pTCG[i]
2211 : :
2212 : : where group group
2213 : : k = type number
2214 : : TCG_Plus0 BNS_VT_C_POS 0
2215 : : TCG_Plus1, BNS_VT_C_POS 1
2216 : : TCG_Minus0, BNS_VT_C_NEG 0
2217 : : TCG_Minus1, BNS_VT_C_NEG 1
2218 : : TCG_Plus_C0, BNS_VT_C_POS_C 0
2219 : : TCG_Plus_C1, BNS_VT_C_POS_C 1
2220 : : TCG_Minus_C0, BNS_VT_C_NEG_C 0
2221 : : TCG_Minus_C1, BNS_VT_C_NEG_C 1
2222 : : TCG_Plus, BNS_VT_C_POS_ALL 0
2223 : : TCG_Minus, BNS_VT_C_NEG_ALL 0
2224 : :
2225 : : only groups with number 0 are processed
2226 : :
2227 : : 3. If only one of the groups in pairs mentioned in (1) above
2228 : : is present then
2229 : :
2230 : : pTCGroups->nGroup[TCG_Plus] := pTCGroups->nGroup[TCG_Plus0] or
2231 : : pTCGroups->nGroup[TCG_Plus] := pTCGroups->nGroup[TCG_Plus_C0];
2232 : : an additional BNS_VT_C_POS_ALL vertex is not created
2233 : :
2234 : : same for pTCGroups->nGroup[TCG_Minus] and BNS_VT_C_NEG_ALL
2235 : :
2236 : : 4. Adds to these new "supergroups" (TCG_Plus, TCG_Minus)
2237 : : descriptions in pTCGroups->pTCG[k]
2238 : : st_cap, st_flow, edges cap and flow from the corresponding
2239 : : groups {TCG_Plus0 and TCG_Plus_C0}. Same for the Minus groups.
2240 : : Stores indexes k in
2241 : : pTCGroups->nGroup[TCG_Plus], pTCGroups->nGroup[TCG_Minus]
2242 : :
2243 : : ****************************************************************/
2244 : 0 : int nAddSuperCGroups( ALL_TC_GROUPS *pTCGroups )
2245 : : {
2246 : 0 : int i, k, n, n1, n2, n3, ret = 0, nNumToConnect; /* djb-rwth: removing redundant variables */
2247 : :
2248 [ # # ]: 0 : for (i = 0; i < pTCGroups->num_tc_groups; i++)
2249 : : {
2250 [ # # ]: 0 : if (pTCGroups->pTCG[i].type & BNS_VERT_TYPE_TGROUP)
2251 : : {
2252 : : /* djb-rwth: removing redundant code */
2253 : 0 : continue; /* t-group */
2254 : : }
2255 [ # # ]: 0 : if (IS_BNS_VT_C_GR( pTCGroups->pTCG[i].type ) ||
2256 [ # # ]: 0 : IS_BNS_VT_M_GR( pTCGroups->pTCG[i].type ))
2257 : : {
2258 : : /* ChargeValence (cn) group */
2259 [ # # # # : 0 : switch (pTCGroups->pTCG[i].type)
# # # # ]
2260 : : {
2261 : 0 : case BNS_VT_C_POS:
2262 : 0 : k = TCG_Plus0;
2263 : 0 : break;
2264 : 0 : case BNS_VT_C_NEG:
2265 : 0 : k = TCG_Minus0;
2266 : 0 : break;
2267 : 0 : case BNS_VT_C_POS_C:
2268 : 0 : k = TCG_Plus_C0;
2269 : 0 : break;
2270 : 0 : case BNS_VT_C_NEG_C:
2271 : 0 : k = TCG_Minus_C0;
2272 : 0 : break;
2273 : 0 : case BNS_VT_C_POS_M:
2274 : 0 : k = TCG_Plus_M0;
2275 : 0 : break;
2276 : 0 : case BNS_VT_C_NEG_M:
2277 : 0 : k = TCG_Minus_M0;
2278 : 0 : break;
2279 : 0 : case BNS_VT_M_GROUP:
2280 [ # # # # : 0 : switch (pTCGroups->pTCG[i].ord_num)
# ]
2281 : : {
2282 : 0 : case 0:
2283 : 0 : k = TCG_MeFlower0;
2284 : 0 : break;
2285 : 0 : case 1:
2286 : 0 : k = TCG_MeFlower1;
2287 : 0 : break;
2288 : 0 : case 2:
2289 : 0 : k = TCG_MeFlower2;
2290 : 0 : break;
2291 : 0 : case 3:
2292 : 0 : k = TCG_MeFlower3;
2293 : 0 : break;
2294 : 0 : default:
2295 : 0 : ret = RI_ERR_PROGR; /* unexpected group type */
2296 : 0 : goto exit_function;
2297 : : }
2298 : 0 : break;
2299 : :
2300 : 0 : default:
2301 : 0 : ret = RI_ERR_PROGR; /* unexpected group type */
2302 : 0 : goto exit_function;
2303 : : }
2304 : :
2305 [ # # # # : 0 : if (pTCGroups->nGroup[k] >= 0 || (pTCGroups->pTCG[i].ord_num && !IS_BNS_VT_M_GR( pTCGroups->pTCG[i].type ))) /* djb-rwth: addressing LLVM warning */
# # ]
2306 : : {
2307 : 0 : ret = RI_ERR_PROGR;
2308 : 0 : goto exit_function;
2309 : : }
2310 : 0 : pTCGroups->nGroup[k] = i; /* ordering number of the Charge group, starting from 0 */
2311 : : }
2312 : : }
2313 : :
2314 : : /* add (+) supergroup */
2315 : 0 : n1 = pTCGroups->nGroup[TCG_Plus0];
2316 : 0 : n2 = pTCGroups->nGroup[TCG_Plus_C0];
2317 : 0 : n3 = pTCGroups->nGroup[TCG_Plus_M0];
2318 : 0 : nNumToConnect = ( n1 >= 0 ) + ( n2 >= 0 ) + ( n3 >= 0 );
2319 : :
2320 [ # # ]: 0 : if (nNumToConnect)
2321 : : {
2322 : : /* if both groups are present then add a supergroup */
2323 : 0 : ret = RegisterTCGroup( pTCGroups,
2324 : : BNS_VT_C_POS_ALL,
2325 : : 0,
2326 : : 0 /* st_cap */,
2327 : : 0 /* st_flow */,
2328 : : 0 /* edge cap */,
2329 : : 0 /* edge flow */,
2330 : : 1 + nNumToConnect
2331 : : /* one more edge to connect to */
2332 : : /* an additional (+/-) vertex */ );
2333 : :
2334 [ # # ]: 0 : if (ret <= 0)
2335 : : {
2336 [ # # ]: 0 : ret = !ret ? RI_ERR_PROGR : ret;
2337 : 0 : goto exit_function;
2338 : : }
2339 : :
2340 : 0 : pTCGroups->nGroup[TCG_Plus] = ret - 1; /* newly added group number */
2341 : 0 : pTCGroups->nVertices += 2; /* two vertices including itself */
2342 : 0 : pTCGroups->nEdges += 1 + nNumToConnect; /* one more edge to connect to an additional (+/-) vertex */
2343 : : }
2344 : :
2345 : : /* add (-) supergroup */
2346 : 0 : n1 = pTCGroups->nGroup[TCG_Minus0];
2347 : 0 : n2 = pTCGroups->nGroup[TCG_Minus_C0];
2348 : 0 : n3 = pTCGroups->nGroup[TCG_Minus_M0];
2349 : 0 : nNumToConnect = ( n1 >= 0 ) + ( n2 >= 0 ) + ( n3 >= 0 );
2350 [ # # ]: 0 : if (nNumToConnect)
2351 : : {
2352 : : /* if both groups are present then add a supergroup */
2353 : :
2354 : 0 : ret = RegisterTCGroup( pTCGroups,
2355 : : BNS_VT_C_NEG_ALL,
2356 : : 0,
2357 : : 0 /* st_cap */,
2358 : : 0 /* st_flow */,
2359 : : 0 /* edge cap */,
2360 : : 0 /* edge flow */,
2361 : : 1 + nNumToConnect /* one more edge to connect to an additional (+/-) vertex */ );
2362 : :
2363 [ # # ]: 0 : if (ret < 0)
2364 : : {
2365 : 0 : goto exit_function;
2366 : : }
2367 : 0 : pTCGroups->nGroup[TCG_Minus] = ret - 1; /* newly added group number */
2368 : 0 : pTCGroups->nVertices += 2; /* needs two vertices including itself */
2369 : 0 : pTCGroups->nEdges += 1 + nNumToConnect; /* one more edge to connect to an additional (+/-) vertex */
2370 : : }
2371 : :
2372 : : /* add neutralization vertex: (+)-()=(-) connection */
2373 : 0 : k = pTCGroups->nGroup[TCG_Minus];
2374 : 0 : n = pTCGroups->nGroup[TCG_Plus];
2375 : 0 : nNumToConnect = ( k >= 0 ) + ( n >= 0 );
2376 [ # # ]: 0 : if (nNumToConnect)
2377 : : {
2378 : 0 : pTCGroups->nVertices += 1;
2379 : 0 : pTCGroups->nEdges += nNumToConnect; /* one edge per super-c-group */
2380 : : }
2381 : :
2382 : 0 : ret = 0;
2383 : :
2384 : 0 : exit_function:
2385 : 0 : return ret;
2386 : : }
2387 : :
2388 : :
2389 : : /*********************************************************************************/
2390 : 0 : int AddTGroups2TCGBnStruct( BN_STRUCT *pBNS,
2391 : : StrFromINChI *pStruct,
2392 : : VAL_AT *pVA,
2393 : : ALL_TC_GROUPS *pTCGroups,
2394 : : int nMaxAddEdges )
2395 : : {
2396 : 0 : int ret = 0;
2397 : 0 : inp_ATOM *at = pStruct->at;
2398 : 0 : int num_atoms = pStruct->num_atoms;
2399 : : int tot_st_cap, tot_st_flow;
2400 : : /* ret = ReInitBnStruct( pBNS ); */
2401 : :
2402 [ # # ]: 0 : if (pTCGroups->num_tgroups /* tgi && tgi->num_t_groups && tgi->t_group*/)
2403 : : {
2404 : : int i, k, endpoint, /*centerpoint,*/ fictpoint;
2405 : 0 : int num_tg = pTCGroups->num_tgroups;
2406 : 0 : int num_edges = pBNS->num_edges;
2407 : 0 : int num_vertices = pBNS->num_vertices;
2408 : : BNS_VERTEX *vert_ficpoint, *vert_ficpoint_prev; /* fictitious vertex describing t-group */
2409 : : BNS_VERTEX *vert_endpoint;
2410 : : BNS_EDGE *edge; /* edge between that vertex and the tautomeric endpoint */
2411 : 0 : int nMaxTGroupNumber = 0;
2412 : : /*ENDPOINT_INFO eif;*/
2413 : :
2414 : : /* Debug: check overflow */
2415 [ # # ]: 0 : if (num_vertices + num_tg >= pBNS->max_vertices)
2416 : : {
2417 : 0 : return BNS_VERT_EDGE_OVFL;
2418 : : }
2419 [ # # ]: 0 : if (num_edges + pTCGroups->num_tgroup_edges >= pBNS->max_edges)
2420 : : {
2421 : 0 : return BNS_VERT_EDGE_OVFL;
2422 : : }
2423 : :
2424 : : /* find the largest t-group ID */
2425 [ # # ]: 0 : for (i = 0; i < pTCGroups->num_tc_groups; i++)
2426 : : {
2427 [ # # ]: 0 : if (pTCGroups->pTCG[i].type & BNS_VERT_TYPE_TGROUP)
2428 : : {
2429 : 0 : k = pTCGroups->pTCG[i].ord_num;
2430 [ # # ]: 0 : if (k <= 0)
2431 : : {
2432 : 0 : return BNS_CPOINT_ERR; /* t-group does not have a number or has a wrong number */
2433 : : }
2434 [ # # ]: 0 : if (k > pTCGroups->num_tc_groups)
2435 : : {
2436 : 0 : return BNS_CPOINT_ERR; /* t-group has a wrong number */
2437 : : }
2438 [ # # ]: 0 : if (k != nMaxTGroupNumber + 1)
2439 : : {
2440 : 0 : return BNS_CPOINT_ERR; /* t-group numbers are not contiguously ascending */
2441 : : }
2442 : 0 : nMaxTGroupNumber = k;
2443 : : }
2444 : : else
2445 : : {
2446 : 0 : break; /* t-groups are contiguous and first in the list */
2447 : : }
2448 : : }
2449 : :
2450 [ # # ]: 0 : if (i != num_tg)
2451 : : {
2452 : 0 : return BNS_CPOINT_ERR; /* number of t-groups is wrong */
2453 : : }
2454 : :
2455 : : /* since t-group IDs may be not contiguous, clear all vertices that will be added.
2456 : : all-zeroes-vertex will be ignored by the BNS
2457 : : */
2458 : 0 : memset( pBNS->vert + num_vertices, 0, nMaxTGroupNumber * sizeof( pBNS->vert[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
2459 : :
2460 : : /* initialize new fictitious vertices */
2461 : 0 : vert_ficpoint_prev = pBNS->vert + num_vertices - 1;
2462 : :
2463 : 0 : tot_st_cap = tot_st_flow = 0;
2464 : :
2465 [ # # ]: 0 : for (i = 0; i < num_tg; i++, vert_ficpoint_prev = vert_ficpoint)
2466 : : {
2467 : : /*
2468 : : vert_ficpoint-1 is the last vertex;
2469 : : vert_ficpoint is the vertex that is being added
2470 : : Note: nGroupNumber are not contiguous
2471 : : */
2472 : 0 : vert_ficpoint = pBNS->vert + num_vertices + pTCGroups->pTCG[i].ord_num - 1;
2473 : 0 : vert_ficpoint->iedge = vert_ficpoint_prev->iedge + vert_ficpoint_prev->max_adj_edges;
2474 : 0 : vert_ficpoint->max_adj_edges = pTCGroups->pTCG[i].num_edges + nMaxAddEdges + BNS_ADD_SUPER_TGROUP;
2475 : 0 : vert_ficpoint->num_adj_edges = 0;
2476 : 0 : vert_ficpoint->st_edge.flow = vert_ficpoint->st_edge.flow0 = 0;
2477 : 0 : vert_ficpoint->st_edge.cap = vert_ficpoint->st_edge.cap0 = pTCGroups->pTCG[i].st_cap;
2478 : 0 : tot_st_cap += pTCGroups->pTCG[i].st_cap;
2479 : 0 : vert_ficpoint->type = pTCGroups->pTCG[i].type;
2480 : 0 : pTCGroups->pTCG[i].nVertexNumber = (int) ( vert_ficpoint - pBNS->vert );
2481 : : }
2482 : :
2483 [ # # ]: 0 : for (endpoint = 0; endpoint < num_atoms; endpoint++)
2484 : : {
2485 [ # # ]: 0 : if (!at[endpoint].endpoint)
2486 : 0 : continue;
2487 : 0 : fictpoint = at[endpoint].endpoint + num_vertices - 1;
2488 : 0 : vert_ficpoint = pBNS->vert + fictpoint; /* t-group vertex */
2489 : 0 : vert_endpoint = pBNS->vert + endpoint; /* endpoint vertex */
2490 : : /* Debug: check overflow */
2491 [ # # ]: 0 : if (fictpoint >= pBNS->max_vertices ||
2492 [ # # ]: 0 : num_edges >= pBNS->max_edges ||
2493 [ # # ]: 0 : vert_ficpoint->num_adj_edges >= vert_ficpoint->max_adj_edges ||
2494 [ # # ]: 0 : vert_endpoint->num_adj_edges >= vert_endpoint->max_adj_edges)
2495 : : {
2496 : 0 : ret = BNS_VERT_EDGE_OVFL;
2497 : 0 : break;
2498 : : }
2499 : :
2500 : : #ifdef NEVER
2501 : : /* obtain donor/acceptor info */
2502 : : if (!nGetEndpointInfo( at, endpoint, &eif ))
2503 : : {
2504 : : ret = BNS_BOND_ERR;
2505 : : break;
2506 : : }
2507 : : #endif
2508 : :
2509 : 0 : vert_endpoint->type |= BNS_VERT_TYPE_ENDPOINT;
2510 : :
2511 : : #ifdef NEVER
2512 : : /* set capacity = 1 to the edges from the endpoint to the centerpoint(s) */
2513 : : for (k = 0; k < vert_endpoint->num_adj_edges; k++)
2514 : : {
2515 : : int iedge = vert_endpoint->iedge[k];
2516 : : if (!pBNS->edge[iedge].cap)
2517 : : {
2518 : : /* single bond, possibly between endpoint and centerpoint */
2519 : : centerpoint = ( pBNS->edge[iedge].neighbor12 ^ endpoint );
2520 : : if (centerpoint < pBNS->num_atoms &&
2521 : : pBNS->vert[centerpoint].st_edge.cap >= 1)
2522 : : {
2523 : : int bond_type = ( at[endpoint].bond_type[k] & BOND_TYPE_MASK );
2524 : : if (bond_type == BOND_TAUTOM ||
2525 : : bond_type == BOND_ALTERN ||
2526 : : bond_type == BOND_ALT12NS ||
2527 : : bond_type == BOND_SINGLE)
2528 : : {
2529 : : pBNS->edge[iedge].cap = 1;
2530 : : }
2531 : : }
2532 : : }
2533 : : }
2534 : : #endif
2535 : :
2536 : : /* create a new edge connecting endpoint to the new fictitious t-group vertex vert_ficpoint */
2537 : 0 : edge = pBNS->edge + num_edges;
2538 : 0 : edge->cap = vert_endpoint->st_edge.cap - vert_endpoint->st_edge.flow;
2539 : 0 : edge->cap = inchi_min( edge->cap, MAX_TGROUP_EDGE_CAP );
2540 : 0 : edge->cap = inchi_max( edge->cap, 0 );
2541 : 0 : edge->flow = 0;
2542 : 0 : edge->pass = 0;
2543 : :
2544 : : #if ( RESET_EDGE_FORBIDDEN_MASK == 1 )
2545 : : edge->forbidden &= pBNS->edge_forbidden_mask;
2546 : : #endif
2547 : :
2548 : : #ifdef NEVER
2549 : : /* later include case when the charge change allows the endpoint to become tautomeric */
2550 : : /* mark endoint having moveable H atom with flow=1 */
2551 : :
2552 : : /* -- old "no charges" version -- */
2553 : : /* if (at[endpoint].chem_bonds_valence == at[endpoint].valence) */
2554 : : /* -- the following line takes charges into account -- */
2555 : : if (eif.cDonor) /* means the endpoint has an H-atom to donate */
2556 : : {
2557 : : /* increment edge flow */
2558 : : edge->flow++;
2559 : : /* increment one vertex st-flow & cap */
2560 : : vert_ficpoint->st_edge.flow++;
2561 : : vert_ficpoint->st_edge.cap++;
2562 : : /* increment another vertex st-flow & cap */
2563 : : vert_endpoint->st_edge.flow++;
2564 : : vert_endpoint->st_edge.cap++;
2565 : : }
2566 : : #endif
2567 : :
2568 : : /* connect edge to endpoint and fictpoint and increment the counters of neighbors and edges */
2569 : 0 : ret = ConnectTwoVertices( vert_endpoint, vert_ficpoint, edge, pBNS, 0 );
2570 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
2571 : : {
2572 : 0 : break;
2573 : : }
2574 : 0 : num_edges++;
2575 : 0 : edge->cap0 = edge->cap;
2576 : 0 : edge->flow0 = edge->flow;
2577 : 0 : pVA[endpoint].nTautGroupEdge = num_edges; /* edge index + 1 */
2578 : : }
2579 : :
2580 : 0 : pBNS->num_edges = num_edges;
2581 : 0 : pBNS->num_vertices += nMaxTGroupNumber;
2582 : 0 : pBNS->num_t_groups = num_tg;
2583 : 0 : pBNS->tot_st_cap += tot_st_cap;
2584 : 0 : pBNS->tot_st_flow += tot_st_flow;
2585 : : }
2586 : :
2587 : 0 : return ret;
2588 : : }
2589 : :
2590 : :
2591 : : /*****************************************************************************************************/
2592 : 0 : int ConnectTwoVertices( BNS_VERTEX *p1, BNS_VERTEX *p2, BNS_EDGE *e, BN_STRUCT *pBNS, int bClearEdge )
2593 : : {
2594 : 0 : int ip1 = (int) ( p1 - pBNS->vert );
2595 : 0 : int ip2 = (int) ( p2 - pBNS->vert );
2596 : 0 : int ie = (int) ( e - pBNS->edge );
2597 : :
2598 : : /* debug: check bounds */
2599 [ # # # # ]: 0 : if (ip1 >= pBNS->max_vertices || ip1 < 0 ||
2600 [ # # # # ]: 0 : ip2 >= pBNS->max_vertices || ip2 < 0 ||
2601 [ # # # # ]: 0 : ie >= pBNS->max_edges || ie < 0 ||
2602 [ # # ]: 0 : ( p1->iedge - pBNS->iedge ) < 0 ||
2603 [ # # ]: 0 : ( p1->iedge - pBNS->iedge ) + p1->max_adj_edges > pBNS->max_iedges ||
2604 [ # # ]: 0 : ( p2->iedge - pBNS->iedge ) < 0 ||
2605 [ # # ]: 0 : ( p2->iedge - pBNS->iedge ) + p2->max_adj_edges > pBNS->max_iedges ||
2606 [ # # ]: 0 : p1->num_adj_edges >= p1->max_adj_edges ||
2607 [ # # ]: 0 : p2->num_adj_edges >= p2->max_adj_edges)
2608 : : {
2609 : 0 : return BNS_VERT_EDGE_OVFL;
2610 : : }
2611 : : /* clear the edge */
2612 [ # # ]: 0 : if (bClearEdge)
2613 : : {
2614 : 0 : memset( e, 0, sizeof( *e ) ); /* djb-rwth: memset_s C11/Annex K variant? */
2615 : : }
2616 : : else
2617 [ # # # # ]: 0 : if (e->neighbor1 || e->neighbor12)
2618 : : {
2619 : 0 : return BNS_PROGRAM_ERR;
2620 : : }
2621 : :
2622 : : /* connect */
2623 : 0 : e->neighbor1 = inchi_min( ip1, ip2 );
2624 : 0 : e->neighbor12 = ip1 ^ ip2;
2625 : 0 : p1->iedge[p1->num_adj_edges] = ie;
2626 : 0 : p2->iedge[p2->num_adj_edges] = ie;
2627 : 0 : e->neigh_ord[ip1 > ip2] = p1->num_adj_edges++;
2628 : :
2629 : 0 : e->neigh_ord[ip1 < ip2] = p2->num_adj_edges++;
2630 : 0 : return 0;
2631 : : }
2632 : :
2633 : :
2634 : : /***********************************************************************************************************
2635 : : METAL ATOMS' FLOWER - Provides a source/sink of "free valences"
2636 : : ***********************************************************************************************************
2637 : :
2638 : : c1+...+cn = 2c+dc - total cap and flow of edges to the flower base from metal atoms
2639 : : f1+...+fn = 2f+df they should allow changing bonds to metals from 0-order to triple
2640 : : dc,df = 0 or 1 hence c=3*n, f=0 (initial zero bond order) or n
2641 : : Gi=vertex(M-group)
2642 : : Ci=its st_cap [C3,F3] C0 = F0 = 2c + 2D + dc (st_cap & st_flow)
2643 : : Fi=its st_flow G3 C2 = F2 = c + 2D
2644 : : / \ C1 = F1 = c + 2D + dc-df
2645 : : ci=cap of edge i cx,fx/ \cy,fy C3 = F3 = 0
2646 : : fi=edge flow / \ Constraints
2647 : : [C2,F2]/ cd,fd \[C1,F1] -----------------
2648 : : G2--------G1 fa+fb+2f+df=F0=C0
2649 : : \ / ca = c + 2D (edge cap) fa+fd =F2=C2
2650 : : ca,fa \ / cb,fb fa = c + D - f (edge flow) fb+fd =C1=F1
2651 : : \ / fi <= ci
2652 : : G0 [C0,F0] cb = c + 2D + dc -----------------
2653 : : /\ fb = c + D + dc - (f + df)
2654 : : ci=3, fi=0 or 1 c1,f1 /... \ cn,fn ------------------------------------
2655 : : / \ cd = c + 2D D is an arbitrary integer > 0
2656 : : all n Metal atoms: M1 ... Mn fd = f + D it allows to apply
2657 : : C3++ (add st_flow to cancel radicals)
2658 : : For each Mi add cap and flow=cap cx = cy = D D times.
2659 : : to M-charge group fx = fy = 0
2660 : : --------------------------------------------------------------------------------------
2661 : : | f=0 | f=c, dc>=df | 0 <= 2f+df <= 2c+dc
2662 : : edge +------------------------+-----------+----------+-------------+-------------
2663 : : | flow | rescap | flow | rescap | flow | rescap
2664 : : ----------+------------+-----------+-----------+----------+-------------+-------------
2665 : : f1+..+fn | df | 2c+dc-df | 2c+df | dc-df | 2f+df | 2c-2f+dc-df
2666 : : fa | c+D | D | D | c+D | c+D-f | c+D
2667 : : fb | c+D+dc-df | D+df | D+dc-df | c+D+df | c+D+dc-f-df| c+D+df
2668 : : fd | D | c+D | c+D | D | f+D | D
2669 : : --------------------------------------------------------------------------------------
2670 : : ***********************************************************************************************************/
2671 : 0 : int AddRadicalToMetal(int* tot_st_cap, int* tot_st_flow, ICHICONST SRM* pSrm, BN_STRUCT* pBNS, ALL_TC_GROUPS* pTCGroups)
2672 : : {
2673 : 0 : int iG0 = pTCGroups->nGroup[TCG_MeFlower0]; /* index in pTCGroups->pTCG[] */
2674 : 0 : int iG1 = pTCGroups->nGroup[TCG_MeFlower1];
2675 : 0 : int iG2 = pTCGroups->nGroup[TCG_MeFlower2];
2676 : 0 : int iG3 = pTCGroups->nGroup[TCG_MeFlower3];
2677 : 0 : int n = (iG0 >= 0) + (iG1 >= 0) + (iG2 >= 0) + (iG3 >= 0);
2678 : : int vG0, vG1, vG2, vG3; /* M-vertex number */
2679 : 0 : BNS_VERTEX* pG0 = NULL, * pG1 = NULL, * pG2 = NULL, * pG3 = NULL;
2680 : :
2681 [ # # ]: 0 : if (pTCGroups->num_metal_atoms &&
2682 [ # # ]: 0 : pSrm->bMetalAddFlower &&
2683 [ # # # # ]: 0 : *tot_st_cap % 2 &&
2684 : : n == 4)
2685 : : {
2686 : 0 : vG0 = pTCGroups->pTCG[iG0].nVertexNumber;
2687 : 0 : vG1 = pTCGroups->pTCG[iG1].nVertexNumber;
2688 : 0 : vG2 = pTCGroups->pTCG[iG2].nVertexNumber;
2689 : 0 : vG3 = pTCGroups->pTCG[iG3].nVertexNumber;
2690 : :
2691 : 0 : pG0 = pBNS->vert + vG0;
2692 : 0 : pG1 = pBNS->vert + vG1;
2693 : 0 : pG2 = pBNS->vert + vG2;
2694 : 0 : pG3 = pBNS->vert + vG3;
2695 : :
2696 : : /* add 1 unit to metal flower st_cap */
2697 : 0 : pG3->st_edge.cap++;
2698 : 0 : pG3->st_edge.cap0++;
2699 : 0 : (*tot_st_cap)++;
2700 : 0 : return 1;
2701 : : }
2702 : :
2703 : 0 : return 0;
2704 : : }
2705 : :
2706 : :
2707 : : /***********************************************************************************************************/
2708 : 0 : int ConnectMetalFlower( int *pcur_num_vertices, int *pcur_num_edges,
2709 : : int *tot_st_cap, int *tot_st_flow, ICHICONST SRM *pSrm,
2710 : : BN_STRUCT *pBNS, ALL_TC_GROUPS *pTCGroups )
2711 : : {
2712 : 0 : int iG0 = pTCGroups->nGroup[TCG_MeFlower0]; /* index in pTCGroups->pTCG[] */
2713 : 0 : int iG1 = pTCGroups->nGroup[TCG_MeFlower1];
2714 : 0 : int iG2 = pTCGroups->nGroup[TCG_MeFlower2];
2715 : 0 : int iG3 = pTCGroups->nGroup[TCG_MeFlower3];
2716 : 0 : int n = ( iG0 >= 0 ) + ( iG1 >= 0 ) + ( iG2 >= 0 ) + ( iG3 >= 0 );
2717 : : int vG0, vG1, vG2, vG3; /* M-vertex number */
2718 : 0 : int cur_num_edges = *pcur_num_edges;
2719 : 0 : int cur_num_vertices = *pcur_num_vertices;
2720 : 0 : BNS_VERTEX *pG0 = NULL, *pG1 = NULL, *pG2 = NULL, *pG3 = NULL;
2721 : 0 : BNS_EDGE *ea = NULL, *eb = NULL, *ed = NULL, *ex = NULL, *ey = NULL, *e;
2722 : : int ia, ib, id, ix, iy;
2723 : : int c, f, dc, df, ca, fa, cb, fb, cd, fd, cx, fx, cy, fy;
2724 : : int C0, F0, C1, F1, C2, F2, C3, F3, D;
2725 : 0 : int ret = 0, i;
2726 : :
2727 [ # # ]: 0 : if (0 == n)
2728 : : {
2729 : 0 : goto exit_function;
2730 : : }
2731 : :
2732 [ # # ]: 0 : if (4 != n)
2733 : : {
2734 : 0 : ret = RI_ERR_PROGR;
2735 : 0 : goto exit_function;
2736 : : }
2737 : :
2738 : 0 : vG0 = pTCGroups->pTCG[iG0].nVertexNumber;
2739 : 0 : vG1 = pTCGroups->pTCG[iG1].nVertexNumber;
2740 : 0 : vG2 = pTCGroups->pTCG[iG2].nVertexNumber;
2741 : 0 : vG3 = pTCGroups->pTCG[iG3].nVertexNumber;
2742 : :
2743 : 0 : pG0 = pBNS->vert + vG0;
2744 : 0 : pG1 = pBNS->vert + vG1;
2745 : 0 : pG2 = pBNS->vert + vG2;
2746 : 0 : pG3 = pBNS->vert + vG3;
2747 : :
2748 : : /* count G0 edges cap and flow (currently only atoms are connected to G0) */
2749 [ # # ]: 0 : for (i = 0, c = 0, f = 0; i < pG0->num_adj_edges; i++)
2750 : : {
2751 : 0 : e = pBNS->edge + pG0->iedge[i];
2752 : 0 : c += e->cap;
2753 : 0 : f += e->flow;
2754 : : }
2755 : :
2756 : : /* consistency checks */
2757 [ # # ]: 0 : if (!IS_BNS_VT_M_GR( pTCGroups->pTCG[iG0].type ) &&
2758 [ # # ]: 0 : ( pTCGroups->pTCG[iG0].edges_cap != pG0->st_edge.cap ||
2759 [ # # ]: 0 : pTCGroups->pTCG[iG0].edges_flow != pG0->st_edge.flow ))
2760 : : {
2761 : 0 : ret = RI_ERR_PROGR;
2762 : 0 : goto exit_function;
2763 : : }
2764 : :
2765 [ # # ]: 0 : if (pTCGroups->pTCG[iG0].edges_cap != c ||
2766 [ # # ]: 0 : pTCGroups->pTCG[iG0].edges_flow != f)
2767 : : {
2768 : 0 : ret = RI_ERR_PROGR;
2769 : 0 : goto exit_function;
2770 : : }
2771 : :
2772 : : /* get new edges */
2773 : :
2774 : 0 : ea = pBNS->edge + (ia = cur_num_edges++);
2775 : 0 : eb = pBNS->edge + (ib = cur_num_edges++);
2776 : 0 : ed = pBNS->edge + (id = cur_num_edges++);
2777 : 0 : ex = pBNS->edge + (ix = cur_num_edges++);
2778 : 0 : ey = pBNS->edge + (iy = cur_num_edges++);
2779 : :
2780 : : /* connect vertices with edges */
2781 : 0 : ret = ConnectTwoVertices( pG0, pG1, eb, pBNS, 1 );
2782 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
2783 : : {
2784 : 0 : goto exit_function;
2785 : : }
2786 : :
2787 : 0 : ret = ConnectTwoVertices( pG0, pG2, ea, pBNS, 1 );
2788 : :
2789 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
2790 : : {
2791 : 0 : goto exit_function;
2792 : : }
2793 : :
2794 : 0 : ret = ConnectTwoVertices( pG1, pG2, ed, pBNS, 1 );
2795 : :
2796 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
2797 : : {
2798 : 0 : goto exit_function;
2799 : : }
2800 : :
2801 : 0 : ret = ConnectTwoVertices( pG1, pG3, ey, pBNS, 1 );
2802 : :
2803 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
2804 : : {
2805 : 0 : goto exit_function;
2806 : : }
2807 : :
2808 : 0 : ret = ConnectTwoVertices( pG2, pG3, ex, pBNS, 1 );
2809 : :
2810 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
2811 : : {
2812 : 0 : goto exit_function;
2813 : : }
2814 : :
2815 : : /* calculate caps and flows */
2816 : :
2817 : 0 : dc = c % 2;
2818 : 0 : c /= 2;
2819 : 0 : df = f % 2;
2820 : 0 : f /= 2;
2821 : :
2822 : 0 : D = pSrm->nMetalFlowerParam_D;
2823 : :
2824 : 0 : C0 = F0 = 2 * c + 2 * D + dc;
2825 : 0 : C1 = F1 = c + 2 * D + dc - df;
2826 : 0 : C2 = F2 = c + 2 * D;
2827 : 0 : C3 = F3 = 0;
2828 : :
2829 : 0 : ca = c + 2 * D;
2830 : 0 : fa = c + D - f;
2831 : :
2832 : 0 : cb = c + 2 * D + dc;
2833 : 0 : fb = c + D + dc - ( f + df );
2834 : :
2835 : 0 : cd = c + 2 * D;
2836 : 0 : fd = f + D;
2837 : :
2838 : 0 : cx = cy = D;
2839 : 0 : fx = fy = 0;
2840 : :
2841 : : /* check overflow */
2842 [ # # # # : 0 : if (C0 >= EDGE_FLOW_ST_MASK || F0 >= EDGE_FLOW_ST_MASK ||
# # ]
2843 [ # # # # ]: 0 : C1 >= EDGE_FLOW_ST_MASK || F1 >= EDGE_FLOW_ST_MASK ||
2844 [ # # # # ]: 0 : C2 >= EDGE_FLOW_ST_MASK || F2 >= EDGE_FLOW_ST_MASK ||
2845 [ # # ]: 0 : C3 >= EDGE_FLOW_ST_MASK || F3 >= EDGE_FLOW_ST_MASK)
2846 : : {
2847 : 0 : return BNS_PROGRAM_ERR; /* cannot handle too large st-cap or st-flow */
2848 : : }
2849 : :
2850 : : /* set st caps and flows */
2851 : :
2852 : 0 : SetStCapFlow( pG0, tot_st_flow, tot_st_cap, C0, F0 );
2853 : 0 : SetStCapFlow( pG1, tot_st_flow, tot_st_cap, C1, F1 );
2854 : 0 : SetStCapFlow( pG2, tot_st_flow, tot_st_cap, C2, F2 );
2855 : 0 : SetStCapFlow( pG3, tot_st_flow, tot_st_cap, C3, F3 );
2856 : :
2857 : 0 : SetEdgeCapFlow( ea, ca, fa );
2858 : 0 : SetEdgeCapFlow( eb, cb, fb );
2859 : 0 : SetEdgeCapFlow( ed, cd, fd );
2860 : 0 : SetEdgeCapFlow( ex, cx, fx );
2861 : 0 : SetEdgeCapFlow( ey, cy, fy );
2862 : :
2863 : :
2864 : 0 : *pcur_num_edges = cur_num_edges;
2865 : 0 : *pcur_num_vertices = cur_num_vertices;
2866 : :
2867 : 0 : ret = 0;
2868 : :
2869 : 0 : exit_function:
2870 : :
2871 : 0 : return ret;
2872 : : }
2873 : :
2874 : :
2875 : : /********************************************************************************/
2876 : 0 : void SetEdgeCapFlow( BNS_EDGE *e, int edge_cap, int edge_flow )
2877 : : {
2878 : 0 : e->cap = e->cap0 = edge_cap;
2879 : 0 : e->flow = e->flow0 = edge_flow;
2880 : 0 : }
2881 : :
2882 : :
2883 : : /*********************************************************************************
2884 : : Add cap and flow to an edge
2885 : : Add edge flow to the source vertex st_flow
2886 : : Add edge cap & flow to the destination vertex cap and flow
2887 : : *********************************************************************************/
2888 : 0 : int AddEdgeFlow( int edge_cap, int edge_flow, BNS_EDGE *e01, BNS_VERTEX *pSrc /*src*/,
2889 : : BNS_VERTEX *pDst/*dest*/, int *tot_st_cap, int *tot_st_flow )
2890 : : {
2891 : :
2892 : : /* overflow check */
2893 [ # # # # : 0 : if (e01->cap < 0 || edge_cap < 0 || (int) e01->cap + edge_cap >= EDGE_FLOW_MASK)
# # ]
2894 : : {
2895 : 0 : return BNS_PROGRAM_ERR;
2896 : : }
2897 : :
2898 [ # # # # ]: 0 : if (pDst->st_edge.cap < 0 || (int) pDst->st_edge.cap + edge_cap >= EDGE_FLOW_ST_MASK ||
2899 [ # # # # ]: 0 : pDst->st_edge.flow < 0 || (int) pDst->st_edge.flow + edge_flow >= EDGE_FLOW_ST_MASK ||
2900 [ # # # # ]: 0 : pSrc->st_edge.cap < 0 || pSrc->st_edge.flow < 0 ||
2901 [ # # ]: 0 : (int) pSrc->st_edge.flow + edge_flow >= EDGE_FLOW_ST_MASK)
2902 : : {
2903 : 0 : return BNS_PROGRAM_ERR;
2904 : : }
2905 : :
2906 : : /* add flow */
2907 : 0 : e01->cap += edge_cap;
2908 : 0 : e01->flow += edge_flow;
2909 : 0 : e01->cap0 = e01->cap;
2910 : 0 : e01->flow0 = e01->flow;
2911 : :
2912 : 0 : pDst->st_edge.cap += edge_cap;
2913 : 0 : pDst->st_edge.cap0 = pDst->st_edge.cap;
2914 : 0 : *tot_st_cap += edge_cap;
2915 : :
2916 : 0 : pDst->st_edge.flow += edge_flow;
2917 : 0 : pDst->st_edge.flow0 = pDst->st_edge.flow;
2918 : 0 : *tot_st_flow += edge_flow;
2919 : :
2920 : 0 : pSrc->st_edge.flow += edge_flow;
2921 : 0 : pSrc->st_edge.flow0 = pSrc->st_edge.flow;
2922 : 0 : *tot_st_flow += edge_flow;
2923 : :
2924 : : /*
2925 : : pDst->st_edge.cap += e01->cap;
2926 : : pDst->st_edge.cap0 = pDst->st_edge.cap;
2927 : : *tot_st_cap += e01->cap;
2928 : :
2929 : : pDst->st_edge.flow += e01->flow;
2930 : : pDst->st_edge.flow0 = pDst->st_edge.flow;
2931 : : *tot_st_flow += e01->flow;
2932 : :
2933 : : pSrc->st_edge.flow += e01->flow;
2934 : : pSrc->st_edge.flow0 = pSrc->st_edge.flow;
2935 : : *tot_st_flow += e01->flow;
2936 : : */
2937 : 0 : return 0;
2938 : : }
2939 : :
2940 : :
2941 : : /**************************************************************
2942 : : (+) and (-) group V - connection
2943 : : ================================
2944 : :
2945 : : BNS_VERT_TYPE__AUX (+/-)-connection
2946 : : (v) st_cap =
2947 : : / \ st_flow = (cap0 - Delta0 - flow0) + (cap1 - Delta1 -flow1)
2948 : : / \
2949 : : / \ cap = cap1
2950 : : / \ flow = (cap1 - Delta1 - flow1)
2951 : : / \
2952 : : (-) (+) st_cap = cap1
2953 : : / \ / \ st_flow = cap1 - Delta1
2954 : : /cap0 \ /cap1 \
2955 : : flow0 flow1
2956 : :
2957 : : ***************************************************************
2958 : :
2959 : : (+) supergroup Y - connection
2960 : : ==============================
2961 : :
2962 : : (+) BNS_VT_C_POS_ALL (+) supergroup
2963 : : Delta0 | ==============
2964 : : not shown | cap = cap0+cap1
2965 : : | flow = flow0+flow1-Delta0-Delta1
2966 : : BNS_VERT_TYPE__AUX (y) <------------------ additional vertex: st_cap = cap0+cap1
2967 : : / \ st_flow = cap0+cap1
2968 : : cap=cap0 / \ cap = cap1
2969 : : flow=cap0-flow0 / \ flow = cap1 - flow1 - Delta1
2970 : : -Delta0 / \
2971 : : not-C (+) (+) Carbons st_cap = cap1
2972 : : BNS_VT_C_POS / \ / \ BNS_VT_C_POS_C st_flow = cap1 - Delta1
2973 : : / \ / \
2974 : : totals cap0 cap1 = sum of all cap going up into (+) from atoms or ChargeStruct
2975 : : to (+): flow0 flow1= sum of all flow going up into (+)
2976 : : Delta0 Delta1 = st_cap(+)-st_flow(+) before connection
2977 : : Observations
2978 : : ============
2979 : : A. Any Delta > 0 on (+) or (-) group decreases total (signed) charge by Delta
2980 : :
2981 : : B. Any alt path from an atom through ChargeStruct to an atom
2982 : : does not change the total charge
2983 : :
2984 : : C. st_flow(+/-) = cap(+)-flow(+)-Delta(+) + cap(-)-flow(-)-Delta(-) =
2985 : : = charge(+) + |max (-) charge| + charge(-) = const
2986 : : (charge conservation)
2987 : :
2988 : : D. To decrease total charge: increase st_cap on (+) or (-) group, including supergroup
2989 : : E. To increase total charge: increase st_cap on any (y) or (v)-connecting vertex
2990 : :
2991 : : F. To cancel charges:
2992 : : 1. Forbid (+/-)-(+) or (+/-)-(-) edge
2993 : : 2. Add delta>0 to (+/-) st_cap
2994 : : 3. Add same delta to (+) or (-) st_cap
2995 : :
2996 : :
2997 : : ****************************************************************/
2998 : :
2999 : : /************************************************************************************
3000 : : j2,j3 < j1 < j0
3001 : :
3002 : : (+/-) <---- next step; if does not exist then
3003 : : / \ st_cap1' := st_flow1'
3004 : : / \
3005 : : / \ st_cap1' := cap01'
3006 : : pv1 (+)super st_flow1':= cap01'-flow01' = flow02'+flow03'
3007 : : j1 |
3008 : : | cap01' := st_cap0'
3009 : : | flow01':= st_cap0'-flow02'-flow03'
3010 : : |
3011 : : | st_cap0' :=
3012 : : ( ) pv0,j0 st_flow0':= cap2+st_cap3
3013 : : / \
3014 : : / \ cap03' = cap3
3015 : : / \ flow03' = cap3 - flow3 - Delta3
3016 : : / \
3017 : : st_cap2, st_flow2 (+) (+C) st_cap3' := cap3
3018 : : pv2,j2 pv3,j3 st_flow3' := cap3-Delta3
3019 : : / \ / \ Delta3 := st_cap3 - st_flow3
3020 : : cap2, flow2 cap3, flow3 = sums of incoming
3021 : : **************************************************************************************/
3022 : :
3023 : 0 : int ConnectSuperCGroup( int nSuperCGroup, int nAddGroups[], int num_add,
3024 : : int *pcur_num_vertices, int *pcur_num_edges,
3025 : : int *tot_st_cap, int *tot_st_flow,
3026 : : BN_STRUCT *pBNS, ALL_TC_GROUPS *pTCGroups )
3027 : : {
3028 : 0 : BNS_EDGE **e0X = NULL, *e;
3029 : 0 : BNS_VERTEX **pvX = NULL, *pv0 = NULL, *pv1 = NULL, *pv = NULL;
3030 : 0 : int *jX = NULL, *iX = NULL;
3031 : 0 : int i, j, num_groups, j0, i1, j1, iXX, ret = 0, fst = 0;
3032 : 0 : int cur_num_vertices = *pcur_num_vertices;
3033 : 0 : int cur_num_edges = *pcur_num_edges;
3034 : :
3035 [ # # ]: 0 : if (nSuperCGroup >= 0)
3036 : : {
3037 : 0 : i1 = pTCGroups->nGroup[nSuperCGroup]; /* the supergroup */
3038 [ # # ]: 0 : if (i1 < 0)
3039 : 0 : return 0;
3040 : : }
3041 : : else
3042 : : {
3043 : 0 : i1 = -1;
3044 : 0 : fst = 1;
3045 : : }
3046 : :
3047 [ # # ]: 0 : for (i = num_groups = 0; i < num_add; i++)
3048 : : {
3049 : 0 : iXX = pTCGroups->nGroup[nAddGroups[i]];
3050 [ # # # # ]: 0 : num_groups += ( iXX >= 0 && iXX != i1 );
3051 : : }
3052 [ # # ]: 0 : if (num_groups < 1)
3053 : : { /* Y connect only 2 or more groups; V connects even 1 group */
3054 : 0 : return 0;
3055 : : }
3056 : :
3057 : 0 : e0X = (BNS_EDGE **) inchi_calloc( (long long)num_groups + 1, sizeof( e0X[0] ) ); /* djb-rwth: cast operator added */
3058 : 0 : pvX = (BNS_VERTEX **) inchi_calloc( (long long)num_groups + 1, sizeof( pvX[0] ) ); /* djb-rwth: cast operator added */
3059 : 0 : jX = (int *) inchi_calloc( (long long)num_groups + 1, sizeof( jX[0] ) ); /* djb-rwth: cast operator added */
3060 : 0 : iX = (int *) inchi_calloc( (long long)num_groups + 1, sizeof( iX[0] ) ); /* djb-rwth: cast operator added */
3061 : :
3062 [ # # # # : 0 : if (!e0X || !pvX || !jX || !iX)
# # # # ]
3063 : : {
3064 : 0 : ret = RI_ERR_ALLOC;
3065 : 0 : goto exit_function;
3066 : : }
3067 : :
3068 : : /* create vert_ficpoint -- central Y-connection vertex */
3069 : :
3070 : 0 : j0 = cur_num_vertices;
3071 : 0 : pv0 = pBNS->vert + j0; /* center of the Y-connection; has number j0 */
3072 : 0 : pv0->iedge = ( pv0 - 1 )->iedge + ( pv0 - 1 )->max_adj_edges;
3073 : 0 : pv0->max_adj_edges = num_groups + 1 + BNS_ADD_EDGES; /* Y-connection num. edges */
3074 : 0 : pv0->num_adj_edges = 0; /* nothing connected yet */
3075 : 0 : pv0->type = BNS_VT_YVCONNECTOR;
3076 : 0 : cur_num_vertices++;
3077 : :
3078 [ # # ]: 0 : if (fst == 0)
3079 : : {
3080 : : /* find super c-group vertex pv1, number j1 */
3081 : 0 : jX[0] = j1 = pTCGroups->pTCG[i1].nVertexNumber;
3082 : 0 : iX[0] = i1;
3083 : 0 : pvX[0] = pv1 = pBNS->vert + j1;
3084 : : }
3085 : :
3086 : : /* find other c-group vertices */
3087 [ # # ]: 0 : for (i = 0, j = 1; i < num_add; i++)
3088 : : {
3089 : 0 : iXX = pTCGroups->nGroup[nAddGroups[i]];
3090 [ # # # # ]: 0 : if (( iXX >= 0 ) && ( iXX != i1 ))
3091 : : {
3092 : 0 : iX[j] = iXX;
3093 : 0 : jX[j] = pTCGroups->pTCG[iXX].nVertexNumber;
3094 : 0 : pvX[j] = pBNS->vert + jX[j];
3095 : 0 : j++;
3096 : : }
3097 : : }
3098 : :
3099 : : /* grab (num_groups+1) free edges */
3100 [ # # ]: 0 : for (i = fst; i <= num_groups; i++)
3101 : : {
3102 : 0 : e = e0X[i] = pBNS->edge + cur_num_edges;
3103 : 0 : pv = pvX[i];
3104 : 0 : j = jX[i];
3105 : 0 : iXX = iX[i];
3106 : : /* connect all to pv0 */
3107 : 0 : ret = ConnectTwoVertices( pv0, pv, e, pBNS, 1 );
3108 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
3109 : : {
3110 : 0 : goto exit_function;
3111 : : }
3112 [ # # ]: 0 : if (i)
3113 : : {
3114 : : /* from c-group to central Y-connecting vertex of from supergroup to (+/-) vertex */
3115 : 0 : pTCGroups->pTCG[iX[i]].nForwardEdge = cur_num_edges;
3116 : : }
3117 : : else
3118 : : {
3119 : : /* from central Y-connecting vertex to supergroup */
3120 : 0 : pTCGroups->pTCG[iX[i]].nBackwardEdge = cur_num_edges;
3121 : : }
3122 : 0 : cur_num_edges++;
3123 : : }
3124 : :
3125 : : /* set flow and cap for incoming into pv0 edges */
3126 [ # # ]: 0 : for (i = 1; i <= num_groups; i++)
3127 : : {
3128 : 0 : int nDelta = pTCGroups->pTCG[iX[i]].st_cap - pTCGroups->pTCG[iX[i]].edges_cap;
3129 : 0 : int edge_cap = pTCGroups->pTCG[iX[i]].edges_cap + nDelta; /* added nDelta */
3130 : 0 : int edge_flow = pTCGroups->pTCG[iX[i]].edges_cap - pTCGroups->pTCG[iX[i]].edges_flow /*-nDelta*/;
3131 : :
3132 : 0 : ret = AddEdgeFlow( edge_cap, edge_flow,
3133 : 0 : e0X[i], pvX[i]/*src*/, pv0 /* dest*/, tot_st_cap, tot_st_flow );
3134 : :
3135 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
3136 : : {
3137 : 0 : goto exit_function;
3138 : : }
3139 : : }
3140 : :
3141 [ # # ]: 0 : if (fst == 0)
3142 : : {
3143 : : /* set flow and cap for going out of pv0 and into pv1 edge */
3144 : 0 : int edge_cap = pv0->st_edge.cap;
3145 : 0 : int edge_flow = pv0->st_edge.cap - pv0->st_edge.flow;
3146 : :
3147 : 0 : ret = AddEdgeFlow( pv0->st_edge.cap, pv0->st_edge.cap - pv0->st_edge.flow,
3148 : : e0X[0], pv0/*src*/, pv1 /* dest*/, tot_st_cap, tot_st_flow );
3149 : :
3150 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
3151 : : {
3152 : 0 : goto exit_function;
3153 : : }
3154 : :
3155 : 0 : pTCGroups->pTCG[iX[0]].edges_cap += edge_cap;
3156 : 0 : pTCGroups->pTCG[iX[0]].edges_flow += edge_flow;
3157 : 0 : pTCGroups->pTCG[iX[0]].st_cap += edge_cap;
3158 : 0 : pTCGroups->pTCG[iX[0]].st_flow += edge_flow;
3159 : : }
3160 : : else
3161 : : {
3162 : : /* no supergroup => change cap to flow */
3163 : 0 : *tot_st_cap += pv0->st_edge.flow - pv0->st_edge.cap;
3164 : 0 : pv0->st_edge.cap += pv0->st_edge.flow - pv0->st_edge.cap;
3165 : 0 : pv0->st_edge.cap0 = pv0->st_edge.cap;
3166 : : }
3167 : :
3168 : 0 : *pcur_num_vertices = cur_num_vertices;
3169 : 0 : *pcur_num_edges = cur_num_edges;
3170 : 0 : ret = num_groups;
3171 : :
3172 : 0 : exit_function:
3173 [ # # # # ]: 0 : if (e0X) inchi_free( e0X );
3174 [ # # # # ]: 0 : if (pvX) inchi_free( pvX );
3175 [ # # # # ]: 0 : if (jX) inchi_free( jX );
3176 [ # # # # ]: 0 : if (iX) inchi_free( iX );
3177 : :
3178 : 0 : return ret;
3179 : : }
3180 : :
3181 : :
3182 : : /*********************************************************************************/
3183 : 0 : void AddStCapFlow( BNS_VERTEX *vert_ficpoint, int *tot_st_flow, int *tot_st_cap, int cap, int flow )
3184 : : {
3185 : 0 : vert_ficpoint->st_edge.flow += flow;
3186 : 0 : *tot_st_flow += flow;
3187 : 0 : vert_ficpoint->st_edge.cap += cap;
3188 : 0 : *tot_st_cap += cap;
3189 : :
3190 : 0 : vert_ficpoint->st_edge.flow0 = vert_ficpoint->st_edge.flow;
3191 : 0 : vert_ficpoint->st_edge.cap0 = vert_ficpoint->st_edge.cap;
3192 : 0 : }
3193 : :
3194 : :
3195 : : /*********************************************************************************/
3196 : 0 : void SetStCapFlow( BNS_VERTEX *vert_ficpoint, int *tot_st_flow, int *tot_st_cap, int cap, int flow )
3197 : : {
3198 : 0 : *tot_st_flow += flow - vert_ficpoint->st_edge.flow;
3199 : 0 : vert_ficpoint->st_edge.flow = flow;
3200 : 0 : *tot_st_cap += cap - vert_ficpoint->st_edge.cap;
3201 : 0 : vert_ficpoint->st_edge.cap = cap;
3202 : :
3203 : 0 : vert_ficpoint->st_edge.flow0 = vert_ficpoint->st_edge.flow;
3204 : 0 : vert_ficpoint->st_edge.cap0 = vert_ficpoint->st_edge.cap;
3205 : 0 : }
3206 : :
3207 : :
3208 : : /*********************************************************************************
3209 : : int AddCGroups2TCGBnStruct( BN_STRUCT *pBNS, StrFromINChI *pStruct,
3210 : : VAL_AT *pVA, ALL_TC_GROUPS *pTCGroups )
3211 : :
3212 : :
3213 : : *********************************************************************************/
3214 : 0 : int AddCGroups2TCGBnStruct( BN_STRUCT *pBNS, StrFromINChI *pStruct, VAL_AT *pVA,
3215 : : ALL_TC_GROUPS *pTCGroups, int nMaxAddEdges )
3216 : : {
3217 : 0 : int ret = 0, ret1, ret2, ret3, bNeedsFlower; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
3218 : 0 : inp_ATOM *at = pStruct->at;
3219 : 0 : int num_atoms = pStruct->num_atoms;
3220 : : /*int num_tg = pTCGroups->num_tgroups;*/
3221 : 0 : int num_cg = pTCGroups->num_tc_groups - pTCGroups->num_tgroups;
3222 : 0 : int fst_cg_vertex = pBNS->num_vertices;
3223 : 0 : int fst_cg_group = pTCGroups->num_tgroups;
3224 : 0 : int num_vertices = pBNS->num_vertices;
3225 : 0 : int num_edges = pBNS->num_edges;
3226 : 0 : int cg_charge = 0;
3227 : 0 : ICHICONST SRM *pSrm = pStruct->pSrm;
3228 : :
3229 : : /* ret = ReInitBnStruct( pBNS ); */
3230 : :
3231 [ # # ]: 0 : if (num_cg > 0)
3232 : : {
3233 : : /* if ( cgi && cgi->num_c_groups && cgi->c_group ) */
3234 : : int i, i1, i2, j, j1, j2, k, k1, k2, n, c_point, c_neigh, cap, flow;
3235 : : int cur_num_vertices, cur_num_edges;
3236 : : BNS_VERTEX *vert_ficpoint, *vert_ficpoint_prev, *vert_ficpoint_base; /* fictitious vertex describing charge c-group */
3237 : : BNS_VERTEX *pv1, *pv2;
3238 : : BNS_EDGE *edge; /* edge between that vertex and the tautomeric c_point */
3239 : 0 : int nMaxCGroupNumber = 0;
3240 : : MY_CONST C_NODE *pCN;
3241 : : int cn_len, cn_bits, bMetalAtoms;
3242 : : int type;
3243 : : int tot_st_cap, tot_st_flow;
3244 : : int nAddGroups[16];
3245 : :
3246 : :
3247 : : /* Debug: check overflow */
3248 [ # # ]: 0 : if (num_vertices >= pBNS->max_vertices)
3249 : : {
3250 : 0 : return BNS_VERT_EDGE_OVFL;
3251 : : }
3252 : :
3253 : 0 : nMaxCGroupNumber = num_cg;
3254 : : /* clear all vertices not used until now */
3255 : 0 : memset( pBNS->vert + num_vertices, 0, ( (long long)pBNS->max_vertices - num_vertices ) * sizeof( pBNS->vert[0] ) ); /* djb-rwth: cast operator added; memset_s C11/Annex K variant? */
3256 : 0 : tot_st_cap = pBNS->tot_st_cap;
3257 : 0 : tot_st_flow = pBNS->tot_st_flow;
3258 : :
3259 : : /*****************************************/
3260 : : /* initialize new fictitious vertices */
3261 : : /* representing c-point groups, c-groups */
3262 : : /*****************************************/
3263 : 0 : vert_ficpoint_prev = pBNS->vert + fst_cg_vertex - 1;
3264 : :
3265 [ # # ]: 0 : for (i = 0; i < num_cg; i++)
3266 : : {
3267 : : /*
3268 : : vert_ficpoint-1 is the last vertex;
3269 : : vert_ficpoint is the being added vertex
3270 : : Note: nGroupNumber are not contiguous
3271 : : */
3272 : 0 : vert_ficpoint = vert_ficpoint_prev + 1;
3273 : 0 : vert_ficpoint->iedge = vert_ficpoint_prev->iedge + vert_ficpoint_prev->max_adj_edges;
3274 : 0 : vert_ficpoint->max_adj_edges = pTCGroups->pTCG[i + fst_cg_group].num_edges + nMaxAddEdges;
3275 : 0 : vert_ficpoint->num_adj_edges = 0;
3276 : :
3277 : 0 : vert_ficpoint->st_edge.flow += pTCGroups->pTCG[i + fst_cg_group].st_flow;
3278 : 0 : tot_st_flow += pTCGroups->pTCG[i + fst_cg_group].st_flow;
3279 : 0 : vert_ficpoint->st_edge.cap += pTCGroups->pTCG[i + fst_cg_group].st_cap;
3280 : 0 : tot_st_cap += pTCGroups->pTCG[i + fst_cg_group].st_cap;
3281 : :
3282 : 0 : vert_ficpoint->st_edge.flow0 = vert_ficpoint->st_edge.flow;
3283 : 0 : vert_ficpoint->st_edge.cap0 = vert_ficpoint->st_edge.cap;
3284 : :
3285 : 0 : vert_ficpoint->type = pTCGroups->pTCG[i + fst_cg_group].type;
3286 : : /* save the vertex number */
3287 : 0 : pTCGroups->pTCG[i + fst_cg_group].nVertexNumber = (int) ( vert_ficpoint - pBNS->vert );
3288 : :
3289 : 0 : vert_ficpoint_prev = vert_ficpoint; /* keep track of iedges */
3290 : : }
3291 : :
3292 : 0 : cur_num_vertices = (int) ( vert_ficpoint_prev - pBNS->vert ) + 1;
3293 : 0 : cur_num_edges = num_edges;
3294 : :
3295 : : /*************************************************************/
3296 : : /* pass 1: */
3297 : : /* create ChargeStruct for c-points and connect them to */
3298 : : /* the vertices representing c-point groups; */
3299 : : /* set final atom st_cap, st_flow */
3300 : : /*************************************************************/
3301 [ # # ]: 0 : for (c_point = 0; c_point < num_atoms; c_point++)
3302 : : {
3303 [ # # ]: 0 : if (!( k = pVA[c_point].cnListIndex ))
3304 : 0 : continue; /* not a c-point */
3305 : :
3306 : 0 : k--;
3307 : 0 : pCN = cnList[k].pCN; /* pointer to the ChargeStruct */
3308 : 0 : cn_len = cnList[k].len; /* length of the ChargeStruct */
3309 : 0 : cn_bits = cnList[k].bits; /* bits: for M-recognition */
3310 : : /* cn_bits = cnList[k].bits; */ /* ChargeStruct type */
3311 : 0 : bMetalAtoms = ( cn_bits == cn_bits_Me );
3312 : 0 : vert_ficpoint_base = vert_ficpoint_prev; /* add aux vertices after this */
3313 : :
3314 : : /* create disconnected auxiliary vertices of the at[c_point] ChargeStruct; add to them st_flow & st_cap */
3315 [ # # ]: 0 : for (i1 = 0; i1 < cn_len; i1++)
3316 : : {
3317 [ # # # # ]: 0 : if (!IS_BNS_VT_CHRG_STRUCT( pCN[i1].v.type ))
3318 : : {
3319 : 0 : continue;
3320 : : }
3321 : : /* the atom is always the first; the attached c-points are always the last */
3322 : 0 : vert_ficpoint = vert_ficpoint_base + i1; /* i1 = 1, 2,.. less number of attached c-points */
3323 : 0 : vert_ficpoint->iedge = vert_ficpoint_prev->iedge + vert_ficpoint_prev->max_adj_edges;
3324 : 0 : vert_ficpoint->max_adj_edges = pCN[i1].v.valence; /* do not add additional edges to aux vertices */
3325 : 0 : vert_ficpoint->num_adj_edges = 0;
3326 : :
3327 [ # # # # ]: 0 : cap = !bMetalAtoms ? pCN[i1].v.cap : pCN[i1].v.cap ? pSrm->nMetalMaxCharge_D : 0;
3328 [ # # # # ]: 0 : flow = !bMetalAtoms ? pCN[i1].v.flow : pCN[i1].v.flow ? pSrm->nMetalMaxCharge_D : 0;
3329 : :
3330 : 0 : AddStCapFlow( vert_ficpoint, &tot_st_flow, &tot_st_cap, cap, flow );
3331 : 0 : vert_ficpoint->type = pCN[i1].v.type; /* =BNS_VERT_TYPE__AUX */
3332 : :
3333 : 0 : vert_ficpoint_prev = vert_ficpoint; /* the last one will be vert_ficpoint for the next c-point */
3334 : 0 : cur_num_vertices = (int) ( vert_ficpoint - pBNS->vert ) + 1;
3335 : :
3336 [ # # ]: 0 : if (vert_ficpoint->iedge + vert_ficpoint->max_adj_edges - pBNS->iedge >= pBNS->max_iedges)
3337 : : {
3338 : 0 : return BNS_VERT_EDGE_OVFL;
3339 : : }
3340 [ # # ]: 0 : if (cur_num_vertices >= pBNS->max_vertices)
3341 : : {
3342 : 0 : return BNS_VERT_EDGE_OVFL;
3343 : : }
3344 : : }
3345 : :
3346 : : /* connect the vertices with new edges, add edge flow and cap */
3347 [ # # ]: 0 : for (i1 = 0; i1 < cn_len; i1++)
3348 : : {
3349 : 0 : pv1 = NULL;
3350 : 0 : k1 = -1;
3351 : :
3352 : : /* find vertex corresponding to i1 */
3353 : :
3354 [ # # ]: 0 : if (pCN[i1].v.type & BNS_VERT_TYPE_ATOM)
3355 : : {
3356 : 0 : pv1 = pBNS->vert + c_point; /* may be only one atom -- the current c_point at i1==0 */
3357 : : /* add atom vertex st_cap and st_flow */
3358 [ # # # # ]: 0 : cap = !bMetalAtoms ? pCN[i1].v.cap : pCN[i1].v.cap ? pSrm->nMetalMaxCharge_D : 0;
3359 [ # # # # ]: 0 : flow = !bMetalAtoms ? pCN[i1].v.flow : pCN[i1].v.flow ? pSrm->nMetalMaxCharge_D : 0;
3360 : 0 : AddStCapFlow( pv1, &tot_st_flow, &tot_st_cap, cap, flow );
3361 : : }
3362 : :
3363 [ # # ]: 0 : else if (IS_BNS_VT_C_GR( pCN[i1].v.type ))
3364 : : {
3365 : : /* find c-group vertex by looking for its type */
3366 [ # # ]: 0 : for (j = 0; j < num_cg; j++)
3367 : : {
3368 [ # # ]: 0 : if (pCN[i1].v.type == pBNS->vert[fst_cg_vertex + j].type)
3369 : : {
3370 : 0 : pv1 = pBNS->vert + fst_cg_vertex + j;
3371 : 0 : break;
3372 : : }
3373 : : }
3374 : : /* index of the pTCGroups->pTCG[] */
3375 [ # # ]: 0 : if (pv1)
3376 : : {
3377 : 0 : k1 = j + fst_cg_group;
3378 [ # # ]: 0 : if (pTCGroups->pTCG[k1].type != pCN[i1].v.type ||
3379 [ # # ]: 0 : pTCGroups->pTCG[k1].ord_num)
3380 : : {
3381 : 0 : return RI_ERR_PROGR;
3382 : : }
3383 : : }
3384 : : }
3385 : :
3386 [ # # ]: 0 : else if (IS_BNS_VT_M_GR( pCN[i1].v.type ))
3387 : : {
3388 : 0 : k1 = pTCGroups->nGroup[TCG_MeFlower0];
3389 [ # # ]: 0 : if (k1 < 0 ||
3390 [ # # ]: 0 : pTCGroups->pTCG[k1].type != pCN[i1].v.type ||
3391 [ # # ]: 0 : pTCGroups->pTCG[k1].ord_num ||
3392 [ # # ]: 0 : !pSrm->bMetalAddFlower)
3393 : : {
3394 : 0 : return RI_ERR_PROGR;
3395 : : }
3396 : 0 : pv1 = pBNS->vert + pTCGroups->pTCG[k1].nVertexNumber;
3397 : : }
3398 : :
3399 [ # # # # ]: 0 : else if (IS_BNS_VT_CHRG_STRUCT( pCN[i1].v.type ))
3400 : : {
3401 : : /* aux vertex */
3402 : 0 : pv1 = vert_ficpoint_base + i1;
3403 : : }
3404 : :
3405 [ # # ]: 0 : if (!pv1)
3406 : : {
3407 : 0 : return BNS_BOND_ERR;
3408 : : }
3409 : :
3410 : : /* connect pairs of vertices with new edges */
3411 [ # # # # ]: 0 : for (k = 0; k < MAX_CN_VAL && ( i2 = pCN[i1].e[k].neigh ); k++)
3412 : : {
3413 : 0 : pv2 = NULL;
3414 : 0 : k2 = -1;
3415 : 0 : i2--; /* neighbor */
3416 : :
3417 : : /* find vertex corresponding to i2 */
3418 : :
3419 [ # # ]: 0 : if (pCN[i2].v.type & BNS_VERT_TYPE_ATOM)
3420 : : {
3421 : 0 : pv2 = pBNS->vert + c_point;
3422 [ # # # # ]: 0 : cap = !bMetalAtoms ? pCN[i2].v.cap : pCN[i2].v.cap ? pSrm->nMetalMaxCharge_D : 0;
3423 [ # # # # ]: 0 : flow = !bMetalAtoms ? pCN[i2].v.flow : pCN[i2].v.flow ? pSrm->nMetalMaxCharge_D : 0;
3424 : : /* add atom vertex st_cap and st_flow; this normally should not happen */
3425 : 0 : AddStCapFlow( pv2, &tot_st_flow, &tot_st_cap, cap, flow );
3426 : : }
3427 : :
3428 [ # # ]: 0 : else if (IS_BNS_VT_C_GR( pCN[i2].v.type ))
3429 : : {
3430 : : /* find c-group vertex by looking for its type */
3431 [ # # ]: 0 : for (j = 0; j < num_cg; j++)
3432 : : {
3433 [ # # ]: 0 : if (pCN[i2].v.type == pBNS->vert[fst_cg_vertex + j].type)
3434 : : {
3435 : 0 : pv2 = pBNS->vert + fst_cg_vertex + j;
3436 : 0 : break;
3437 : : }
3438 : : }
3439 [ # # ]: 0 : if (pv2)
3440 : : {
3441 : 0 : k2 = j + fst_cg_group;
3442 [ # # ]: 0 : if (pTCGroups->pTCG[k2].type != pCN[i2].v.type ||
3443 [ # # ]: 0 : pTCGroups->pTCG[k2].ord_num)
3444 : : {
3445 : 0 : return RI_ERR_PROGR;
3446 : : }
3447 : : }
3448 : : }
3449 : :
3450 [ # # ]: 0 : else if (IS_BNS_VT_M_GR( pCN[i2].v.type ))
3451 : : {
3452 : 0 : k2 = pTCGroups->nGroup[TCG_MeFlower0];
3453 [ # # ]: 0 : if (k2 < 0 ||
3454 [ # # ]: 0 : pTCGroups->pTCG[k2].type != pCN[i2].v.type ||
3455 [ # # ]: 0 : pTCGroups->pTCG[k2].ord_num ||
3456 [ # # ]: 0 : !pSrm->bMetalAddFlower)
3457 : : {
3458 : 0 : return RI_ERR_PROGR;
3459 : : }
3460 : 0 : pv2 = pBNS->vert + pTCGroups->pTCG[k2].nVertexNumber;
3461 : : }
3462 : :
3463 [ # # # # ]: 0 : else if (IS_BNS_VT_CHRG_STRUCT( pCN[i2].v.type ))
3464 : : {
3465 : 0 : pv2 = vert_ficpoint_base + i2;
3466 : : }
3467 : :
3468 : : /* connect pv1 and pv2 */
3469 [ # # # # : 0 : if (!pv1 || !pv2 || pv1 == pv2)
# # ]
3470 : : {
3471 : 0 : return BNS_BOND_ERR;
3472 : : }
3473 : :
3474 : 0 : j1 = (int)(pv1 - pBNS->vert);
3475 : 0 : j2 = (int)(pv2 - pBNS->vert);
3476 : :
3477 : : /* create a new edge connecting pv1 and pv2 */
3478 : 0 : edge = pBNS->edge + cur_num_edges;
3479 [ # # # # ]: 0 : if ((IS_BNS_VT_M_GR( pCN[i1].v.type ) && IS_BNS_VT_ATOM( pCN[i2].v.type )) ||
3480 [ # # # # ]: 0 : (IS_BNS_VT_M_GR( pCN[i2].v.type ) && IS_BNS_VT_ATOM( pCN[i1].v.type ))) /* djb-rwth: addressing LLVM warnings */
3481 : 0 : {
3482 : : /* at[c_point] is a metal or is treated as a metal; connect it to M-group */
3483 : : /* metal - M-group (i.e. Metal-Flower) edge */
3484 : : int nStCap, nStFlow;
3485 : 0 : bNeedsFlower = AtomStcapStflow( at, pVA, pSrm, c_point, &nStCap, &nStFlow, &edge->cap, &edge->flow );
3486 : : /* GetAtomToMCGroupInitEdgeCapFlow( &edge->cap, &edge->flow, pSrm, at, pVA, c_point ); */
3487 [ # # ]: 0 : if (!bNeedsFlower)
3488 : : {
3489 : 0 : return RI_ERR_PROGR;
3490 : : }
3491 : 0 : pVA[c_point].nMetalGroupEdge = cur_num_edges + 1;
3492 : : /* pBNS->vert[c_point].st_edge.cap += edge->flow;*/ /* where was this done ???*/
3493 : 0 : pBNS->vert[c_point].st_edge.flow += edge->flow;
3494 : 0 : pBNS->vert[c_point].st_edge.cap += edge->flow + pVA[c_point].cInitFreeValences;
3495 : 0 : pBNS->vert[c_point].st_edge.flow0 = pBNS->vert[c_point].st_edge.flow;
3496 : 0 : pBNS->vert[c_point].st_edge.cap0 = pBNS->vert[c_point].st_edge.cap;
3497 : 0 : tot_st_flow += edge->flow;
3498 : 0 : tot_st_cap += edge->flow + pVA[c_point].cInitFreeValences;
3499 : : }
3500 : : else
3501 : : {
3502 [ # # # # ]: 0 : edge->cap = !bMetalAtoms ? pCN[i1].e[k].cap : pCN[i1].e[k].cap ? pSrm->nMetalMaxCharge_D : 0;
3503 [ # # # # ]: 0 : edge->flow = !bMetalAtoms ? pCN[i1].e[k].flow : pCN[i1].e[k].flow ? pSrm->nMetalMaxCharge_D : 0;
3504 : : }
3505 : :
3506 : 0 : edge->forbidden = pCN[i1].e[k].bForbiddenEdge ? BNS_EDGE_FORBIDDEN_MASK : 0;
3507 : :
3508 : : /* c-group incoming edges cap and flow needed in ConnectSuperCGroup() */
3509 : : /*
3510 : : if ( k1 >= 0 ) {
3511 : : pTCGroups->pTCG[k1].edges_cap += pCN[i1].e[k].cap;
3512 : : pTCGroups->pTCG[k1].edges_flow += pCN[i1].e[k].flow;
3513 : : }
3514 : : if ( k2 >= 0 ) {
3515 : : pTCGroups->pTCG[k2].edges_cap += pCN[i1].e[k].cap;
3516 : : pTCGroups->pTCG[k2].edges_flow += pCN[i1].e[k].flow;
3517 : : }
3518 : : */
3519 : :
3520 : 0 : edge->pass = 0;
3521 : :
3522 : : #if ( RESET_EDGE_FORBIDDEN_MASK == 1 )
3523 : : edge->forbidden &= pBNS->edge_forbidden_mask;
3524 : : #endif
3525 : :
3526 : : /* check edge overflow */
3527 [ # # ]: 0 : if (pv1->num_adj_edges >= pv1->max_adj_edges ||
3528 [ # # ]: 0 : pv2->num_adj_edges >= pv2->max_adj_edges ||
3529 [ # # ]: 0 : cur_num_edges >= pBNS->max_edges)
3530 : : {
3531 : 0 : return BNS_VERT_EDGE_OVFL;
3532 : : }
3533 : :
3534 : : /* connect edge to the incident vertices and increment the counters of neighbors and edges */
3535 : :
3536 : 0 : ret = ConnectTwoVertices( pv1, pv2, edge, pBNS, 0 );
3537 : :
3538 [ # # # # ]: 0 : if (IS_BNS_ERROR( ret ))
3539 : : {
3540 : 0 : return ret;
3541 : : }
3542 : :
3543 : 0 : edge->cap0 = edge->cap;
3544 : 0 : edge->flow0 = edge->flow;
3545 : :
3546 : : /* save the edge index */
3547 [ # # ]: 0 : type = IS_BNS_VT_C_GR( pv1->type ) ? pv1->type :
3548 [ # # ]: 0 : IS_BNS_VT_C_GR( pv2->type ) ? pv2->type : 0;
3549 [ # # ]: 0 : if (type)
3550 : : {
3551 : : /* the edge connects to a c-group */
3552 [ # # ]: 0 : if (type & BNS_VERT_TYPE_C_NEGATIVE)
3553 : : {
3554 : 0 : pVA[c_point].nCMinusGroupEdge = cur_num_edges + 1;
3555 : : }
3556 : : else
3557 : : {
3558 : 0 : pVA[c_point].nCPlusGroupEdge = cur_num_edges + 1;
3559 : : }
3560 : : }
3561 : 0 : cur_num_edges++; /* end of new edge creation */
3562 : : }
3563 : : }
3564 : : }
3565 : :
3566 : :
3567 : : /*************************************************************/
3568 : : /* pass 2: */
3569 : : /* adjust bond cap, flow from the final atom st_cap, st_flow */
3570 : : /*************************************************************/
3571 [ # # ]: 0 : for (c_point = 0; c_point < num_atoms; c_point++)
3572 : : {
3573 : : int st_cap, st_cap2, max_edge_flow;
3574 : 0 : pv1 = pBNS->vert + c_point; /* atom vertex */
3575 : 0 : st_cap = pv1->st_edge.cap;
3576 [ # # ]: 0 : for (k = 0; k < pv1->num_adj_edges; k++)
3577 : : {
3578 : 0 : edge = pBNS->edge + pv1->iedge[k]; /* incident edge */
3579 : 0 : c_neigh = edge->neighbor12 ^ c_point; /* adjacent vertex */
3580 : 0 : pv2 = pBNS->vert + c_neigh;
3581 [ # # # # ]: 0 : if (c_neigh > c_point || !( pv2->type & BNS_VERT_TYPE_ATOM ))
3582 : : {
3583 : 0 : continue;
3584 : : }
3585 : : /* adjacent vertex is an atom; the edge is a bond; process each bond only once */
3586 : 0 : st_cap2 = pv2->st_edge.cap;
3587 : : /* the edge flow <= min( incident atom st_caps) */
3588 : 0 : max_edge_flow = inchi_min( st_cap, st_cap2 );
3589 : : /* bond order <= triple bond (flow=2) */
3590 [ # # # # ]: 0 : if (((pSrm->bMetalAddFlower && !pSrm->nMetalMinBondOrder &&
3591 [ # # # # ]: 0 : ( pVA[c_point].cMetal && pVA[c_point].cNumBondsToMetal)) ||
3592 [ # # # # ]: 0 : (pVA[c_neigh].cMetal && pVA[c_neigh].cNumBondsToMetal) )) /* djb-rwth: addressing LLVM warnings */
3593 : : {
3594 : 0 : max_edge_flow = inchi_min( max_edge_flow, MAX_BOND_EDGE_CAP + 1 );
3595 : : }
3596 : : else
3597 : : {
3598 : 0 : max_edge_flow = inchi_min( max_edge_flow, MAX_BOND_EDGE_CAP );
3599 : : }
3600 [ # # ]: 0 : if (at[c_point].bond_type[k] == BOND_TYPE_SINGLE)
3601 : : {
3602 : : /* the bond has not been changed due to stereo */
3603 : 0 : edge->cap = edge->cap0 = max_edge_flow;
3604 : : }
3605 : : }
3606 : : }
3607 : :
3608 : : /***********************************************************/
3609 : : /************** ************/
3610 : : /************** connect M-flower with new edges ************/
3611 : : /************** ************/
3612 : : /***********************************************************/
3613 : :
3614 : 0 : ret = ConnectMetalFlower( &cur_num_vertices, &cur_num_edges, &tot_st_cap, &tot_st_flow, pSrm, pBNS, pTCGroups );
3615 : :
3616 [ # # ]: 0 : if (ret < 0)
3617 : : {
3618 : 0 : goto exit_function;
3619 : : }
3620 : :
3621 : : /***********************************************************/
3622 : : /************** ************/
3623 : : /************** add additional vertices & edges ************/
3624 : : /************** to connect c-groups ************/
3625 : : /************** ************/
3626 : : /***********************************************************/
3627 : :
3628 : : /* (+) supergroup, Y-connection */
3629 : :
3630 : 0 : k = 0;
3631 : 0 : nAddGroups[k++] = TCG_Plus0;
3632 : 0 : nAddGroups[k++] = TCG_Plus_C0;
3633 : 0 : nAddGroups[k++] = TCG_Plus_M0;
3634 : :
3635 : 0 : ret1 = ConnectSuperCGroup( TCG_Plus,
3636 : : nAddGroups,
3637 : : k,
3638 : : &cur_num_vertices,
3639 : : &cur_num_edges,
3640 : : &tot_st_cap,
3641 : : &tot_st_flow,
3642 : : pBNS,
3643 : : pTCGroups ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
3644 : :
3645 : : /* (-) supergroup, Y-connection */
3646 : :
3647 : 0 : k = 0;
3648 : 0 : nAddGroups[k++] = TCG_Minus0;
3649 : 0 : nAddGroups[k++] = TCG_Minus_C0;
3650 : 0 : nAddGroups[k++] = TCG_Minus_M0;
3651 : :
3652 : 0 : ret2 = ConnectSuperCGroup( TCG_Minus,
3653 : : nAddGroups,
3654 : : k,
3655 : : &cur_num_vertices,
3656 : : &cur_num_edges,
3657 : : &tot_st_cap,
3658 : : &tot_st_flow,
3659 : : pBNS,
3660 : : pTCGroups ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
3661 : :
3662 : : /******** connect (+) and (-) ***************/
3663 : :
3664 : 0 : k = 0;
3665 : 0 : nAddGroups[k++] = TCG_Plus;
3666 : 0 : nAddGroups[k++] = TCG_Minus;
3667 : :
3668 : 0 : ret3 = ConnectSuperCGroup( -1,
3669 : : nAddGroups,
3670 : : k,
3671 : : &cur_num_vertices,
3672 : : &cur_num_edges,
3673 : : &tot_st_cap,
3674 : : &tot_st_flow,
3675 : : pBNS,
3676 : : pTCGroups );
3677 : :
3678 : : /* Take care of the full charge */
3679 : :
3680 : 0 : cg_charge = pTCGroups->total_charge - pTCGroups->tgroup_charge - pTCGroups->charge_on_atoms;
3681 : 0 : ret = 1;
3682 [ # # ]: 0 : if (ret3 > 0)
3683 : : {
3684 : : /* (+) and (-) or at least one of them have been connected */
3685 : 0 : int nVertPlusMinus = cur_num_vertices - 1;
3686 : 0 : BNS_VERTEX *pVertPlusMinus = pBNS->vert + nVertPlusMinus;
3687 : 0 : BNS_VERTEX *pVertPlus = NULL, *pVertMinus = NULL, *pVert = NULL;
3688 : 0 : BNS_EDGE *pEdgePlus = NULL, *pEdgeMinus = NULL, *pEdge = NULL;
3689 : 0 : n = pTCGroups->nGroup[TCG_Plus] >= 0; /* (+)-supergroup exists */
3690 : 0 : k = pTCGroups->nGroup[TCG_Minus] >= 0; /* (-)-supergroup exists */
3691 : :
3692 [ # # # # ]: 0 : if (pVertPlusMinus->num_adj_edges == 2 && k + n == 2)
3693 : : {
3694 : 0 : pEdgePlus = pBNS->edge + pVertPlusMinus->iedge[0]; /* TCG_Plus was the 1st */
3695 : 0 : pEdgeMinus = pBNS->edge + pVertPlusMinus->iedge[1]; /* TCG_Minus was the 2nd */
3696 : : }
3697 : :
3698 [ # # # # ]: 0 : else if (pVertPlusMinus->num_adj_edges == 1 && k + n == 1)
3699 : : {
3700 [ # # ]: 0 : if (pTCGroups->nGroup[TCG_Plus] >= 0)
3701 : : {
3702 : 0 : pEdgePlus = pBNS->edge + pVertPlusMinus->iedge[0];
3703 : : }
3704 [ # # ]: 0 : else if (pTCGroups->nGroup[TCG_Minus] >= 0)
3705 : : {
3706 : 0 : pEdgeMinus = pBNS->edge + pVertPlusMinus->iedge[0];
3707 : : }
3708 : : }
3709 : :
3710 [ # # ]: 0 : else if (k + n)
3711 : : {
3712 : : /* program error check */
3713 : 0 : ret = BNS_BOND_ERR;
3714 : 0 : goto exit_function;
3715 : : }
3716 : :
3717 [ # # ]: 0 : if (pEdgePlus)
3718 : : {
3719 : 0 : pVertPlus = pBNS->vert + ( pEdgePlus->neighbor12 ^ nVertPlusMinus );
3720 : : }
3721 [ # # ]: 0 : if (pEdgeMinus)
3722 : : {
3723 : 0 : pVertMinus = pBNS->vert + ( pEdgeMinus->neighbor12 ^ nVertPlusMinus );
3724 : : }
3725 : :
3726 [ # # ]: 0 : pVert = pVertPlus ? pVertPlus : pVertMinus ? pVertMinus : NULL;
3727 [ # # ]: 0 : pEdge = pEdgePlus ? pEdgePlus : pEdgeMinus ? pEdgeMinus : NULL;
3728 [ # # ]: 0 : if (pEdgeMinus)
3729 : : {
3730 : 0 : pTCGroups->nEdgeMinus = (int) ( pEdgeMinus - pBNS->edge );
3731 : : }
3732 [ # # ]: 0 : if (pEdgePlus)
3733 : : {
3734 : 0 : pTCGroups->nEdgePlus = (int) ( pEdgePlus - pBNS->edge );
3735 : : }
3736 [ # # ]: 0 : if (pEdge)
3737 : : {
3738 : 0 : pTCGroups->nEdge4charge = (int) ( pEdge - pBNS->edge );
3739 : : }
3740 : :
3741 : : /* set total charge */
3742 : :
3743 [ # # # # ]: 0 : if (pVert && pEdge)
3744 : : {
3745 : : /* do not check rescaps for now */
3746 [ # # ]: 0 : if (cg_charge > 0)
3747 : : {
3748 : 0 : pVertPlusMinus->st_edge.cap += cg_charge;
3749 : 0 : tot_st_cap += cg_charge;
3750 : 0 : pVertPlusMinus->st_edge.cap0 = pVertPlusMinus->st_edge.cap;
3751 : : }
3752 : :
3753 [ # # ]: 0 : if (cg_charge < 0)
3754 : : {
3755 : 0 : pVert->st_edge.cap -= cg_charge;
3756 : 0 : tot_st_cap -= cg_charge;
3757 : 0 : pVert->st_edge.cap0 = pVert->st_edge.cap;
3758 : :
3759 [ # # ]: 0 : if (pEdge->cap - pEdge->flow + cg_charge < 0)
3760 : : {
3761 : : /* 2006-02-06: increase edge capacity to avoid clogging */
3762 : 0 : pEdge->cap = pEdge->flow - cg_charge;
3763 : : }
3764 : : }
3765 : 0 : pTCGroups->added_charge = cg_charge;
3766 : : }
3767 [ # # # # : 0 : if (!cg_charge || ( pVert && pEdge ))
# # ]
3768 : : {
3769 : 0 : ret = 2;
3770 : : }
3771 : : }
3772 : :
3773 : 0 : AddRadicalToMetal( &tot_st_cap, &tot_st_flow, pSrm, pBNS, pTCGroups );
3774 : :
3775 : :
3776 : 0 : pBNS->num_edges = cur_num_edges;
3777 : 0 : pBNS->num_vertices = cur_num_vertices;
3778 : 0 : pBNS->num_c_groups = num_cg;
3779 : 0 : pBNS->tot_st_cap = tot_st_cap;
3780 : 0 : pBNS->tot_st_flow = tot_st_flow;
3781 : : }
3782 : :
3783 : 0 : exit_function:
3784 : 0 : return ret;
3785 : : }
3786 : :
3787 : :
3788 : : /********************************************************************************/
3789 : 0 : int nNumEdgesToCnVertex( MY_CONST C_NODE *pCN, int len, int v )
3790 : : {
3791 : 0 : int i, j, n, num_edges, v1 = v + 1;
3792 [ # # ]: 0 : for (i = 0, num_edges = 0; i < len; i++)
3793 : : {
3794 [ # # # # ]: 0 : for (j = 0; j < MAX_CN_VAL && ( n = pCN[i].e[j].neigh ); j++)
3795 : : {
3796 [ # # # # ]: 0 : num_edges += ( i == v || n == v1 );
3797 : : }
3798 : : }
3799 : 0 : return num_edges;
3800 : : }
3801 : :
3802 : :
3803 : : /*********************************************************************************
3804 : : BN_STRUCT* AllocateAndInitTCGBnStruct( StrFromINChI *pStruct, VAL_AT *pVA,
3805 : : ALL_TC_GROUPS *pTCGroups,
3806 : : int nMaxAddAtoms, int nMaxAddEdges,
3807 : : int max_altp, int *pNum_changed_bonds )
3808 : : allocate BN_STRUCT that has:
3809 : :
3810 : : pBNS->max_vertices = pTCGroups->nVertices + nMaxAddAtoms
3811 : : pBNS->max_edges = pTCGroups->nEdges +
3812 : : pBNS->max_vertices * (nMaxAddEdges + NUM_KINDS_OF_GROUPS)
3813 : : pBNS->max_iedges = 2*pBNS->max_edges + pTCGroups->nAddIedges
3814 : :
3815 : : pBNS->len_alt_path = pBNS->max_vertices + iALTP_HDR_LEN + 1 +
3816 : : max( pBNS->max_vertices/2, 16 )
3817 : : pBNS->max_altp = max_altp
3818 : :
3819 : : other members:
3820 : :
3821 : : pBNS->num_atoms = num_atoms;
3822 : : pBNS->num_bonds = num_bonds;
3823 : : pBNS->num_added_atoms = 0;
3824 : : pBNS->num_t_groups = 0;
3825 : : pBNS->num_c_groups = 0;
3826 : : pBNS->nMaxAddAtoms = nMaxAddAtoms;
3827 : : pBNS->nMaxAddEdges = nMaxAddEdges;
3828 : :
3829 : : atom vertices and bond edges:
3830 : : --- vertex(atom) ---
3831 : : st_cap = (at[].chem_bonds_valence - at[].valence) + pVA[].cInitFreeValences
3832 : : st_flow = SUM{bond_orders; ALT_BOND counted as SINGLE} - at[].valence
3833 : : --- edge(bond) ---
3834 : : flow = bond_order - 1; for ALT_BOND flow = 0
3835 : : cap = min(min(st_cap of neighbors),2); for ALT_BOND cap = 1
3836 : : max number of edges per atom = number of bonds +
3837 : : number of edges to ChargeStruct +
3838 : : 1 (if atom is a tautomeric endpoint) +
3839 : : nMaxAddEdges
3840 : : --- NOTE ---
3841 : : Here are not included nDelta(dots) from ChargeStruct and flow to ChargeStruct
3842 : :
3843 : : *********************************************************************************/
3844 : 0 : BN_STRUCT* AllocateAndInitTCGBnStruct( StrFromINChI *pStruct, VAL_AT *pVA,
3845 : : ALL_TC_GROUPS *pTCGroups,
3846 : : int nMaxAddAtoms, int nMaxAddEdges,
3847 : : int max_altp, int *pNum_changed_bonds )
3848 : : {
3849 : 0 : inp_ATOM *at = pStruct->at;
3850 : 0 : int num_atoms = pStruct->num_atoms;
3851 : 0 : ICHICONST SRM *pSrm = pStruct->pSrm;
3852 : :
3853 : 0 : BN_STRUCT *pBNS = NULL;
3854 : : BNS_VERTEX *vert;
3855 : : BNS_IEDGE *iedge;
3856 : :
3857 : 0 : int neigh, num_changed_bonds = 0;
3858 : : U_CHAR bond_type, bond_mark;
3859 : : int bNeedsFlower1, bNeedsFlower2, min_order;
3860 : :
3861 : : int i, j, k, m, n_edges, num_bonds, num_edges;
3862 : : int f1, f2, c1, c2, edge_cap, edge_flow, st_cap, st_flow, flag_alt_bond;
3863 : : int tot_st_cap, tot_st_flow;
3864 : : int max_tg, max_edges, max_vertices, len_alt_path, max_iedges, num_iedges, num_altp;
3865 : :
3866 : : /* count vertices */
3867 : 0 : max_tg = pTCGroups->num_tgroups;
3868 : : /* +1 for a super-tautomeric group */
3869 : : /* max_vertices = num_atoms + nMaxAddAtoms + max_tg + 1; */
3870 : 0 : max_vertices = pTCGroups->nVertices + nMaxAddAtoms;
3871 : :
3872 : : /* count edges */
3873 : 0 : num_changed_bonds = 0;
3874 : 0 : num_bonds = pTCGroups->num_bonds;
3875 : :
3876 : : /* each atom has enough edges to belong to a tautomeric group + nMaxAddEdges */
3877 : : /* number of atoms is large enough to accommodate max. possible number of t-groups + nMaxAddAtoms */
3878 : : /* max_altp cannot be larger than BN_MAX_ALTP = 16 */
3879 : 0 : num_edges = pTCGroups->nEdges;
3880 : :
3881 : : /* +max_tg for edges between t-groups and super-tautomeric group */
3882 : 0 : max_edges = num_edges + ( nMaxAddEdges + NUM_KINDS_OF_GROUPS )*max_vertices;
3883 : 0 : max_iedges = 2 * max_edges + pTCGroups->nAddIedges;
3884 : 0 : len_alt_path = max_vertices + iALTP_HDR_LEN + 1; /* may overflow if an edge is traversed in 2 directions */
3885 [ # # ]: 0 : len_alt_path += inchi_max( max_vertices / 2, 16 ); /* to avoid the overflow */
3886 : :
3887 [ # # ]: 0 : if (!(pBNS = (BN_STRUCT*)inchi_calloc(1, sizeof(BN_STRUCT))) ||
3888 [ # # ]: 0 : !(pBNS->edge = (BNS_EDGE*)inchi_calloc(max_edges, sizeof(BNS_EDGE))) ||
3889 [ # # ]: 0 : !(pBNS->vert = (BNS_VERTEX*)inchi_calloc(max_vertices, sizeof(BNS_VERTEX))) ||
3890 [ # # ]: 0 : !(pBNS->iedge = (BNS_IEDGE*)inchi_calloc(max_iedges, sizeof(BNS_IEDGE))))
3891 : : {
3892 : 0 : return DeAllocateBnStruct( pBNS );
3893 : : }
3894 : :
3895 : : /* alt path init (standard spell) */
3896 [ # # # # ]: 0 : for (num_altp = 0; num_altp < max_altp && num_altp < BN_MAX_ALTP; num_altp++)
3897 : : {
3898 [ # # ]: 0 : if (!( pBNS->altp[num_altp] = (BNS_ALT_PATH*) inchi_calloc( len_alt_path, sizeof( BNS_ALT_PATH ) ) ))
3899 : : {
3900 : 0 : return DeAllocateBnStruct( pBNS );
3901 : : }
3902 : 0 : ALTP_ALLOCATED_LEN( pBNS->altp[num_altp] ) = len_alt_path;
3903 : 0 : pBNS->len_alt_path = len_alt_path; /* ??? duplication ??? */
3904 : : /* re-init */
3905 : 0 : ALTP_DELTA( pBNS->altp[num_altp] ) = 0;
3906 : 0 : ALTP_START_ATOM( pBNS->altp[num_altp] ) = NO_VERTEX;
3907 : 0 : ALTP_END_ATOM( pBNS->altp[num_altp] ) = NO_VERTEX;
3908 : 0 : ALTP_PATH_LEN( pBNS->altp[num_altp] ) = 0;
3909 : : }
3910 : 0 : pBNS->alt_path = NULL;
3911 : 0 : pBNS->num_altp = 0;
3912 : 0 : pBNS->max_altp = num_altp;
3913 : :
3914 : :
3915 : : /* fill vertices (no connectivity) */
3916 : 0 : iedge = pBNS->iedge;
3917 : 0 : num_iedges = 0;
3918 : 0 : tot_st_cap = tot_st_flow = 0;
3919 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
3920 : : {
3921 : : /* count edges incident to pBNS->vert[i] */
3922 : 0 : k = at[i].valence + ( at[i].endpoint != 0 ) + ( nMaxAddEdges /*+ NUM_KINDS_OF_GROUPS*/ );
3923 [ # # ]: 0 : if (( j = pVA[i].cnListIndex - 1 ) >= 0)
3924 : : {
3925 : : /* add number of neighbors in the ChargeStruct */
3926 : 0 : k += nNumEdgesToCnVertex( cnList[j].pCN, cnList[j].len, 0 );
3927 : : }
3928 : : /* set max number of edges for the vertex */
3929 : 0 : pBNS->vert[i].max_adj_edges = k;
3930 : 0 : pBNS->vert[i].iedge = iedge;
3931 : 0 : iedge += k;
3932 : : /* add atom vertex cap */
3933 : 0 : st_cap = 0;
3934 : 0 : st_flow = 0;
3935 : 0 : bNeedsFlower1 = AtomStcapStflow( at, pVA, pSrm, i, &c1, &f1, NULL, NULL );
3936 : : /* pVA[i].cNumBondsToMetal = bNeedsFlower1; */
3937 : : /* GetAtomStCapFlow( at, pVA, pSrm, i, &c1, &f1 ); */
3938 : 0 : st_cap += c1;
3939 [ # # ]: 0 : st_cap += bNeedsFlower1 ? 0 : pVA[i].cInitFreeValences;
3940 : 0 : pBNS->vert[i].st_edge.cap = st_cap; /* the 1st time st_cap is set */
3941 : 0 : pBNS->vert[i].st_edge.cap0 = pBNS->vert[i].st_edge.cap;
3942 : 0 : tot_st_cap += st_cap;
3943 : : }
3944 : :
3945 : 0 : num_iedges = (int) ( iedge - pBNS->iedge );
3946 [ # # ]: 0 : if (max_iedges - num_iedges < ( nMaxAddEdges + NUM_KINDS_OF_GROUPS )*max_vertices)
3947 : : {
3948 : 0 : return DeAllocateBnStruct( pBNS );
3949 : : }
3950 : :
3951 : 0 : pBNS->num_atoms = num_atoms; /* number of real atoms */
3952 : 0 : pBNS->num_added_atoms = 0;
3953 : 0 : pBNS->num_t_groups = 0; /* number of added t-groups */
3954 : 0 : pBNS->num_c_groups = 0;
3955 : 0 : pBNS->nMaxAddAtoms = nMaxAddAtoms;
3956 : 0 : pBNS->nMaxAddEdges = nMaxAddEdges;
3957 : :
3958 : 0 : pBNS->num_vertices = num_atoms; /* current number of vertices, in general a sum of
3959 : : pBNS->num_atoms
3960 : : pBNS->num_t_groups
3961 : : number of c-groups
3962 : : number of auxiliary vertices
3963 : : pBNS->num_added_atoms
3964 : : */
3965 : 0 : pBNS->max_vertices = max_vertices;
3966 : :
3967 : :
3968 : 0 : pBNS->num_bonds = num_bonds; /* number of real edges (bonds) */
3969 : 0 : pBNS->max_edges = max_edges;
3970 : 0 : pBNS->max_iedges = max_iedges;
3971 : :
3972 : :
3973 : : /*
3974 : : To remove t-groups and added atoms:
3975 : :
3976 : : for ( i = 0; i < pBNS->num_atoms; i ++ )
3977 : : {
3978 : : for ( j = pBNS->vert[i].num_adj_edges-1; 0 <= j; j -- )
3979 : : {
3980 : : k = pBNS->edge[pBNS->vert[i].iedge[j]].neighbor12 ^ i;
3981 : : if ( pBNS->vert[k].type & BNS_VERT_TYPE_ATOM )
3982 : : {
3983 : : pBNS->vert[i].num_adj_edges = j+1;
3984 : : break;
3985 : : }
3986 : : }
3987 : : }
3988 : :
3989 : : pBNS->num_vertices = pBNS->num_atoms;
3990 : : pBNS->num_edges = pBNS->num_bonds;
3991 : : pBNS->num_added_atoms = 0;
3992 : : pBNS->num_t_groups = 0;
3993 : : pBNS->num_added_edges = 0;
3994 : :
3995 : : ALTP_DELTA(pBNS->alt_path) = 0;
3996 : : ALTP_START_ATOM(pBNS->alt_path) = NO_VERTEX;
3997 : : ALTP_END_ATOM(pBNS->alt_path) = NO_VERTEX;
3998 : : ALTP_PATH_LEN(pBNS->alt_path) = 0;
3999 : :
4000 : : */
4001 : :
4002 : :
4003 : : /* add and fill edges and connectivity */
4004 [ # # ]: 0 : for (i = 0, n_edges = 0; i < num_atoms; i++)
4005 : : {
4006 : 0 : vert = pBNS->vert + i; /* pointer to the ith vertex */
4007 : 0 : st_cap = 0;
4008 : 0 : st_flow = 0;
4009 : 0 : flag_alt_bond = 0;
4010 [ # # ]: 0 : for (j = 0; j < at[i].valence; j++)
4011 : : {
4012 : 0 : neigh = at[i].neighbor[j];
4013 : : /* find this bond at the neighbor */
4014 [ # # ]: 0 : for (k = 0; k < at[neigh].valence; k++)
4015 : : {
4016 [ # # ]: 0 : if (at[neigh].neighbor[k] == i)
4017 : : {
4018 : 0 : break;
4019 : : }
4020 : : }
4021 : 0 : bond_type = ( at[i].bond_type[j] & BOND_TYPE_MASK );
4022 : 0 : bond_mark = ( at[i].bond_type[j] & ~BOND_TYPE_MASK );
4023 [ # # # # : 0 : if (bond_type != BOND_SINGLE && bond_type != BOND_DOUBLE && bond_type != BOND_TRIPLE)
# # ]
4024 : : {
4025 : : /* make unknown bonds single */
4026 : 0 : bond_type = BOND_SINGLE;
4027 : 0 : at[i].bond_type[j] = bond_mark | bond_type;
4028 : 0 : num_changed_bonds++;
4029 : : }
4030 [ # # ]: 0 : if (neigh > i)
4031 : : {
4032 : : /* this is the first time we encounter this bond */
4033 : 0 : bNeedsFlower1 = AtomStcapStflow( at, pVA, pSrm, i, &c1, &f1, NULL, NULL );
4034 : : /* GetAtomStCapFlow( at, pVA, pSrm, i, &c1, &f1 ); */
4035 [ # # ]: 0 : c1 += bNeedsFlower1 ? 0 : pVA[i].cInitFreeValences; /* elevate cap to the lowest valence in ChargeStruct */
4036 : 0 : bNeedsFlower2 = AtomStcapStflow( at, pVA, pSrm, neigh, &c2, &f2, NULL, NULL );
4037 : : /* GetAtomStCapFlow( at, pVA, pSrm, neigh, &c2, &f2 ); */
4038 [ # # ]: 0 : c2 += bNeedsFlower2 ? 0 : pVA[neigh].cInitFreeValences; /* elevate cap to the lowest valence in ChargeStruct */
4039 : :
4040 : : /* at this point -O would have st_cap=st_flow=0 because the lowest valence=1 for charge=-1 */
4041 : : /* however, if -O belongs to a t-group its cap would be 1, flow = 0 */
4042 : : /*f1 = MAX_AT_FLOW(at[i]);*/
4043 : : /*f2 = MAX_AT_FLOW(at[neigh]);*/
4044 : :
4045 : 0 : edge_flow = BondFlowMaxcapMinorder( at, pVA, pSrm, i, j, &edge_cap, &min_order, NULL );
4046 : :
4047 : 0 : pBNS->edge[n_edges].neighbor1 = (AT_NUMB) i;
4048 : 0 : pBNS->edge[n_edges].neighbor12 = (AT_NUMB) ( i ^ neigh );
4049 : 0 : pBNS->edge[n_edges].flow =
4050 : 0 : pBNS->edge[n_edges].flow0 = edge_flow;
4051 : 0 : pBNS->edge[n_edges].cap =
4052 : 0 : pBNS->edge[n_edges].cap0 = edge_cap;
4053 : 0 : pBNS->edge[n_edges].neigh_ord[0] = j; /* iedge to neigh index at vertex[i], i < neigh */
4054 : 0 : pBNS->edge[n_edges].neigh_ord[1] = k; /* iedge to i index at vertex[neigh], i < neigh */
4055 : 0 : pBNS->edge[n_edges].pass = 0;
4056 : 0 : pBNS->edge[n_edges].forbidden = 0; /* may be forbidden if edge_flow = 1: stereogenic fixed double bond */
4057 [ # # ]: 0 : if (bond_type == BOND_TYPE_DOUBLE)
4058 : : {
4059 : : /* forbid changing stereogenic double bonds */
4060 [ # # # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_BONDS && at[i].sb_parity[m]; m++)
4061 : : {
4062 [ # # ]: 0 : if (at[i].sb_ord[m] == j)
4063 : : {
4064 : 0 : pBNS->edge[n_edges].forbidden |= BNS_EDGE_FORBIDDEN_MASK;
4065 : 0 : break;
4066 : : }
4067 : : }
4068 : : }
4069 [ # # ]: 0 : if (pBNS->vert[neigh].iedge) /* djb-rwth: fixing a NULL pointer dereference */
4070 : : {
4071 : 0 : vert->iedge[j] = pBNS->vert[neigh].iedge[k] = n_edges++; /* same iedge index as neighbor index in at[] */
4072 : : }
4073 : : }
4074 : : else
4075 : : {
4076 : : /* this is the second time we encounter this bond. It was stored at */
4077 : 0 : int iedge2 = pBNS->vert[neigh].iedge[k];
4078 : 0 : edge_cap = pBNS->edge[iedge2].cap;
4079 : 0 : edge_flow = pBNS->edge[iedge2].flow;
4080 : : }
4081 : 0 : st_flow += edge_flow;
4082 : : /*
4083 : : st_cap += edge_cap;
4084 : : */
4085 : : }
4086 : :
4087 : 0 : vert->num_adj_edges = j;
4088 : : /*
4089 : : vert->st_edge.cap =
4090 : : vert->st_edge.cap0 = st_cap;
4091 : : */
4092 : 0 : vert->st_edge.flow =
4093 : 0 : vert->st_edge.flow0 = st_flow;
4094 : 0 : vert->type = BNS_VERT_TYPE_ATOM;
4095 : : /*
4096 : : tot_st_cap += vert->st_edge.cap;
4097 : : */
4098 : 0 : tot_st_flow += vert->st_edge.flow;
4099 : : }
4100 : :
4101 : 0 : *pNum_changed_bonds = num_changed_bonds / 2;
4102 : :
4103 : 0 : pBNS->num_edges = n_edges; /* number of edges */
4104 : 0 : pBNS->num_iedges = num_iedges;
4105 : 0 : pBNS->num_added_edges = 0;
4106 : :
4107 : 0 : pBNS->tot_st_cap = tot_st_cap;
4108 : 0 : pBNS->tot_st_flow = tot_st_flow;
4109 : :
4110 : : /* exit_function: */
4111 : :
4112 : 0 : return pBNS;
4113 : : }
4114 : :
4115 : :
4116 : : /****************************************************************************/
4117 : 0 : void IncrZeroBondsAndClearEndpts( inp_ATOM *at, int num_at, int iComponent )
4118 : : {
4119 : : int i, j;
4120 [ # # ]: 0 : for (i = 0; i < num_at; i++)
4121 : : {
4122 : 0 : at[i].endpoint = 0;
4123 : 0 : at[i].component = iComponent;
4124 [ # # ]: 0 : for (j = 0; j < at[i].valence; j++)
4125 : : {
4126 [ # # ]: 0 : if (!at[i].bond_type[j])
4127 : : {
4128 : 0 : at[i].bond_type[j] = BOND_TYPE_SINGLE;
4129 : 0 : at[i].chem_bonds_valence += BOND_TYPE_SINGLE;
4130 : : }
4131 : : }
4132 : : }
4133 : 0 : }
4134 : :
4135 : :
4136 : : /****************************************************************************/
4137 : 0 : void IncrZeroBonds( inp_ATOM *at, int num_at, int iComponent )
4138 : : {
4139 : : int i, j;
4140 [ # # ]: 0 : for (i = 0; i < num_at; i++)
4141 : : {
4142 : 0 : at[i].component = iComponent;
4143 [ # # ]: 0 : for (j = 0; j < at[i].valence; j++)
4144 : : {
4145 [ # # ]: 0 : if (!at[i].bond_type[j])
4146 : : {
4147 : 0 : at[i].bond_type[j] = BOND_TYPE_SINGLE;
4148 : 0 : at[i].chem_bonds_valence += BOND_TYPE_SINGLE;
4149 : : }
4150 : : }
4151 : : }
4152 : 0 : }
4153 : :
4154 : :
4155 : : /****************************************************************************/
4156 : 0 : void ClearEndpts( inp_ATOM *at, int num_at )
4157 : : {
4158 : : int i;
4159 [ # # ]: 0 : for (i = 0; i < num_at; i++)
4160 : : {
4161 : 0 : at[i].endpoint = 0;
4162 : : }
4163 : 0 : }
4164 : :
4165 : :
4166 : : /****************************************************************************/
4167 : : #define ANY_VERT_TYPE(X) (((X) & (BNS_VERT_TYPE_ATOM | BNS_VERT_TYPE_TGROUP | BNS_VERT_TYPE_C_GROUP)) && \
4168 : : !((X) & (BNS_VERT_TYPE_SUPER_TGROUP)))
4169 : : #define GRP_VERT_TYPE(X) (((X) & (BNS_VERT_TYPE_TGROUP | BNS_VERT_TYPE_C_GROUP)) && \
4170 : : !((X) & (BNS_VERT_TYPE_SUPER_TGROUP)))
4171 : :
4172 : : typedef struct tagVertexFlow
4173 : : {
4174 : : int type;
4175 : : Vertex v;
4176 : : EdgeIndex e_In;
4177 : : EdgeIndex e_Out;
4178 : : EdgeFlow delta_In;
4179 : : EdgeFlow delta_Out;
4180 : : Vertex bUsed; /* indicates the charge edge belongs to already processed atom */
4181 : : } VF;
4182 : : #define NUM_VF 3
4183 : : #define VF_USED_IN 1
4184 : : #define VF_USED_OUT 2
4185 : : #define VF_USED_ALL (VF_USED_IN | VF_USED_OUT)
4186 : :
4187 : :
4188 : :
4189 : : int GetDeltaChargeFromVF( BN_STRUCT *pBNS, VAL_AT *pVA, VF *vf );
4190 : :
4191 : :
4192 : :
4193 : : /****************************************************************************/
4194 : 0 : int GetDeltaChargeFromVF( BN_STRUCT *pBNS, VAL_AT *pVA, VF *vf )
4195 : : {
4196 : 0 : int i, v = NO_VERTEX;
4197 [ # # # # : 0 : int ieIn1 = ( !( vf->bUsed & VF_USED_IN ) && vf->e_In >= 0 && vf->delta_In ) ? vf->e_In + 1 : NO_VERTEX;
# # ]
4198 [ # # # # : 0 : int ieOut1 = ( !( vf->bUsed & VF_USED_OUT ) && vf->e_Out >= 0 && vf->delta_Out ) ? vf->e_Out + 1 : NO_VERTEX;
# # ]
4199 : : int nInitCharge, nPlusFlow, nMinusFlow, nDeltaCharge, nNumDeltaCharge, eCPlus, eCMinus;
4200 : :
4201 [ # # ]: 0 : if (!( vf->type & BNS_VERT_TYPE_C_GROUP ) ||
4202 [ # # # # ]: 0 : ( vf->type & BNS_VERT_TYPE_SUPER_TGROUP ) ||
4203 [ # # ]: 0 : ( ieIn1 == NO_VERTEX && ieOut1 == NO_VERTEX ))
4204 : : {
4205 : 0 : return 0;
4206 : : }
4207 [ # # ]: 0 : if (vf->type & BNS_VERT_TYPE_C_NEGATIVE)
4208 : : {
4209 : : /* negative charge edge */
4210 [ # # ]: 0 : for (i = 0; i < pBNS->num_atoms; i++)
4211 : : {
4212 [ # # # # ]: 0 : if (pVA[i].nCMinusGroupEdge == ieIn1 || pVA[i].nCMinusGroupEdge == ieOut1)
4213 : : {
4214 : 0 : v = i;
4215 : 0 : break;
4216 : : }
4217 : : }
4218 : : }
4219 : : else
4220 : : {
4221 : : /* positive charge edge */
4222 [ # # ]: 0 : for (i = 0; i < pBNS->num_atoms; i++)
4223 : : {
4224 [ # # # # ]: 0 : if (pVA[i].nCPlusGroupEdge == ieIn1 || pVA[i].nCPlusGroupEdge == ieOut1)
4225 : : {
4226 : 0 : v = i;
4227 : 0 : break;
4228 : : }
4229 : : }
4230 : : }
4231 : :
4232 [ # # ]: 0 : if (v == NO_VERTEX)
4233 : 0 : return 0;
4234 : :
4235 : 0 : nInitCharge = pVA[v].cInitCharge;
4236 : 0 : nPlusFlow = nMinusFlow = 0;
4237 : 0 : nNumDeltaCharge = 0;
4238 : :
4239 [ # # ]: 0 : if (( eCPlus = pVA[v].nCPlusGroupEdge - 1 ) >= 0)
4240 : : {
4241 : 0 : nPlusFlow = pBNS->edge[eCPlus].cap
4242 : 0 : - pBNS->edge[eCPlus].flow;
4243 : : }
4244 : :
4245 [ # # ]: 0 : if (( eCMinus = pVA[v].nCMinusGroupEdge - 1 ) >= 0)
4246 : : {
4247 : 0 : nMinusFlow = -pBNS->edge[eCMinus].flow;
4248 : : }
4249 : 0 : nInitCharge += nPlusFlow + nMinusFlow;
4250 : :
4251 : 0 : nDeltaCharge = 0;
4252 : :
4253 [ # # ]: 0 : if (!( vf[0].bUsed & VF_USED_OUT ))
4254 : : {
4255 [ # # # # ]: 0 : if (vf[0].e_Out == eCPlus || vf[0].e_Out == eCMinus)
4256 : : {
4257 : 0 : nDeltaCharge -= vf[0].delta_Out;
4258 : 0 : vf[0].bUsed |= VF_USED_OUT;
4259 : : }
4260 : : }
4261 : :
4262 [ # # ]: 0 : if (!( vf[0].bUsed & VF_USED_IN ))
4263 : : {
4264 [ # # # # ]: 0 : if (vf[0].e_In == eCPlus || vf[0].e_In == eCMinus)
4265 : : {
4266 : 0 : nDeltaCharge -= vf[0].delta_In;
4267 : 0 : vf[0].bUsed |= VF_USED_IN;
4268 : : }
4269 : : }
4270 : :
4271 [ # # # # ]: 0 : if (!nInitCharge && nDeltaCharge)
4272 : : {
4273 : 0 : nNumDeltaCharge++;
4274 : : }
4275 [ # # # # ]: 0 : else if (nInitCharge && 0 == nInitCharge + nDeltaCharge)
4276 : : {
4277 : 0 : nNumDeltaCharge--;
4278 : : }
4279 : :
4280 : 0 : return nNumDeltaCharge;
4281 : : }
4282 : :
4283 : :
4284 : : /****************************************************************************/
4285 : 0 : int EvaluateChargeChanges( BN_STRUCT *pBNS, VAL_AT *pVA, int *pnDeltaH, int *pnDeltaCharge, int *pnNumVisitedAtoms )
4286 : : {
4287 : 0 : int pass, i, j, v0, v1, v2, v, ineigh1, /*ineigh2,*/ vLast, n, delta, ret, ie, err = 0;
4288 : : BNS_EDGE *edge;
4289 : : int nDeltaH, nDeltaCharge, iPrev, nInitCharge, nPlusFlow, nMinusFlow;
4290 : 0 : int nNumDeltaH = 0;
4291 : 0 : int nNumDeltaCharge = 0;
4292 : 0 : int nNumVisitedAtoms = 0;
4293 : : VF vf[NUM_VF + 1];
4294 : :
4295 : 0 : *pnDeltaH = 0;
4296 : 0 : *pnDeltaCharge = 0;
4297 : 0 : *pnNumVisitedAtoms = 0;
4298 : :
4299 [ # # ]: 0 : for (pass = pBNS->num_altp - 1, ret = 0; 0 <= pass; pass--)
4300 : : {
4301 : :
4302 : 0 : pBNS->alt_path = pBNS->altp[pass];
4303 : 0 : v1 = ALTP_START_ATOM( pBNS->alt_path );
4304 : 0 : n = ALTP_PATH_LEN( pBNS->alt_path );
4305 : 0 : delta = ALTP_DELTA( pBNS->alt_path );
4306 : 0 : vLast = ALTP_END_ATOM( pBNS->alt_path );
4307 : 0 : v0 = v2 = NO_VERTEX;
4308 : :
4309 : 0 : memset( vf, 0, sizeof( vf ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4310 [ # # ]: 0 : for (i = 0; i < (int) ( sizeof( vf ) / sizeof( vf[0] ) ); i++)
4311 : : {
4312 : 0 : vf[i].v = NO_VERTEX; /* = -2 */
4313 : 0 : vf[i].e_In = NO_VERTEX;
4314 : 0 : vf[i].e_Out = NO_VERTEX;
4315 : : }
4316 : 0 : iPrev = 0;
4317 : : /* add to the queue */
4318 [ # # # # ]: 0 : if (ANY_VERT_TYPE( pBNS->vert[v1].type ))
4319 : : {
4320 [ # # ]: 0 : if (pBNS->vert[v1].type & BNS_VERT_TYPE_ATOM)
4321 : : {
4322 : 0 : nNumVisitedAtoms++;
4323 : : }
4324 : 0 : vf[2].type = pBNS->vert[v1].type;
4325 : 0 : vf[2].v = v1;
4326 : 0 : iPrev = 2;
4327 : : }
4328 : :
4329 : 0 : nNumDeltaH = 0;
4330 : 0 : nNumDeltaCharge = 0;
4331 : 0 : nNumVisitedAtoms = 0;
4332 : :
4333 [ # # ]: 0 : for (i = 0; i < n; i++, delta = -delta, v0 = v1, v1 = v2) /* djb-rwth: removing redundant code */
4334 : : {
4335 : 0 : ineigh1 = ALTP_THIS_ATOM_NEIGHBOR( pBNS->alt_path, i ); /* v1->v2 neighbor */
4336 : : /*ineigh2 = ALTP_NEXT_ATOM_NEIGHBOR(pBNS->alt_path, i);*/ /* v2->v1 neighbor */
4337 : 0 : edge = pBNS->edge + ( ie = pBNS->vert[v1].iedge[ineigh1] );
4338 : : /* follow the BN Structure, not the inp_ATOM, to take care of swithching to
4339 : : t-groups, c-groups or other fictitious edges/vertices
4340 : : */
4341 : :
4342 [ # # ]: 0 : if (iPrev)
4343 : : {
4344 : : /* add exit delta and edge */
4345 : 0 : vf[2].e_Out = ie;
4346 : 0 : vf[2].delta_Out = delta;
4347 : : }
4348 : :
4349 : 0 : v2 = edge->neighbor12 ^ v1; /* next vertex */
4350 [ # # ]: 0 : if (pBNS->vert[v2].type & BNS_VERT_TYPE_ATOM)
4351 : : {
4352 : 0 : nNumVisitedAtoms++;
4353 : : }
4354 : :
4355 [ # # # # : 0 : if (( ANY_VERT_TYPE( pBNS->vert[v2].type ) || i == n - 1 ) &&
# # ]
4356 [ # # # # ]: 0 : ( vf[0].type & BNS_VERT_TYPE_C_GROUP ) && vf[0].bUsed != VF_USED_ALL)
4357 : : {
4358 : : /* unused vertex is about to be discarded */
4359 : 0 : nNumDeltaCharge += GetDeltaChargeFromVF( pBNS, pVA, &vf[0] );
4360 : : }
4361 : :
4362 [ # # # # ]: 0 : if (ANY_VERT_TYPE( pBNS->vert[v2].type ))
4363 : : {
4364 : : /* shift the queue */
4365 : 0 : vf[0] = vf[1];
4366 : 0 : vf[1] = vf[2];
4367 : 0 : vf[2] = vf[3]; /* make vf[2] empty */
4368 : : /* add next vertex */
4369 : 0 : vf[2].v = v2;
4370 : 0 : vf[2].type = pBNS->vert[v2].type;
4371 : 0 : vf[2].e_In = ie;
4372 : 0 : vf[2].delta_In = delta;
4373 : 0 : iPrev = 2; /* indicates a newly added vertex */
4374 : : }
4375 [ # # ]: 0 : else if (i == n - 1)
4376 : : {
4377 : : /* shift the queue */
4378 : 0 : vf[0] = vf[1];
4379 : 0 : vf[1] = vf[2];
4380 : 0 : vf[2] = vf[3]; /* make vf[2] empty */
4381 : 0 : iPrev = 1; /* indicates the last vertex */
4382 : : }
4383 : : else
4384 : : {
4385 : 0 : iPrev = 0; /* no new vertex has been added */
4386 : : }
4387 : :
4388 [ # # # # ]: 0 : if (iPrev && ( vf[1].type & BNS_VERT_TYPE_ATOM ))
4389 : : {
4390 : : /* a new vertex has just been added and */
4391 : : /* an atom is in the middle of the queue */
4392 : : EdgeIndex eCPlus, eCMinus;
4393 : 0 : v = vf[1].v;
4394 : 0 : nInitCharge = pVA[v].cInitCharge;
4395 : 0 : nPlusFlow = nMinusFlow = 0;
4396 [ # # ]: 0 : if (( eCPlus = pVA[v].nCPlusGroupEdge - 1 ) >= 0)
4397 : : {
4398 : 0 : nPlusFlow = pBNS->edge[eCPlus].cap
4399 : 0 : - pBNS->edge[eCPlus].flow;
4400 : : }
4401 [ # # ]: 0 : if (( eCMinus = pVA[v].nCMinusGroupEdge - 1 ) >= 0)
4402 : : {
4403 : 0 : nMinusFlow = -pBNS->edge[eCMinus].flow;
4404 : : }
4405 : 0 : nInitCharge += nPlusFlow + nMinusFlow;
4406 : :
4407 : 0 : nDeltaH = nDeltaCharge = 0;
4408 : :
4409 [ # # ]: 0 : if (vf[0].type & BNS_VERT_TYPE_TGROUP)
4410 : : {
4411 : 0 : nDeltaH -= delta;
4412 : : }
4413 [ # # ]: 0 : else if (( vf[0].type & BNS_VERT_TYPE_C_GROUP ) &&
4414 [ # # ]: 0 : !( vf[0].bUsed & VF_USED_OUT ))
4415 : : {
4416 [ # # # # ]: 0 : if (vf[0].e_Out == eCPlus || vf[0].e_Out == eCMinus)
4417 : : {
4418 : 0 : nDeltaCharge -= vf[0].delta_Out;
4419 : 0 : vf[0].bUsed |= VF_USED_OUT;
4420 : : }
4421 : : }
4422 : :
4423 [ # # ]: 0 : if (vf[2].type & BNS_VERT_TYPE_TGROUP)
4424 : : {
4425 : 0 : nDeltaH += delta;
4426 : : }
4427 [ # # ]: 0 : else if (( vf[2].type & BNS_VERT_TYPE_C_GROUP ) &&
4428 [ # # ]: 0 : !( vf[2].bUsed & VF_USED_IN ))
4429 : : {
4430 [ # # # # ]: 0 : if (vf[2].e_In == eCPlus || vf[2].e_In == eCMinus)
4431 : : {
4432 : 0 : nDeltaCharge -= vf[2].delta_In;
4433 : 0 : vf[2].bUsed |= VF_USED_IN;
4434 : : }
4435 : : }
4436 : :
4437 [ # # # # ]: 0 : if (!nInitCharge && nDeltaCharge)
4438 : : {
4439 : 0 : nNumDeltaCharge++;
4440 : : }
4441 [ # # # # ]: 0 : else if (nInitCharge && 0 == nInitCharge + nDeltaCharge)
4442 : : {
4443 : 0 : nNumDeltaCharge--;
4444 : : }
4445 : :
4446 : 0 : nNumDeltaH += abs( nDeltaH );
4447 : : /* nNumDeltaCharge += abs(nDeltaCharge); */
4448 : 0 : vf[1].bUsed = VF_USED_ALL;
4449 : : }
4450 : : }
4451 : :
4452 [ # # ]: 0 : for (j = 0; j < 3; j++)
4453 : : {
4454 : 0 : nNumDeltaCharge += GetDeltaChargeFromVF( pBNS, pVA, &vf[j] );
4455 : : }
4456 : :
4457 : 0 : *pnDeltaH += nNumDeltaH;
4458 : 0 : *pnDeltaCharge += nNumDeltaCharge;
4459 : 0 : *pnNumVisitedAtoms += nNumVisitedAtoms;
4460 : :
4461 : :
4462 [ # # ]: 0 : if (v2 != vLast)
4463 : : {
4464 : 0 : err = BNS_PROGRAM_ERR;
4465 : : }
4466 : : }
4467 [ # # ]: 0 : return err ? err : ret;
4468 : : }
4469 : :
4470 : :
4471 : : /****************************************************************************/
4472 : 0 : int RunBnsTestOnce( BN_STRUCT *pBNS, BN_DATA *pBD, VAL_AT *pVA, Vertex *pvFirst, Vertex *pvLast,
4473 : : int *pPathLen, int *pnDeltaH, int *pnDeltaCharge, int *pnNumVisitedAtoms )
4474 : : {
4475 : 0 : int bChangeFlow = 0; /* do not change flow */
4476 : : int delta, ret, ret2, pass; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
4477 : :
4478 : 0 : ReInitBnStructAltPaths( pBNS );
4479 : 0 : pass = 0;
4480 : 0 : pBNS->alt_path = pBNS->altp[pass];
4481 : 0 : pBNS->num_altp = 0;
4482 : 0 : pBNS->bChangeFlow = 0;
4483 : 0 : delta = BalancedNetworkSearch( pBNS, pBD, bChangeFlow );
4484 [ # # ]: 0 : if (delta > 0)
4485 : : {
4486 : 0 : pBNS->alt_path = pBNS->altp[pass];
4487 : 0 : *pvFirst = ALTP_START_ATOM( pBNS->alt_path );
4488 : 0 : *pPathLen = ALTP_PATH_LEN( pBNS->alt_path );
4489 : 0 : *pvLast = ALTP_END_ATOM( pBNS->alt_path );
4490 : 0 : pBNS->num_altp++;
4491 : 0 : ret2 = EvaluateChargeChanges( pBNS, pVA, pnDeltaH, pnDeltaCharge, pnNumVisitedAtoms ); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
4492 : : }
4493 : : else
4494 : : {
4495 : 0 : *pvFirst = NO_VERTEX;
4496 : 0 : *pPathLen = 0;
4497 : 0 : *pvLast = NO_VERTEX;
4498 : 0 : ret2 = 0;
4499 : : }
4500 : :
4501 : 0 : ReInitBnStructAltPaths( pBNS );
4502 : :
4503 : 0 : ret = ReInitBnData( pBD );
4504 : :
4505 [ # # ]: 0 : return ( delta >= 0 && ret > 0 ) ? -ret
4506 [ # # ]: 0 : : delta;
4507 : : }
4508 : :
4509 : :
4510 : : /****************************************************************************/
4511 : 0 : int RunBnsRestoreOnce( BN_STRUCT *pBNS, BN_DATA *pBD, VAL_AT *pVA, ALL_TC_GROUPS *pTCGroups )
4512 : : {
4513 : : /* run BNS for the first time */
4514 : 0 : int nTotalDelta = 0, ret = 0;
4515 : : int nDelta;
4516 : :
4517 : 0 : ReInitBnStructAltPaths( pBNS );
4518 : :
4519 : : do
4520 : : {
4521 : 0 : nDelta = RunBalancedNetworkSearch( pBNS, pBD, BNS_EF_CHNG_FLOW );
4522 [ # # # # ]: 0 : if (IS_BNS_ERROR( nDelta ))
4523 : : {
4524 : 0 : ret = nDelta;
4525 : 0 : goto exit_function;
4526 : : }
4527 : 0 : nTotalDelta += nDelta;
4528 : 0 : ReInitBnStructAltPaths( pBNS );
4529 : 0 : ret = ReInitBnData( pBD );
4530 [ # # ]: 0 : if (ret > 0)
4531 : : {
4532 : 0 : ret = -ret;
4533 : 0 : goto exit_function;
4534 : : }
4535 : : }
4536 [ # # # # ]: 0 : while (nDelta > 0 && ret == 0);
4537 : :
4538 : 0 : pBNS->tot_st_flow += 2 * nTotalDelta;
4539 : :
4540 : 0 : ret = nTotalDelta;
4541 : :
4542 : 0 : exit_function:
4543 : 0 : return ret;
4544 : : }
4545 : :
4546 : :
4547 : : /****************************************************************************/
4548 : : /* djb-rwth: this function is completely senseless -- discussion required */
4549 : 0 : int comp_cc_cand( const void *a1, const void *a2 )
4550 : : {
4551 : 0 : const CC_CAND *p1 = (const CC_CAND *) a1;
4552 : 0 : const CC_CAND *p2 = (const CC_CAND *) a2;
4553 : : int ret;
4554 : :
4555 [ # # ]: 0 : if ((ret = (int) p2->cMetal - (int) p1->cMetal)) /* djb-rwth: addressing LLVM warning */
4556 : 0 : return ret; /* metal first */
4557 [ # # ]: 0 : if ((ret = (int) p2->cNumBondsToMetal - (int) p1->cNumBondsToMetal)) /* djb-rwth: addressing LLVM warning */
4558 : 0 : return ret; /* connected to metal first */
4559 [ # # ]: 0 : if ((ret = (int) p2->cPeriodicRowNumber - (int) p1->cPeriodicRowNumber)) /* djb-rwth: addressing LLVM warning */
4560 : 0 : return ret; /* heaviest first */
4561 [ # # ]: 0 : if ((ret = (int) p2->num_bonds - (int) p1->num_bonds)) /* djb-rwth: addressing LLVM warning */
4562 : 0 : return ret; /* more bonds first */
4563 [ # # ]: 0 : if ((ret = (int) p1->chem_valence - (int) p2->chem_valence)) /* djb-rwth: addressing LLVM warning */
4564 : 0 : return ret; /* less bond order first */
4565 [ # # # # ]: 0 : if ((!p1->cNumValenceElectrons && p2->cNumValenceElectrons)) /* djb-rwth: addressing LLVM warning */
4566 : 0 : return -1; /* no valence electrons first */
4567 [ # # # # ]: 0 : if ((!p2->cNumValenceElectrons && p1->cNumValenceElectrons)) /* djb-rwth: addressing LLVM warning */
4568 : 0 : return -1; /* no valence electrons first */
4569 [ # # ]: 0 : if (((int) p2->cNumValenceElectrons - (int) p1->cNumValenceElectrons)) /* djb-rwth: addressing LLVM warning */
4570 : 0 : return ret; /* more valence electrons first */
4571 : 0 : ret = (int) p2->iat - (int) p1->iat; /* greater canon number first */
4572 : :
4573 : 0 : return ret;
4574 : : }
4575 : :
4576 : :
4577 : : /*****************************************************************************************************
4578 : : Locate E1=C-E2 where
4579 : : e ev are the edges
4580 : :
4581 : : E1 and E2 are atoms that belong to the same t-group
4582 : : C is an atom that does not belong to any t-group
4583 : : e is a forbidden edge
4584 : : ev is not a forbidden edge
4585 : :
4586 : : Make changes so that:
4587 : : E1(d)-C(d)-E2
4588 : :
4589 : : where (d) means doublet radical
4590 : :
4591 : : */
4592 : : /**************************************************************************************************/
4593 : 0 : int get_pVA_atom_type( VAL_AT *pVA, inp_ATOM *at, int iat, int bond_type )
4594 : : {
4595 : 0 : int type = 0, val;
4596 [ # # ]: 0 : if (pVA[iat].cNumValenceElectrons == 4)
4597 : : {
4598 [ # # ]: 0 : if (pVA[iat].cPeriodicRowNumber == 1)
4599 : : {
4600 : 0 : type |= EL_TYPE_C;
4601 : : }
4602 : : }
4603 : :
4604 [ # # ]: 0 : else if (pVA[iat].cNumValenceElectrons == 6)
4605 : : {
4606 [ # # ]: 0 : if (pVA[iat].cPeriodicRowNumber == 1)
4607 : : {
4608 : 0 : type |= EL_TYPE_O;
4609 : : }
4610 [ # # ]: 0 : else if (pVA[iat].cPeriodicRowNumber < 5)
4611 : : {
4612 : 0 : type |= EL_TYPE_S;
4613 : : }
4614 [ # # ]: 0 : if (bond_type == BOND_TYPE_SINGLE &&
4615 [ # # # # ]: 0 : ( type & ( EL_TYPE_O | EL_TYPE_S ) ) &&
4616 [ # # ]: 0 : 1 == nNoMetalBondsValence( at, iat ) &&
4617 : 0 : 1 == nNoMetalNumBonds( at, iat ))
4618 : : {
4619 : 0 : type |= EL_TYPE_OSt;
4620 : : }
4621 : : }
4622 : :
4623 [ # # ]: 0 : else if (pVA[iat].cNumValenceElectrons == 5)
4624 : : {
4625 [ # # ]: 0 : if (pVA[iat].cPeriodicRowNumber == 1)
4626 : : {
4627 : 0 : type |= EL_TYPE_N;
4628 : : }
4629 : : else
4630 : : {
4631 : 0 : type |= EL_TYPE_P;
4632 : : }
4633 : : }
4634 : :
4635 [ # # ]: 0 : else if (!is_el_a_metal( pVA[iat].cPeriodicNumber ))
4636 : : {
4637 : 0 : type |= EL_TYPE_X;
4638 : : }
4639 : :
4640 : : /* check for possibility to be a tautomeric endpoint (that is, be a Mobile H site) */
4641 : 0 : val = get_endpoint_valence( at[iat].el_number );
4642 : :
4643 [ # # # # : 0 : if (val && val > at[iat].valence && !at[iat].radical &&
# # ]
4644 [ # # # # ]: 0 : -1 <= at[iat].charge && at[iat].charge <= 0 &&
4645 [ # # ]: 0 : val == at[iat].chem_bonds_valence - at[iat].charge + at[iat].num_H)
4646 : : {
4647 : 0 : type |= EL_TYPE_PT;
4648 : : }
4649 : :
4650 : 0 : return type;
4651 : : }
4652 : :
4653 : :
4654 : : /*************************************************************************************/
4655 : 0 : int AllocEdgeList( EDGE_LIST *pEdges, int nLen )
4656 : : {
4657 [ # # # ]: 0 : switch (nLen)
4658 : : {
4659 : 0 : case EDGE_LIST_FREE:
4660 [ # # ]: 0 : if (NULL != pEdges->pnEdges)
4661 : : {
4662 [ # # ]: 0 : inchi_free( pEdges->pnEdges );
4663 : : }
4664 : : /* fall through */
4665 : : case EDGE_LIST_CLEAR:
4666 : 0 : memset( pEdges, 0, sizeof( *pEdges ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4667 : 0 : break;
4668 : 0 : default:
4669 [ # # # # ]: 0 : if (nLen > 0 && nLen != pEdges->num_alloc)
4670 : : {
4671 : 0 : EdgeIndex *tmp_edges = pEdges->pnEdges;
4672 : 0 : int tmp_num = pEdges->num_edges;
4673 : 0 : pEdges->pnEdges = (EdgeIndex *) inchi_calloc( nLen, sizeof( pEdges->pnEdges[0] ) );
4674 [ # # ]: 0 : if (!pEdges->pnEdges)
4675 : : {
4676 : 0 : return RI_ERR_ALLOC;
4677 : : }
4678 : 0 : tmp_num = inchi_min( tmp_num, nLen );
4679 [ # # # # ]: 0 : if (tmp_edges && tmp_num > 0)
4680 : : {
4681 : 0 : memcpy(pEdges->pnEdges, tmp_edges, tmp_num * sizeof(pEdges->pnEdges[0]));
4682 : 0 : pEdges->num_edges = tmp_num;
4683 : : }
4684 : : else
4685 : : {
4686 : 0 : pEdges->num_edges = 0;
4687 : : }
4688 [ # # ]: 0 : if (tmp_edges)
4689 : : {
4690 [ # # ]: 0 : inchi_free( tmp_edges );
4691 : : }
4692 : 0 : pEdges->num_alloc = nLen;
4693 : 0 : return 0;
4694 : : }
4695 : 0 : break;
4696 : : }
4697 : :
4698 : 0 : return 0;
4699 : : }
4700 : :
4701 : :
4702 : : /********************************************************************/
4703 : 0 : int AddToEdgeList( EDGE_LIST *pEdges, int iedge, int nAddLen )
4704 : : {
4705 [ # # ]: 0 : if (pEdges->num_alloc == pEdges->num_edges)
4706 : : {
4707 : : int ret;
4708 [ # # ]: 0 : if (nAddLen <= 0)
4709 : : {
4710 : 0 : return RI_ERR_PROGR;
4711 : : }
4712 [ # # ]: 0 : if ((ret = AllocEdgeList( pEdges, pEdges->num_alloc + nAddLen ))) /* djb-rwth: addressing LLVM warning */
4713 : : {
4714 : 0 : return ret;
4715 : : }
4716 : : }
4717 : :
4718 : 0 : pEdges->pnEdges[pEdges->num_edges++] = (EdgeIndex) iedge;
4719 : 0 : return 0;
4720 : : }
4721 : :
4722 : :
4723 : : /********************************************************************/
4724 : 0 : int RemoveFromEdgeListByIndex( EDGE_LIST *pEdges, int index )
4725 : : {
4726 : : int len;
4727 [ # # ]: 0 : if (0 <= ( len = pEdges->num_edges - index - 1 ))
4728 : : {
4729 [ # # ]: 0 : if (len)
4730 : : {
4731 : 0 : memmove(pEdges->pnEdges + index, pEdges->pnEdges + index + 1, len * sizeof(pEdges->pnEdges[0]));
4732 : : }
4733 : 0 : pEdges->num_edges--;
4734 : 0 : pEdges->pnEdges[pEdges->num_edges] = 0;
4735 : 0 : return 0;
4736 : : }
4737 : 0 : return -1;
4738 : : }
4739 : :
4740 : :
4741 : : /********************************************************************/
4742 : 0 : int FindInEdgeList( EDGE_LIST *pEdges, int iedge )
4743 : : {
4744 : : int i;
4745 : 0 : EdgeIndex ie = iedge;
4746 [ # # ]: 0 : for (i = pEdges->num_edges - 1; 0 <= i; i--)
4747 : : {
4748 [ # # ]: 0 : if (ie == pEdges->pnEdges[i])
4749 : : {
4750 : 0 : return i;
4751 : : }
4752 : : }
4753 : :
4754 : 0 : return -1;
4755 : : }
4756 : :
4757 : :
4758 : : /********************************************************************/
4759 : 0 : int RemoveFromEdgeListByValue( EDGE_LIST *pEdges, int iedge )
4760 : : {
4761 : 0 : int i, ret, n = 0;
4762 : 0 : EdgeIndex ie = iedge;
4763 [ # # ]: 0 : for (i = pEdges->num_edges - 1; 0 <= i; i--)
4764 : : {
4765 [ # # ]: 0 : if (ie == pEdges->pnEdges[i])
4766 : : {
4767 [ # # ]: 0 : if ((ret = RemoveFromEdgeListByIndex( pEdges, i ))) /* djb-rwth: addressing LLVM warning */
4768 : : {
4769 : 0 : return ret;
4770 : : }
4771 : 0 : n++;
4772 : : }
4773 : : }
4774 : :
4775 : 0 : return n;
4776 : : }
4777 : :
4778 : :
4779 : : /********************************************************************/
4780 : 0 : int AllocBfsQueue( BFS_Q *pQ, int num_at, int min_ring_size )
4781 : : {
4782 : 0 : int ret = 0;
4783 [ # # # ]: 0 : switch (num_at)
4784 : : {
4785 : 0 : case BFS_Q_FREE:
4786 [ # # ]: 0 : if (pQ->q)
4787 : : {
4788 : 0 : pQ->q = QueueDelete( pQ->q );
4789 : : }
4790 [ # # ]: 0 : if (pQ->nAtomLevel)
4791 : : {
4792 [ # # ]: 0 : inchi_free( pQ->nAtomLevel );
4793 : : }
4794 [ # # ]: 0 : if (pQ->cSource)
4795 : : {
4796 [ # # ]: 0 : inchi_free( pQ->cSource );
4797 : : }
4798 : : /* fall through */
4799 : : case BFS_Q_CLEAR:
4800 : 0 : memset( pQ, 0, sizeof( *pQ ) ); /* djb-rwth: memset_s C11/Annex K variant? */
4801 : 0 : return 0;
4802 : :
4803 : 0 : default:
4804 [ # # ]: 0 : if (num_at <= 0)
4805 : : {
4806 : 0 : ret = RI_ERR_PROGR;
4807 : 0 : goto exit_function;
4808 : : }
4809 [ # # ]: 0 : if (num_at > pQ->num_at)
4810 : : {
4811 [ # # ]: 0 : if (pQ->num_at)
4812 : : {
4813 : 0 : AllocBfsQueue( pQ, BFS_Q_FREE, 0 );
4814 : : }
4815 : 0 : pQ->q = QueueCreate( num_at + 1, sizeof( qInt ) );
4816 : 0 : pQ->nAtomLevel = (AT_RANK*) inchi_calloc( num_at, sizeof( pQ->nAtomLevel[0] ) );
4817 : 0 : pQ->cSource = (S_CHAR *) inchi_calloc( num_at, sizeof( pQ->cSource[0] ) );
4818 [ # # # # : 0 : if (!pQ->q || !pQ->cSource || !pQ->nAtomLevel)
# # ]
4819 : : {
4820 : 0 : ret = RI_ERR_ALLOC;
4821 : 0 : goto exit_function;
4822 : : }
4823 : 0 : pQ->num_at = num_at;
4824 : : }
4825 : 0 : pQ->min_ring_size = min_ring_size;
4826 : : }
4827 : :
4828 : 0 : exit_function:
4829 : 0 : return ret;
4830 : : }
4831 : :
4832 : :
4833 : : /*************************************************************************************/
4834 : 0 : void RemoveForbiddenEdgeMask( BN_STRUCT *pBNS, EDGE_LIST *pEdges, int forbidden_edge_mask )
4835 : : {
4836 : 0 : int i, mask = ~forbidden_edge_mask;
4837 [ # # ]: 0 : for (i = 0; i < pEdges->num_edges; i++)
4838 : : {
4839 : 0 : pBNS->edge[pEdges->pnEdges[i]].forbidden &= mask;
4840 : : }
4841 : 0 : }
4842 : :
4843 : :
4844 : : /*************************************************************************************/
4845 : 0 : void SetForbiddenEdgeMask( BN_STRUCT *pBNS, EDGE_LIST *pEdges, int forbidden_edge_mask )
4846 : : {
4847 : : int i;
4848 [ # # ]: 0 : for (i = 0; i < pEdges->num_edges; i++)
4849 : : {
4850 : 0 : pBNS->edge[pEdges->pnEdges[i]].forbidden |= forbidden_edge_mask;
4851 : : }
4852 : 0 : }
4853 : :
4854 : :
4855 : : /****************************************************************************/
4856 : 0 : void RemoveForbiddenBondFlowBits( BN_STRUCT *pBNS, int forbidden_edge_mask_int )
4857 : : {
4858 : : BNS_EDGE *e;
4859 : : int i;
4860 : 0 : int inv_forbidden_edge_mask = ~forbidden_edge_mask_int;
4861 [ # # ]: 0 : for (i = 0, e = pBNS->edge; i < pBNS->num_bonds; i++, e++)
4862 : : {
4863 : 0 : e->forbidden &= inv_forbidden_edge_mask;
4864 : : }
4865 : 0 : }
4866 : :
4867 : :
4868 : : /******************************************************************************************************
4869 : : upper vc
4870 : : edge /
4871 : : v1[i0]---v0
4872 : : \ /
4873 : : \ /
4874 : : \ /
4875 : : v1[i1]
4876 : : |
4877 : : |
4878 : : atom
4879 : : */
4880 : 0 : int GetChargeFlowerUpperEdge( BN_STRUCT *pBNS, VAL_AT *pVA, int nChargeEdge )
4881 : : {
4882 : 0 : int ret = NO_VERTEX, i, j, k, i0, i1;
4883 : : Vertex v0, v1[3], vc, v_t, v;
4884 : : BNS_EDGE *pe, *pe1[3], *pe_t;
4885 : : BNS_VERTEX *pv0, *pv1[3], *pv_t;
4886 : :
4887 [ # # ]: 0 : if (nChargeEdge < 0)
4888 : : {
4889 : 0 : goto exit_function;
4890 : : }
4891 : :
4892 : 0 : pe = pBNS->edge + nChargeEdge;
4893 : 0 : vc = pe->neighbor1; /* charge vertex */
4894 [ # # ]: 0 : if (!IS_BNS_VT_C_GR( pBNS->vert[vc].type ))
4895 : : {
4896 : 0 : vc = vc ^ pe->neighbor12;
4897 : : }
4898 : :
4899 : 0 : v0 = vc ^ pe->neighbor12; /* ChargeStruct vertex ? */
4900 : 0 : pv0 = pBNS->vert + v0;
4901 [ # # ]: 0 : if (IS_BNS_VT_ATOM( pv0->type ))
4902 : : {
4903 : 0 : goto exit_function; /* no charge flower exists */
4904 : : }
4905 : :
4906 : : /* 2 edges from v0 */
4907 [ # # # # ]: 0 : for (i = j = 0; i < pv0->num_adj_edges && j < 3; i++)
4908 : : {
4909 : 0 : pe1[j] = pBNS->edge + pv0->iedge[i];
4910 [ # # # # ]: 0 : if (vc != ( v1[j] = pe1[j]->neighbor12 ^ v0 ) &&
4911 : 0 : ( pv1[j] = pBNS->vert + v1[j],
4912 [ # # # # ]: 0 : !IS_BNS_VT_ATOM( pv1[j]->type ) && !IS_BNS_VT_C_GR( pv1[j]->type ) ))
4913 : : {
4914 : 0 : j++;
4915 : : }
4916 : : }
4917 : :
4918 [ # # # # ]: 0 : if (j != 2 || i != pv0->num_adj_edges)
4919 : : {
4920 : 0 : goto exit_function;
4921 : : }
4922 : :
4923 [ # # ]: 0 : if (pv1[1]->num_adj_edges == 2 &&
4924 [ # # ]: 0 : pv1[0]->num_adj_edges == 3)
4925 : : {
4926 : 0 : i0 = 1;
4927 : 0 : i1 = 0;
4928 : : }
4929 [ # # ]: 0 : else if (pv1[0]->num_adj_edges == 2 &&
4930 [ # # ]: 0 : pv1[1]->num_adj_edges == 3)
4931 : : {
4932 : 0 : i0 = 0;
4933 : 0 : i1 = 1;
4934 : : }
4935 : : else
4936 : : {
4937 : 0 : goto exit_function;
4938 : : }
4939 : :
4940 : : /* additional check: traverse edges around v1[i1] */
4941 : 0 : pv_t = pv1[i1];
4942 : 0 : v_t = v1[i1];
4943 [ # # ]: 0 : for (i = k = 0; i < pv_t->num_adj_edges; i++)
4944 : : {
4945 : 0 : pe_t = pBNS->edge + pv_t->iedge[i];
4946 : 0 : v = pe_t->neighbor12 ^ v_t; /* v1[i1] neighbor */
4947 [ # # ]: 0 : if (v == v0)
4948 : : {
4949 : 0 : k += 1;
4950 : : }
4951 [ # # ]: 0 : if (v == v1[i0])
4952 : : {
4953 : 0 : k += 2;
4954 : : }
4955 [ # # ]: 0 : if (IS_BNS_VT_ATOM( pBNS->vert[v].type ))
4956 : : {
4957 : 0 : k += 4;
4958 : : }
4959 : : }
4960 [ # # ]: 0 : if (k != 7)
4961 : : {
4962 : 0 : goto exit_function;
4963 : : }
4964 : :
4965 : 0 : ret = (int) ( pe1[i0] - pBNS->edge );
4966 : :
4967 : 0 : exit_function:
4968 : 0 : return ret;
4969 : : }
4970 : :
4971 : :
4972 : :
4973 : : #if (INCLUDE_NORMALIZATION_ENTRY_POINT == 1 )
4974 : : /********************************************************************************************
4975 : : input: allocate (num_at+num_deleted_H) atoms in inp_ATOM *at_norm, *at_fixed_bonds_out
4976 : : allocate t_group_info
4977 : : *********************************************************************************************/
4978 : : int NormalizeStructure( ICHICONST INPUT_PARMS *ip, STRUCT_DATA *sd, BN_STRUCT *pBNS,
4979 : : StrFromINChI *pStruct, inp_ATOM *at, inp_ATOM *at2,
4980 : : VAL_AT *pVA, ALL_TC_GROUPS *pTCGroups,
4981 : : inp_ATOM *at_norm, inp_ATOM *at_fixed_bonds_out, T_GROUP_INFO *t_group_info )
4982 : : {
4983 : : int i, ret, num_endpoints, nLenTaut;
4984 : : int num_at = pStruct->num_atoms;
4985 : : int num_deleted_H = pStruct->num_deleted_H;
4986 : : int len_at = num_at + num_deleted_H;
4987 : : /*
4988 : : T_GROUP_INFO tgi;
4989 : : T_GROUP_INFO *t_group_info = &tgi;
4990 : : inp_ATOM *at_fixed_bonds_out = NULL;
4991 : : inp_ATOM *at_norm = NULL;
4992 : :
4993 : : at_norm = (inp_ATOM *)inchi_calloc( len_at, sizeof(at_norm[0]) );
4994 : : at_fixed_bonds_out = (inp_ATOM *)inchi_calloc( len_at, sizeof(at_fixed_bonds_out[0]) );
4995 : : if ( !at_norm || !at_fixed_bonds_out ) {
4996 : : if ( at_norm ) inchi_free( at_norm );
4997 : : if ( at_fixed_bonds_out ) inchi_free( at_fixed_bonds_out );
4998 : : ret = RI_ERR_ALLOC;
4999 : : goto exit_function;
5000 : : }
5001 : : */
5002 : : /* call normalization only */
5003 : : memset( t_group_info, 0, sizeof( t_group_info[0] ) );
5004 : : t_group_info->tni.nNumRemovedExplicitH = pStruct->num_deleted_H;
5005 : : t_group_info->bTautFlags = ip->bTautFlags;
5006 : : t_group_info->bTautFlagsDone = 0; /* (ip->bTautFlagsDone | sd->bTautFlagsDone[INCHI_BAS]);*/
5007 : :
5008 : : memcpy( at2, at, len_at * sizeof( at2[0] ) );
5009 : : pStruct->at = at2;
5010 : : ret = CopyBnsToAtom( pStruct, pBNS, pVA, pTCGroups, 1 );
5011 : : pStruct->at = at;
5012 : : if (ret < 0)
5013 : : {
5014 : : goto exit_function;
5015 : : }
5016 : : #if ( FIND_RING_SYSTEMS == 1 )
5017 : : ret = MarkRingSystemsInp( at2, num_at, 0 );
5018 : : if (ret < 0)
5019 : : {
5020 : : goto exit_function;
5021 : : }
5022 : : #endif
5023 : : memcpy( at_norm, at2, len_at * sizeof( at_norm[0] ) );
5024 : : for (i = 0, num_endpoints = 0; i < num_at; i++)
5025 : : {
5026 : : num_endpoints += ( 0 != at_norm[i].endpoint );
5027 : : at_norm[i].endpoint = 0;
5028 : : }
5029 : :
5030 : : ret = mark_alt_bonds_and_taut_groups( ic, pCG, at_norm, at_fixed_bonds_out, num_at, t_group_info,
5031 : : NULL /* &inpbTautFlags*/, NULL /*inpbTautFlagsDone*/ );
5032 : : if (ret < 0)
5033 : : {
5034 : : goto exit_function;/* out of RAM or other normalization problem */
5035 : : }
5036 : : /* after normalization, t_group_info->t_group[i].num[0] = number of H + number of (-) */
5037 : : /* t_group_info->t_group[i].num[1] = number of (-) */
5038 : :
5039 : : /* --- count t-groups, remove (-)-only t-groups, replace -------------------------------*/
5040 : : /* t_group_info->t_group[i].num[0] with */
5041 : : /* t_group_info->t_group[i].num[0]-t_group_info->t_group[i].num[1] */
5042 : : nLenTaut = CountTautomerGroupsInpAt( at_norm, num_at, t_group_info );
5043 : : ret = nLenTaut;
5044 : : exit_function:
5045 : : return ret;
5046 : : }
5047 : : #endif
5048 : :
5049 : :
5050 : :
5051 : : /****************************************************************************/
5052 : 0 : int MakeOneInChIOutOfStrFromINChI2( struct tagCANON_GLOBALS *pCG,
5053 : : INCHI_CLOCK *ic,
5054 : : ICHICONST INPUT_PARMS *ip_inp,
5055 : : STRUCT_DATA *sd_inp,
5056 : : BN_STRUCT *pBNS, StrFromINChI *pStruct,
5057 : : inp_ATOM *at, inp_ATOM *at2, inp_ATOM *at3,
5058 : : VAL_AT *pVA, ALL_TC_GROUPS *pTCGroups,
5059 : : T_GROUP_INFO **t_group_info,
5060 : : inp_ATOM **at_norm, inp_ATOM **at_prep )
5061 : : {
5062 : : int ret;
5063 : : INPUT_PARMS ip_loc, *ip;
5064 : : STRUCT_DATA sd_loc, *sd;
5065 : :
5066 : 0 : ip_loc = *ip_inp;
5067 : 0 : sd_loc = *sd_inp;
5068 : 0 : ip = &ip_loc;
5069 : 0 : sd = &sd_loc;
5070 : :
5071 : 0 : memset( sd, 0, sizeof( *sd ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5072 : :
5073 : : /* create structure out of BNS */
5074 : 0 : memcpy(at2, at, ((long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H) * sizeof(at2[0])); /* djb-rwth: cast operator added */
5075 : 0 : pStruct->at = at2;
5076 : 0 : ret = CopyBnsToAtom( pStruct, pBNS, pVA, pTCGroups, 1 );
5077 : 0 : pStruct->at = at;
5078 [ # # ]: 0 : if (ret < 0)
5079 : : {
5080 : 0 : goto exit_function;/* out of RAM or other normalization problem */
5081 : : }
5082 : :
5083 : 0 : pStruct->at = at;
5084 : :
5085 : 0 : ret = MakeOneInChIOutOfStrFromINChI( pCG, ic, ip, sd, pStruct,
5086 : : at2, at3, pTCGroups );
5087 : :
5088 [ # # ]: 0 : if (ret < 0)
5089 : : {
5090 : 0 : goto exit_function;/* out of RAM or other normalization problem */
5091 : : }
5092 [ # # ]: 0 : if (at_norm)
5093 : : {
5094 : 0 : *at_norm = pStruct->pOne_norm_data[0]->at;
5095 : : }
5096 [ # # ]: 0 : if (at_prep)
5097 : : {
5098 [ # # # # ]: 0 : if (pStruct->pOne_norm_data[0]->bTautPreprocessed && pStruct->pOne_norm_data[0]->at_fixed_bonds)
5099 : : {
5100 : 0 : *at_prep = pStruct->pOne_norm_data[0]->at_fixed_bonds;
5101 : : }
5102 : : else
5103 : : /* get preprocessed structure in case of Fixed-H */
5104 [ # # # # : 0 : if (pStruct->iMobileH == TAUT_NON && pStruct->pOne_norm_data[1] && pStruct->pOne_norm_data[1]->bTautPreprocessed)
# # ]
5105 : : {
5106 : 0 : *at_prep = pStruct->pOne_norm_data[1]->at_fixed_bonds;
5107 : : }
5108 : : else
5109 : : {
5110 : 0 : *at_prep = NULL;
5111 : : }
5112 : : }
5113 [ # # ]: 0 : if (t_group_info)
5114 : : {
5115 [ # # ]: 0 : if (pStruct->iMobileH == TAUT_YES &&
5116 [ # # ]: 0 : pStruct->One_ti.num_t_groups &&
5117 [ # # # # ]: 0 : pStruct->One_ti.t_group && pStruct->One_ti.nEndpointAtomNumber)
5118 : : {
5119 : 0 : *t_group_info = &pStruct->One_ti;
5120 : : }
5121 : : else
5122 : : {
5123 : 0 : *t_group_info = NULL;
5124 : : }
5125 : : }
5126 : :
5127 : 0 : exit_function:
5128 : 0 : return ret;
5129 : : }
5130 : :
5131 : :
5132 : : /****************************************************************************/
5133 : 0 : int MakeOneInChIOutOfStrFromINChI( struct tagCANON_GLOBALS *pCG,
5134 : : INCHI_CLOCK *ic,
5135 : : ICHICONST INPUT_PARMS *ip,
5136 : : STRUCT_DATA *sd,
5137 : : StrFromINChI *pStruct,
5138 : : inp_ATOM *at2,
5139 : : inp_ATOM *at3,
5140 : : ALL_TC_GROUPS *pTCGroups )
5141 : : {
5142 : :
5143 : 0 : INCHI_MODE bTautFlags = ip->bTautFlags | TG_FLAG_H_ALREADY_REMOVED;
5144 : 0 : INCHI_MODE bTautFlagsDone = 0; /*(ip->bTautFlagsDone | sd->bTautFlagsDone[INCHI_BAS]);*/
5145 : : INChI *cur_INChI[TAUT_NUM];
5146 : : INChI_Aux *cur_INChI_Aux[TAUT_NUM];
5147 : : int i, j, k;
5148 : 0 : int iComponent = pTCGroups->iComponent;
5149 : 0 : int len_at = pStruct->num_atoms + pStruct->num_deleted_H;
5150 : 0 : int num_atoms = pStruct->num_atoms;
5151 : : long ulStructTime;
5152 : :
5153 : : INP_ATOM_DATA InpCurAtData;
5154 : : INP_ATOM_DATA *inp_cur_data;
5155 : :
5156 : : INP_ATOM_DATA InpNormAtData, InpNormTautData;
5157 : : INP_ATOM_DATA *inp_norm_data[TAUT_NUM]; /* = { &InpNormAtData, &InpNormTautData }; */
5158 : :
5159 : 0 : int bOrigCoord = 0;
5160 : 0 : int num_at, ret = RI_ERR_PROGR;
5161 : : struct tagInchiTime ulMaxTime;
5162 : :
5163 : 0 : T_GROUP_INFO *t_group_info = NULL;
5164 : : /* initialization */
5165 : 0 : inp_cur_data = &InpCurAtData;
5166 : 0 : inp_norm_data[TAUT_NON] = &InpNormAtData;
5167 : 0 : inp_norm_data[TAUT_YES] = &InpNormTautData;
5168 : :
5169 : 0 : memset( inp_cur_data, 0, sizeof( *inp_cur_data ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5170 : 0 : memset( inp_norm_data[TAUT_NON], 0, sizeof( *inp_norm_data[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5171 : 0 : memset( inp_norm_data[TAUT_YES], 0, sizeof( *inp_norm_data[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5172 : 0 : ulStructTime = sd->ulStructTime;
5173 : 0 : memset( sd, 0, sizeof( *sd ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5174 : :
5175 : : /* deallocate old results */
5176 : 0 : free_t_group_info( &pStruct->One_ti );
5177 [ # # ]: 0 : for (k = 0; k < TAUT_NUM; k++)
5178 : : {
5179 : 0 : Free_INChI( &pStruct->pOneINChI[k] );
5180 : 0 : Free_INChI_Aux( &pStruct->pOneINChI_Aux[k] );
5181 [ # # ]: 0 : if (pStruct->pOne_norm_data[k])
5182 : : {
5183 : 0 : FreeInpAtomData( pStruct->pOne_norm_data[k] );
5184 [ # # ]: 0 : inchi_free( pStruct->pOne_norm_data[k] );
5185 : 0 : pStruct->pOne_norm_data[k] = NULL;
5186 : : }
5187 : 0 : cur_INChI[k] = NULL;
5188 : 0 : cur_INChI_Aux[k] = NULL;
5189 : : }
5190 : :
5191 : 0 : memcpy(at3, at2, sizeof(at3[0]) * len_at);
5192 : :
5193 : : /* prepare the structure */
5194 : 0 : IncrZeroBondsAndClearEndpts( at3, num_atoms, iComponent + 1 );
5195 : :
5196 : 0 : CopySt2At( at3, pStruct->st, pStruct->num_atoms );
5197 : :
5198 : 0 : FixUnkn0DStereoBonds( at3, pStruct->num_atoms );
5199 : :
5200 : 0 : ret = ReconcileAllCmlBondParities( at3, pStruct->num_atoms, 0 );
5201 : :
5202 [ # # ]: 0 : if (ret < 0)
5203 : : {
5204 : 0 : goto exit_function;
5205 : : }
5206 : :
5207 [ # # ]: 0 : if (0 < fix_odd_things( num_atoms, at3, 1, ip->bFixNonUniformDraw ))
5208 : : {
5209 [ # # ]: 0 : if (sd->nErrorType < _IS_WARNING)
5210 : : {
5211 : 0 : sd->nErrorType = _IS_WARNING;
5212 : : }
5213 : 0 : sd->bTautFlagsDone[INCHI_BAS] |= TG_FLAG_FIX_ODD_THINGS_DONE;
5214 : : }
5215 : :
5216 : : /* allocate and set parameters */
5217 : 0 : inp_cur_data->at = at3;
5218 : 0 : inp_cur_data->num_at = num_atoms;
5219 : 0 : inp_cur_data->num_removed_H = pStruct->num_deleted_H;
5220 : :
5221 : 0 : bTautFlagsDone &= ~( TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE );
5222 : :
5223 [ # # ]: 0 : if ((i = bNumHeterAtomHasIsotopicH( at3, num_atoms ))) /* djb-rwth: addressing LLVM warning */
5224 : : {
5225 [ # # ]: 0 : if (i & 1)
5226 : : {
5227 : 0 : bTautFlagsDone |= TG_FLAG_FOUND_ISOTOPIC_H_DONE;
5228 : : }
5229 [ # # ]: 0 : if (i & 2)
5230 : : {
5231 : 0 : bTautFlagsDone |= TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE;
5232 : : }
5233 : : }
5234 : :
5235 : 0 : memset( &ulMaxTime, 0, sizeof( ulMaxTime ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5236 : :
5237 : : /* allocate memory for non-tautimeric (k=0) and tautomeric (k=1) results */
5238 [ # # ]: 0 : for (k = 0; k < TAUT_NUM; k++)
5239 : : {
5240 : :
5241 [ # # # # ]: 0 : if (!pStruct->bMobileH || k == pStruct->bMobileH)
5242 : 0 : {
5243 : : /* pStruct->bMobileH=0: k = 0, 1 => allow allocation of both Fixed-H and Mobile-H InChI
5244 : : pStruct->bMobileH=1: k = 1 only => allow allocation of only Mobile-H InChI */
5245 : :
5246 : : /* djb-rwth: introducing variables for correct nAllocMode expression */
5247 : 0 : int nAM1 = 0, nAM2 = 0;
5248 : 0 : int nAllocMode = 0; /* copied from below 2024-09-01 DT */
5249 : :
5250 [ # # ]: 0 : if (k == TAUT_YES)
5251 : 0 : nAM1 = REQ_MODE_TAUT;
5252 : :
5253 [ # # ]: 0 : if (bTautFlagsDone & (TG_FLAG_FOUND_ISOTOPIC_H_DONE | TG_FLAG_FOUND_ISOTOPIC_ATOM_DONE))
5254 : 0 : nAM2 = ip->nMode & REQ_MODE_ISO;
5255 : :
5256 : 0 : nAllocMode = nAM1 | nAM2; /* djb-rwth: original sequence of bit-wise operations had to be rewritten */
5257 : :
5258 [ # # # # : 0 : if ((k == TAUT_NON && ( ip->nMode & REQ_MODE_BASIC )) ||
# # ]
5259 [ # # ]: 0 : (k == TAUT_YES && ( ip->nMode & REQ_MODE_TAUT ))) /* djb-rwth: addressing LLVM warnings */
5260 : : {
5261 : : /* alloc INChI and INChI_Aux only if ip->nMode allows this */
5262 : 0 : cur_INChI[k] = Alloc_INChI( inp_cur_data->at, inp_cur_data->num_at, &inp_cur_data->num_bonds,
5263 : : &inp_cur_data->num_isotopic, nAllocMode );
5264 : 0 : cur_INChI_Aux[k] = Alloc_INChI_Aux( inp_cur_data->num_at,
5265 : : inp_cur_data->num_isotopic, nAllocMode, bOrigCoord );
5266 [ # # ]: 0 : if (cur_INChI_Aux[k])
5267 : : {
5268 : 0 : cur_INChI_Aux[k]->bIsIsotopic = inp_cur_data->num_isotopic;
5269 : : }
5270 : :
5271 : : /* alloc memory for the output structure: non-tautomeric and tautomeric (for displaying) */
5272 : 0 : CreateInpAtomData( inp_norm_data[k], inp_cur_data->num_at + inp_cur_data->num_removed_H, k );
5273 : :
5274 : 0 : inp_norm_data[k]->num_removed_H = inp_cur_data->num_removed_H;
5275 : : }
5276 : : else
5277 : : {
5278 : 0 : FreeInpAtomData( inp_norm_data[k] );
5279 : : }
5280 : : }
5281 : : else
5282 : : {
5283 : 0 : FreeInpAtomData( inp_norm_data[k] );
5284 : : }
5285 : : }
5286 : :
5287 : 0 : k = pStruct->bMobileH;
5288 : :
5289 : : /* In case of Fixed-H we have to create InChI for both Fixed-H and Mobile-H */
5290 : :
5291 : 0 : num_at = Create_INChI( pCG,
5292 : : ic,
5293 : : (INPUT_PARMS *) ip,
5294 : : cur_INChI,
5295 : : cur_INChI_Aux,
5296 : : NULL,
5297 : : inp_cur_data->at,
5298 : : inp_norm_data,
5299 : 0 : inp_cur_data->num_at + inp_cur_data->num_removed_H,
5300 : : NULL,
5301 : 0 : ip->nMode,
5302 : : &bTautFlags,
5303 : : &bTautFlagsDone,
5304 : : NULL /* &ulMaxTime*/,
5305 : 0 : &pStruct->One_ti,
5306 : 0 : sd->pStrErrStruct );
5307 : :
5308 : 0 : SetConnectedComponentNumber( inp_cur_data->at, inp_cur_data->num_at, iComponent + 1 ); /* normalization alters structure component number */
5309 : :
5310 : : /* Detect InChI errors */
5311 : :
5312 [ # # ]: 0 : if (num_at < 0)
5313 : : {
5314 : 0 : ret = num_at;
5315 : : }
5316 [ # # # # ]: 0 : else if (cur_INChI[k] && cur_INChI[k]->nErrorCode)
5317 : : {
5318 : 0 : ret = cur_INChI[k]->nErrorCode;
5319 : : }
5320 [ # # # # ]: 0 : else if (cur_INChI_Aux[k] && cur_INChI_Aux[k]->nErrorCode)
5321 : : {
5322 : 0 : ret = cur_INChI_Aux[k]->nErrorCode;
5323 : : }
5324 : : else
5325 : : {
5326 : 0 : ret = 0;
5327 : : }
5328 : :
5329 : : /* Fill out the output */
5330 : :
5331 [ # # ]: 0 : if (!ret) /* djb-rwth: fixing a NULL pointer dereference */
5332 : : {
5333 : 0 : int bMobileH = pStruct->bMobileH;
5334 [ # # ]: 0 : if (bMobileH == TAUT_NON &&
5335 [ # # ]: 0 : 0 == cur_INChI[TAUT_NON]->nNumberOfAtoms &&
5336 [ # # ]: 0 : 0 < cur_INChI[TAUT_YES]->nNumberOfAtoms)
5337 : : {
5338 : : /* tautomerism or H(+) removal/addition was not discovered */
5339 : 0 : bMobileH = TAUT_YES;
5340 : : }
5341 : :
5342 [ # # ]: 0 : if (cur_INChI[1])
5343 : 0 : pStruct->nChargeRevrs = cur_INChI[TAUT_YES]->nTotalCharge; /* djb-rwth: fixing a NULL pointer dereference */
5344 : :
5345 : 0 : pStruct->pOneINChI[0] = cur_INChI[bMobileH];
5346 : 0 : pStruct->pOneINChI_Aux[0] = cur_INChI_Aux[bMobileH];
5347 : 0 : pStruct->nOneINChI_bMobileH = bMobileH;
5348 : 0 : cur_INChI[bMobileH] = NULL; /* remove pointer to avoid deallocation at exit_function */
5349 : 0 : cur_INChI_Aux[bMobileH] = NULL; /* remove pointer to avoid deallocation at exit_function */
5350 : :
5351 [ # # ]: 0 : pStruct->nNumRemovedProtons = ( pStruct->iMobileH == TAUT_YES ) ? pStruct->One_ti.tni.nNumRemovedProtons : 0;
5352 : :
5353 : :
5354 : : /* set correct t-group numbers to endpoints */
5355 : :
5356 : 0 : t_group_info = &pStruct->One_ti;
5357 : :
5358 [ # # # # : 0 : if (t_group_info->num_t_groups && t_group_info->t_group && t_group_info->nEndpointAtomNumber)
# # ]
5359 : : {
5360 : 0 : inp_ATOM *at_norm = inp_norm_data[TAUT_YES]->at;
5361 : 0 : int num_at_norm = inp_norm_data[TAUT_YES]->num_at;
5362 [ # # ]: 0 : for (i = 0; i < num_at_norm; i++)
5363 : : {
5364 : 0 : at_norm[i].endpoint = 0;
5365 : : }
5366 [ # # ]: 0 : for (i = 0; i < t_group_info->num_t_groups; i++)
5367 : : {
5368 : 0 : k = t_group_info->t_group[i].nFirstEndpointAtNoPos;
5369 : : /* add number of mobile (-) to the number of mobile H */
5370 : 0 : t_group_info->t_group[i].num[0] += t_group_info->t_group[i].num[1];
5371 [ # # ]: 0 : for (j = 0; j < t_group_info->t_group[i].nNumEndpoints; j++, k++)
5372 : : {
5373 : 0 : at_norm[t_group_info->nEndpointAtomNumber[k]].endpoint = t_group_info->t_group[i].nGroupNumber;
5374 : : }
5375 : : }
5376 : : }
5377 : :
5378 : 0 : pStruct->pOne_norm_data[0] = (INP_ATOM_DATA *) inchi_malloc( sizeof( pStruct->pOne_norm_data[0][0] ) );
5379 : :
5380 [ # # ]: 0 : if (pStruct->pOne_norm_data[0])
5381 : : {
5382 : 0 : memcpy(pStruct->pOne_norm_data[0], inp_norm_data[bMobileH], sizeof(pStruct->pOne_norm_data[0][0]));
5383 : 0 : memset( inp_norm_data[bMobileH], 0, sizeof( *inp_norm_data[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5384 : : }
5385 : : else
5386 : : {
5387 : 0 : ret = RI_ERR_ALLOC;
5388 : : }
5389 : :
5390 [ # # # # ]: 0 : if (bMobileH == TAUT_NON && cur_INChI[TAUT_YES]->nNumberOfAtoms > 0)
5391 : : {
5392 [ # # ]: 0 : int bMobileHalt = ALT_TAUT( bMobileH ); /* = TAUT_YES */
5393 : 0 : pStruct->pOneINChI[1] = cur_INChI[bMobileHalt];
5394 : 0 : pStruct->pOneINChI_Aux[1] = cur_INChI_Aux[bMobileHalt];
5395 : 0 : cur_INChI[bMobileHalt] = NULL;
5396 : 0 : cur_INChI_Aux[bMobileHalt] = NULL;
5397 : 0 : pStruct->pOne_norm_data[1] = (INP_ATOM_DATA *) inchi_malloc( sizeof( pStruct->pOne_norm_data[0][0] ) );
5398 [ # # ]: 0 : if (pStruct->pOne_norm_data[1])
5399 : : {
5400 : 0 : memcpy(pStruct->pOne_norm_data[1], inp_norm_data[bMobileHalt], sizeof(pStruct->pOne_norm_data[0][0]));
5401 : 0 : memset( inp_norm_data[bMobileHalt], 0, sizeof( *inp_norm_data[0] ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5402 : : }
5403 : : else
5404 : : {
5405 : 0 : ret = RI_ERR_ALLOC;
5406 : : }
5407 : : }
5408 : : }
5409 : : else
5410 : : {
5411 : : #if ( bRELEASE_VERSION != 1 )
5412 : : #ifndef TARGET_API_LIB
5413 : : fprintf( stdout, "ERROR: Create_INChI returned %d\n", ret );
5414 : : #endif
5415 : : #endif
5416 : : }
5417 : :
5418 : 0 : exit_function:
5419 : : /* deallocate unused */
5420 [ # # ]: 0 : for (k = 0; k < TAUT_NUM; k++)
5421 : : {
5422 : 0 : Free_INChI( &cur_INChI[k] );
5423 : 0 : Free_INChI_Aux( &cur_INChI_Aux[k] );
5424 : 0 : FreeInpAtomData( inp_norm_data[k] );
5425 : : }
5426 : 0 : sd->ulStructTime = ulStructTime;
5427 : :
5428 : 0 : return ret;
5429 : : }
5430 : :
5431 : :
5432 : : /****************************************************************************
5433 : : Input:
5434 : : at[].num_H = total number of all terminal H connected to the atom
5435 : : at[].num_iso_H[] = numbers of isotopic H among at[].num_H
5436 : : Explicit H are disconnected
5437 : : Calculate InChI with normalization only in MakeOneInChIOutOfStrFromINChI()
5438 : : with (TG_FLAG_H_ALREADY_REMOVED & bTautFlags) != 0
5439 : : Output:
5440 : : at[].num_H = number of implicit non-isotopic H connected to the atom
5441 : : at[].num_iso_H[] = numbers of implicit isotopic H (not included in at[].num_H)
5442 : : Explicit H are connected
5443 : : Calculate InChI with full preprocessing MakeInChIOutOfStrFromINChI2()
5444 : : with (TG_FLAG_H_ALREADY_REMOVED & bTautFlags) == 0
5445 : : ****************************************************************************/
5446 : 0 : int ConnectDisconnectedH( inp_ATOM *at, int num_atoms, int num_deleted_H )
5447 : : {
5448 : : int i, j, k, n, m, num_H;
5449 : 0 : int tot_atoms = num_atoms + num_deleted_H;
5450 : :
5451 [ # # ]: 0 : for (i = num_atoms; i < tot_atoms; i = j)
5452 : : {
5453 : 0 : k = at[i].neighbor[0]; /* a[k] is the atom connected to the explicit hydrogen at[i] */
5454 : :
5455 [ # # # # ]: 0 : for (j = i; j < tot_atoms && at[j].neighbor[0] == k; j++)
5456 : : {
5457 : : ;
5458 : : }
5459 : :
5460 : 0 : num_H = j - i; /* number of explicit H for at[k] */
5461 [ # # ]: 0 : if (num_H > at[k].num_H)
5462 : : {
5463 : 0 : return RI_ERR_PROGR;
5464 : : }
5465 [ # # ]: 0 : if (num_H + at[k].valence > MAXVAL)
5466 : : {
5467 : 0 : return RI_ERR_SYNTAX;
5468 : : }
5469 : :
5470 : : /* insert links to explicit H before all other links in the connection list */
5471 : 0 : n = at[k].valence;
5472 : 0 : memmove(at[k].neighbor + num_H, at[k].neighbor, sizeof(at[k].neighbor[0]) * n);
5473 : 0 : memmove(at[k].bond_stereo + num_H, at[k].bond_stereo, sizeof(at[k].bond_stereo[0]) * n);
5474 : 0 : memmove(at[k].bond_type + num_H, at[k].bond_type, sizeof(at[k].bond_type[0]) * n);
5475 [ # # ]: 0 : for (n = 0; n < num_H; n++)
5476 : : {
5477 : 0 : at[k].neighbor[n] = i + n;
5478 : 0 : at[k].bond_stereo[n] = 0;
5479 : 0 : at[k].bond_type[n] = BOND_TYPE_SINGLE;
5480 : : }
5481 : :
5482 [ # # # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_BONDS && at[k].sb_parity[m]; m++)
5483 : : {
5484 : 0 : at[k].sb_ord[m] += num_H;
5485 [ # # ]: 0 : if (at[k].sn_ord[m] < 0)
5486 : : {
5487 [ # # ]: 0 : for (n = i; n < j; n++)
5488 : : {
5489 [ # # ]: 0 : if (at[n].orig_at_number == at[k].sn_orig_at_num[m])
5490 : : {
5491 : 0 : at[k].sn_ord[m] = n - i;
5492 : 0 : break;
5493 : : }
5494 : : }
5495 : :
5496 [ # # ]: 0 : if (n == j)
5497 : : {
5498 : 0 : return RI_ERR_PROGR;
5499 : : }
5500 : : }
5501 : : else
5502 : : {
5503 : 0 : at[k].sn_ord[m] += num_H;
5504 : : }
5505 : : }
5506 : :
5507 : 0 : at[k].valence += num_H;
5508 : 0 : at[k].chem_bonds_valence += num_H;
5509 : 0 : at[k].num_H -= num_H; /* cannot be negative */
5510 : :
5511 : : /*memset( at[k].num_iso_H, 0, sizeof(at[0].num_iso_H) );*/ /* attached H must carry all isotopic shifts */
5512 [ # # ]: 0 : for (n = i; n < j; n++)
5513 : : {
5514 : 0 : at[n].chem_bonds_valence = BOND_TYPE_SINGLE;
5515 : : }
5516 : :
5517 : : /* isotopic H */
5518 [ # # # # ]: 0 : for (m = j - 1; i <= m && at[m].iso_atw_diff > 0; m--)
5519 : : {
5520 [ # # ]: 0 : if (at[m].iso_atw_diff > NUM_H_ISOTOPES)
5521 : : {
5522 : 0 : return RI_ERR_PROGR;
5523 : : }
5524 [ # # ]: 0 : if (0 >= at[k].num_iso_H[(int) at[m].iso_atw_diff - 1] --)
5525 : : {
5526 : 0 : return RI_ERR_PROGR;
5527 : : }
5528 : : }
5529 : : }
5530 : :
5531 : : /* subtract isotopic H */
5532 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
5533 : : {
5534 [ # # ]: 0 : for (m = 0; m < NUM_H_ISOTOPES; m++)
5535 : : {
5536 : 0 : at[i].num_H -= at[i].num_iso_H[m];
5537 : : }
5538 [ # # ]: 0 : if (0 > at[i].num_H)
5539 : : {
5540 : 0 : return RI_ERR_PROGR;
5541 : : }
5542 : : }
5543 : :
5544 : 0 : return tot_atoms;
5545 : : }
5546 : :
5547 : :
5548 : : /******************************************************************************************************
5549 : : Input:
5550 : : at[].num_H = number of implicit non-isotopic H connected to the atom
5551 : : at[].num_iso_H[] = numbers of implicit isotopic H (not included in at[].num_H)
5552 : : Explicit H are connected
5553 : : Calculate InChI with (TG_FLAG_H_ALREADY_REMOVED & bTautFlags) == 0
5554 : : Output:
5555 : : at[].num_H = total number of all terminal H connected to the atom
5556 : : at[].num_iso_H[] = numbers of isotopic H among at[].num_H
5557 : : Explicit H are disconnected
5558 : : Calculate InChI with (TG_FLAG_H_ALREADY_REMOVED & bTautFlags) != 0
5559 : : *******************************************************************************************************/
5560 : 0 : int DisconnectedConnectedH( inp_ATOM *at, int num_atoms, int num_deleted_H )
5561 : : {
5562 : : int i, j, k, n, m, num_H, num_iso_H;
5563 : 0 : int tot_atoms = num_atoms + num_deleted_H;
5564 : 0 : S_CHAR ctmp = '0';
5565 : :
5566 : : /* add implicit isotopic H to total implicit H */
5567 [ # # ]: 0 : for (i = 0; i < num_atoms; i++)
5568 : : {
5569 [ # # ]: 0 : for (m = 0; m < NUM_H_ISOTOPES; m++)
5570 : : {
5571 : 0 : at[i].num_H += at[i].num_iso_H[m];
5572 : : }
5573 : : }
5574 : :
5575 [ # # ]: 0 : for (i = num_atoms; i < tot_atoms; i = j)
5576 : : {
5577 : 0 : k = at[i].neighbor[0]; /* a[k] is the atom connected to the explicit hydrogen at[i] */
5578 : :
5579 [ # # # # ]: 0 : for (j = i; j < tot_atoms && at[j].neighbor[0] == k; j++)
5580 : : {
5581 : 0 : at[j].chem_bonds_valence = 0;
5582 : : }
5583 : 0 : num_H = j - i; /* number of explicit H for at[k] */
5584 : :
5585 : : /* verify correct number of explicit H */
5586 [ # # # # ]: 0 : for (n = 0; n < at[k].valence && at[k].neighbor[n] >= num_atoms; n++)
5587 : : {
5588 : : ;
5589 : : }
5590 [ # # ]: 0 : if (n != num_H)
5591 : : {
5592 : 0 : return RI_ERR_PROGR;
5593 : : }
5594 : :
5595 : : /* remove bonds to explicit H located in front of all other bonds in the connection list */
5596 : 0 : at[k].valence -= num_H; /* djb-rwth: use of cast operators avoided */
5597 : 0 : n = at[k].valence; /* new number of bonds */
5598 : 0 : at[k].chem_bonds_valence -= num_H; /* new no-H valence */
5599 [ # # ]: 0 : if (n)
5600 : : {
5601 : 0 : memmove(at[k].neighbor, at[k].neighbor + num_H, sizeof(at[k].neighbor[0]) * n);
5602 : 0 : memmove(at[k].bond_stereo, at[k].bond_stereo + num_H, sizeof(at[k].bond_stereo[0]) * n);
5603 : 0 : memmove(at[k].bond_type, at[k].bond_type + num_H, sizeof(at[k].bond_type[0]) * n);
5604 : : }
5605 : : /* clear the 'tails' */
5606 : 0 : memset( at[k].neighbor + n, 0, sizeof( at[k].neighbor[0] ) * num_H ); /* djb-rwth: memset_s C11/Annex K variant? */
5607 : 0 : memset( at[k].bond_stereo + n, 0, sizeof( at[k].bond_stereo[0] ) * num_H ); /* djb-rwth: memset_s C11/Annex K variant? */
5608 : 0 : memset( at[k].bond_type + n, 0, sizeof( at[k].bond_type[0] ) * num_H ); /* djb-rwth: memset_s C11/Annex K variant? */
5609 : :
5610 [ # # # # ]: 0 : for (m = 0; m < MAX_NUM_STEREO_BONDS && at[k].sb_parity[m]; m++)
5611 : : {
5612 : 0 : at[k].sb_ord[m] -= num_H;
5613 [ # # # # ]: 0 : if (0 <= at[k].sn_ord[m] && at[k].sn_ord[m] < num_H)
5614 : : {
5615 : 0 : at[k].sn_ord[m] = -1; /* disconnected explicit H */
5616 : : }
5617 : : }
5618 : : /* add explicit isotopic H (already included in num_H) */
5619 [ # # # # ]: 0 : for (num_iso_H = 0, m = j - 1; i <= m && at[m].iso_atw_diff > 0; m--)
5620 : : {
5621 [ # # ]: 0 : if (at[m].iso_atw_diff > NUM_H_ISOTOPES)
5622 : : {
5623 : 0 : return RI_ERR_PROGR;
5624 : : }
5625 : 0 : at[k].num_iso_H[(int) at[m].iso_atw_diff - 1] ++;
5626 : : }
5627 : 0 : at[k].num_H += num_H; /* add all explicit H including isotopic */
5628 : : }
5629 : :
5630 : 0 : return tot_atoms;
5631 : : }
5632 : :
5633 : :
5634 : : /****************************************************************************/
5635 : 0 : int MakeInChIOutOfStrFromINChI2( INCHI_CLOCK *ic,
5636 : : CANON_GLOBALS *pCG,
5637 : : ICHICONST INPUT_PARMS *ip_inp,
5638 : : STRUCT_DATA *sd_inp,
5639 : : StrFromINChI *pStruct,
5640 : : int iComponent,
5641 : : int iAtNoOffset,
5642 : : long num_inp )
5643 : : {
5644 : : char szTitle[MAX_SDF_HEADER + MAX_SDF_VALUE + 256];
5645 : :
5646 : : int len, ret;
5647 : : /*
5648 : : PINChI2 *pINChI[INCHI_NUM];
5649 : : PINChI_Aux2 *pINChI_Aux[INCHI_NUM];
5650 : : */
5651 : : INPUT_PARMS local_ip;
5652 : : STRUCT_DATA local_sd;
5653 : 0 : INPUT_PARMS *ip = &local_ip;
5654 : 0 : STRUCT_DATA *sd = &local_sd;
5655 : :
5656 : : ORIG_ATOM_DATA OrigAtData; /* 0=> disconnected, 1=> original */
5657 : 0 : ORIG_ATOM_DATA *orig_inp_data = &OrigAtData;
5658 : : ORIG_ATOM_DATA PrepAtData[2]; /* 0=> disconnected, 1=> original */
5659 : 0 : ORIG_ATOM_DATA *prep_inp_data = PrepAtData;
5660 : :
5661 : : INCHI_IOS_STRING temp_string_container;
5662 : 0 : INCHI_IOS_STRING *strbuf = &temp_string_container;
5663 : 0 : memset( strbuf, 0, sizeof( *strbuf ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5664 : :
5665 [ # # ]: 0 : if (0 >= inchi_strbuf_init(strbuf, INCHI_STRBUF_INITIAL_SIZE, INCHI_STRBUF_SIZE_INCREMENT))
5666 : : {
5667 : 0 : ret = RI_ERR_ALLOC;
5668 : 0 : goto early_exit_error; /* djb-rwth: avoiding garbage value */
5669 : : }
5670 : :
5671 : : /* djb-rwth: fixing oss-fuzz issue #70552 */
5672 [ # # # # : 0 : if (!ip_inp || !sd_inp || !pStruct)
# # ]
5673 : : {
5674 : 0 : ret = RI_ERR_ALLOC;
5675 : 0 : goto early_exit_error; /* djb-rwth: avoiding garbage value */
5676 : : }
5677 : :
5678 : 0 : *ip = *ip_inp;
5679 : 0 : ip->bDisplay = 0;
5680 : 0 : ip->bDisplayCompositeResults = 0;
5681 : 0 : ip->bDisplayEachComponentINChI = 0;
5682 : 0 : ip->bDisplayIfRestoreWarnings = 0;
5683 : 0 : ip->bINChIOutputOptions = INCHI_OUT_NO_AUX_INFO;
5684 : :
5685 : : /*
5686 : : if ( pStruct->bMobileH ) {
5687 : : ip->nMode &= ~REQ_MODE_BASIC;
5688 : : ip->nMode |= REQ_MODE_TAUT;
5689 : : } else {
5690 : : ip->nMode |= (REQ_MODE_TAUT | REQ_MODE_BASIC);
5691 : : }
5692 : : */
5693 : :
5694 : 0 : memset( sd, 0, sizeof( *sd ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5695 : 0 : sd->fPtrStart = -1;
5696 : 0 : sd->fPtrEnd = -1;
5697 : :
5698 : : /*
5699 : : if ( ip->nMode & REQ_MODE_STEREO ) {
5700 : : if ( ip->nMode & (REQ_MODE_RELATIVE_STEREO | REQ_MODE_RACEMIC_STEREO) ) {
5701 : : sd->bChiralFlag |= FLAG_INP_AT_NONCHIRAL;
5702 : : } else {
5703 : : sd->bChiralFlag |= FLAG_INP_AT_CHIRAL;
5704 : : }
5705 : : }
5706 : : */
5707 : :
5708 : 0 : memset( orig_inp_data, 0, sizeof( *orig_inp_data ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5709 : 0 : memset( prep_inp_data, 0, 2 * sizeof( *prep_inp_data ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5710 : 0 : memset( pStruct->RevInChI.pINChI, 0, sizeof( pStruct->RevInChI.pINChI ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5711 : 0 : memset( pStruct->RevInChI.pINChI_Aux, 0, sizeof( pStruct->RevInChI.pINChI_Aux ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5712 : 0 : memset( szTitle, 0, sizeof( szTitle ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5713 : :
5714 : 0 : len = sizeof( orig_inp_data->at[0] )*( (long long)pStruct->num_atoms + (long long)pStruct->num_deleted_H ); /* djb-rwth: cast operators added */
5715 : :
5716 : 0 : orig_inp_data->at = (inp_ATOM *) inchi_malloc( len );
5717 : :
5718 [ # # ]: 0 : if (orig_inp_data->at)
5719 : : {
5720 : : /*memcpy( orig_inp_data->at, pStruct->at2, len );*/
5721 : : /*ret = ConnectDisconnectedH( orig_inp_data->at, pStruct->num_atoms, pStruct->num_deleted_H );*/
5722 : :
5723 : 0 : CopySt2At( pStruct->at2, pStruct->st, pStruct->num_atoms );
5724 : :
5725 : 0 : ret = ConnectDisconnectedH( pStruct->at2, pStruct->num_atoms, pStruct->num_deleted_H );
5726 [ # # ]: 0 : if (ret < 0)
5727 : : {
5728 : 0 : goto exit_error;
5729 : : }
5730 : :
5731 : 0 : orig_inp_data->num_inp_atoms = ret;
5732 : : /* connections changed => reconcile parities even if they were reconciled before */
5733 : : /* remove t-group markings and increment zero-order bonds,
5734 : : otherwise MakeInChIOutOfStrFromINChI2() woild fail */
5735 : : /*
5736 : : IncrZeroBondsAndClearEndpts(pStruct->at2, pStruct->num_atoms, iComponent+1);
5737 : : */
5738 : :
5739 : 0 : IncrZeroBonds( pStruct->at2, pStruct->num_atoms, iComponent + 1 );
5740 : :
5741 : : /* CopySt2At() moved to the position before ConnectDisconnectedH() because
5742 : : in case stereo exists only in Mobile-H layer and the processd here
5743 : : component is restored in Fixed-H layer the parities needed by
5744 : : ConnectDisconnectedH() must be there before calling
5745 : : ConnectDisconnectedH()
5746 : : */
5747 : : /*CopySt2At( pStruct->at2, pStruct->st, pStruct->num_atoms );*/
5748 : :
5749 : 0 : ret = ReconcileAllCmlBondParities( pStruct->at2, orig_inp_data->num_inp_atoms, 0 );
5750 [ # # ]: 0 : if (ret < 0)
5751 : : {
5752 : 0 : goto exit_error;
5753 : : }
5754 : :
5755 : : #if USE_BCF
5756 : : memcpy_s( orig_inp_data->at, (long long)len, pStruct->at2, len ); /* djb-rwth: function replaced with its safe C11 variant */
5757 : : #else
5758 : 0 : memcpy(orig_inp_data->at, pStruct->at2, len);
5759 : : #endif
5760 : :
5761 : 0 : ClearEndpts( orig_inp_data->at, pStruct->num_atoms );
5762 : :
5763 [ # # ]: 0 : if (FixUnkn0DStereoBonds( orig_inp_data->at, pStruct->num_atoms ))
5764 : : {
5765 : 0 : ret = ReconcileAllCmlBondParities( pStruct->at2, orig_inp_data->num_inp_atoms, 0 );
5766 [ # # ]: 0 : if (ret < 0)
5767 : : {
5768 : 0 : goto exit_error;
5769 : : }
5770 : : }
5771 : : /* keep endpoint[] markings in at2[] for subsequent add/remove protons */
5772 : : }
5773 : : else
5774 : : {
5775 : 0 : ret = RI_ERR_ALLOC;
5776 : 0 : goto exit_error;
5777 : : }
5778 : :
5779 : 0 : memset( sd->num_components, 0, sizeof( sd->num_components ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5780 : 0 : memset( sd->num_taut, 0, sizeof( sd->num_taut ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5781 : 0 : memset( sd->num_non_taut, 0, sizeof( sd->num_non_taut ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5782 : 0 : memset( sd->bTautFlagsDone, 0, sizeof( sd->bTautFlagsDone ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5783 : 0 : memset( sd->bTautFlags, 0, sizeof( sd->bTautFlags ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5784 : :
5785 : :
5786 : 0 : ret = ProcessOneStructure( ic, pCG, sd, ip, szTitle,
5787 : 0 : pStruct->RevInChI.pINChI,
5788 : 0 : pStruct->RevInChI.pINChI_Aux,
5789 : : NULL /*inp_file*/,
5790 : : NULL /*log_file*/,
5791 : : NULL /*out_file*/,
5792 : : NULL /*prb_file*/,
5793 : : orig_inp_data, prep_inp_data,
5794 : : num_inp, strbuf,
5795 : : 0 /* save_opt_bits */ );
5796 : :
5797 : 0 : memcpy(pStruct->RevInChI.num_components, sd->num_components, sizeof(pStruct->RevInChI.num_components));
5798 : 0 : memcpy(sd_inp->pStrErrStruct, sd->pStrErrStruct, sizeof(sd_inp->pStrErrStruct));
5799 : 0 : pStruct->RevInChI.nRetVal = ret;
5800 : :
5801 : : /* translate returned value */
5802 [ # # # # : 0 : if (ret == _IS_ERROR || ret == _IS_FATAL || ret == _IS_UNKNOWN)
# # ]
5803 : : {
5804 : 0 : ret = RI_ERR_PROGR;
5805 : : }
5806 [ # # ]: 0 : else if (ret == _IS_OKAY)
5807 : : {
5808 : 0 : ret = 0;
5809 : : }
5810 [ # # ]: 0 : else if (ret == _IS_WARNING)
5811 : : {
5812 : 0 : ret = 1;
5813 : : }
5814 : : else
5815 : : {
5816 : 0 : ret = RI_ERR_PROGR;
5817 : : }
5818 : :
5819 : : /* save total charge from Mobile-H layer */
5820 : :
5821 : 0 : pStruct->nChargeRevrs = 0;
5822 [ # # ]: 0 : if (ret >= 0)
5823 : : {
5824 [ # # ]: 0 : if (bRevInchiComponentExists( pStruct, INCHI_REC, TAUT_YES, 0 ))
5825 : : {
5826 : 0 : pStruct->nChargeRevrs = pStruct->RevInChI.pINChI[INCHI_REC][0][TAUT_YES]->nTotalCharge;
5827 : : }
5828 [ # # ]: 0 : else if (bRevInchiComponentExists( pStruct, INCHI_BAS, TAUT_YES, 0 ))
5829 : : {
5830 : 0 : pStruct->nChargeRevrs = pStruct->RevInChI.pINChI[INCHI_BAS][0][TAUT_YES]->nTotalCharge;
5831 : : }
5832 : : }
5833 : :
5834 : : /* free structure data */
5835 : 0 : FreeOrigAtData( orig_inp_data );
5836 : 0 : FreeOrigAtData( prep_inp_data );
5837 : 0 : FreeOrigAtData( prep_inp_data + 1 );
5838 : :
5839 : 0 : exit_error:
5840 : : #if ( FIX_GAF_2019_2==1 )
5841 [ # # ]: 0 : if (orig_inp_data->at)
5842 : : {
5843 : 0 : FreeInpAtom(&orig_inp_data->at);
5844 : : }
5845 : : #endif
5846 : 0 : early_exit_error:
5847 : 0 : inchi_strbuf_close( strbuf );
5848 : :
5849 : 0 : return ret;
5850 : : }
5851 : :
5852 : :
5853 : : /****************************************************************************/
5854 : 0 : int OutputInChIOutOfStrFromINChI( struct tagINCHI_CLOCK *ic,
5855 : : struct tagCANON_GLOBALS *pCG,
5856 : : ICHICONST INPUT_PARMS *ip_inp,
5857 : : STRUCT_DATA *sd_inp,
5858 : : long num_inp,
5859 : : int bINChIOutputOptions,
5860 : : INCHI_IOSTREAM *pout,
5861 : : INCHI_IOSTREAM *plog,
5862 : : InpInChI *pOneInput,
5863 : : int bHasSomeFixedH,
5864 : : unsigned char save_opt_bits )
5865 : : {
5866 : : char szTitle[MAX_SDF_HEADER + MAX_SDF_VALUE + 256];
5867 : :
5868 : : int len, ret;
5869 : : /*
5870 : : PINChI2 *pINChI[INCHI_NUM];
5871 : : PINChI_Aux2 *pINChI_Aux[INCHI_NUM];
5872 : : */
5873 : : REV_INCHI RevInChI;
5874 : :
5875 : : INPUT_PARMS local_ip;
5876 : : STRUCT_DATA local_sd;
5877 : 0 : INPUT_PARMS *ip = &local_ip;
5878 : 0 : STRUCT_DATA *sd = &local_sd;
5879 : :
5880 : : ORIG_ATOM_DATA OrigAtData; /* 0=> disconnected, 1=> original */
5881 : 0 : ORIG_ATOM_DATA *orig_inp_data = &OrigAtData;
5882 : : ORIG_ATOM_DATA PrepAtData[2]; /* 0=> disconnected, 1=> original */
5883 : 0 : ORIG_ATOM_DATA *prep_inp_data = PrepAtData;
5884 : :
5885 : : INCHI_IOS_STRING temp_string_container;
5886 : 0 : INCHI_IOS_STRING *strbuf = &temp_string_container;
5887 : 0 : memset( strbuf, 0, sizeof( *strbuf ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5888 : :
5889 [ # # ]: 0 : if (0 >= inchi_strbuf_init( strbuf, INCHI_STRBUF_INITIAL_SIZE, INCHI_STRBUF_SIZE_INCREMENT ))
5890 : : {
5891 : 0 : ret = RI_ERR_ALLOC;
5892 : 0 : goto exit_error;
5893 : : }
5894 : :
5895 : 0 : *ip = *ip_inp;
5896 : 0 : ip->bNoStructLabels = 1;
5897 : 0 : ip->bDisplay = 0;
5898 : 0 : ip->bDisplayCompositeResults = 0;
5899 : 0 : ip->bDisplayEachComponentINChI = 0;
5900 : 0 : ip->bDisplayIfRestoreWarnings = 0;
5901 : :
5902 : : #if ( I2S_MODIFY_OUTPUT == 1 )
5903 : : if (bINChIOutputOptions & INCHI_OUT_SDFILE_ONLY)
5904 : : {
5905 : : ip->bINChIOutputOptions = bINChIOutputOptions & ~( INCHI_OUT_PLAIN_TEXT | INCHI_OUT_PLAIN_TEXT_COMMENTS );
5906 : : }
5907 : : else
5908 : : {
5909 : : if (bINChIOutputOptions & INCHI_OUT_PLAIN_TEXT)
5910 : : {
5911 : : ip->bINChIOutputOptions = (bINChIOutputOptions & ~INCHI_OUT_SDFILE_ONLY) | INCHI_OUT_EMBED_REC; /* djb-rwth: addressing LLVM warning */
5912 : : }
5913 : : else
5914 : : {
5915 : : if (bINChIOutputOptions & ( INCHI_OUT_NO_AUX_INFO | INCHI_OUT_SHORT_AUX_INFO | INCHI_OUT_ONLY_AUX_INFO | INCHI_OUT_TABBED_OUTPUT ))
5916 : : {
5917 : : ip->bINChIOutputOptions = ( INCHI_OUT_PLAIN_TEXT | INCHI_OUT_EMBED_REC | bINChIOutputOptions );
5918 : : }
5919 : : else
5920 : : {
5921 : : ip->bINChIOutputOptions = ( INCHI_OUT_PLAIN_TEXT | INCHI_OUT_EMBED_REC );
5922 : : }
5923 : : }
5924 : : }
5925 : : #else
5926 : 0 : ip->bINChIOutputOptions = ( INCHI_OUT_PLAIN_TEXT | INCHI_OUT_EMBED_REC );
5927 : : #endif
5928 : :
5929 [ # # ]: 0 : if (bHasSomeFixedH)
5930 : : {
5931 : 0 : ip->nMode |= ( REQ_MODE_TAUT | REQ_MODE_BASIC );
5932 : : }
5933 : : else
5934 : : {
5935 : 0 : ip->nMode &= ~REQ_MODE_BASIC;
5936 : 0 : ip->nMode |= REQ_MODE_TAUT;
5937 : : }
5938 : :
5939 : 0 : memset( sd, 0, sizeof( *sd ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5940 : 0 : sd->fPtrStart = -1;
5941 : 0 : sd->fPtrEnd = -1;
5942 : : /*
5943 : : if ( ip->nMode & REQ_MODE_STEREO ) {
5944 : : if ( ip->nMode & (REQ_MODE_RELATIVE_STEREO | REQ_MODE_RACEMIC_STEREO) ) {
5945 : : sd->bChiralFlag |= FLAG_INP_AT_NONCHIRAL;
5946 : : } else {
5947 : : sd->bChiralFlag |= FLAG_INP_AT_CHIRAL;
5948 : : }
5949 : : }
5950 : : */
5951 : 0 : memset( orig_inp_data, 0, sizeof( *orig_inp_data ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5952 : 0 : memset( prep_inp_data, 0, 2 * sizeof( *prep_inp_data ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5953 : 0 : memset( RevInChI.pINChI, 0, sizeof( RevInChI.pINChI ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5954 : 0 : memset( RevInChI.pINChI_Aux, 0, sizeof( RevInChI.pINChI_Aux ) ); /* djb-rwth: memset_s C11/Annex K variant? */
5955 : :
5956 : 0 : len = sizeof( orig_inp_data->at[0] ) * pOneInput->num_atoms;
5957 : 0 : orig_inp_data->at = (inp_ATOM *) inchi_malloc( len );
5958 : 0 : orig_inp_data->szCoord = (MOL_COORD *) inchi_calloc( pOneInput->num_atoms, sizeof( orig_inp_data->szCoord[0] ) );
5959 : :
5960 : 0 : orig_inp_data->polymer = pOneInput->polymer;
5961 : 0 : orig_inp_data->v3000 = pOneInput->v3000;
5962 : 0 : pOneInput->polymer = NULL;
5963 : 0 : pOneInput->v3000 = NULL;
5964 : 0 : orig_inp_data->valid_polymer = pOneInput->valid_polymer;
5965 : :
5966 [ # # # # ]: 0 : if (orig_inp_data->at && orig_inp_data->szCoord)
5967 : 0 : {
5968 : : int i, k;
5969 : 0 : memcpy(orig_inp_data->at, pOneInput->atom, len);
5970 : 0 : orig_inp_data->num_inp_atoms = pOneInput->num_atoms;
5971 : 0 : ClearEndpts( orig_inp_data->at, orig_inp_data->num_inp_atoms );
5972 : : /* otherwise fails on CID=450438 */
5973 [ # # ]: 0 : if (FixUnkn0DStereoBonds( orig_inp_data->at, orig_inp_data->num_inp_atoms ))
5974 : : {
5975 : 0 : ret = ReconcileAllCmlBondParities( orig_inp_data->at, orig_inp_data->num_inp_atoms, 0 );
5976 [ # # ]: 0 : if (ret < 0)
5977 : : {
5978 : : /* djb-rwth: fixing a NULL pointer dereference */
5979 : 0 : free(orig_inp_data->at);
5980 : 0 : free(orig_inp_data->szCoord);
5981 : 0 : goto exit_error;
5982 : : }
5983 : : }
5984 : :
5985 : : /* To obtain rA,rB,rC in AuxInfo we have to emulate input coordinates; make all of them zeroes */
5986 [ # # ]: 0 : for (i = 0; i < pOneInput->num_atoms; i++)
5987 : : {
5988 [ # # ]: 0 : for (k = 0; k < NUM_COORD*LEN_COORD; k += LEN_COORD)
5989 : : {
5990 : 0 : orig_inp_data->szCoord[i][k] = '0';
5991 : : }
5992 : : }
5993 : : }
5994 : : else
5995 : : {
5996 : : /* djb-rwth: fixing a NULL pointer dereference */
5997 : 0 : ret = RI_ERR_ALLOC;
5998 : 0 : free(orig_inp_data->at);
5999 : 0 : free(orig_inp_data->szCoord);
6000 : 0 : goto exit_error;
6001 : : }
6002 : :
6003 : 0 : memset( sd->num_components, 0, sizeof( sd->num_components ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6004 : 0 : memset( sd->num_taut, 0, sizeof( sd->num_taut ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6005 : 0 : memset( sd->num_non_taut, 0, sizeof( sd->num_non_taut ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6006 : 0 : memset( sd->bTautFlagsDone, 0, sizeof( sd->bTautFlagsDone ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6007 : 0 : memset( sd->bTautFlags, 0, sizeof( sd->bTautFlags ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6008 : 0 : memset( szTitle, 0, sizeof( szTitle ) ); /* djb-rwth: memset_s C11/Annex K variant? */
6009 : :
6010 : :
6011 : :
6012 : 0 : ret = ProcessOneStructure( ic, pCG, sd, ip, szTitle,
6013 : : RevInChI.pINChI,
6014 : : RevInChI.pINChI_Aux,
6015 : : NULL /*inp_file*/,
6016 : : plog /*log_file*/,
6017 : : pout /*out_file*/,
6018 : : NULL /*prb_file*/,
6019 : : orig_inp_data, prep_inp_data,
6020 : : num_inp, strbuf, save_opt_bits );
6021 : :
6022 : 0 : memcpy(RevInChI.num_components, sd->num_components, sizeof(RevInChI.num_components));
6023 : : /*
6024 : : memcpy(sd_inp->pStrErrStruct, sd->pStrErrStruct, sizeof(sd_inp->pStrErrStruct) );
6025 : : */
6026 : :
6027 : 0 : RevInChI.nRetVal = ret;
6028 : :
6029 : : /* translate returned value */
6030 [ # # # # : 0 : if (ret == _IS_ERROR || ret == _IS_FATAL || ret == _IS_UNKNOWN)
# # ]
6031 : : {
6032 : 0 : ret = RI_ERR_PROGR;
6033 : : }
6034 [ # # ]: 0 : else if (ret == _IS_OKAY)
6035 : : {
6036 : 0 : ret = 0;
6037 : : }
6038 [ # # ]: 0 : else if (ret == _IS_WARNING)
6039 : : {
6040 : 0 : ret = 1;
6041 : : }
6042 : : else
6043 : : {
6044 : 0 : ret = RI_ERR_PROGR;
6045 : : }
6046 : :
6047 : : /* free structure data */
6048 : 0 : FreeOrigAtData( orig_inp_data );
6049 : 0 : FreeOrigAtData( prep_inp_data );
6050 : 0 : FreeOrigAtData( prep_inp_data + 1 );
6051 : 0 : FreeAllINChIArrays( RevInChI.pINChI,
6052 : : RevInChI.pINChI_Aux,
6053 : : RevInChI.num_components );
6054 : :
6055 : :
6056 : 0 : exit_error:
6057 : 0 : inchi_strbuf_close( strbuf );
6058 : :
6059 : 0 : return ret;
6060 : : }
6061 : : #endif
|