I'm having trouble moving a vector after I iterate through it

I have a project where I have a bunch of molecules moving around a box, I need the molecules to form a polymer if they come close to each other, if the polymer exists add a molecule to the end of the polymer until the polymer vector is > the molecule vector.
I have created everything and I believe it polymerizes, but no matter what I try I just can't get the polymer chain to move after it adds a molecule.
so I know that is a bit confusing here is my code
I hope that makes sense to everyone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
#include <vector>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <time.h>
#include <algorithm>
using namespace std;
#include "random.h"
#include "molecule.h"
#include "polymer.h"
#include "box.h"



void Box::move()
{

	for(int i=0;i<molecules.size();i++) {
		 	molecules[i]->move();
			checkForMoleculeWrapping(molecules[i]);
			if(!checkOverlap(i)) {
			molecules[i]->goBack();
				}
				if(polymer.size()<molecules.size()) {
				checkPolymerization(i);
		}
	}
 }	

bool Box::checkOverlap(int molecule_index)
{
	vector<Molecule*>::iterator it;
	Molecule* m1;
	m1 = molecules[molecule_index];
	Molecule* m2;
	for(int i=0;i<molecules.size();i++) {
		if(i!=molecule_index) {
			m2 = molecules[i];
			if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),m2->getX(),m2->getY(),m2->getZ()) < (m1->getDiameter()/2.0 + m2->getDiameter()/2.0)) return false; //this code checks for molecule overlap
				it=find(polymer.begin(),polymer.end(),molecules[molecule_index]);	// to check if the molecules in the polymer are misbehaving all of the code below
				if(it!=polymer.end()) {
				m1=*it;
					if(it==polymer.begin()){
					m2=*it+1;
						if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),m2->getX(),m2->getY(),m2->getZ())>max_distance_between_neighboring_molecules){
						return false;
				}
			}
			else if(it==(polymer.end()-1)){
			m2=*it-1;
				if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),m2->getX(),m2->getY(),m2->getZ())>max_distance_between_neighboring_molecules){
				return false;
				}
				
			}
			else {
			m2=*it+1;
				if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),m2->getX(),m2->getY(),m2->getZ())>max_distance_between_neighboring_molecules){
				return false;
				}
			m2=*it-1;
				if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),m2->getX(),m2->getY(),m2->getZ())>max_distance_between_neighboring_molecules){
				return false;
				}
			}
		}
	}
} return true;
}
bool Box::MoleInPoly(int molecule_index){
	
	vector<Molecule*>::iterator it;
	Molecule* m1;
	m1 = polymer[molecule_index];
	Molecule* m2;
	for(int i=0;i<polymer.size();i++) {
		if(i!=molecule_index) {
			m2 = polymer[i];
			if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),m2->getX(),m2->getY(),m2->getZ()) < (m1->getDiameter() + m2->getDiameter())) return false;
			else { polymer[i]->move();}
				}
}
return true;
}
void Box::checkPolymerization(int molecule_index)
{
	vector<Molecule*>::iterator iter;

	Molecule* m1;
	Molecule* m2;
	m1 = molecules[molecule_index];
	if(polymer.empty()) {                         
		for(int i=0;i<molecules.size();i++) {
			if(i!=molecule_index) {
				m2 = molecules[molecule_index];
				if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),m2->getX(),m2->getY(),m2->getZ()) < max_distance_between_neighboring_molecules) {
					polymer.push_back(m1);
					polymer.push_back(m2);
				}
			}
		}
	}
	else {
		// is m1 in the polymer?
		iter = find(polymer.begin(),polymer.end(),m1);
		if(iter!=polymer.end()) {
			if( m1==polymer.back()){// m1 is in the polmyer
			  // is m1 an end?
				for(int i=0;i<molecules.size();i++){				  
					 if(find(polymer.begin(),polymer.end(),molecules[i])==polymer.end()){
						if(	distanceCalc(molecules[i]->getX(),molecules[i]->getY(),molecules[i]->getZ(),polymer.back()->getX(),polymer.back()->getY(),polymer.back()->getZ())<max_distance_between_neighboring_molecules){
							polymer.push_back(molecules[i]);
						}
					 }
				}
			}
			else if( iter == polymer.begin()) {
				for(int i=0;i<molecules.size();i++){				  
					 if(find(polymer.begin(),polymer.end(),molecules[i])==polymer.end()){
						if(	distanceCalc(molecules[i]->getX(),molecules[i]->getY(),molecules[i]->getZ(),polymer.back()->getX(),polymer.back()->getY(),polymer.back()->getZ())<max_distance_between_neighboring_molecules){
							polymer.insert(polymer.begin(),molecules[i]);
						}
					 }
				}
			}
		}                        
		else {
			if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),(*polymer.begin())->getX(),(*polymer.begin())->getY(),(*polymer.begin())->getZ())<max_distance_between_neighboring_molecules)                                        // is the distance between m1 and polymer.end()-1 < 1.2 then add m1 to back
				polymer.insert(polymer.begin(),m1);		
			else{
			  if(distanceCalc(m1->getX(),m1->getY(),m1->getZ(),(*polymer.end()-1)->getX(),(*polymer.end()-1)->getY(),(*polymer.end()-1)->getZ())<max_distance_between_neighboring_molecules)
		       polymer.push_back(m1);
			}
		}
	}
}

