MPD78
07-18-2011, 05:59 PM
Hello all,
Would any one be able to help me with an example program of how to implement Mglin? I want to use this algorithm to solve some problems in "Computational Fluid Dynamics" by John D. Anderson.
Thanks
Matt
davekw7x
07-19-2011, 08:13 AM
//
// Driver for routine mglin
//
#include "../code/nr3.h"
#include "../code/mglin.h"
int main()
{
int jmax = 17; // Must be one more than a power of 2
int nstep = 1; // For large matrices, print out fewer values
// Will set all values to zero except the one in the middle
int middle = jmax / 2;
int i, j;
int ncycles;
// Will only accept a positive integer for ncycles
cout << "Enter the number of cycles: ";
while ((cin >> ncycles) && (ncycles > 0)) {
MatDoub f(jmax, jmax);
MatDoub u(jmax, jmax, 0.0);
u[middle][middle] = 2.0;
Mglin(u, ncycles);
cout << "Results for " << ncycles << " cycles of mglin:" << endl;
cout << "Mglin solution:" << endl;
cout << fixed << setprecision(4);
for (i = 0; i < jmax; i += nstep) {
for (j = 0; j < jmax; j += nstep) {
cout << setw(8) << u[i][j];
}
cout << endl;
}
cout << endl << "Plug mglin solution into the difference equations:";
cout << endl;
for (i = nstep; i < jmax - 1; i += nstep) {
cout << " ";
for (j = nstep; j < jmax - 1; j += nstep) {
f[i][j] = u[i + 1][j] + u[i - 1][j] + u[i][j + 1] + u[i][j - 1]
- 4.0 * u[i][j];
}
for (j = nstep; j < jmax - 1; j += nstep) {
cout << setw(8) << f[i][j] * (jmax - 1) * (jmax - 1);
}
cout << endl;
}
cout << endl << "Enter the number of cycles: ";
}
cout << endl << "Goodbye for now" << endl;
return 0;
}
Output after compiling with GNU g++ version 4.1.2:
Enter the number of cycles: 3
Results for 3 cycles of mglin:
Mglin solution:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 -0.0000 -0.0001 -0.0001 -0.0001 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0001 -0.0001 -0.0001 -0.0000 0.0000
0.0000 -0.0001 -0.0001 -0.0002 -0.0003 -0.0003 -0.0004 -0.0004 -0.0004 -0.0004 -0.0004 -0.0003 -0.0003 -0.0002 -0.0001 -0.0001 0.0000
0.0000 -0.0001 -0.0002 -0.0003 -0.0004 -0.0005 -0.0006 -0.0006 -0.0007 -0.0006 -0.0006 -0.0005 -0.0004 -0.0003 -0.0002 -0.0001 0.0000
0.0000 -0.0001 -0.0003 -0.0004 -0.0005 -0.0007 -0.0008 -0.0009 -0.0010 -0.0009 -0.0008 -0.0007 -0.0005 -0.0004 -0.0003 -0.0001 0.0000
0.0000 -0.0002 -0.0003 -0.0005 -0.0007 -0.0009 -0.0011 -0.0012 -0.0013 -0.0012 -0.0011 -0.0009 -0.0007 -0.0005 -0.0003 -0.0002 0.0000
0.0000 -0.0002 -0.0004 -0.0006 -0.0008 -0.0011 -0.0014 -0.0017 -0.0018 -0.0017 -0.0014 -0.0011 -0.0008 -0.0006 -0.0004 -0.0002 0.0000
0.0000 -0.0002 -0.0004 -0.0006 -0.0009 -0.0012 -0.0017 -0.0022 -0.0027 -0.0022 -0.0017 -0.0012 -0.0009 -0.0006 -0.0004 -0.0002 0.0000
0.0000 -0.0002 -0.0004 -0.0007 -0.0010 -0.0013 -0.0018 -0.0027 -0.0047 -0.0027 -0.0018 -0.0013 -0.0010 -0.0007 -0.0004 -0.0002 0.0000
0.0000 -0.0002 -0.0004 -0.0006 -0.0009 -0.0012 -0.0017 -0.0022 -0.0027 -0.0022 -0.0017 -0.0012 -0.0009 -0.0006 -0.0004 -0.0002 0.0000
0.0000 -0.0002 -0.0004 -0.0006 -0.0008 -0.0011 -0.0014 -0.0017 -0.0018 -0.0017 -0.0014 -0.0011 -0.0008 -0.0006 -0.0004 -0.0002 0.0000
0.0000 -0.0002 -0.0003 -0.0005 -0.0007 -0.0009 -0.0011 -0.0012 -0.0013 -0.0012 -0.0011 -0.0009 -0.0007 -0.0005 -0.0003 -0.0002 0.0000
0.0000 -0.0001 -0.0003 -0.0004 -0.0005 -0.0007 -0.0008 -0.0009 -0.0010 -0.0009 -0.0008 -0.0007 -0.0005 -0.0004 -0.0003 -0.0001 0.0000
0.0000 -0.0001 -0.0002 -0.0003 -0.0004 -0.0005 -0.0006 -0.0006 -0.0007 -0.0006 -0.0006 -0.0005 -0.0004 -0.0003 -0.0002 -0.0001 0.0000
0.0000 -0.0001 -0.0001 -0.0002 -0.0003 -0.0003 -0.0004 -0.0004 -0.0004 -0.0004 -0.0004 -0.0003 -0.0003 -0.0002 -0.0001 -0.0001 0.0000
0.0000 -0.0000 -0.0001 -0.0001 -0.0001 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0001 -0.0001 -0.0001 -0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Plug mglin solution into the difference equations:
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 -0.0000 0.0000 0.0000 0.0000 0.0001 0.0000 -0.0002 0.0000 0.0001 0.0000 0.0000 0.0000 -0.0000 0.0000
0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 0.0000
0.0000 0.0000 0.0000 0.0001 0.0000 0.0002 0.0000 -0.0004 0.0000 0.0002 0.0000 0.0001 0.0000 0.0000 0.0000
0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 0.0000
0.0000 0.0001 0.0000 0.0002 0.0000 0.0006 0.0000 -0.0002 0.0000 0.0006 0.0000 0.0002 0.0000 0.0001 0.0000
0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0001 0.0000 -0.0001 0.0000 -0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 -0.0002 0.0000 -0.0004 0.0000 -0.0002 0.0000 1.9986 0.0000 -0.0002 0.0000 -0.0004 0.0000 -0.0002 0.0000
0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0001 0.0000 -0.0001 0.0000 -0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0001 0.0000 0.0002 0.0000 0.0006 0.0000 -0.0002 0.0000 0.0006 0.0000 0.0002 0.0000 0.0001 0.0000
0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 0.0000
0.0000 0.0000 0.0000 0.0001 0.0000 0.0002 0.0000 -0.0004 0.0000 0.0002 0.0000 0.0001 0.0000 0.0000 0.0000
0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0000 0.0001 0.0000 0.0000
0.0000 -0.0000 0.0000 0.0000 0.0000 0.0001 0.0000 -0.0002 0.0000 0.0001 0.0000 0.0000 0.0000 -0.0000 0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Enter the number of cycles: quit
Goodbye for now