@@ -997,17 +997,36 @@ void calculate_valences(MOL_FMT_DATA *mfdata,
997997 bDoNotAddH ,
998998 bHasMetalNeighbor );
999999
1000- /* (@nnuk)
1001- * Add hydrogens for coordination bonds on non-metals
1002- *
1003- * (@Felix Bänsch)(@Gerd Blanke)(@nnuk)
1004- * An alternative approach to consider is to increase the valence of the mfdata (mfdata->ctab.atoms[a1].valence).
1005- * If the value is 0 (indicating a default value), it would be necessary to obtain the default value and increase it by 1.
1006- * This calculation of the implicit hydrogens should consequently yield the desired result.
1007- */
1008- if (!is_el_a_metal (at [a1 ].el_number ))
1000+ /* Special handling for non-metals with coordination bonds */
1001+ if (!is_el_a_metal (at [a1 ].el_number ) && additional_H > 0 )
10091002 {
1010- at [a1 ].num_H += additional_H ;
1003+ /* Count explicit hydrogens bonded to this atom */
1004+ int explicit_H = 0 ;
1005+ for (n1 = 0 ; n1 < at [a1 ].valence ; n1 ++ )
1006+ {
1007+ if (at [a1 ].bond_type [n1 ] == BOND_TYPE_SINGLE && at [at [a1 ].neighbor [n1 ]].el_number == EL_NUMBER_H )
1008+ {
1009+ explicit_H ++ ;
1010+ }
1011+ }
1012+
1013+ /* Get expected valence for this element */
1014+ int expected_valence = get_el_valence (at [a1 ].el_number , at [a1 ].charge , 0 ) + 1 ; /* Default valence state */
1015+
1016+ /* Calculate current bonding state */
1017+ int current_bonding = at [a1 ].chem_bonds_valence ;
1018+
1019+ /* Check if we need additional hydrogens */
1020+ if ((expected_valence - current_bonding ) > 0 )
1021+ {
1022+ at [a1 ].num_H += additional_H ;
1023+ }
1024+ else if ((expected_valence - current_bonding ) < 0 )
1025+ {
1026+ /* Valence overflow - log error */
1027+ * err |= 64 ;
1028+ TREAT_ERR (* err , 0 , "Valence overflow" );
1029+ }
10111030 }
10121031 }
10131032 }
0 commit comments