Repository navigation
Expand file tree
/
Copy pathgrid_omp.cc
More file actions
158 lines (143 loc) · 4.21 KB
/
Copy pathgrid_omp.cc
File metadata and controls
158 lines (143 loc) · 4.21 KB
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
#include <stdio.h>
#include <math.h>
#include "grid.h"
//implementation of Euler that works on a square grid
Grid::Grid(int nside_x, int nside_y, double x_min, double y_min, double x_max, double y_max)
: dimen_x_(nside_x),
dimen_y_(nside_y),
min_x_(x_min),
min_y_(y_min),
max_x_(x_max),
max_y_(y_max),
dx_((x_max-x_min)/(nside_x-1.)),
dy_((y_max-y_min)/(nside_y-1.)) {
grid_ = new double*[dimen_x_];
grid_[0] = new double[dimen_x_*dimen_y_];
for (int i = 1; i < dimen_x_; i++){
grid_[i] = grid_[i-1] + dimen_y_;
}
//initialize all to zero by default, initialize edges for T in a separate function
#pragma omp parallel for default(none)
for(int i=0; i<dimen_x_; i++){
for(int j=0; j<dimen_y_; j++){
grid_[i][j]=0.;
}
}
}
Grid::~Grid() {
delete[] grid_[0];
delete[] grid_;
}
//i is x index, j is y index
double Grid::Get(int i, int j)const {
//check i, i in correct range
return grid_[i][j];
}
int Grid::Set(int i, int j, double val){
//check i, i in correct range
grid_[i][j]=val;
return 0;
}
int Grid::SetYColumn(int i, double *val){
//check i, i in correct range
#pragma omp parallel for default(none) shared(val, i)
for(int j=0; j<dimen_y_; j++){
grid_[i][j]=val[j];
}
return 0;
}
//i is x index, j is y index
int Grid::GetYColumn(int i, double *val)const { //sets val to column
//check i, i in correct range
#pragma omp parallel for default(none) shared(val, i)
for(int j=0; j<dimen_y_; j++){
val[j]=grid_[i][j];
}
return 0;
}
double Grid::GetMean()const {
//check i, i in correct range
//calculate mean temp
double sum=0;
#pragma omp parallel for default(none) reduction(+:sum)
for(int i=0; i<dimen_x_; i++){
for(int j=0; j<dimen_y_; j++){
sum+=grid_[i][j];
}
}
double mean=sum/(dimen_x_*dimen_y_);
return mean;
}
int Grid::InitializeTEdges(){
double x;
#pragma omp parallel for default(none) private(x)
for(int i=0; i<dimen_x_; i++){
x=i*dx_+min_x_;
grid_[i][0]=cos(x)*cos(x);
grid_[i][dimen_y_-1]=sin(x)*sin(x);
}
return 0;
}
int Grid::MultiplyByConstant(double c){
#pragma omp parallel for default(none) shared(c)
for(int i=0; i<dimen_x_; i++){
for(int j=0; j<dimen_y_; j++){
grid_[i][j]*=c;
}
}
return 0;
}
int Grid::AddGridTimesConst(double c, Grid &grid2){
#pragma omp parallel for default(none) shared(c, grid2)
for(int i=0; i<dimen_x_; i++){
for(int j=0; j<dimen_y_; j++){
grid_[i][j]+=c*grid2.Get(i,j);
}
}
return 0;
}
int Grid::GradSq(Grid &grad_sq_T)const {
double f;
#pragma omp parallel for default(none) shared(grad_sq_T) private(f)
for(int i=0; i<dimen_x_; i++){
for(int j=0; j<dimen_y_; j++){
//grad^2 T using finite differences for spatial gradient, 2 or 3 sided differences at corners/edges of grid
if(i>0 && j>0 && i<dimen_x_-1 && j<dimen_y_-1){
f= (grid_[i-1][j]+grid_[i+1][j]+grid_[i][j-1]+grid_[i][j+1]-4*grid_[i][j])/(dx_*dx_);
} else if(i==0 and j==0){ //should I take periodicity into account?
f= (grid_[i+1][j]+grid_[i][j+1]-2*grid_[i][j])/(dx_*dx_);
} else if(i==0 and j==dimen_y_-1){
f= (grid_[i+1][j]+grid_[i][j-1]-2*grid_[i][j])/(dx_*dx_);
} else if(i==dimen_x_-1 and j==0){
f= (grid_[i-1][j]+grid_[i][j+1]-2*grid_[i][j])/(dx_*dx_);
} else if (i==dimen_x_-1 and j==dimen_y_-1){
f= (grid_[i-1][j]+grid_[i][j-1]-2*grid_[i][j])/(dx_*dx_);
} else if(i==0){
f= (grid_[i+1][j]+grid_[i][j-1]+grid_[i][j+1]-3*grid_[i][j])/(dx_*dx_);
} else if(i==dimen_x_-1){
f= (grid_[i-1][j]+grid_[i][j-1]+grid_[i][j+1]-3*grid_[i][j])/(dx_*dx_);
} else if(j==0){
f= (grid_[i-1][j]+grid_[i+1][j]+grid_[i][j+1]-3*grid_[i][j])/(dx_*dx_);
} else if(j==dimen_y_-1){
f= (grid_[i-1][j]+grid_[i+1][j]+grid_[i][j-1]-3*grid_[i][j])/(dx_*dx_);
}
grad_sq_T.Set(i,j,f);
}
}
return 0;
}
int Grid::WriteToFile(const char *fname) const{
FILE *f_out=fopen(fname,"w");
if (f_out == 0){
printf("failed to open file\n");
return 1;
}
for(int i=0; i<dimen_x_; i++){
for(int j=0; j<dimen_y_; j++){
fprintf(f_out, "%15.8f", grid_[i][j]);
}
fprintf(f_out, "\n");
}
fclose(f_out);
return 0;
}