double Box::distanceCalc(double _x1, double _y1, double _z1, double _x2, double _y2, double _z2) 
{
	double dx,dy,dz;
	dx = fabs(_x1-_x2);
	dy = fabs(_y1-_y2);
	dz = fabs(_z1-_z2);
	if(dx > 0.5*x) { dx = dx - x; }
	if(dy > 0.5*y) { dy = dy - y; }
	if(dz > 0.5*z) { dz = dz - z; }
	return sqrt( pow( dx,2.0 ) + pow( dy,2.0 ) + pow( dz,2.0 ) );
}

void Box::checkForMoleculeWrapping(Molecule* m)
{
	if(m->getX() < -x/2.0) m->setPosition(m->getX()+x,m->getY(),m->getZ());
	if(m->getY() < -y/2.0) m->setPosition(m->getX(),m->getY()+y,m->getZ());
	if(m->getZ() < -z/2.0) m->setPosition(m->getX(),m->getY(),m->getZ()+z);
	if(m->getX() > x/2.0) m->setPosition(m->getX()-x,m->getY(),m->getZ());
	if(m->getY() > y/2.0) m->setPosition(m->getX(),m->getY()-y,m->getZ());
	if(m->getZ() > z/2.0) m->setPosition(m->getX(),m->getY(),m->getZ()-z);
}	

/*
void Box::movePoly()   // new function to see if I can move the entire polymer
{
	vector<Molecule*>::iterator k;
	for(k=polymer.begin();k!=polymer.end();k++){
		for(int i=0;i<polymer.size();i++){
		if(k!=polymer.begin()){
			polymer[i]->setPosition(polymer[i-1]->getX(),polymer[i-1]->getY(),polymer[i-1]->getZ());
		}
		else if (k==polymer.begin()){
			polymer[i]->setPosition(polymer[i]->getX()-x/2.0,polymer[i]->getY()-y/2.0,polymer[i]->getZ()-z/2.0);
		}
		}
	}
		/*
	vector<Molecule*> polymer(copypolymer);
	for(int i=0;i<polymer.size();i++){
		
		//d=polymer[i]->getDiameter();
		//xi=polymer[i]->getX();
		//yi=polymer[i]->getY();
		//zi=polymer[i]->getZ();
		for(int j=0;j<copypolymer.size();j++){
			if(distanceCalc(polymer[i]->getX(),polymer[i]->getY(),polymer[i]->getZ(),copypolymer[j]->getX(),copypolymer[j]->getY(),copypolymer[j]->getZ())==0){
				copypolymer[j+1]=polymer[i];
				//polymer[i]->move();
			}
		}
	} 

}


*/ 
Last edited on
please use code tags: http://www.cplusplus.com/articles/jEywvCM9/

Aceix.
thanks, sorry am new didn't have time to read the rules
... p.s. I added the new function MoleInPoly() and it seems to be moving now, I can't view the .vtf file it outputs in main because of windows, so I can't be sure of it working entirely. I really appreciate any help :)
Topic archived. No new replies allowed.