source: src/Box.cpp@ bcf653

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 bcf653 was bcf653, checked in by Frederik Heber <heber@…>, 14 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
[83c09a]8/*
9 * Box.cpp
10 *
11 * Created on: Jun 30, 2010
12 * Author: crueger
13 */
14
[bf3817]15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
[bbbad5]20#include "Helpers/MemDebug.hpp"
[6e00dd]21
[83c09a]22#include "Box.hpp"
23
[f429d7]24#include <cmath>
[16648f]25#include <iostream>
[d2938f]26#include <cstdlib>
[f429d7]27
[57f243]28#include "LinearAlgebra/Matrix.hpp"
29#include "LinearAlgebra/Vector.hpp"
30#include "LinearAlgebra/Plane.hpp"
[83c09a]31
[6e00dd]32#include "Helpers/Assert.hpp"
33
[16648f]34using namespace std;
35
[83c09a]36Box::Box()
37{
38 M= new Matrix();
39 M->one();
40 Minv = new Matrix();
41 Minv->one();
[77374e]42 conditions.resize(3);
43 conditions[0] = conditions[1] = conditions[2] = Wrap;
[83c09a]44}
45
[7579a4b]46Box::Box(const Box& src){
47 M=new Matrix(*src.M);
48 Minv = new Matrix(*src.Minv);
[77374e]49 conditions = src.conditions;
[7579a4b]50}
51
[83c09a]52Box::~Box()
53{
54 delete M;
55 delete Minv;
56}
57
[7579a4b]58const Matrix &Box::getM() const{
[83c09a]59 return *M;
60}
[7579a4b]61const Matrix &Box::getMinv() const{
[83c09a]62 return *Minv;
63}
64
65void Box::setM(Matrix _M){
[3bf9e2]66 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
[83c09a]67 *M =_M;
68 *Minv = M->invert();
69}
[7579a4b]70
[014475]71Vector Box::translateIn(const Vector &point) const{
[3dcb1f]72 return (*M) * point;
73}
74
[014475]75Vector Box::translateOut(const Vector &point) const{
[3dcb1f]76 return (*Minv) * point;
77}
78
[014475]79Vector Box::WrapPeriodically(const Vector &point) const{
[f429d7]80 Vector helper = translateOut(point);
81 for(int i=NDIM;i--;){
[c72562]82
83 switch(conditions[i]){
84 case Wrap:
85 helper.at(i)=fmod(helper.at(i),1);
[d2938f]86 helper.at(i)+=(helper.at(i)>=0)?0:1;
[c72562]87 break;
88 case Bounce:
89 {
90 // there probably is a better way to handle this...
91 // all the fabs and fmod modf probably makes it very slow
92 double intpart,fracpart;
93 fracpart = modf(fabs(helper.at(i)),&intpart);
94 helper.at(i) = fabs(fracpart-fmod(intpart,2));
95 }
96 break;
97 case Ignore:
98 break;
99 default:
100 ASSERT(0,"No default case for this");
101 }
102
[f429d7]103 }
104 return translateIn(helper);
105}
106
[0ff6b5]107bool Box::isInside(const Vector &point) const
108{
109 bool result = true;
[29ac78]110 Vector tester = translateOut(point);
[0ff6b5]111
112 for(int i=0;i<NDIM;i++)
[f3be87]113 result = result &&
114 ((conditions[i] == Ignore) ||
115 ((tester[i] >= -MYEPSILON) &&
116 ((tester[i] - 1.) < MYEPSILON)));
[0ff6b5]117
118 return result;
119}
120
121
[a630fd]122VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
[16648f]123 ASSERT(isInside(point),"Exploded point not inside Box");
[12cf773]124 VECTORSET(std::list) res;
[89e820]125
[16648f]126 Vector translater = translateOut(point);
127 Vector mask; // contains the ignored coordinates
128
129 // count the number of coordinates we need to do
130 int dims = 0; // number of dimensions that are not ignored
131 vector<int> coords;
132 vector<int> index;
133 for(int i=0;i<NDIM;++i){
134 if(conditions[i]==Ignore){
135 mask[i]=translater[i];
136 continue;
137 }
138 coords.push_back(i);
139 index.push_back(-n);
140 dims++;
141 } // there are max vectors in total we need to create
142
143 if(!dims){
144 // all boundaries are ignored
145 res.push_back(point);
146 return res;
[89e820]147 }
[7ac4af]148
[d2938f]149 bool done = false;
150 while(!done){
[16648f]151 // create this vector
152 Vector helper;
153 for(int i=0;i<dims;++i){
154 switch(conditions[coords[i]]){
155 case Wrap:
156 helper[coords[i]] = index[i]+translater[coords[i]];
157 break;
158 case Bounce:
159 {
160 // Bouncing the coordinate x produces the series:
161 // 0 -> x
162 // 1 -> 2-x
163 // 2 -> 2+x
164 // 3 -> 4-x
165 // 4 -> 4+x
166 // the first number is the next bigger even number (n+n%2)
167 // the next number is the value with alternating sign (x-2*(n%2)*x)
168 // the negative numbers produce the same sequence reversed and shifted
[d2938f]169 int n = abs(index[i]) + ((index[i]<0)?-1:0);
[16648f]170 int sign = (index[i]<0)?-1:+1;
171 int even = n%2;
172 helper[coords[i]]=n+even+translater[coords[i]]-2*even*translater[coords[i]];
173 helper[coords[i]]*=sign;
174 }
175 break;
176 case Ignore:
177 ASSERT(0,"Ignored coordinate handled in generation loop");
178 default:
179 ASSERT(0,"No default case for this switch-case");
[7ac4af]180 }
[16648f]181
182 }
183 // add back all ignored coordinates (not handled in above loop)
184 helper+=mask;
185 res.push_back(translateIn(helper));
186 // set the new indexes
[d2938f]187 int pos=0;
[16648f]188 ++index[pos];
[d2938f]189 while(index[pos]>n){
[16648f]190 index[pos++]=-n;
[d2938f]191 if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
192 done = true;
193 break;
194 }
[16648f]195 ++index[pos];
[7ac4af]196 }
197 }
198 return res;
199}
200
[16648f]201VECTORSET(std::list) Box::explode(const Vector &point) const{
202 ASSERT(isInside(point),"Exploded point not inside Box");
203 return explode(point,1);
204}
205
[014475]206double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
[f1c838]207 Vector helper1 = WrapPeriodically(point1);
208 Vector helper2 = WrapPeriodically(point2);
209 VECTORSET(std::list) expansion = explode(helper1);
210 double res = expansion.minDistSquared(helper2);
[014475]211 return res;
212}
213
214double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
215 double res;
216 res = sqrt(periodicDistanceSquared(point1,point2));
217 return res;
[527de2]218}
219
[77374e]220const Box::Conditions_t Box::getConditions(){
221 return conditions;
222}
223
224void Box::setCondition(int i,Box::BoundaryCondition_t condition){
225 conditions[i]=condition;
226}
227
[29ac78]228const vector<pair<Plane,Plane> > Box::getBoundingPlanes(){
229 vector<pair<Plane,Plane> > res;
230 for(int i=0;i<NDIM;++i){
231 Vector base1,base2,base3;
232 base2[(i+1)%NDIM] = 1.;
233 base3[(i+2)%NDIM] = 1.;
234 Plane p1(translateIn(base1),
235 translateIn(base2),
236 translateIn(base3));
237 Vector offset;
238 offset[i]=1;
239 Plane p2(translateIn(base1+offset),
240 translateIn(base2+offset),
241 translateIn(base3+offset));
242 res.push_back(make_pair(p1,p2));
243 }
244 return res;
245}
246
[e1ab97]247void Box::setCuboid(const Vector &endpoint){
248 ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
249 M->one();
250 M->diagonal()=endpoint;
251 Vector &dinv = Minv->diagonal();
252 for(int i=NDIM;i--;)
253 dinv[i]=1/endpoint[i];
254}
255
[7579a4b]256Box &Box::operator=(const Box &src){
257 if(&src!=this){
258 delete M;
259 delete Minv;
260 M = new Matrix(*src.M);
261 Minv = new Matrix(*src.Minv);
[77374e]262 conditions = src.conditions;
[7579a4b]263 }
264 return *this;
265}
266
267Box &Box::operator=(const Matrix &mat){
268 setM(mat);
269 return *this;
270}
Note: See TracBrowser for help on using the repository browser.