info.clear();
var model       = project.current_model();
var build       = model.build    ();

var result        = true;
var it            = 0;
var attempts      = 1000;
//var lowest_energy = 1000000;
var lowest_energy = [];
lowest_energy[0] = 1000000;
lowest_energy[1] = 1000000;
lowest_energy[2] = 1000000;
lowest_energy[3] = 1000000;
lowest_energy[4] = 1000000;
lowest_energy[5] = 1000000;
lowest_energy[6] = 1000000;
lowest_energy[7] = 1000000;
lowest_energy[8] = 1000000;
lowest_energy[9] = 1000000;

for (it=0;  it<attempts;  ++it)
{
	attempt (model);

	var interaction = model.interaction ();
	var P = interaction.potential();
	var E = interaction.intermolecular_energy();

	for (i=0;  i<10;  ++i)
	{
		if (lowest_energy[i] > E)
		{
			lowest_energy[i] = E;
			info.append ('attempt ' + it + ' E=' + E + ' P=' + P);
			model.save_as ('attempt ' + it + '.mlm');
			break;
		}
	}

	pause.millisecond (50);
}

function attempt (model)
{
	do
	{
		var bond = choose_random_bond (model);
		result = isomerize (bond, model);
	}
	while(!result)

	model.invert_selection();
	window.menubar.menu_Select.Freeze.trigger();
	window.menubar.menu_Build.Remove_clashes.trigger();
	window.menubar.menu_Build.Remove_clashes.trigger();
	window.menubar.menu_Select.Freeze.trigger();

	//window.menubar.menu_Build.Remove_clashes.trigger();
	//window.menubar.menu_Build.Remove_clashes.trigger();
	//window.menubar.menu_Build.Remove_clashes.trigger();
	//window.menubar.menu_Build.Remove_clashes.trigger();
	optimize (model);

	model.invert_selection();
}

function optimize (model) 
{
	var MD    = model.MD();
	var min   = model.optimize();

	window.menubar.menu_Build.Remove_clashes.trigger();

	window.menubar.menu_Compute.Dynamics.trigger();

	MD.set_thermostat ('Andersen');

	MD.set_duration(2);                // ps
	MD.set_desired_temperature (0.1);  // K
	MD.run();

	MD.set_thermostat ('Nose-Hoover');

	MD.set_duration(5);
	MD.set_desired_temperature (1);    // K
	MD.run();

	MD.set_duration(5);
	MD.set_desired_temperature (0.1);  // K
	MD.run();

	MD.set_thermostat ('Andersen');

	MD.set_duration(2);                
	MD.set_desired_temperature (0.01); // K
	MD.run();

	window.menubar.menu_Compute.Optimize.trigger();
	min.run();
}

function choose_random_bond (model)
{
	var count1 = 0, count2 = 0;
	var new_bond;

	while (count1 < 2 || count2 < 2)
	{
		var rnd = random.exc (model.bond_count());
		new_bond = model.bond (rnd);

		if (!(new_bond.atom (0).is_selected() && new_bond.atom (1).is_selected()))
			continue;

		count1 = new_bond.atom (0).bound_count();
		count2 = new_bond.atom (1).bound_count();
	}
	//new_bond.set_style ('stick');
	return new_bond;
}

function isomerize (bond, model)
{
	var erase = model.erase ();
	var count1 = 0, count2 = 0, count3 = 0, count4 = 0;
	var atom1, atom2, atom3, atom4, bond1, bond4;
	var it = 0;

	while (!(count1 > 1 || count4 > 1) && ++it < 10)
	{
		atom2 = bond.atom(0);		count2 = atom2.bound_count();
  		atom3 = bond.atom(1);		count3 = atom3.bound_count();
		if (count2 < 2 || count3 < 2) {info.append ('error 1'); continue;}

		do
		{
			var rnd1 = random.inc (count2-1);
			bond1 = atom2.bond (rnd1);
//info.append (rnd1);
		}
		while (bond1.is(bond));

		do
		{
			var rnd4 = random.inc (count3-1);
			bond4 = atom3.bond (rnd4);
//info.append (rnd4);
		}
		while (bond4.is(bond))

		atom1 = bond1.atom (0);		if (atom1.is(atom2))	atom1 = bond1.atom (1);
		atom4 = bond4.atom (0);		if (atom4.is(atom3))	atom4 = bond4.atom (1);

		if (atom1.bound_with(atom3) || atom4.bound_with(atom2) || atom1.is(atom4))	return false;
		//if (atom1.is(atom4))	{info.append ('error 99'); continue;}

		count1 = atom1.bound_count();
		count4 = atom4.bound_count();
		if (atom1.is(atom3) || atom4.is(atom2)) {info.append ('error 3'); continue;}
		if (bond1.is(bond)  || bond4.is(bond))  {info.append ('error 2'); continue;}
	}
	if (it >=99) {info.append ('error in it ' + count1 + ' ' + count4); return;}

	erase.add (bond1);
	erase.add (bond4);
	erase.execute ();

	build.new_bond (atom1, atom3, 'Single').set_style ('ball_and_stick');
	build.new_bond (atom2, atom4, 'Single').set_style ('ball_and_stick');
	//build.new_bond (atom1, atom3, 'Single').set_style ('wireframe_CPK');
	//build.new_bond (atom2, atom4, 'Single').set_style ('wireframe_CPK');
	//window.menubar.menu_Build.Detect_molecules.trigger();
	return true;
}