problem with some data structures

Hi at all...
I'm develop an application in c++, for the simulaiton of some particles. my problems is this:

1 - I created a data structure called grid (Inside the gridy, there is the gridLayer)
2 - I created a gridLayer(where there are the particles).Post the code of the h files

class Grid {
public:
Grid(float posX, float posY, float posZ, float rows, float cols);
Grid(const Grid& orig);
Grid();
virtual ~Grid();
void update();
void updatePressure();
void updateVelocity();
void updateViscosity();
void updateSurfaceTension();
void updateExternalForces();
std::vector<GridLayer> getGridLayer();
void addGridLayer(GridLayer g);
void updateGradientAS(VectorR r, float h, float sum, int index);
float getViscosity();
float getPressure();
float getVelocity();
float getDensityA();
void updateDensityA(VectorR r, float h);
float evaluateKernelWPoly6(VectorR r, float h);
float evaluateKernelWSpiky(VectorR r, float h);
float evaluateKernelViscosity(VectorR r, float h);
float evaluateSecondDerivateKernelViscosity(VectorR r, float h);
float evaluateParticleDensity(VectorR r, float h);
void evaluateParticlePressure(float k);
void evaluateExternalForces();
void evaluateViscosity(VectorR r, float h, float mu);
float evaluateVelocityJ(VectorR r, float h);
void resetDensityA();
void resetPressure();
void resetVelocity();
void resetViscosity();
void resetSurfaceTension();
void resetExternalForces();
float getPosX();
float getPosY();
float getPosZ();

private:
float posX;
float posY;
float posZ;
float rows;
float cols;
VectorR **gradientA;
std::vector<GridLayer> gridLayer;
};


class GridLayer {

public:
GridLayer(float labelI, float labelJ, float labelK);
GridLayer(const GridLayer& orig);
GridLayer();
virtual ~GridLayer();
int getParticlesNumber();
Particle getParticleAt(int index);
void addParticle(Particle p);
std::vector<Particle> getAllParticles();
void deleteParticleAt(int p);
void setDistributionAs(float d);
bool operator==(const GridLayer& gridA) const;
bool operator!=(const GridLayer& gridA) const;
GridLayer returnGrid();
float getLabelI();
float getLabelJ();
float getLabelK();

private:
float labelI;
float labelJ;
float labelK;
float distributionAs;
std::vector<Particle> particles;
};

When I create the grid in the main, I do that in the follow ways(for example, i create 2 grid)

Grid* grid[2];
grid[0] = new Grid(0.0,0.0,0.0,20.0,20.0);
grid[1] = new Grid(0.1, 0.1, 0.1,20,20);

//now, i create the gridLayer, and I insert the particle inside... one for each layer

for(int i = 0; i<2; i++)
GridLayer* gr = new GridLayer(newI, newJ, 0);
Particle* p = new Particle(1,1,1,newI,newJ,0,newI,newJ,0,1);
gr->addParticle(*p);
grid[0]->addGridLayer(*gr);
grid[1]->addGridLayer(*gr);

now, the problem is this:

In gridLayer I've a vector of Particle. The method addParticle defined in the h file and implemented in the cpp file, contain only this istruction

void GridLayer::addParticle(Particle p){
particles->push_bach(p);
}

but, when in the main I check the status of the particles, I don't have any particles in the vector. Infact, if in the main I do

grid[0]->getGridLayer().at(i).getParticlesNumber();

I 've, like size, a value 0 (the value should be 1), and I lost the particles.

(note, getParticlesNumber has only a instruction of return this->particles.size())

why? it's should be a problem about the passing of the variable for value or reference or wath?

NB: like test, I put a cout<"hello" instruction in the distructor of GridLayer and Particle, and, during the esecution, This is invoked (because I can see "hello" on my consolle) before the endo of the programm.

Thanks for an answer, please, help me.
can u post the cpp files too?
mmmm I'm new of this forum... can you said me where can I post file inside? I don'ts nothing about upload procedure...
you only can post the code itself, between [code] tags ;)
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

#include "GridLayer.h"
#include "Particle.h"
#include <vector>
#include <iostream>
using namespace std;
GridLayer::GridLayer(float lbI, float lbJ, float lbK) {

    this->labelI = lbI;
    this->labelJ = lbJ;
    this->labelK = lbK;

}

