diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..bb07c5c3d8d3de8af4cc69bc5e46a8f793e2a159 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.15) +project(scm_io) + +set(CMAKE_CXX_STANDARD 14) + +include_directories(src) + +enable_language(Fortran) +set(dialect " -free -fimplicit-none ") +set(bounds "-free -fimplicit-none ") +set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} ${bounds} -g -fbacktrace -O0 -fcheck=all") +set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${dialect}") + +set(plt_io + src/scm_io_plt.f90 + src/test_io_plt.f90) + +add_executable(test_plt ${plt_io}) diff --git a/src/scm_io_plt.f90 b/src/scm_io_plt.f90 new file mode 100644 index 0000000000000000000000000000000000000000..587b0a5605a4a6d3e02038a871d8144ca17d2f02 --- /dev/null +++ b/src/scm_io_plt.f90 @@ -0,0 +1,94 @@ +! Created by Andrey Debolskiy on 21.05.2024. + +module scm_io_plt + implicit none + !type declaration + type io_struct + character(len = 160) :: fname + integer :: status ! 0 - closed, 1 - opened + integer :: unit_id + end type io_struct + + character(len = 160) tmp_str + integer, public :: nunits_max + public + +contains + subroutine set_file( f, fname ) + implicit none + type(io_struct), intent(inout):: f + character(*), intent(in):: fname + + if (f%status == 0) then + f%unit_id = get_file_unit() + open(f%unit_id, FILE=trim(fname)) + f%status = 1 + f%fname = trim(fname) + end if + end subroutine set_file + + subroutine write_series(stamp, vals, nlength, f) + implicit none + type(io_struct), intent(inout):: f + integer, intent(in):: nlength + real, intent(in), dimension(nlength)::vals + real, intent(in):: stamp + + write(f%unit_id,*) stamp, vals(:) + end subroutine write_series + + subroutine write_profile(profs, nprof, z, nz, f) + implicit none + + type(io_struct), intent(inout):: f + integer, intent(in):: nprof, nz + real, intent(in), dimension(nz, nprof):: profs + real, intent(in), dimension(nz):: z + integer k, n + + if (f%status==1) then + do k = 1,nz + write(f%unit_id,*) z(k), profs(k,:) + end do + else + write(*,*) "Error in writing file:" + write(*,*) " ", f%fname + stop + end if + end subroutine write_profile + + subroutine close_file(f) + implicit none + type(io_struct), intent(inout):: f + + close(f%unit_id) + f%status = 0 + end subroutine close_file + + ! get_file_unit returns a unit number that is not in use + integer function get_file_unit () + integer lu, iostat + integer, save:: m + logical, save:: initialized = .true. + logical opened + + if (initialized) then + m = nunits_max + initialized = .false. + end if + + if (m < 8 ) then + m = 2 * nunits_max + end if + + do lu = m,7,-1 + inquire (unit=lu, opened=opened, iostat=iostat) + if (iostat.ne.0) cycle + if (.not.opened) exit + end do + ! + get_file_unit = lu + return + end function get_file_unit + +end module scm_io_plt \ No newline at end of file diff --git a/src/test_io_plt.f90 b/src/test_io_plt.f90 new file mode 100644 index 0000000000000000000000000000000000000000..da4e779dbed7af65c1d6e5b19b2b3420a0c616cc --- /dev/null +++ b/src/test_io_plt.f90 @@ -0,0 +1,29 @@ +! Created by Andrey Debolskiy on 21.05.2024. + +program test_io_plt + use scm_io_plt + implicit none + integer, parameter :: nvar=5, nlength=10 + + !io variables + real, dimension (nvar):: series_val + type (io_struct) :: series_f + + !local vars + integer nt, n + real t + + !set output filenames + series_f%fname = 'test_io.plt' + call set_file(series_f, series_f%fname) + + do nt=1,nlength + t = nt/100.0 + do n=1,nvar + series_val(n)= 0.1 * n + end do + call write_series(t, series_val, nvar, series_f) + end do + call close_file(series_f) + +end program test_io_plt \ No newline at end of file