| 1 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | // Author: Kristian Lytje |
| 3 | |
| 4 | #include <rigidbody/constraints/generation/LinearConstraints.h> |
| 5 | #include <rigidbody/constraints/DistanceConstraintAtom.h> |
| 6 | #include <rigidbody/constraints/ConstraintManager.h> |
| 7 | #include <settings/GeneralSettings.h> |
| 8 | #include <constants/Constants.h> |
| 9 | #include <utility/Console.h> |
| 10 | #include <data/Molecule.h> |
| 11 | #include <data/Body.h> |
| 12 | |
| 13 | #include <limits> |
| 14 | |
| 15 | using namespace ausaxs; |
| 16 | using namespace ausaxs::rigidbody::constraints; |
| 17 | using namespace ausaxs::data; |
| 18 | |
| 19 | std::vector<std::unique_ptr<IDistanceConstraint>> LinearConstraints::generate() const { |
| 20 | if (settings::general::verbose) {console::print_info("\tGenerating simple constraints for rigid body optimization.");} |
| 21 | std::vector<std::unique_ptr<IDistanceConstraint>> constraints; |
| 22 | |
| 23 | auto& protein = *manager->molecule; |
| 24 | for (unsigned int ibody1 = 0; ibody1 < protein.size_body()-1; ibody1++) { |
| 25 | unsigned int ibody2 = ibody1 + 1; |
| 26 | |
| 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 | unsigned int min_atom1 = -1, min_atom2 = -1; |
| 32 | for (unsigned int iatom1 = 0; iatom1 < body1.size_atom(); 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.size_atom(); 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 | if (min_atom1 == -1u || min_atom2 == -1u) { |
| 54 | throw except::unexpected("LinearConstraints::generate: No suitable atoms were found for constraint generation. "); |
| 55 | } |
| 56 | constraints.emplace_back(std::make_unique<DistanceConstraintAtom>(manager->molecule, ibody1, min_atom1, ibody2, min_atom2)); |
| 57 | if (settings::general::verbose) { |
| 58 | std::cout |
| 59 | << "\tConstraint created between bodies " << ibody1 << " and " << ibody2 << " on atoms " |
| 60 | << 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()) |
| 61 | << std::endl; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (constraints.empty()) { |
| 66 | throw except::unexpected("LinearConstraints::generate: No constraints were generated. This is probably a bug."); |
| 67 | } |
| 68 | |
| 69 | return constraints; |
| 70 | } |