GridLayer::GridLayer()
{

}
GridLayer::GridLayer(const GridLayer& orig) : labelI(orig.labelI), labelJ(orig.labelJ), labelK(orig.labelI), distributionAs(orig.distributionAs)

{
}

GridLayer::~GridLayer() {
   
  }

int GridLayer::getParticlesNumber()
{
    return particles.size();
}


GridLayer GridLayer::returnGrid()
{
    return *this;

}


bool GridLayer::operator==(const GridLayer& grid) const
{
 if (this->labelI == grid.labelI && this->labelJ == grid.labelJ && this->labelK == grid.labelK)
   // if(this == &grid)
    return true;

    return false;
}

float GridLayer::getLabelI()
{
    return this->labelI;
}

float GridLayer::getLabelJ()

{
        return this->labelJ;
}

float GridLayer::getLabelK()
{
    return this->labelK;
}

bool GridLayer::operator!=(const GridLayer& grid) const
{
   //if (this->labelI != grid.getLabelI() || this->labelJ != grid.getLabelJ() || this->labelK != grid.getLabelK())
    if(this != &grid)
    return true;

    return false;
}
void GridLayer::deleteParticleAt(int position)
{  
    vector<Particle>::iterator iterator;
    iterator = this->particles.begin() + position;
    this->particles.erase(iterator);
}

Particle GridLayer::getParticleAt(int index)
{
    return this->particles.at(index);
}

void GridLayer::setDistributionAs(float d)
{  this->distributionAs = d;
}

vector<Particle> GridLayer::getAllParticles()
{  return this->particles;
}

void GridLayer::addParticle(Particle p)
{
    this->particles.push_back(p);
}


grid.cpp

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

#include "GridLayer.h"
#include "Grid.h"
#include "Particle.h"
#include "VectorR.h"
#include <vector>
#include <math.h>
#include <iostream>
#include <GL/glut.h>

using namespace std;


Grid::Grid(float pX, float pY, float pZ,float rows, float cols) {

    this->posX = pX;
    this->posY = pY;
    this->posZ = pZ;
    this->rows = rows;
    this->cols = cols;

    
    

}


void Grid::addGridLayer(GridLayer g)
{
    this->gridLayer.push_back(g);
}

Grid::Grid(const Grid& g) : posX(g.posX), posY(g.posY), posZ(g.posZ),  rows(g.rows),  cols(g.cols),  gridLayer(g.gridLayer)

{

    

}


Grid::Grid()
{

    this->posX = 0;
    this->posY = 0;
    this->posZ = 0;
    this->rows = 0;
    this->cols = 0;
}
Grid::~Grid() {

    delete [] gradientA;
}

void Grid::updateDensityA(VectorR r, float h)
{
    float sum = 0;
    
        sum = 0;

        //first, I take the gridLyaer founded by the vector R
         vector<GridLayer> gridLayerTmp;
         
         //take the counter
         int counter = 0;
         // take a tmp counter for the cast
         float counterTmp = 0;

         for(float k = r.getZ(); k<=r.getZ()+h; k+=0.1)
         {
             //counterTMP * 10 because I need to cast the result to have an idnex
             counterTmp = k*10;
             counter = (int) counterTmp;
             if(&this->gridLayer.at(counter) != NULL)
                 gridLayerTmp.push_back(gridLayer.at(counter));

         }
        
        //now, I've the gridLayer along Z axys. I can evaluate the Density A fo every cell
         for(int i = 0; i<gridLayerTmp.size(); i++)
         {

             //I take all particles of the layer
             for(int j = 0; j<gridLayerTmp.at(i).getParticlesNumber(); j++)
             {
                 //check if I've a particles in a layer
                
                 if(&gridLayerTmp.at(i).getParticleAt(j)!=NULL)
                 {  //if a particle exist in a position individuated by a vector, a Take it
                     if(gridLayerTmp.at(i).getParticleAt(j).getWhereImX() <= r.getX()+h  && gridLayerTmp.at(i).getParticleAt(j).getWhereImY() <= r.getY()+h)
                     {

                         

                         Particle p = gridLayer.at(i).getParticleAt(i);
            VectorR ri(p.getPositionX(), p.getPositionY(), p.getPositionZ());
            r.sub(ri);
            sum = p.getMass()*(this->gridLayer.at(i).getParticlesNumber()/p.getDensity())*evaluateKernelWPoly6(r, h);
            this->gridLayer.at(i).setDistributionAs(sum);
                     }
                 
                 }
             }
         
              }

         //now call the gradient function
    

}

