source: src/Actions/PotentialAction/GeneratePotentialsAction.cpp@ 20fc6f

Candidate_v1.7.0 stable
Last change on this file since 20fc6f was 9b0dcd, checked in by Frederik Heber <frederik.heber@…>, 4 years ago

Added potential-list argument to GeneratePotentialsAction.

  • DOCU: Extended documentation on new option.
  • TEST: Added a test case on this.
  • Property mode set to 100644
File size: 6.6 KB
RevLine 
[55c494]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
[9b0dcd]45#include <boost/foreach.hpp>
46
[55c494]47#include "Actions/PotentialAction/GeneratePotentialsAction.hpp"
48
49#include "CodePatterns/Log.hpp"
50
51#include "Potentials/EmpiricalPotential.hpp"
52#include "Potentials/Exceptions.hpp"
53#include "Potentials/PotentialFactory.hpp"
54#include "Potentials/PotentialRegistry.hpp"
55#include "Potentials/PotentialTrainer.hpp"
56
57using namespace MoleCuilder;
58
59// and construct the stuff
60#include "GeneratePotentialsAction.def"
61#include "Action_impl_pre.hpp"
62/** =========== define the function ====================== */
63
64ActionState::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
[9b0dcd]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
[55c494]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;
[9b0dcd]98 BOOST_FOREACH(std::string &potential_name, potentials) {
[55c494]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
[9b0dcd]113 const enum PotentialTypes potential_type = factory.getTypeForName(potential_name);
114 EmpiricalPotential const * const defaultPotential = factory.getDefaultPotential(potential_type);
[55c494]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 * We have num_particles places and fragmentnumbers.size() elements to place.
127 * We use a "selection" array and pick every possible selection from the
128 * fragmentnumbers array, kudos to https://stackoverflow.com/a/9430993
129 */
130
131 /// 2. create all combinations for the given elements and the number of particles
132 std::vector<bool> selection(fragmentnumbers.size());
133 std::fill(selection.begin(), selection.begin() + num_particles, true);
134 do {
135 charges.clear();
136 for (unsigned int i = 0; i < fragmentnumbers.size(); ++i) {
137 if (selection[i]) {
138 charges.push_back(fragmentnumbers[i]);
139 }
140 }
141 /// 3. create the potential
142 EmpiricalPotential* potential = factory.createInstance(potential_name, charges);
143
144 /// 4. Gather all created potential's binding model in a set
145 std::pair<unique_models_t::iterator, bool> inserter = unique_models.insert(potential->getBindingModel());
146
147 /// 5. if the binding model is already contained, discard the potential
148 if (inserter.second) {
149 /// 6. if the binding model is not contained in the fragment's graph, discard it
150 if (graph.contains(potential->getBindingModel().getGraph())) {
151 /// 7. If still valid, register potential
152 LOG(2, "DEBUG: Registering potential " << *potential);
153 registry.registerInstance(potential);
154 continue;
155 }
156 }
157 LOG(2, "DEBUG: Discarding potential " << *potential);
158 delete(potential);
159 } while (std::prev_permutation(selection.begin(), selection.end()));
160 }
161
162 return Action::success;
163}
164
165ActionState::ptr PotentialGeneratePotentialsAction::performUndo(ActionState::ptr _state) {
166 STATUS("Undo of PotentialGeneratePotentialsAction not implemented.");
167 return Action::failure;
168}
169
170ActionState::ptr PotentialGeneratePotentialsAction::performRedo(ActionState::ptr _state){
171 STATUS("Redo of PotentialGeneratePotentialsAction not implemented.");
172 return Action::failure;
173}
174
175bool PotentialGeneratePotentialsAction::canUndo() {
176 return false;
177}
178
179bool PotentialGeneratePotentialsAction::shouldUndo() {
180 return false;
181}
182/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.