From 2a100a608f639ce1845af1b3e5508fb00796fb75 Mon Sep 17 00:00:00 2001 From: zoziha Date: Wed, 6 Dec 2023 00:16:32 +0800 Subject: [PATCH] mkdir --- app/sph.f90 | 4 ++- src/output/output_m.f90 | 13 ++++++++++ src/utils/filesystem.F90 | 54 ++++++++++++++++++++++++++++++++++++++++ src/utils/utils.f90 | 2 +- 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/utils/filesystem.F90 diff --git a/app/sph.f90 b/app/sph.f90 index 019d73e..3cda086 100644 --- a/app/sph.f90 +++ b/app/sph.f90 @@ -19,6 +19,7 @@ program sph use sph_kind, only: rk use parameter + use output_m, only: mkdirs implicit none !> 在模拟中所使用的粒子总数 @@ -41,13 +42,14 @@ program sph if (shocktube) dt = 0.005_rk if (shearcavity) dt = 5.e-5_rk call input(x, vx, mass, rho, p, u, itype, hsml, ntotal) + call mkdirs() 1 write (*, *) ' ***************************************************' write (*, *) ' please input the maximal time steps ' write (*, *) ' ***************************************************' read (*, *) maxtimestep call time_integration(x, vx, mass, rho, p, u, c, s, e, itype, hsml, ntotal, maxtimestep, dt) - !> 输出最后一个时间布的求解信息 + !> 输出最后一个时间步的求解信息 call output(x, vx, mass, rho, p, u, c, itype, hsml, ntotal) write (*, *) ' ***************************************************' diff --git a/src/output/output_m.f90 b/src/output/output_m.f90 index 7373083..05e78b0 100644 --- a/src/output/output_m.f90 +++ b/src/output/output_m.f90 @@ -2,6 +2,7 @@ module output_m use utils, only: to_string + use filesystem, only: exists, mkdir implicit none contains @@ -77,4 +78,16 @@ subroutine output_all(x, vx, mass, rho, p, u, c, itype, hsml, ntotal, n) end subroutine output_all + !> 创建文件夹 + subroutine mkdirs() + + if (.not. exists("data\\all", is_directory=.true.)) then + call mkdir('data\\all') + end if + if (.not. exists("data\\paraview", is_directory=.true.)) then + call mkdir('data\\paraview') + end if + + end subroutine mkdirs + end module output_m diff --git a/src/utils/filesystem.F90 b/src/utils/filesystem.F90 new file mode 100644 index 0000000..afaf4ed --- /dev/null +++ b/src/utils/filesystem.F90 @@ -0,0 +1,54 @@ +module filesystem + + implicit none + +contains + + !> 判断当前系统是否为 Windows 系统 + logical function is_windows() + character(16) :: os_name + logical, save :: is_windows_ = .false. + logical, save :: is_first_run = .true. + + if (is_first_run) then + call get_environment_variable("OS", os_name) + is_windows_ = trim(os_name) == "Windows_NT" + is_first_run = .false. + is_windows = is_windows_ + else + is_windows = is_windows_ + end if + + end function is_windows + + !> 创建目录 + subroutine mkdir(path) + character(*), intent(in) :: path !! 目录路径 + + if (is_windows()) then + call execute_command_line("md "//path) + else + call execute_command_line("mkdir -p "//path) + end if + + end subroutine mkdir + + !> 判断目录是否存在 + function exists(path, is_directory) + character(*), intent(in) :: path !! 目录路径 + logical, intent(in), optional :: is_directory !! 是否为目录 + logical :: exists + +#if defined __INTEL_COMPILER + if (present(is_directory)) then + if (is_directory) then + inquire (directory=path, exist=exists) + return + end if + end if +#endif + inquire (file=path, exist=exists) + + end function exists + +end module filesystem diff --git a/src/utils/utils.f90 b/src/utils/utils.f90 index 09599e8..99e7935 100644 --- a/src/utils/utils.f90 +++ b/src/utils/utils.f90 @@ -5,7 +5,7 @@ module utils implicit none contains - + ! SPDX-Identifier: MIT !> 将整数转化为字符串。借鉴了Fortran标准库的to_string。 !> Change integer to string.