float Grid::evaluateParticleDensity(VectorR r, float h)
{

    float sum = 0;

        sum = 0;

        //first, I take the gridLyaer founded by the vector R
         vector<GridLayer> gridLayerTmp;

         //take the counter
         int counter = 0;
         // take a tmp counter for the cast
         float counterTmp = 0;

         for(float k = r.getZ(); k<=r.getZ()+h; k+=0.1)
         {
             //counterTMP * 10 because I need to cast the result to have an idnex
             counterTmp = k*10;
             counter = (int) counterTmp;
             if(&this->gridLayer.at(counter) != NULL)
                 gridLayerTmp.push_back(gridLayer.at(counter));

         }

        //now, I've the gridLayer along Z axys. I can evaluate the Density A fo every cell
         for(int i = 0; i<gridLayerTmp.size(); i++)
         {

             //I take all particles of the layer
             for(int j = 0; j<gridLayerTmp.at(i).getParticlesNumber(); j++)
             {
                 //check if I've a particles in a layer

                 if(&gridLayerTmp.at(i).getParticleAt(j)!=NULL)
                 {  //if a particle exist in a position individuated by a vector, a Take it
                     if(gridLayerTmp.at(i).getParticleAt(j).getWhereImX() <= r.getX()+h  && gridLayerTmp.at(i).getParticleAt(j).getWhereImY() <= r.getY()+h)
                     {
                         Particle p = gridLayer.at(i).getParticleAt(i);
            VectorR ri(p.getPositionX(), p.getPositionY(), p.getPositionZ());
            r.sub(ri);
            sum = p.getMass()*evaluateKernelWPoly6(r, h);
            this->gridLayer.at(i).setDistributionAs(sum);
                     }

                 }
             }

              }

}


float Grid::getPosX()
{
    return this->posX;
}

float Grid::getPosY()
{
    return this->posY;
}

float Grid::getPosZ()
{
    return this->posZ;
}

/* da eliminare
float Grid::getSizeX()
{
    return this->sizeX;
}


float Grid::getSizeY()
{
    return this->sizeY;
}

float Grid::getSizeZ()
{
    return this->sizeZ;
}

*/

float Grid::evaluateKernelWPoly6(VectorR r, float h)
{
    if (0 <= r.getModule() && r.getModule() <= h)
        return (315/(64*3.14159*powf(h,9)))*powf((powf(h,2)-powf(r.getModule(),2)),3);

    else
        return 0;

}

float Grid::evaluateKernelWSpiky(VectorR r, float h)
{
    if (0 <= r.getModule() && r.getModule() <= h)
        return (15/(3.14159*powf(h,6)))*powf((h-r.getModule()),3);

    else
        return 0;
}

float Grid::evaluateKernelViscosity(VectorR r, float h)
{
    if (0 <= r.getModule() && r.getModule() <= h)
        return ((15/2*3.14159*pow(h,3))*-(powf(r.getModule(),3)/(2*pow(h,3)))+powf(r.getModule(),2)/pow(h,2)+(h/2*r.getModule())-1);

    else
        return 0;

}




vector<GridLayer> Grid::getGridLayer()
{
    return this->gridLayer;
}


main (where I've the problem). The array is length 21, I put only 1 for simplfy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Grid *grid[1] = new Grid(0,0,0,2,2);
   GridLayer gr(1,1,0);
    Particle p(1,1,1,1,1,0,1,1,0,1);
    gr.addParticle(p);
    grid[0]->addGridLayer(gr);
    GridLayer gr2(1,2,0);
    Particle p2(1,1,1,1,2,0,1,2,0,1);
    gr.addParticle(p2);
    grid[0]->addGridLayer(gr2);
    GridLayer gr3(2,1,0);
    Particle p3(1,1,1,2,1,0,2,1,0,1);
    gr.addParticle(p3);
    grid[0]->addGridLayer(gr3);
    GridLayer gr4(2,2,0);
    Particle p4(1,1,1,2,2,0,2,2,0,1);
    gr.addParticle(p4);
    grid[0]->addGridLayer(gr4);


but, when I make

1
2
for(int h = 0; h<4; h++)
                        cout<<h<<" particelle:"<<grid[0]->getGridLayer().at(h).getParticlesNumber()<<" ";


the number of particle is 0
Topic archived. No new replies allowed.