I'm a beginner in C++ (physics undergraduate) and am currently taking a course on computational physics. I have an exercise to numerically solve - using Euler's method - a 'flow problem' for a 3-level-system.
That is, I want to compute the values of a 2-dimensional array like so
double flow(double values, const double W12, const double W13, const double W23)
{
for(int i = 1; i<STEPS; i++)
{
values[i][0] = -W12*values[i-1][0] + W13*values[i-1][2];
values[i][1] = -W23*values[i-1][1] + W12*values[i-1][0];
values[i][2] = -W13*values[i-1][2] + W23*values[i-1][1];
return values;
}
}
However, when compiling this, I get an error saying 'invalid types 'double[int]' for array subscript', and I don't know what the problem is exactly (as I said, beginner). Shouldn't this code take the values from the row above to compute the current row (3 values), then move to the next, etc.?
The complete code looks like this (Euler not included, still working on this. First I just want the values)
#include <iostream>
#include <vector>
#include <fstream>
#include <ctime>
#define DIM 3 // for 3 level system
#define STEPS 1000 // 10000 steps
using namespace std;
double flow(double values, const double W12, const double W13, const double W23)
{
for(int i = 1; i<STEPS; i++)
{
values[i][0] = -W12*values[i-1][0] + W13*values[i-1][2];
values[i][1] = -W23*values[i-1][1] + W12*values[i-1][0];
values[i][2] = -W13*values[i-1][2] + W23*values[i-1][1];
return values;
}
}
int main()
{
const double E1 = 10; // define energy levels
const double E2 = 20;
const double E3 = 16;
const double W12, W13, W23; // initialize spacings
double N1 = 1000; // initial fill values for energy levels N1-Ni
double N2 = 1000;
double N3 = 1000;
W12 = E2 - E1; // calculate spacings
W13 = E3 - E1;
W23 = E3 - E2;
double values [STEPS][DIM]; // setup a (3 x steps) array
values [0][0] = N1; // fill the first row with our initial values
values [0][1] = N2;
values [0][2] = N3;
values.reserve(STEPS); // reserve memory
//ofstream myfile; // setup writing to file
//myfile.open("values.txt");
flow(values, W12, W13, W23);
void printArray(double (&values)[STEPS][DIM])
{
for(int x = 0; x < STEPS; x++){
for(int y = 0; y < ROW; y++){
cout << values[x][y] << " ";
}
cout << endl;
}
}
// myfile.close();
return 0;
}
I realize this is probably a very noobish question, and the answer may be obvious, but I can't for my life figure it out.
Any help would be greatly appreciated :)