def test8(): """ Build a composite of more than 2 atoms. Repeat 100: Get a composite c that is stable Add it to a list composites Get an atom a for each composite in composites: for each atom in composite: try to bond a to atom (which uses the composite's attractor) """ composites = [] for i in range(100): a, sitea, b, siteb = getbondingpair() assert reaction.sitescanbond(sitea, siteb) reaction.do_edge_swaps(sitea, siteb) c = particle.Composite([a] + [b]) sitea.bondedto, siteb.bondedto = siteb, sitea stable = reaction.sitescanbond(sitea, siteb) if not stable: print(f'{i} Failed stability') else: composites.append(c) a = particle.Atom.new(12, 2) for composite in composites: for atom in composite.atoms: sitea, siteb = reaction.getbondablesites(a, atom) if sitea and siteb: reaction.do_edge_swaps(sitea, siteb) sitea.bondedto, siteb.bondedto = siteb, sitea c = particle.Composite([a] + composite.atoms) print(c.atoms) break else: print("No bonding possible between composite and atom.")
def break_bond(site1, site2): assert site1.bondedto is site2 and site2.bondedto is site1, "Trying to break bond between unbonded Atoms." do_edge_swaps(site1, site1) site1.bondedto, site2.bondedto = None, None # Need to set new parent composites here otherwise wrong attractor will be calculated. # Create composite from each atom by traversing. # Then return two composites. return particle.Composite(particle.Composite.traverse( site1.parent_atom)), particle.Composite( particle.Composite.traverse(site2.parent_atom))
def test9(): # ! Problems here. The number of atoms in self.atoms disagrees with the atoms returned # ! by the Atom traversal. The traversal is correct, so bonds are happening where # ! they shouldn't be. # * The problem was that I kept going through the 2-composites over and over and the # * the second and subsequent times through some of them were already bonded. # Build some larger molecules to test the traversal defined in Composite. composites = [] for i in range(10000): a, sitea, b, siteb = getbondingpair() assert reaction.sitescanbond(sitea, siteb) reaction.do_edge_swaps(sitea, siteb) c = particle.Composite([a] + [b]) sitea.bondedto, siteb.bondedto = siteb, sitea stable = reaction.sitescanbond(sitea, siteb) if stable: composites.append(c) print(len(composites)) composites3 = [] a = particle.Atom.new(12, 2) for composite in composites: if len(composite.atoms) != len(particle.Composite.traverse(composite.atoms[0])): print("error") for atom in composite.atoms: sitea, siteb = reaction.getbondablesites(a, atom) if sitea and siteb: reaction.do_edge_swaps(sitea, siteb) sitea.bondedto, siteb.bondedto = siteb, sitea c = particle.Composite([a] + composite.atoms) composites3.append(c) a = particle.Atom.new(12, 2) break # for composite in composites3: # print([atom.id for atom in particle.Composite.traverse(composite.atoms[0])]) # print([atom.id for atom in particle.Composite.traverse(composite.atoms[1])]) # print([atom.id for atom in particle.Composite.traverse(composite.atoms[2])]) # print() print(len(composites3)) composites4 = [] a = particle.Atom.new(12, 2) for composite in composites3: if len(composite.atoms) != len(particle.Composite.traverse(composite.atoms[0])): print("error") for atom in composite.atoms: sitea, siteb = reaction.getbondablesites(a, atom) if sitea and siteb: reaction.do_edge_swaps(sitea, siteb) sitea.bondedto, siteb.bondedto = siteb, sitea c = particle.Composite([a] + composite.atoms) composites4.append(c) a = particle.Atom.new(12, 2) break print(len(composites4))
def test6(): # This checks that interaction sites are the same after bonding and unbonding. for i in range(1000): a, sitea, b, siteb = getbondingpair() # Get spikes before typea1 = sitea.spike_type() vala1 = sitea.spike_value() typeb1 = siteb.spike_type() valb1 = siteb.spike_value() # print(f'{typea1}, {vala1}, {typeb1}, {valb1}') # Now bond reaction.do_edge_swaps(sitea, siteb) c = particle.Composite([a] + [b]) # Now unbond reaction.do_edge_swaps(sitea, siteb) sitea.parent_atom.parent_composite = None siteb.parent_atom.parent_composite = None # Get spikes after typea2 = sitea.spike_type() vala2 = sitea.spike_value() typeb2 = siteb.spike_type() valb2 = siteb.spike_value() # print(f'{typea2}, {vala2}, {typeb2}, {valb2}') # Check if typea1 != typea2 or vala1 != vala2 or typeb1 != typeb2 or valb1 != valb2: print("Error") break
def test7(): # Get two Atoms that meeting the bonding criteria but fail the stability criteria. for i in range(100): a, sitea, b, siteb = getbondingpair() assert reaction.sitescanbond(sitea, siteb) reaction.do_edge_swaps(sitea, siteb) c = particle.Composite([a] + [b]) stable = reaction.sitescanbond(sitea, siteb) if not stable: print(f'{i} Failed stability') break
def test10(): # Create 100 2-atom composites and check that their len(self.atoms) agrees with # the number of atoms returned by the traversal. composites = [] for i in range(1000): a, sitea, b, siteb = getbondingpair() assert reaction.sitescanbond(sitea, siteb) reaction.do_edge_swaps(sitea, siteb) c = particle.Composite([a] + [b]) sitea.bondedto, siteb.bondedto = siteb, sitea stable = reaction.sitescanbond(sitea, siteb) print([atom.id for atom in c.atoms]) print([atom.id for atom in particle.Composite.traverse(c.atoms[0])]) print([atom.id for atom in particle.Composite.traverse(c.atoms[1])]) if len(c.atoms) != len(particle.Composite.traverse(c.atoms[0])) or len(c.atoms) != len(particle.Composite.traverse(c.atoms[1])): print("error") print('done')
def bond(site1, site2): assert site1.bondedto is None and site2.bondedto is None, "Trying to bonded sites that are already bonded." do_edge_swaps(site1, site1) site1.bondedto, site2.bondedto = site2, site1 return particle.Composite(particle.Composite.traverse(site1.parent_atom))
def test5(): # Bond two Atoms and then unbond them to check the algorithm works. # When I first did this test it occasionally gave a different attractor for an atom # after it had been unbonded from the attractor before it was bonded. I found that # RBN.get_cycle() was zeroing the values of the nodes properly. Test 6 tries to check # that this bug no longer exists. a, sitea, b, siteb = getbondingpair() # print('Before') print(f'Site A: {sitea}') # for strnode in [f'{str(node)}' for node in sitea.nodes]: # print(strnode) # _, attractor = rbn.RBN.get_cycle(sitea.parent_atom.rbn.nodes) # print(attractor) # print() print(f'Site B: {siteb}') # for strnode in [f'{str(node)}' for node in siteb.nodes]: # print(strnode) # _, attractor = rbn.RBN.get_cycle(siteb.parent_atom.rbn.nodes) # print(attractor) # print(f'Sites can bond: {reaction.sitescanbond(sitea, siteb)}') typea1 = sitea.spike_type() vala1 = sitea.spike_value() typeb1 = siteb.spike_type() valb1 = siteb.spike_value() print(f'{typea1}, {vala1}, {typeb1}, {valb1}') # print() reaction.do_edge_swaps(sitea, siteb) c = particle.Composite([a] + [b]) # print('After') print(f'Site A: {sitea}') # for strnode in [f'{str(node)}' for node in sitea.nodes]: # print(strnode) # print() print(f'Site B: {siteb}') # for strnode in [f'{str(node)}' for node in siteb.nodes]: # print(strnode) # print(f'Sites can bond: {reaction.sitescanbond(sitea, siteb)}') # print() reaction.do_edge_swaps(sitea, siteb) sitea.parent_atom.parent_composite = None siteb.parent_atom.parent_composite = None # print('And back again...') print(f'Site A: {sitea}') # ! This was the cause of the error but the zeroing of the nodes states fixes it. # for strnode in [f'{str(node)}' for node in sitea.nodes]: # print(strnode) # _, attractor = rbn.RBN.get_cycle(sitea.parent_atom.rbn.nodes) # print(attractor) # print() print(f'Site B: {siteb}') # for strnode in [f'{str(node)}' for node in siteb.nodes]: # print(strnode) # _, attractor = rbn.RBN.get_cycle(siteb.parent_atom.rbn.nodes) # print(attractor) # print(f'Sites can bond: {reaction.sitescanbond(sitea, siteb)}') typea2 = sitea.spike_type() vala2 = sitea.spike_value() typeb2 = siteb.spike_type() valb2 = siteb.spike_value() print(f'{typea2}, {vala2}, {typeb2}, {valb2}') # Check if typea1 != typea2 or vala1 != vala2 or typeb1 != typeb2 or valb1 != valb2: print("Error")