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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <valarray>
#include <vector>
using namespace std;
// Function prototypes
void step( double dx, double &x, valarray<double> &Y ); // one RungeKutta step
valarray<double> F( double x, valarray<double> Y ); // the derivative function (multivariable)
void readNums(); // read the numbers of species and locations
void readSource(); // read all the source data (Iij)
void readTransfer(); // read all the transfer data (Kijk etc
void readConc( valarray<double> &Y ); // read initial concentrations
void writeData( double t, valarray<double> Y, bool header ); // write data (optionally with header)
int ijIndex( int i, int j ); // convert from i,j to 1d index
void indexij( int n, int &i, int &j ); // convert from 1d index to i,j
//*******
// Global variables
//*******
struct SOURCE
{
int i, j; // the species number (i) and location (j)
double I; // the source term Iij
};
istream & operator >> ( istream &strm, SOURCE &s ) // overload the input operator for a SOURCE
{
strm >> s.i >> s.j >> s.I;
return strm;
}
vector<SOURCE> sourcelist;
struct TRANSFER
{
int i, j, k; // the species number (i), 'from' location (j) and 'to' location (k)
double Kijk, Kikj; // main rate constants
double Aijk, Aikj; // rateconstant exponents
double Gijk, Gikj; // the inertial constant
double Bijk, Bikj; // inertialconstant exponents
};
istream & operator >> ( istream &strm, TRANSFER &t ) // overload the input operator for a TRANSFER
{
strm >> t.i >> t.j >> t.k >> t.Kijk >> t.Kikj >> t.Aijk >> t.Aikj >> t.Gijk >> t.Gikj >> t.Bijk >> t.Bikj;
return strm;
}
vector<TRANSFER> transferlist;
int NUMSPECIES;
int NUMLOCATIONS;
double dt = 0.1; // timestep
int nstep = 100; // number of timesteps
int nprint = 10; // frequency of output
//*************
// End of global variables
//*************
//========
int main()
{
readNums(); // Get NUMSPECIES, NUMLOCATIONS
valarray<double> Y(NUMSPECIES * NUMLOCATIONS); // Set up 1d array to hold dependent variables
readSource(); // Read all source terms Iij into sourcelist
readTransfer(); // Read all transfer parts of the equations into transferlist
readConc( Y ); // read the initial concentrations
double t = 0;
writeData( 0, Y, true ); // Output the starting conditions
for ( int n = 1; n <= nstep; n++ ) // Solve the differential equation using nstep intervals
{
step( dt, t, Y ); // Single step of Runge Kutta
if ( n % nprint == 0 ) writeData( t, Y, false ); // Output every nprint steps
}
}
//========
void step( double dx, double &x, valarray<double> &Y ) // Single RungeKutta step
{
int ndep = Y.size();
valarray<double> dY1(ndep), dY2(ndep), dY3(ndep), dY4(ndep);
dY1 = F( x , Y ) * dx;
dY2 = F( x + 0.5 * dx, Y + 0.5 * dY1 ) * dx;
dY3 = F( x + 0.5 * dx, Y + 0.5 * dY2 ) * dx;
dY4 = F( x + dx, Y + dY3 ) * dx;
Y += ( dY1 + 2.0 * dY2 + 2.0 * dY3 + dY4 ) / 6.0;
x += dx;
}
//========
// YOUR MAIN DERIVATIVE (the RHS) IS SET HERE ***
valarray<double> F( double x, valarray<double> Y )
{
valarray<double> f( Y.size() );
f = 0; // Start with 0
for ( SOURCE s : sourcelist ) // Add the source terms
{
int i = s.i;
int j = s.j;
int nij = ijIndex( i, j );
f[nij] += s.I;
}
for ( TRANSFER t : transferlist ) // Add the transfer terms
{
int i = t.i;
int j = t.j;
int k = t.k;
int nij = ijIndex( i, j );
int nik = ijIndex( i, k );
f[nij] += t.Kikj * pow( Y[nik], t.Aikj ) / ( 1.0 + t.Gikj * pow( Y[nik], t.Bikj ) );
f[nij] = t.Kijk * pow( Y[nij], t.Aijk ) / ( 1.0 + t.Gijk * pow( Y[nij], t.Bijk ) );
}
return f;
}
//========
void readNums()
{
ifstream infile( "numbers.dat" );
infile >> NUMSPECIES >> NUMLOCATIONS;
infile.close();
cout << "Number of species: " << NUMSPECIES << " Number of locations: " << NUMLOCATIONS << '\n';
}
//========
void readSource()
{
SOURCE s;
ifstream infile( "source.dat" );
while ( infile >> s ) sourcelist.push_back( s );
infile.close();
cout << "Number of source items read: " << sourcelist.size() << '\n';
}
//========
void readTransfer()
{
TRANSFER t;
ifstream infile( "transfer.dat" );
while ( infile >> t ) transferlist.push_back( t );
infile.close();
cout << "Number of transfer items read: " << transferlist.size() << '\n';
}
//========
void readConc( valarray<double> &Y )
{
int i, j;
double X;
Y = 0; // Anything NOT set in the file ... is assumed to be 0
ifstream infile( "concentration.dat" );
while ( infile >> i >> j >> X ) Y[ ijIndex( i, j ) ] = X;
infile.close();
}
//========
int ijIndex( int i, int j )
{
return NUMLOCATIONS * ( i  1 ) + ( j  1 );
}
//========
void indexij( int n, int &i, int &j )
{
i = 1 + n % NUMLOCATIONS;
j = 1 + n / NUMLOCATIONS;
}
//========
void writeData( double t, valarray<double> Y, bool header )
{
#define SPH << setw( 10 ) <<
#define SPD << setw( 12 ) << fixed << setprecision( 4 ) <<
if ( header )
{
cout SPH " " << " t";
for ( int i = 1; i <= NUMSPECIES; i++ )
{
for ( int j = 1; j <= NUMLOCATIONS; j++ ) cout SPH 'C' << i << j;
}
cout << '\n';
}
cout SPD t;
for ( int i = 1; i <= NUMSPECIES; i++ )
{
for ( int j = 1; j <= NUMLOCATIONS; j++ ) cout SPD Y[ ijIndex( i, j ) ];
}
cout << '\n';
}
 