Skip to content

Commit 01d2c7d

Browse files
committed
初始化仓库。
0 parents  commit 01d2c7d

38 files changed

+12987
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build/
2+
*.o
3+
*.a
4+
*.x

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Fortran sph Makefile
2+
3+
LIB = sph
4+
5+
FC = gfortran
6+
FFLAGS = -g
7+
8+
export LIB
9+
export FC
10+
export FFLAGS
11+
12+
.PHONY: all clean test
13+
14+
all:
15+
$(MAKE) -f Makefile --directory=src
16+
$(MAKE) -f Makefile --directory=app
17+
$(MAKE) -f Makefile --directory=test
18+
19+
test:
20+
$(MAKE) -f Makefile --directory=test
21+
22+
clean:
23+
$(MAKE) -f Makefile clean --directory=src
24+
$(MAKE) -f Makefile clean --directory=app
25+
$(MAKE) -f Makefile clean --directory=test

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 光滑粒子流体动力学(SPH)
2+
3+
一份社区驱动的开源光滑粒子流体动力学(SPH)代码,起始代码版本源自课本《光滑粒子流体动力学--一种无网格粒子法》。
4+
5+
| 项目 | 描述 |
6+
| --- | --- |
7+
| 版本 | 0.0.1 |
8+
| 许可证 | Public Domain |
9+
| 版权 | Copyright (c) 2021 SPH 贡献者 |
10+
11+
## 开始
12+
13+
### 获取代码
14+
15+
```sh
16+
git clone https://github.com/zoziha/SPH.git
17+
cd SPH
18+
```
19+
20+
### 使用Make构建代码
21+
22+
本项目支持Make工具构建代码:
23+
24+
```sh
25+
make
26+
```
27+
28+
### 使用[fortran-lang/fpm](https://github.com/fortran-lang/fpm)构建代码
29+
30+
FPM是社区驱动的Fortran语言的包管理器和代码构建工具,适用于c/c++/fortran代码的构建。
31+
你可以通过提供的`fpm.toml`构建代码:
32+
33+
```sh
34+
fpm build
35+
fpm run
36+
```
37+
38+
## 链接
39+
40+
+ [spheric/SPH Codes](https://spheric-sph.org/sph-projects-and-codes)

app/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all: sph
2+
3+
# Orginal test
4+
sph: sph.o
5+
$(FC) $(FFLAGS) $< -L../src -l$(LIB) -I../src -o $@.x
6+
cd .. && time ./app/sph.x

app/sph.f

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
program SPH
2+
3+
c----------------------------------------------------------------------
4+
c This is a three dimensional SPH code. the followings are the
5+
c basic parameters needed in this code or calculated by this code
6+
7+
c mass-- mass of particles [in]
8+
c ntotal-- total particle number ues [in]
9+
c dt--- Time step used in the time integration [in]
10+
c itype-- types of particles [in]
11+
c x-- coordinates of particles [in/out]
12+
c vx-- velocities of particles [in/out]
13+
c rho-- densities of particles [in/out]
14+
c p-- pressure of particles [in/out]
15+
c u-- internal energy of particles [in/out]
16+
c hsml-- smoothing lengths of particles [in/out]
17+
c c-- sound velocity of particles [out]
18+
c s-- entropy of particles [out]
19+
c e-- total energy of particles [out]
20+
21+
implicit none
22+
include '../src/param.inc'
23+
24+
integer ntotal, itype(maxn), maxtimestep, d, m, i, yesorno
25+
double precision x(dim,maxn), vx(dim, maxn), mass(maxn),rho(maxn),
26+
& p(maxn), u(maxn), c(maxn), s(maxn), e(maxn), hsml(maxn), dt
27+
double precision s1, s2
28+
29+
call time_print
30+
call time_elapsed(s1)
31+
32+
if (shocktube) dt = 0.005
33+
if (shearcavity) dt = 5.e-5
34+
call input(x, vx, mass, rho, p, u, itype, hsml, ntotal)
35+
1 write(*,*)' ***************************************************'
36+
write(*,*)' Please input the maximal time steps '
37+
write(*,*)' ***************************************************'
38+
read(*,*) maxtimestep
39+
call time_integration(x, vx, mass, rho, p, u, c, s, e, itype,
40+
& hsml, ntotal, maxtimestep, dt )
41+
call output(x, vx, mass, rho, p, u, c, itype, hsml, ntotal)
42+
write(*,*)' ***************************************************'
43+
write(*,*) 'Are you going to run more time steps ? (0=No, 1=yes)'
44+
write(*,*)' ***************************************************'
45+
read (*,*) yesorno
46+
if (yesorno.ne.0) go to 1
47+
call time_print
48+
call time_elapsed(s2)
49+
write (*,*)' Elapsed CPU time = ', s2-s1
50+
write (*,*)'all finish!'
51+
52+
end

fpm.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name = "sph_code"
2+
3+
[build]
4+
auto-executables = false
5+
auto-tests = false
6+
auto-examples = false
7+
8+
[library]
9+
include-dir = "src"
10+
11+
[[executable]]
12+
name = "sph"
13+
source-dir = "app"
14+
main = "sph.f"

src/Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.SUFFIXES: .f90 .o
2+
3+
SRCF = \
4+
art_heat.f \
5+
art_visc.f \
6+
av_vel.f \
7+
density.f \
8+
direct_find.f \
9+
eos.f \
10+
external_force.f \
11+
grid_geom.f \
12+
hsml.f \
13+
init_grid.f \
14+
input.f \
15+
internal_force.f \
16+
kernel.f \
17+
link_list.f \
18+
output.f \
19+
single_step.f \
20+
time_integration.f \
21+
virt_part.f \
22+
viscosity.f
23+
24+
SRCF90 = \
25+
time_elapsed.f90 \
26+
time_print.f90
27+
28+
OBJF := $(SRCF:.f=.o)
29+
OBJF90 := $(SRCF90:.f90=.o)
30+
31+
lib$(LIB).a: $(OBJF) $(OBJF90)
32+
ar -rcs lib$(LIB).a $(OBJF) $(OBJF90)
33+
34+
clean:
35+
rm -f -r *.o *.a *.so *.mod *.smod
36+
37+
%.o: %.f90
38+
$(FC) $(FFLAGS) -c $<
39+
40+
time_elapsed.o: time_elapsed.f90
41+
time_print.o : time_print.f90

src/art_heat.f

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
subroutine art_heat(ntotal,hsml,mass,x,vx,niac,rho,u,
2+
& c,pair_i,pair_j,w,dwdx,dedt)
3+
4+
c----------------------------------------------------------------------
5+
c Subroutine to calculate the artificial heat(Fulk, 1994, p, a-17)
6+
c See Equ.(4.74)
7+
8+
c ntotal : Number of particles [in]
9+
c hsml : Smoothing Length [in]
10+
c mass : Particle masses [in]
11+
c x : Coordinates of all particles [in]
12+
c vx : Velocities of all particles [in]
13+
c rho : Density [in]
14+
c u : specific internal energy [in]
15+
c c : Sound veolcity [in]
16+
c niac : Number of interaction pairs [in]
17+
c pair_i : List of first partner of interaction pair [in]
18+
c pair_j : List of second partner of interaction pair [in]
19+
c w : Kernel for all interaction pairs [in]
20+
c dwdx : Derivative of kernel with respect to x, y and z [in]
21+
c dedt : produced artificial heat, adding to energy Eq. [out]
22+
23+
implicit none
24+
include 'param.inc'
25+
26+
integer ntotal,niac,pair_i(max_interaction),
27+
& pair_j(max_interaction)
28+
double precision hsml(maxn), mass(maxn), x(dim,maxn),vx(dim,maxn),
29+
& rho(maxn), u(maxn), c(maxn),w(max_interaction),
30+
& dwdx(dim,max_interaction), dedt(maxn)
31+
integer i,j,k,d
32+
double precision dx, dvx(dim), vr, rr, h, mc, mrho, mhsml,
33+
& vcc(maxn), hvcc, mui, muj, muij, rdwdx, g1,g2
34+
35+
36+
c--- Parameter for the artificial heat conduction:
37+
38+
g1=0.1
39+
g2=1.0
40+
do i=1,ntotal
41+
vcc(i) = 0.e0
42+
dedt(i) = 0.e0
43+
enddo
44+
45+
do k=1,niac
46+
i = pair_i(k)
47+
j = pair_j(k)
48+
do d=1,dim
49+
dvx(d) = vx(d,j) - vx(d,i)
50+
enddo
51+
hvcc = dvx(1)*dwdx(1,k)
52+
do d=2,dim
53+
hvcc = hvcc + dvx(d)*dwdx(d,k)
54+
enddo
55+
vcc(i) = vcc(i) + mass(j)*hvcc/rho(j)
56+
vcc(j) = vcc(j) + mass(i)*hvcc/rho(i)
57+
enddo
58+
59+
do k=1,niac
60+
i = pair_i(k)
61+
j = pair_j(k)
62+
mhsml= (hsml(i)+hsml(j))/2.
63+
mrho = 0.5e0*(rho(i) + rho(j))
64+
rr = 0.e0
65+
rdwdx = 0.e0
66+
do d=1,dim
67+
dx = x(d,i) - x(d,j)
68+
rr = rr + dx*dx
69+
rdwdx = rdwdx + dx*dwdx(d,k)
70+
enddo
71+
mui=g1*hsml(i)*c(i) + g2*hsml(i)**2*(abs(vcc(i))-vcc(i))
72+
muj=g1*hsml(j)*c(j) + g2*hsml(j)**2*(abs(vcc(j))-vcc(j))
73+
muij= 0.5*(mui+muj)
74+
h = muij/(mrho*(rr+0.01*mhsml**2))*rdwdx
75+
dedt(i) = dedt(i) + mass(j)*h*(u(i)-u(j))
76+
dedt(j) = dedt(j) + mass(i)*h*(u(j)-u(i))
77+
enddo
78+
79+
do i=1,ntotal
80+
dedt(i) = 2.0e0*dedt(i)
81+
enddo
82+
83+
end

src/art_visc.f

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
subroutine art_visc(ntotal,hsml,mass,x,vx,niac,rho,c,
2+
& pair_i,pair_j,w,dwdx,dvxdt,dedt)
3+
4+
c----------------------------------------------------------------------
5+
c Subroutine to calculate the artificial viscosity (Monaghan, 1992)
6+
c See Equ.(4.66) Equ.(4.62)
7+
8+
c ntotal : Number of particles (including virtual particles) [in]
9+
c hsml : Smoothing Length [in]
10+
c mass : Particle masses [in]
11+
c x : Coordinates of all particles [in]
12+
c vx : Velocities of all particles [in]
13+
c niac : Number of interaction pairs [in]
14+
c rho : Density [in]
15+
c c : Temperature [in]
16+
c pair_i : List of first partner of interaction pair [in]
17+
c pair_j : List of second partner of interaction pair [in]
18+
c w : Kernel for all interaction pairs [in]
19+
c dwdx : Derivative of kernel with respect to x, y and z [in]
20+
c dvxdt : Acceleration with respect to x, y and z [out]
21+
c dedt : Change of specific internal energy [out]
22+
23+
implicit none
24+
include 'param.inc'
25+
26+
integer ntotal, niac, pair_i(max_interaction),
27+
& pair_j(max_interaction)
28+
double precision hsml(maxn), mass(maxn), x(dim,maxn),vx(dim,maxn),
29+
& rho(maxn), c(maxn), w(max_interaction),
30+
& dwdx(dim,max_interaction), dvxdt(dim,maxn), dedt(maxn)
31+
integer i,j,k,d
32+
double precision dx, dvx(dim), alpha, beta, etq, piv,
33+
& muv, vr, rr, h, mc, mrho, mhsml
34+
35+
c Parameter for the artificial viscosity:
36+
c Shear viscosity
37+
parameter( alpha = 1.e0 )
38+
39+
c Bulk viscosity
40+
parameter( beta = 1.e0 )
41+
42+
c Parameter to avoid singularities
43+
parameter( etq = 0.1e0 )
44+
45+
do i=1,ntotal
46+
do d=1,dim
47+
dvxdt(d,i) = 0.e0
48+
enddo
49+
dedt(i) = 0.e0
50+
enddo
51+
52+
c Calculate SPH sum for artificial viscosity
53+
54+
do k=1,niac
55+
i = pair_i(k)
56+
j = pair_j(k)
57+
mhsml= (hsml(i)+hsml(j))/2.
58+
vr = 0.e0
59+
rr = 0.e0
60+
do d=1,dim
61+
dvx(d) = vx(d,i) - vx(d,j)
62+
dx = x(d,i) - x(d,j)
63+
vr = vr + dvx(d)*dx
64+
rr = rr + dx*dx
65+
enddo
66+
67+
c Artificial viscous force only if v_ij * r_ij < 0
68+
69+
if (vr.lt.0.e0) then
70+
71+
c Calculate muv_ij = hsml v_ij * r_ij / ( r_ij^2 + hsml^2 etq^2 )
72+
73+
muv = mhsml*vr/(rr + mhsml*mhsml*etq*etq)
74+
75+
c Calculate PIv_ij = (-alpha muv_ij c_ij + beta muv_ij^2) / rho_ij
76+
77+
mc = 0.5e0*(c(i) + c(j))
78+
mrho = 0.5e0*(rho(i) + rho(j))
79+
piv = (beta*muv - alpha*mc)*muv/mrho
80+
81+
c Calculate SPH sum for artificial viscous force
82+
83+
do d=1,dim
84+
h = -piv*dwdx(d,k)
85+
dvxdt(d,i) = dvxdt(d,i) + mass(j)*h
86+
dvxdt(d,j) = dvxdt(d,j) - mass(i)*h
87+
dedt(i) = dedt(i) - mass(j)*dvx(d)*h
88+
dedt(j) = dedt(j) - mass(i)*dvx(d)*h
89+
enddo
90+
endif
91+
enddo
92+
93+
c Change of specific internal energy:
94+
95+
do i=1,ntotal
96+
dedt(i) = 0.5e0*dedt(i)
97+
enddo
98+
99+
end

0 commit comments

Comments
 (0)