main.cpp

  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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "declarations.h"
//#define _INTERACTIVE_
//#define _TEXOUTPUT_
#define _GNUPLOTOUTPUT_
double laplace2d(int x, int y, double** potential);
bool checkError(double oldPotential, double potential, double accuracy);
int main (int argc, char * const argv[]) {
//Set gnuplot path, comment out at home
Gnuplot::set_GNUPlotPath("/opt/local/bin");
//Open Files for editing
intermediateOut.open(intermediateFileName.c_str());
approxSolutionOut.open(approxSolutionFileName.c_str());
exactSolutionOut.open(exactSolutionFileName.c_str());
gnuplotOut.open(gnuplotFileName.c_str());
//Set output style
intermediateOut.setf(ios::showpoint|ios::fixed);
approxSolutionOut.setf(ios::showpoint|ios::fixed);
exactSolutionOut.setf(ios::showpoint|ios::fixed);
gnuplotOut.setf(ios::showpoint|ios::fixed);
//Set output file precision
intermediateOut.precision(2);
approxSolutionOut.precision(2);
exactSolutionOut.precision(2);
gnuplotOut.precision(2);
bool goodToGo = false;
//gnuplot objects for plots
Gnuplot surfacePlot("lines");
Gnuplot contourPlot("lines");
Gnuplot exactContourPlot("lines");
//seed random number
srand((unsigned)time(0));
#ifdef _INTERACTIVE_
//Collect paramaters from the user
cout << "Please enter a resolution: ";
cin >> resolution;
cout << endl << "Please enter an accuracy: ";
cin >> accuracy;
#endif
int arraySize = resolution + 2;
cout << endl << "Initilazing Potential Field." << endl;
//Initializing data arrays
//Final relaxation solution array
double** potential=new double*[arraySize];
for(i=0; i < arraySize; i++){
potential[i]=new double[arraySize];
for(j=0; j<arraySize; j++){
potential[i][j] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
}
//Intermediate relaxation temp array
double** oldPotential=new double*[arraySize];
for(i=0; i<arraySize; i++){
oldPotential[i]=new double[arraySize];
for(j=0; j<arraySize; j++){
oldPotential[i][j] = 0;
}
}
//Exact solution array
double** exactPotential=new double*[arraySize];
for(i=0; i<arraySize; i++){
exactPotential[i]=new double[arraySize];
for(j=0; j<arraySize; j++){
oldPotential[i][j] = 0;
}
}
//Set boundry conditions
for(i=0; i < arraySize; i++){
potential[16][i] = 100.00;
oldPotential[16][i] = 100.00;
}
for(j = 1; j < arraySize; j++){
potential[j][0] = 0.00;
oldPotential[j][0] = 0.00;
}
for(i = 0; i < arraySize; i++){
potential[0][i] = 0.00;
oldPotential[0][i] = 0.00;
}
for(j = 0; j < arraySize; j++){
potential[j][16] = 0.00;
oldPotential[j][16] = 0.00;
}
//data arrays ready to go
//calculate potential using laplaces equation
while(!goodToGo){
for(i = 1; i<= resolution; i++){
for(j = 1; j <= resolution; j++){
oldPotential[i][j] = potential[i][j];
potential[i][j] = laplace2d(i,j,potential);
}
}
//Determine if another iteration is needed
for(i = 1; i < resolution; i++){
for(j = 1; j < resolution; j++){
goodToGo = checkError(oldPotential[i][j], potential[i][j], accuracy);
if(!goodToGo){
break;
}
}
if(!goodToGo){
break;
}
}
}
//Calculate exact solution (first 225 terms)
double x = 0;
double y = 0;
for(i = 0; i < arraySize; i++){
for(j = 0; j < arraySize; j++){
for(int k = 0; k < 225; k++){
if( 0 == ((k+1) % 2)){
x = j/(double)(arraySize - 1);
y = i/(double)(arraySize - 1);
exactPotential[i][j] += (1/sinh(k*PI)) * sin((k*x*PI)) * sinh((k*y*PI))/k;
}
}
exactPotential[i][j] *= ((4*Vnot)/PI);
}
}
//output data to gnuplot data file
for(i = 0; i < arraySize; i++){
for(j=0; j < arraySize; j++){
gnuplotOut << potential[i][j] << " ";
}
gnuplotOut << endl;
}
#ifdef _GNUPLOTOUTPUT_
//Output for plotting
//Output exact solution
for(i = 0; i < arraySize; i++){
for(j = 0; j < arraySize; j++){
//Simple space delimited output.
exactSolutionOut << exactPotential[i][j] << " ";
}
exactSolutionOut << endl;
}
//Output approx solution
for(i = 0; i < arraySize; i++){
for(j = 0; j < arraySize; j++){
//Simple space delimited output.
approxSolutionOut << potential[i][j] << " ";
}
approxSolutionOut << endl;
}
//Output intermediate solution
for(i=resolution-1; i>=0; i--){
for(j=resolution-1; j>=0; j--){
//Simple space delimited output.
intermediateOut << oldPotential[i][j] << " ";
}
intermediateOut << endl;
}
#endif
//TeX Output -- Important! The indexs are switched around here
#ifdef _TEXOUTPUT_
//Output exact solution
for(i = 16; i >= 0; i--){
for(j = 0; j < arraySize; j++){
//TeX tabular output, plotting on this data will fail.
if(0 == j) exactSolutionOut << exactPotential[i][j];
else exactSolutionOut << exactPotential[i][j] << " & ";
}
//TeX tabular output, plotting on this data will fail.
exactSolutionOut << " \\\\ \\hline" << endl;
}
//Output approx solution
for(i = 16; i >= 0; i--){
for(j = 0; j < arraySize; j++){
//TeX tabular output, plotting on this data will fail.
if(0 == j) approxSolutionOut << potential[i][j];
else approxSolutionOut << potential[i][j] << " & ";
}
//TeX tabular output, plotting on this data will fail.
approxSolutionOut << " \\\\ \\hline" << endl;
}
//Output intermediate solution
for(i = 16; i >= 0; i--){
for(j=resolution-1; j>=0; j--){
//TeX tabular output, plotting on this data will fail.
if(0 == j) intermediateOut << oldPotential[i][j];
else intermediateOut << oldPotential[i][j] << " & ";
}
//TeX tabular output, plotting on this data will fail.
intermediateOut << " \\\\ \\hline" << endl;
}
#endif
#ifdef _GNUPLOTOUTPUT_
//This plot should be commented out if the latex output is turned on
//Plot exact solution contour plot, using gnuplot exceptions
try{
exactContourPlot.cmd("set ticslevel 0");
//exactContourPlot.cmd("set view 70, 120");
exactContourPlot.cmd("set zlabel \"V(x,y)\" 5,1");
exactContourPlot.set_xlabel("x");
exactContourPlot.set_ylabel("y");
//exactContourPlot.set_zlabel("\"V(x,y)\" 0,1");
exactContourPlot.set_xrange(0.0, 16.00);
exactContourPlot.set_yrange(0.0, 16.00);
exactContourPlot.cmd(("splot \"" + exactSolutionFileName + "\" matrix title \"Exact Solution\" with lines"));
}
catch (GnuplotException ge){
cout << ge.what() << endl;
}
#endif
//Plot surface plot, using gnuplot exceptions
try{
surfacePlot.cmd("set ticslevel 0");
surfacePlot.cmd("set zlabel \"V(x,y)\" 5,1");
surfacePlot.set_xlabel("x");
surfacePlot.set_ylabel("y");
surfacePlot.set_xrange(0.0, 16.00);
surfacePlot.set_yrange(0.0, 16.00);
surfacePlot.cmd(("splot \"" + gnuplotFileName + "\" matrix title \"Potential\" with lines"));
}
catch (GnuplotException ge){
cout << ge.what() << endl;
}
//Plot equipotential lines, using gnuplot exceptions
try{
contourPlot.set_contour("base");
contourPlot.cmd("set title \"Equipotential Lines\"");
contourPlot.cmd("set view map");
contourPlot.cmd("unset surface");
contourPlot.cmd("set cntrparam bspline");
contourPlot.cmd("set cntrparam levels disc 40,70,90");
contourPlot.cmd("set zlabel \"V(x,y)\" 5,1");
contourPlot.set_xlabel("x");
contourPlot.set_ylabel("y");
contourPlot.set_xrange(0.0, 16.00);
contourPlot.set_yrange(0.0, 16.00);
contourPlot.cmd(("splot \"" + gnuplotFileName + "\" matrix with lines"));
}
catch (GnuplotException ge){
cout << ge.what() << endl;
}
cout << endl << "That's all folks" << endl;
return 0;
}
//function to approximate the solution to the laplace equation around a point x,y
double laplace2d(int x, int y, double** potential){
return(.25*(potential[x+1][y]+potential[x-1][y]+potential[x][y+1]+potential[x][y-1]));
}
//Function to check the error between each iteration
bool checkError(double oldPotential, double potential, double accuracy){
if (abs((oldPotential - potential))/oldPotential <= accuracy){
return true;
}
else{
return false;
}
}