writing to a vector of struct containing vectors

Hi,
I have currently writing a program to calculate a temperature profile of a building construction. For these calculations I am using an iterative approach. In the final version of the program one data set for each time step of the simulations should be saved (that is one per hour). While testing the program (and also later on as an option) I would also like to store the calculated sub-steps (up to 3600 per hour) and that is where I run into a wall just now.

To store the data I have build a few struct in which I intend to store the data. These are as follows (shortened)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Temperature{
    Temperature():  pipeInlet(-FLT_MAX),
                    pipeInletSetpoint(-FLT_MAX){};
    float pipeInlet;
    float pipeInletSetpoint;
    vector<float> node; // the number of nodes depends on the building to be simulated

};

struct HeatFlow{
    HeatFlow(): circuit(-FLT_MAX),
                floorSurface(-FLT_MAX){};
    float circuit;
    float floorSurface;
};

struct Results{
    Temperature temp;
    vector<Temperature> subTemp; // temperatures calculated in sub-timesteps
    HeatFlow heat;
    vector<HeatFlow> subHeat; // HeatFlows calculated in sub-timesteps
};


In the program I am then using vector<Results> results_ to create one container to store all the simulation results in one place. I would prefer to do this in order not to (for what ever reason) get confused as to which sub time-steps belong to which time-step.

My problem is now: How do I write to this last vector (results_)?

Since I don't know what the size is going to be in the end it would not make sense for me to define the vector with a fixed amount of elements. The only solution I can think of (and also believe that I could pull of) is to always append a single element to the vector when I need it (and I have done this elsewhere with success). The problem here is however that I don't relay know what to do with the vectors included in results_ (e.g. subTemp and subHeat). At this point of the simulation I know very little about them (at best I know the first element, which could just be a copy of the data at the current time-step).

Thanks for any help!

Last edited on
How have you previously been appending elements to vectors? Are you using push_back?
Yes, that's what I have been doing. But I never had a vector inside an vector.
Not quite sure on how good a handle I have on your project, but here goes nothing. I'll just post an example of how I'd push data to each vector. Not sure if it'll be what you're looking for, but it may help a little.

Let's assume that the only struct you're really interested in at the end result is the Results struct. The others are temporary, and used only for storing data into results.

So, somewhere in our code, we'll have:
1
2
vector<Results> results;
Results local_result;


To add something to the vector in Temperature
1
2
3
4
local_result.temp.pipeInlet = 0.5;  // Excuse the made up values, example only
local_result.temp.pipeInletSetPoint = 1.5;
local_result.temp.node.push_back(0.4);
local_result.temp.node.push_back(0.5);


If that needs to go into the subTemp vector...
 
local_result.subTemp.push_back(local_result.temp);


Assigning values to the HeatFlow and pushing is similar
1
2
3
4
5
local_result.heat.circuit = 0.4;
local_result.heat.floorSurface = 12;

// Push it back
local_result.subHeat.push_back(local_result.heat);


Then, whenever you want to add that result to the results vector...
 
results.push_back(local_result);


Is that of any use?
Last edited on
Yes, that seams to be exactly what I have been trying to do. I guess my problem was that I wanted to start at the end (sort of). Ordering it as you did seam much more logic (and doable) now.

Thanks for the quick help! I will try it out!
Topic archived. No new replies allowed.