【CG物理模拟系列】流体模拟--粒子法之SPH(代码讲解)

这篇博客深入探讨了粒子法中的SPH( Smoothed Particle Hydrodynamics)方法,特别是WCSPH,用于非压缩性流体模拟。通过讲解WCSPH的代码实现,文章旨在帮助读者理解如何使用SPH解决Navier-Stokes方程,适用于游戏和其他交互式应用。
摘要由CSDN通过智能技术生成

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
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值