-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoving_frame.c
executable file
·166 lines (152 loc) · 4.34 KB
/
moving_frame.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lib.h"
void moving_frame_zero(moving_frame* moving_frame)
{
int i, j;
for(i=0;i<moving_frame->GRID_X;i++)
{
for(j=0;j<moving_frame->GRID_Y;j++)
{
moving_frame->elements[i*moving_frame->GRID_Y+j]=0;
}
}
for(i=0;i<2;i++)
{
for(j=0;j<moving_frame->GRID_Y;j++)
{
moving_frame->guard_cells[i*moving_frame->GRID_Y+j]=0;
}
}
}
moving_frame* moving_frame_create(int GRID_X, int GRID_Y)
{
int i,j;
moving_frame* new_moving_frame=(moving_frame*)malloc(sizeof(moving_frame));
new_moving_frame->GRID_X=GRID_X;
new_moving_frame->GRID_Y=GRID_Y;
new_moving_frame->elements=(double*)malloc((GRID_X*GRID_Y)*sizeof(double));
new_moving_frame->guard_cells=(double*)malloc(4*GRID_Y*sizeof(double));
new_moving_frame->move=0;
moving_frame_zero(new_moving_frame);
return new_moving_frame;
}
double moving_frame_get(moving_frame* moving_frame, int lab_x, int lab_y)
{
if(lab_x<moving_frame->move || lab_x>=(moving_frame->move+moving_frame->GRID_X))
{
int y_position=lab_y;
if(lab_y>moving_frame->GRID_Y-1)
y_position-=moving_frame->GRID_Y;
if(lab_y<0)
y_position+=moving_frame->GRID_Y;
if(lab_x<moving_frame->move)
return moving_frame->guard_cells[lab_y];
else
return moving_frame->guard_cells[moving_frame->GRID_Y+lab_y];
}
else
{
int x_position=lab_x%moving_frame->GRID_X;
int y_position=lab_y;
if(lab_y>=moving_frame->GRID_Y)
y_position-=moving_frame->GRID_Y;
if(lab_y<0)
y_position+=moving_frame->GRID_Y;
return moving_frame->elements[x_position*(moving_frame->GRID_Y)+y_position];
}
}
void moving_frame_set(moving_frame* moving_frame, int lab_x, int lab_y, double value)
{
if(lab_x<moving_frame->move || lab_x>=(moving_frame->move+moving_frame->GRID_X))
{
}
else
{
int x_position=lab_x%(moving_frame->GRID_X);
int y_position=lab_y;
if(lab_y>=moving_frame->GRID_Y)
y_position-=moving_frame->GRID_Y;
if(lab_y<0)
y_position+=moving_frame->GRID_Y;
moving_frame->elements[x_position*(moving_frame->GRID_Y)+y_position]=value;
}
}
int moving_frame_get_move(moving_frame* moving_frame)
{
return moving_frame->move;
}
void moving_frame_move(moving_frame* moving_frame)
{
int j;
for(j=0;j<moving_frame->GRID_Y;j++)
{
moving_frame->elements[(((moving_frame->move)%(moving_frame->GRID_X)))*(moving_frame->GRID_Y)+j]=0;
}
(moving_frame->move)++;
}
void moving_frame_print(moving_frame* moving_frame, char* pattern, int counter)
{
char filename[30];
sprintf(filename, pattern, counter);
FILE *fp;
fp=fopen(filename, "w");
int i, j;
for(j=0;j<moving_frame->GRID_Y;j++)
{
for(i=moving_frame->move;i<(moving_frame->GRID_X+moving_frame->move);i++)
{
fprintf(fp, "%10.8f ", moving_frame_get(moving_frame, i, j));
}
fprintf(fp, "\n");
}
fclose(fp);
}
void moving_frame_print_charge(moving_frame* moving_frame, char* pattern, int counter)
{
char filename[30];
sprintf(filename, pattern, counter);
FILE *fp;
fp=fopen(filename, "w");
int i, j;
for(j=0;j<moving_frame->GRID_Y;j++)
{
for(i=0;i<moving_frame->GRID_X;i++)
{
fprintf(fp, "%10.8f ", moving_frame->elements[i*(moving_frame->GRID_Y)+j]);
}
fprintf(fp, "\n");
}
fclose(fp);
}
void moving_frame_set_charge(moving_frame* moving_frame, int lab_x, int lab_y, double value)
{
int x_position=lab_x-(moving_frame->move);
int y_position=lab_y;
moving_frame->elements[x_position*(moving_frame->GRID_Y)+y_position]=value;
}
double moving_frame_get_charge(moving_frame* moving_frame, int lab_x, int lab_y)
{
int x_position=lab_x-moving_frame->move;
int y_position=lab_y;
return moving_frame->elements[x_position*(moving_frame->GRID_Y)+y_position];
}
void moving_frame_get_guard_cells(moving_frame* moving_frame)
{
int j;
for(j=0;j<moving_frame->GRID_Y;j++)
{
moving_frame->guard_cells[2*moving_frame->GRID_Y+j]=moving_frame->elements[(moving_frame->move % moving_frame->GRID_X)*moving_frame->GRID_Y+j];
moving_frame->guard_cells[3*moving_frame->GRID_Y+j]=moving_frame->elements[((moving_frame->GRID_X-1+moving_frame->move) % moving_frame->GRID_X)*moving_frame->GRID_Y+j];
}
}
void moving_frame_set_guard_cells(moving_frame* moving_frame)
{
int j;
for(j=0;j<moving_frame->GRID_Y;j++)
{
moving_frame->guard_cells[j]=moving_frame->guard_cells[2*moving_frame->GRID_Y+j];
moving_frame->guard_cells[moving_frame->GRID_Y+j]=moving_frame->guard_cells[3*moving_frame->GRID_Y+j];
}
}