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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <valarray>
#include <vector>
#include <string>
using namespace std;
using Array = valarray<double>;
//***** Function prototypes
void step( double dx, double &x, Array &Y ); // One Runge-Kutta step
valarray<double> F( double x, Array Y ); // Derivative function
void readNums(); // Read number of species and locations
void readSource(); // Read source data (Iij)
void readTransfer(); // Read transfer data (Kijk etc
void readConc( double &tfirst, Array &Y ); // Read initial concentrations
void writeData( double t, Array Y, bool header ); // Write data
int ijIndex( int i, int j ); // Convert from i,j to n
void indexij( int n, int &i, int &j ); // Convert from n to i,j
double Apow( double x, int p ); // Quasi-power law
void clip( Array &Y, double value ); // Clip
//***** Global variables
struct SOURCE
{
int i, j; // Species number (i) and location (j)
double I; // Source term Iij
double tmin, tmax; // Start & end times for Iij
};
istream & operator >> ( istream &strm, SOURCE &s )
{
strm >> s.i >> s.j >> s.I >> s.tmin >> s.tmax;
return strm;
}
vector<SOURCE> sourcelist;
struct TRANSFER
{
int i, j, k; // Species number (i), 'from' location (j) and 'to' location (k)
double Kijk, Kikj; // Rate constants
int Aijk, Aikj; // Rate-constant exponents
double Gijk, Gikj; // Inertial constant
int Bijk, Bikj; // Inertial-constant exponents
};
istream & operator >> ( istream &strm, TRANSFER &t )
{
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;
//**** End of global variables
int main()
{
double tfirst;
readNums(); // Get NUMSPECIES, NUMLOCATIONS
Array Y(NUMSPECIES * NUMLOCATIONS); // Set up 1-d array to hold dependent variables
readConc( tfirst, Y ); // Read initial time and concentrations
readSource(); // Read all source terms Iij into sourcelist
readTransfer(); // Read all transfer parts of the equations into transferlist
double dt = 0.1; // Timestep
int nstep = 30, nprint = 1; // Initial number of timesteps and output frequency
const int MAXSTEP = 200000;
const double TOLERANCE = 0.00001;
Array Yfirst = Y, Ylast = Y; // To store the start and end values
double change = 1.0; // To store the maximum absolute change from previous run
double t;
while ( nstep <= MAXSTEP && change > TOLERANCE )
{
t = tfirst; Y = Yfirst;
writeData( t, Y, true ); // Output the starting conditions
for ( int n = 1; n <= nstep; n++ )
{
step( dt, t, Y ); // One timestep
clip( Y, 0.0 ); // Clip if necessary
if ( n % nprint == 0 ) writeData( t, Y, false ); // Output
}
change = abs( Y - Ylast ).max();
cout << "nstep: " << nstep << scientific << " dt: " << dt << " change: " << change << '\n' << string(40,'*') << '\n';
nstep *= 2; nprint *= 2; dt /= 2.0; // Half the global timestep before repeat
Ylast = Y;
}
}
//========
void step( double dx, double &x, Array &Y ) // Single timestep
{
int N = Y.size();
Array dY1(N), dY2(N), dY3(N), dY4(N);
// Runge-Kutta
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;
// Mid-point method
// dY1 = F( x , Y ) * dx;
// dY2 = F( x + 0.5 * dx, Y + 0.5 * dY1 ) * dx;
// Y += dY2;
x += dx;
}
//========
Array F( double x, Array Y ) // Derivative
{
Array 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 );
if ( x > s.tmin && x <= s.tmax ) 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 * Apow( Y[nik], t.Aikj ) / ( 1.0 + t.Gikj * Apow( Y[nik], t.Bikj ) );
f[nij] -= t.Kijk * Apow( Y[nij], t.Aijk ) / ( 1.0 + t.Gijk * Apow( 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( double &tfirst, Array &Y )
{
int i, j;
double X;
Y = 0; // Anything NOT set in the file ... is assumed to be 0
ifstream infile( "concentration.dat" );
infile >> tfirst;
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, Array 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';
}
//========
double Apow( double x, int p ) // QUASI-power law
{
switch ( p )
{
case 0: return ( x >= 1.0e-10 ? 1.0 : 0.0 ); // Step function
case 1: return x; // Linear
case 2: return x * x; // Quadratic
default:
cout << "Faulty exponent in Apow";
exit( 1 );
}
}
//========
void clip( Array &Y, double value )
{
int N = Y.size();
for ( int i = 0; i < N; i++ ) if ( Y[i] < value ) Y[i] = value;
}
//========
|