| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2021 Frederik Heber. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | *
|
|---|
| 7 | * This file is part of MoleCuilder.
|
|---|
| 8 | *
|
|---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
|---|
| 10 | * it under the terms of the GNU General Public License as published by
|
|---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 12 | * (at your option) any later version.
|
|---|
| 13 | *
|
|---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 17 | * GNU General Public License for more details.
|
|---|
| 18 | *
|
|---|
| 19 | * You should have received a copy of the GNU General Public License
|
|---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * GeneratePotentialsAction.cpp
|
|---|
| 25 | *
|
|---|
| 26 | * Created on: May 13, 2021
|
|---|
| 27 | * Author: heber
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | // include config.h
|
|---|
| 31 | #ifdef HAVE_CONFIG_H
|
|---|
| 32 | #include <config.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | // include headers that implement a archive in simple text format
|
|---|
| 36 | // and before MemDebug due to placement new
|
|---|
| 37 | #include <boost/archive/text_oarchive.hpp>
|
|---|
| 38 | #include <boost/archive/text_iarchive.hpp>
|
|---|
| 39 |
|
|---|
| 40 | //#include "CodePatterns/MemDebug.hpp"
|
|---|
| 41 |
|
|---|
| 42 | #include <set>
|
|---|
| 43 | #include <string>
|
|---|
| 44 |
|
|---|
| 45 | #include <boost/foreach.hpp>
|
|---|
| 46 | #include <boost/iterator/counting_iterator.hpp>
|
|---|
| 47 |
|
|---|
| 48 | #include "Actions/PotentialAction/GeneratePotentialsAction.hpp"
|
|---|
| 49 |
|
|---|
| 50 | #include "CodePatterns/Log.hpp"
|
|---|
| 51 |
|
|---|
| 52 | #include "Potentials/EmpiricalPotential.hpp"
|
|---|
| 53 | #include "Potentials/Exceptions.hpp"
|
|---|
| 54 | #include "Potentials/PotentialFactory.hpp"
|
|---|
| 55 | #include "Potentials/PotentialRegistry.hpp"
|
|---|
| 56 | #include "Potentials/PotentialTrainer.hpp"
|
|---|
| 57 |
|
|---|
| 58 | using namespace MoleCuilder;
|
|---|
| 59 |
|
|---|
| 60 | // and construct the stuff
|
|---|
| 61 | #include "GeneratePotentialsAction.def"
|
|---|
| 62 | #include "Action_impl_pre.hpp"
|
|---|
| 63 | /** =========== define the function ====================== */
|
|---|
| 64 | ActionState::ptr PotentialGeneratePotentialsAction::performCall()
|
|---|
| 65 | {
|
|---|
| 66 | // fragment specifies the homology fragment to use
|
|---|
| 67 | SerializablePotential::ParticleTypes_t fragmentnumbers =
|
|---|
| 68 | PotentialTrainer::getNumbersFromElements(params.fragment.get());
|
|---|
| 69 |
|
|---|
| 70 | // parse homologies into container
|
|---|
| 71 | const HomologyContainer &homologies = World::getInstance().getHomologies();
|
|---|
| 72 |
|
|---|
| 73 | // then we ought to pick the right HomologyGraph ...
|
|---|
| 74 | const HomologyGraph graph =
|
|---|
| 75 | PotentialTrainer::getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers);
|
|---|
| 76 | if (graph != HomologyGraph()) {
|
|---|
| 77 | LOG(1, "First representative graph containing fragment "
|
|---|
| 78 | << fragmentnumbers << " is " << graph << ".");
|
|---|
| 79 | } else {
|
|---|
| 80 | STATUS("Specific fragment "+toString(fragmentnumbers)+" not found in homologies!");
|
|---|
| 81 | return Action::failure;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | // gather list of potential candidates
|
|---|
| 85 | std::vector<std::string> potentials;
|
|---|
| 86 | if (!params.potential_list.isSet()) {
|
|---|
| 87 | for (unsigned int i=0; i<PotentialTypesMax; ++i)
|
|---|
| 88 | potentials.push_back(PotentialFactory::getNameForType((enum PotentialTypes)i));
|
|---|
| 89 | } else
|
|---|
| 90 | potentials = params.potential_list.get();
|
|---|
| 91 |
|
|---|
| 92 | // go through all potential potentials :)4
|
|---|
| 93 | const PotentialFactory& factory = PotentialFactory::getConstInstance();
|
|---|
| 94 | PotentialRegistry& registry = PotentialRegistry::getInstance();
|
|---|
| 95 | SerializablePotential::ParticleTypes_t charges;
|
|---|
| 96 | typedef std::set<BindingModel> unique_models_t;
|
|---|
| 97 | unique_models_t unique_models;
|
|---|
| 98 | BOOST_FOREACH(std::string &potential_name, potentials) {
|
|---|
| 99 | unique_models.clear();
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Approach:
|
|---|
| 103 | * 1. get the number of particle types for the potential
|
|---|
| 104 | * 2. create all combinations for the given elements and the number of particles
|
|---|
| 105 | * 3. create the potential
|
|---|
| 106 | * 4. gather all created potential's binding model in a set
|
|---|
| 107 | * 5. if the binding model is already contained, discard the potential
|
|---|
| 108 | * 6. if the binding model is not contained in the fragment's graph, discard it
|
|---|
| 109 | * 7. if still valid, register potential
|
|---|
| 110 | */
|
|---|
| 111 |
|
|---|
| 112 | // first need to construct potential, then may access it
|
|---|
| 113 | const enum PotentialTypes potential_type = factory.getTypeForName(potential_name);
|
|---|
| 114 | EmpiricalPotential const * const defaultPotential = factory.getDefaultPotential(potential_type);
|
|---|
| 115 | /// 1. get its number of particles
|
|---|
| 116 | const unsigned int num_particles = defaultPotential->getParticleTypeNumber();
|
|---|
| 117 | LOG(1, "INFO: Number of particles of " << potential_name << " is " << num_particles);
|
|---|
| 118 |
|
|---|
| 119 | if (num_particles > fragmentnumbers.size()) {
|
|---|
| 120 | LOG(2, "DEBUG: Skipping potential " << potential_name << " as " << num_particles
|
|---|
| 121 | << " required but fragment has only " << fragmentnumbers.size() << " particles.");
|
|---|
| 122 | continue;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * 2. create all unique combinations for the given elements and the number of particles
|
|---|
| 127 | *
|
|---|
| 128 | * Use the {1,...,fragmentnumbers.size()}, create every permutation and pick the first num_particle
|
|---|
| 129 | * from the given charges. Finally, put all those into a set to retain only unique combinations.
|
|---|
| 130 | */
|
|---|
| 131 | std::set<SerializablePotential::ParticleTypes_t> charges_for_potentials;
|
|---|
| 132 | std::vector<size_t> selection(boost::counting_iterator<int>(0), boost::counting_iterator<int>(fragmentnumbers.size()));
|
|---|
| 133 | do {
|
|---|
| 134 | charges.clear();
|
|---|
| 135 | for (unsigned int i = 0; i < num_particles; ++i) {
|
|---|
| 136 | charges.push_back(fragmentnumbers[selection[i]]);
|
|---|
| 137 | }
|
|---|
| 138 | // LOG(3, "DEBUG: Inserting charges " << charges);
|
|---|
| 139 | charges_for_potentials.insert(charges);
|
|---|
| 140 | } while (std::next_permutation(selection.begin(), selection.end()));
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | for (std::set<SerializablePotential::ParticleTypes_t>::const_iterator iter = charges_for_potentials.begin();
|
|---|
| 144 | iter != charges_for_potentials.end(); ++iter) {
|
|---|
| 145 | /// 3. create the potential
|
|---|
| 146 | EmpiricalPotential* potential = factory.createInstance(potential_name, *iter);
|
|---|
| 147 |
|
|---|
| 148 | /// 4. Gather all created potential's binding model in a set
|
|---|
| 149 | std::pair<unique_models_t::iterator, bool> inserter = unique_models.insert(potential->getBindingModel());
|
|---|
| 150 |
|
|---|
| 151 | /// 5. if the binding model is already contained, discard the potential
|
|---|
| 152 | if (inserter.second) {
|
|---|
| 153 | /// 6. if the binding model is not contained in the fragment's graph, discard it
|
|---|
| 154 | if (graph.contains(potential->getBindingModel().getGraph())) {
|
|---|
| 155 | /// 7. If still valid, register potential
|
|---|
| 156 | LOG(2, "DEBUG: Registering potential " << *potential);
|
|---|
| 157 | registry.registerInstance(potential);
|
|---|
| 158 | continue;
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | LOG(2, "DEBUG: Discarding potential " << *potential);
|
|---|
| 162 | delete(potential);
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | return Action::success;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | ActionState::ptr PotentialGeneratePotentialsAction::performUndo(ActionState::ptr _state) {
|
|---|
| 170 | STATUS("Undo of PotentialGeneratePotentialsAction not implemented.");
|
|---|
| 171 | return Action::failure;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | ActionState::ptr PotentialGeneratePotentialsAction::performRedo(ActionState::ptr _state){
|
|---|
| 175 | STATUS("Redo of PotentialGeneratePotentialsAction not implemented.");
|
|---|
| 176 | return Action::failure;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | bool PotentialGeneratePotentialsAction::canUndo() {
|
|---|
| 180 | return false;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | bool PotentialGeneratePotentialsAction::shouldUndo() {
|
|---|
| 184 | return false;
|
|---|
| 185 | }
|
|---|
| 186 | /** =========== end of function ====================== */
|
|---|