WCSPH,PCISPH,IISPH等研究方法,其本质都是以非压缩性为目标,求解Navier-Stokes方程。
本文以WCSPH为例,讲解下SPH方法代码的实现。
代码讲解
sph_type.h里定义几个vector函数类型
#ifndef _SPHTYPE_H
#define _SPHTYPE_H
typedef unsigned int uint;
struct Vec3_f
{
float x;
float y;
float z;
};
struct Vec3_i
{
int x;
int y;
int z;
};
struct uint3
{
uint x;
uint y;
uint z;
};
#endif //_SPHTYPE_H
sph_timer.h是一个时间循环函数,实现如下
#ifndef _SPHTIMER_H
#define _SPHTIMER_H
#include <windows.h>
class Timer
{
private:
int frames;
int update_time;
int last_time;
double FPS;
public:
Timer();
void update();
double get_fps();
};
#endif //_SPHTIMER_H
这是
sph_timer.cpp的具体内容
#include "sph_timer.h"
Timer::Timer()
{
frames=0;
update_time=1000;
last_time=0;
FPS=0;
}
void Timer::update()
{
frames++;
if(GetTickCount()-last_time > update_time)
{
FPS=((double)frames/(double)(GetTickCount()-last_time))*1000.0;
last_time=GetTickCount();
frames=0;
}
}
double Timer::get_fps()
{
return FPS;
}
sph_header.h包含了一些要用到的头文件
#ifndef _SPHHEADER_H
#define _SPHHEADER_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//#include "rx_utility.h"
#define PI 3.141592f
#define INF 1E-12f
#define BOUNDARY 0.0001f
#endif
sph_data.h包含了一些关于窗口的定义
#ifndef _SPHDATA_H
#define _SPHDATA_H
#include "sph_header.h"
#include "sph_type.h"
float window_width=1000;
float window_height=1000;
float xRot = 15.0f;
float yRot = 0.0f;
float xTrans = 0.0f;
float yTrans = 0.0f;
f