include iostream include math include fstream define _DEBUG_ define _I

  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
#include <iostream>
#include <math.h>
#include <fstream>
//#define _DEBUG_
//#define _INTERACTIVE_
//#define _SUPERPOSITIONEXAMPLE_
//#define _PROPNONDISPERSIVE_
#define _PROPDISPERSIVE_
using namespace std;
const double PI = 4.0*atan(1.0);
double waveApprox( double position, double velocity, double time, int numTerms);
double waveApproxDispersive( double position, double velocity, double time, int numTerms);
int main (int argc, char * const argv[]) {
#ifdef _SUPERPOSITIONEXAMPLE_
const string baseDirString = "/Users/rj/Desktop/";
const string fiveTerms = baseDirString + "fiveTerms.dat";
const string twentyTerms = baseDirString + "twentyTerms.dat";
const string twoHundredTerms = baseDirString + "twoHundredTerms.dat";
ofstream fiveTermsOut;
ofstream twentyTermsOut;
ofstream twoHundredTermsOut;
fiveTermsOut.open(fiveTerms.c_str());
twentyTermsOut.open(twentyTerms.c_str());
twoHundredTermsOut.open(twoHundredTerms.c_str());
ofstream* variableTerm_fileArray[] = {&fiveTermsOut, &twentyTermsOut, &twoHundredTermsOut};
#endif
#ifdef _PROPNONDISPERSIVE_
const string baseDirString = "/Users/rj/Desktop/";
const string time0 = baseDirString + "time0.dat";
const string time1 = baseDirString + "time1.dat";
const string time2 = baseDirString + "time2.dat";
ofstream time0Out;
ofstream time1Out;
ofstream time2Out;
time0Out.open(time0.c_str());
time1Out.open(time1.c_str());
time2Out.open(time2.c_str());
ofstream* variableTime_fileArray[] = {&time0Out, &time1Out, &time2Out};
#endif
#ifdef _PROPDISPERSIVE_
const string baseDirString = "/Users/rj/Desktop/";
const string time0Dispers = baseDirString + "time0Dispers.dat";
const string time1Dispers = baseDirString + "time1Dispers.dat";
const string time2Dispers = baseDirString + "time2Dispers.dat";
ofstream time0DispersOut;
ofstream time1DispersOut;
ofstream time2DispersOut;
time0DispersOut.open(time0Dispers.c_str());
time1DispersOut.open(time1Dispers.c_str());
time2DispersOut.open(time2Dispers.c_str());
ofstream* variableTime_fileArray[] = {&time0DispersOut, &time1DispersOut, &time2DispersOut};
#endif
int numTerms;
double position, finalPosition, initialTime, initialVelocity, stepSize;
//set some initial conditions that will work with _INTERACTIVE_ turned off
position = 2;
initialVelocity = 1;
stepSize = 0.1;
initialTime = 0;
numTerms = 5;
finalPosition = 20;
#ifdef _DEBUG_
cout << waveApprox(2,1,1,5) << endl;
#endif
#ifdef _INTERACTIVE_
cout << "Interactive wave motion data generator\n";
cout << "Please enter the number of harmonics: ";
cin >> numTerms;
cout << "Please enter the initial position: ";
cin >> position;
cout << "Please enter the final position: ";
cin >> finalPosition;
cout << "Please enter the initial time: ";
cin >> initialTime;
cout << "Please enter the initial velocity: ";
cin >> initialVelocity;
cout << "Please enter the set size: ";
cin >> stepSize;
#ifdef _DEBUG_
cout << "Number of Terms: " << numTerms
<< " Initial Position: " << position
<< " Final Position: " << finalPosition
<< " Initial Time: " << initialTime
<< " Initial Velocity: " << initialVelocity
<< " Step size: " << stepSize;
#endif
#endif
#ifdef _SUPERPOSITIONEXAMPLE_
double tempPos = 0;
for(int i = 0; i <= finalPosition/stepSize; i++){
tempPos = i*stepSize;
for(int j = 0; j < 3; j++){
if(0 == j) numTerms = 5; else if(1 == j) numTerms = 20; else numTerms = 200;
*variableTerm_fileArray[j] << tempPos << " "
<< waveApprox(tempPos, initialVelocity, initialTime, numTerms)
<< endl;
}
}
#endif
#ifdef _PROPNONDISPERSIVE_
double tempPos = 0;
numTerms = 200;
for(int i = 0; i <= finalPosition/stepSize; i++){
tempPos = i*stepSize;
for(int j = 0; j < 3; j++){
if(0 == j) initialTime = 0; else if(1 == j) initialTime = 1; else initialTime = 2;
*variableTime_fileArray[j] << tempPos << " "
<< waveApprox(tempPos, initialVelocity, initialTime, numTerms)
<< endl;
}
}
#endif
#ifdef _PROPDISPERSIVE_
double tempPos = 0;
numTerms = 200;
for(int i = 0; i <= finalPosition/stepSize; i++){
tempPos = i*stepSize;
for(int j = 0; j < 3; j++){
if(0 == j) initialTime = 0; else if(1 == j) initialTime = 1; else initialTime = 2;
*variableTime_fileArray[j] << tempPos << " "
<< waveApproxDispersive(tempPos, initialVelocity, initialTime, numTerms)
<< endl;
}
}
#endif
cout << "That's all folks!\n";
return 0;
}
double waveApprox( double position, double velocity, double time, int numTerms){
double solution = 0;
int termCount = 0;
for (int i = 1; i <= 2*numTerms; i++){
if( 0 == ((i+1) % 2)){
solution += (1/(double)i) * sin(i * position - i * velocity * time);
termCount++;
}
}// Don't forget to add the other term and multiply by the weight factor
solution *= (4/PI);
solution += (4/PI);
#ifdef _DEBUG_
cout << "Number of terms: " << termCount << endl;
#endif
return solution;
}
double waveApproxDispersive( double position, double velocity, double time, int numTerms){
double solution = 0;
int termCount = 0;
for (int i = 1; i <= 2*numTerms; i++){
if( 0 == ((i+1) % 2)){
solution += (1/(double)i) * sin(i * position - i * (velocity*(1.1+01*i)) * time);
termCount++;
}
}// Don't forget to add the other term and multiply by the weight factor
solution *= (4/PI);
solution += (4/PI);
#ifdef _DEBUG_
cout << "Number of terms: " << termCount << endl;
#endif
return solution;
}