| 1 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | // Author: Kristian Lytje |
| 3 | |
| 4 | #include <rigidbody/constraints/generation/VolumetricConstraints.h> |
| 5 | #include <rigidbody/constraints/DistanceConstraintAtom.h> |
| 6 | #include <rigidbody/constraints/ConstraintManager.h> |
| 7 | #include <settings/RigidBodySettings.h> |
| 8 | #include <settings/GeneralSettings.h> |
| 9 | #include <constants/Constants.h> |
| 10 | #include <utility/Console.h> |
| 11 | #include <data/Molecule.h> |
| 12 | #include <data/Body.h> |
| 13 | |
| 14 | #include <limits> |
| 15 | |
| 16 | using namespace ausaxs; |
| 17 | using namespace ausaxs::rigidbody::constraints; |
| 18 | using namespace ausaxs::data; |
| 19 | |
| 20 | std::vector<std::unique_ptr<IDistanceConstraint>> VolumetricConstraints::generate() const { |
| 21 | if (settings::general::verbose) {console::print_info("\tGenerating simple constraints for rigid body optimization.");} |
| 22 | std::vector<std::unique_ptr<IDistanceConstraint>> constraints; |
| 23 | |
| 24 | auto& protein = *manager->molecule; |
| 25 | for (unsigned int ibody1 = 0; ibody1 < protein.get_bodies().size(); ibody1++) { |
| 26 | for (unsigned int ibody2 = ibody1+1; ibody2 < protein.get_bodies().size(); ibody2++) { |
| 27 | const Body& body1 = protein.get_body(ibody1); |
| 28 | const Body& body2 = protein.get_body(ibody2); |
| 29 | |
| 30 | double min_dist = std::numeric_limits<double>::max(); |
| 31 | int min_atom1 = -1, min_atom2 = -1; |
| 32 | for (unsigned int iatom1 = 0; iatom1 < body1.get_atoms().size(); iatom1++) { |
| 33 | const AtomFF& atom1 = body1.get_atom(iatom1); |
| 34 | if (form_factor::to_atom_type(atom1.form_factor_type()) != constants::atom_t::C) { |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | for (unsigned int iatom2 = 0; iatom2 < body2.get_atoms().size(); iatom2++) { |
| 39 | const AtomFF& atom2 = body2.get_atom(iatom2); |
| 40 | if (form_factor::to_atom_type(atom2.form_factor_type()) != constants::atom_t::C) { |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | double dist = atom1.coordinates().distance(atom2.coordinates()); |
| 45 | if (dist > min_dist) {continue;} |
| 46 | |
| 47 | min_dist = dist; |
| 48 | min_atom1 = iatom1; |
| 49 | min_atom2 = iatom2; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // no carbon atoms found |
| 54 | if (min_atom1 == -1 || min_atom2 == -1) {continue;} |
| 55 | |
| 56 | // check if the bodies are close enough for a constraint to make sense |
| 57 | if (min_dist > settings::rigidbody::bond_distance) {continue;} |
| 58 | |
| 59 | constraints.emplace_back(std::make_unique<DistanceConstraintAtom>(manager->molecule, ibody1, min_atom1, ibody2, min_atom2)); |
| 60 | if (settings::general::verbose) { |
| 61 | std::cout |
| 62 | << "\tConstraint created between bodies " << ibody1 << " and " << ibody2 << " on atoms " |
| 63 | << form_factor::to_string(body1.get_atom(min_atom1).form_factor_type()) << " and " << form_factor::to_string(body2.get_atom(min_atom2).form_factor_type()) |
| 64 | << std::endl; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (constraints.empty()) { |
| 70 | throw except::unexpected("rigidbody::constraints::generation::VolumeConstraints::generate: No constraints were generated. This is probably a bug."); |
| 71 | } |
| 72 | |
| 73 | return constraints; |
| 74 | } |