source: src/Actions/PotentialAction/GeneratePotentialsAction.cpp@ 55c494

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

Added GeneratePotentialsAction.

  • this generates all potential combinations for a given fragment.
  • exposed PotentialFactory::getDefaultInstance() for constant instance to be able to gather more information about the potential such as the binding model.
  • DOCU: Added documentation.
  • TEST: Added regression test case.
  • Property mode set to 100644
File size: 6.3 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
45#include "Actions/PotentialAction/GeneratePotentialsAction.hpp"
46
47#include "CodePatterns/Log.hpp"
48
49#include "Potentials/EmpiricalPotential.hpp"
50#include "Potentials/Exceptions.hpp"
51#include "Potentials/PotentialFactory.hpp"
52#include "Potentials/PotentialRegistry.hpp"
53#include "Potentials/PotentialTrainer.hpp"
54
55using namespace MoleCuilder;
56
57// and construct the stuff
58#include "GeneratePotentialsAction.def"
59#include "Action_impl_pre.hpp"
60/** =========== define the function ====================== */
61
62ActionState::ptr PotentialGeneratePotentialsAction::performCall()
63{
64 // fragment specifies the homology fragment to use
65 SerializablePotential::ParticleTypes_t fragmentnumbers =
66 PotentialTrainer::getNumbersFromElements(params.fragment.get());
67
68 // parse homologies into container
69 const HomologyContainer &homologies = World::getInstance().getHomologies();
70
71 // then we ought to pick the right HomologyGraph ...
72 const HomologyGraph graph =
73 PotentialTrainer::getFirstGraphwithSpecifiedElements(homologies,fragmentnumbers);
74 if (graph != HomologyGraph()) {
75 LOG(1, "First representative graph containing fragment "
76 << fragmentnumbers << " is " << graph << ".");
77 } else {
78 STATUS("Specific fragment "+toString(fragmentnumbers)+" not found in homologies!");
79 return Action::failure;
80 }
81
82 // go through all potential potentials :)4
83 const PotentialFactory& factory = PotentialFactory::getConstInstance();
84 PotentialRegistry& registry = PotentialRegistry::getInstance();
85 SerializablePotential::ParticleTypes_t charges;
86 typedef std::set<BindingModel> unique_models_t;
87 unique_models_t unique_models;
88 for (unsigned int i=0; i<PotentialTypesMax; ++i) {
89 const std::string potential_name = PotentialFactory::getNameForType((enum PotentialTypes)i);
90 unique_models.clear();
91
92 /**
93 * Approach:
94 * 1. get the number of particle types for the potential
95 * 2. create all combinations for the given elements and the number of particles
96 * 3. create the potential
97 * 4. gather all created potential's binding model in a set
98 * 5. if the binding model is already contained, discard the potential
99 * 6. if the binding model is not contained in the fragment's graph, discard it
100 * 7. if still valid, register potential
101 */
102
103 // first need to construct potential, then may access it
104 EmpiricalPotential const * const defaultPotential = factory.getDefaultPotential((enum PotentialTypes)i);
105 /// 1. get its number of particles
106 const unsigned int num_particles = defaultPotential->getParticleTypeNumber();
107 LOG(1, "INFO: Number of particles of " << potential_name << " is " << num_particles);
108
109 if (num_particles > fragmentnumbers.size()) {
110 LOG(2, "DEBUG: Skipping potential " << potential_name << " as " << num_particles
111 << " required but fragment has only " << fragmentnumbers.size() << " particles.");
112 continue;
113 }
114
115 /**
116 * We have num_particles places and fragmentnumbers.size() elements to place.
117 * We use a "selection" array and pick every possible selection from the
118 * fragmentnumbers array, kudos to https://stackoverflow.com/a/9430993
119 */
120
121 /// 2. create all combinations for the given elements and the number of particles
122 std::vector<bool> selection(fragmentnumbers.size());
123 std::fill(selection.begin(), selection.begin() + num_particles, true);
124 do {
125 charges.clear();
126 for (unsigned int i = 0; i < fragmentnumbers.size(); ++i) {
127 if (selection[i]) {
128 charges.push_back(fragmentnumbers[i]);
129 }
130 }
131 /// 3. create the potential
132 EmpiricalPotential* potential = factory.createInstance(potential_name, charges);
133
134 /// 4. Gather all created potential's binding model in a set
135 std::pair<unique_models_t::iterator, bool> inserter = unique_models.insert(potential->getBindingModel());
136
137 /// 5. if the binding model is already contained, discard the potential
138 if (inserter.second) {
139 /// 6. if the binding model is not contained in the fragment's graph, discard it
140 if (graph.contains(potential->getBindingModel().getGraph())) {
141 /// 7. If still valid, register potential
142 LOG(2, "DEBUG: Registering potential " << *potential);
143 registry.registerInstance(potential);
144 continue;
145 }
146 }
147 LOG(2, "DEBUG: Discarding potential " << *potential);
148 delete(potential);
149 } while (std::prev_permutation(selection.begin(), selection.end()));
150 }
151
152 return Action::success;
153}
154
155ActionState::ptr PotentialGeneratePotentialsAction::performUndo(ActionState::ptr _state) {
156 STATUS("Undo of PotentialGeneratePotentialsAction not implemented.");
157 return Action::failure;
158}
159
160ActionState::ptr PotentialGeneratePotentialsAction::performRedo(ActionState::ptr _state){
161 STATUS("Redo of PotentialGeneratePotentialsAction not implemented.");
162 return Action::failure;
163}
164
165bool PotentialGeneratePotentialsAction::canUndo() {
166 return false;
167}
168
169bool PotentialGeneratePotentialsAction::shouldUndo() {
170 return false;
171}
172/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.