Making a Bond

Our strategy instead will be to have the bond hold the IDs of the atoms.

With that in mind, we can define our constructor:

SetChemicalSystemUpValues@Bond;

CanBond[a1_Atom,a2_Atom,type_:1]:=(a1=!=a2&&a1["System"]===a2["System"]&&(
 MatchQ[type,Except[_?NumericQ]]||
 a1["Valence"]>=type&&a2["Valence"]>=type)
);
Bond[a1_Atom,a2_Atom,type_:1]:=If[CanBond[a1,a2,type],
 If[MatchQ[type,_?NumericQ],
  Do[
   ChemicalSystemApply[
    #["Valence"]-=type&,
    a
    ],
   {a,{a1,a2} }
   ];
  With[{b=ChemicalSystemAdd[
    With[{s=a1["System"]},ChemicalSystem@s],
    Bond@<|
     "Atom1"->Atom[a1["ID"],a1["System"]],
     "Atom2"->Atom[a2["ID"],a1["System"]],
     "Type"->type
     |>
     ]},
   AppendTo[a1["Bonds"],b];
   AppendTo[a2["Bonds"],b];
   b
   ],
  $Failed
  ]
 ];

results matching ""

    No results matching ""