source: src/Fragmentation/SetValues/Fragment.cpp@ 302345

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 302345 was f3bc5f, checked in by Frederik Heber <heber@…>, 12 years ago

Added extensive unit tests and some more functions to Fragment.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. 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 * Fragment.cpp
25 *
26 * Created on: Aug 8, 2012
27 * Author: heber
28 */
29
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "CodePatterns/MemDebug.hpp"
37
38#include "Fragment.hpp"
39
40#include <algorithm>
41#include <cmath>
42#include <iostream>
43#include <limits>
44
45#include "CodePatterns/Assert.hpp"
46
47Fragment::Fragment()
48{}
49
50Fragment::Fragment(const positions_t &_positions, const charges_t &_charges)
51{
52 ASSERT( _positions.size() == _charges.size(),
53 "Fragment::Fragment() - given charges and positions don't match in size.");
54 positions_t::const_iterator piter = _positions.begin();
55 charges_t::const_iterator citer = _charges.begin();
56 for (;piter != _positions.end(); ++piter, ++citer)
57 nuclei.push_back( make_pair( *piter, *citer) );
58}
59
60
61Fragment& Fragment::operator+=(const Fragment &other)
62{
63 for (nuclei_t::const_iterator iter = other.nuclei.begin();
64 iter != other.nuclei.end(); ++iter) {
65 if (!containsNuclei(*iter)) {
66 nuclei.push_back(*iter);
67 }
68 }
69 return *this;
70}
71
72Fragment& Fragment::operator=(const Fragment &other)
73{
74 if (this != &other) {
75 nuclei.clear();
76 nuclei = other.nuclei;
77 }
78 return *this;
79}
80
81Fragment& Fragment::operator-=(const Fragment &other)
82{
83 for (nuclei_t::const_iterator iter = other.nuclei.begin();
84 iter != other.nuclei.end(); ++iter)
85 removeNuclei(*iter);
86 return *this;
87}
88
89bool Fragment::containsNuclei(const nucleus_t &n) const
90{
91 for (nuclei_t::const_iterator iter = nuclei.begin();
92 iter != nuclei.end(); ++iter)
93 if (Fragment::isPositionEqual(iter->first, n.first))
94 return true;
95 return false;
96}
97
98void Fragment::removeNuclei(const nucleus_t &n)
99{
100 for (nuclei_t::iterator iter = nuclei.begin();
101 iter != nuclei.end(); ++iter)
102 if (Fragment::isPositionEqual(iter->first, n.first)) {
103 nuclei.erase(iter);
104 break;
105 }
106}
107
108Fragment::positions_t Fragment::getPositions() const
109{
110 positions_t positions;
111 for (nuclei_t::const_iterator iter = nuclei.begin();
112 iter != nuclei.end(); ++iter)
113 positions.push_back(iter->first);
114 return positions;
115}
116
117Fragment::charges_t Fragment::getCharges() const
118{
119 charges_t charges;
120 for (nuclei_t::const_iterator iter = nuclei.begin();
121 iter != nuclei.end(); ++iter)
122 charges.push_back(iter->second);
123 return charges;
124}
125
126Fragment::nucleus_t Fragment::createNucleus(const position_t &position, const double charge)
127{
128 return nucleus_t( make_pair( position, charge ) );
129}
130
131bool Fragment::isPositionEqual(const position_t &a, const position_t &b)
132{
133 bool status = true;
134 for (size_t i=0;i<3;++i)
135 status &= fabs(a[i] - b[i]) < std::numeric_limits<double>::epsilon();
136 return status;
137}
138
139bool Fragment::operator==(const Fragment& other) const
140{
141 bool status = true;
142 // compare sizes
143 if (nuclei.size() != other.nuclei.size()) {
144 return false;
145 } else {
146 // if equal, sort, and compare each nuclei
147 Fragment::nuclei_t thisnuclei(nuclei);
148 Fragment::nuclei_t othernuclei(other.nuclei);
149 std::sort(thisnuclei.begin(), thisnuclei.end());
150 std::sort(othernuclei.begin(), othernuclei.end());
151 Fragment::nuclei_t::const_iterator iter = thisnuclei.begin();
152 Fragment::nuclei_t::const_iterator oiter = othernuclei.begin();
153 for (; iter != thisnuclei.end(); ++iter,++oiter)
154 status &= (*iter == *oiter);
155 }
156 return status;
157}
158
159std::ostream & operator<<(std::ostream &ost, const Fragment &f)
160{
161 size_t NucleiCounter = 1;
162 for (Fragment::nuclei_t::const_iterator iter = f.nuclei.begin();
163 iter != f.nuclei.end(); ++iter)
164 ost << "[" << NucleiCounter++ << "]:" << *iter << ", ";
165 return ost;
166}
167
168std::ostream & operator<<(std::ostream &ost, const Fragment::nucleus_t &n)
169{
170 ost << n.first << " with charge " << n.second;
171 return ost;
172}
173
174bool operator==(const Fragment::nucleus_t &a, const Fragment::nucleus_t &b)
175{
176 bool status = true;
177 // check positions
178 status &= Fragment::isPositionEqual(a.first, b.first);
179 status &= fabs(a.second - b.second) < std::numeric_limits<double>::epsilon();
180 return status;
181}
182
183template<> Fragment ZeroInstance<Fragment>()
184{
185 Fragment::positions_t positions;
186 Fragment::charges_t charges;
187 Fragment returnvalue(positions, charges);
188 return returnvalue;
189}
190
Note: See TracBrowser for help on using the repository browser.