📄 grid.cpp
字号:
#include <assert.h>
#include <vector>
#include <list>
#include "grid.h"
using namespace std;
const node node::dummy(0,0,0,0);
grid::grid():array(0),prev_array(0)
{
memset(bound_arrays,0,sizeof(bound_arrays));
}
grid::~grid(){
delete[] array;
delete[] prev_array;
for(int i = 0; i < 4; i++)delete[] bound_arrays[i];
}
void grid::set_dims(const rect<int> &r,real _dx,real _dy,const node &init)
{
bound = r;
dx = _dx;
dy = _dy;
width = (bound.b.x - bound.a.x);
height = (bound.b.y - bound.a.y);
int size = width*height;
delete[] array;
delete[] prev_array;
array = new node[size];
prev_array = new node[size];
for(int i = 0; i < 4; i++)delete[] bound_arrays[i];
bound_arrays[left] = new node[height];
bound_arrays[right] = new node[height];
bound_arrays[top] = new node[width];
bound_arrays[bottom] = new node[width];
for(int i = 0; i < width*height; i++)array[i] = prev_array[i] = init;
mass = init.rho*width*height;
minvalue = maxvalue = init;
}
void grid::set_cond( const bound_cond &n )
{
left_cond = right_cond = top_cond = bottom_cond = n;
}
const node* grid::from_local_coords(int x,int y) const
{
if(x >= 0 && x < width && y >= 0 && y < height)
return prev_array + x + y*width;
int inner_x = x, inner_y = y;
if(x < 0){
inner_x = 0;
}else if(x >= width){
inner_x = width - 1;
}
if(y < 0){
inner_y = 0;
}else if(y >= height){
inner_y = height - 1;
}
if(x < 0){
return &(bound_arrays[left][inner_y] =
left_cond.get(prev_array[1 + inner_y*width]));
}
if(x >= width){
return &(bound_arrays[right][inner_y] =
right_cond.get(prev_array[width - 1 + inner_y*width]));
}
if(y < 0){
return &(bound_arrays[top][inner_x] =
top_cond.get(prev_array[inner_x]));
}
if(y >= height){
return &(bound_arrays[bottom][inner_x] =
bottom_cond.get(prev_array[inner_x + width*(height-1)]));
}
assert(false);
return &node::dummy;
}
const node* grid::from_global_coords(int x,int y) const
{
int a = x - bound.a.x;
int b = y - bound.a.y;
if(a >= 0 && a < width && b >= 0 && b < height)
return prev_array + a + b*width;
//assert(false);
return &node::dummy;
}
void grid::update_minmax()
{
minvalue.u = maxvalue.u = array[0].velocity();
minvalue.rho = maxvalue.rho = array[0].rho;
minvalue.T = maxvalue.T = array[0].T;
mass = 0;
for(int y = 0; y < height; y++)for(int x = 0; x < width; x++){
const node *p = array + x + y*width;
if(p->rho > maxvalue.rho)maxvalue.rho = p->rho;
if(p->rho < minvalue.rho)minvalue.rho = p->rho;
if(p->T > maxvalue.T)maxvalue.T = p->T;
if(p->T < minvalue.T)minvalue.T = p->T;
real l = p->velocity();
if(l > maxvalue.u)maxvalue.u = l;
if(l < minvalue.u)minvalue.u = l;
mass += p->rho;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -