字符串连接在Fortran中不起作用

问题描述

我有一个子例程,该子例程将字符串连接起来以创建文件名,因此我可以跟踪非线性迭代中的解决方案更改。但是,它似乎不起作用。我之前使用此代码段没有任何问题,所以我想知道这是否依赖某些编译器标志吗?字符串连接部分位于底部,我返回以下内容

output_name:plt / Res 文件名1: plt / Res
文件名2: plt / Res
写入文件... 0

清楚表明我的字符串未连接。 我用以下方式调用子例程:

   IF (MOD(counter,plot_freq) == 0) call plotSolution(2,counter,'plt/Solution',Mesh,W)

例程在下面。

subroutine plot_nodal_field(opt,iter,output_file,Q)
   use Globals_module,only : c_in
   use inputs_module,only : p_in,gamma,rho_in,M_inf
!--------------------------------------------end of use--------------------------------------------!
   integer(i4),intent(in) :: iter,opt
   type(Mesh_Type),intent(in) :: Mesh
   character(*),intent(in) :: output_file
   real(dp),intent(in) :: Q(Mesh%numFields,Mesh%numNodes)
!------------------------------------------end of intents------------------------------------------!
   integer(i4)   :: elem_id,n1,n2,n3
   character(80) :: filename,file_num
   integer(i4)   :: iunit
   integer(i4)   :: n,io,i
   real(dp)      :: x,y,rho,rhoU,rhoV,rhoE,u,v,P,CP,c,MachNum,s
!------------------------------------------end of declare------------------------------------------!


  iunit = 2

  write(file_num,'(I6.6)') iter
  write(*,*) 'output_name:',output_file
  filename = adjustl(trim(output_file)) // '_' // adjustl(trim(file_num)) // '.plt'
  write(*,'(A)') 'filename1:',filename
  filename = adjustl(trim(filename))
  write(*,'(A)') 'filename2:',filename
  print*,'# Writing to File...',iter

   open(unit = iunit,file = filename,status = 'replace',iostat = io)
   if( io /= 0) then
       print*,'ERROR: opening 2nd order plt file'
   else
       if (opt == 1) then 
           write(iunit,'(A)')  'VARIABLES = "x","y","rho","U","V","P","MachNum","entropy"'
       elseif(opt == 2) then
           write(iunit,"P"'
   end if
       write(iunit,'(A7,I,A3,A41)')  'ZONE N=',Mesh%numNodes,' E=',Mesh%numTri,' DATAPACKING=POINT ZONETYPE=FETRIANGLE'
       do i = 1,Mesh%numNodes
           x    = Mesh%nodeCoords(1,i)
           y    = Mesh%nodeCoords(2,i)
           if (opt == 1) then
               rho  = Q(1,i)
               U = Q(2,i)
               V = Q(3,i)
               P = Q(4,i)
               c = sqrt(gamma*P/Q(1,i))
               MachNum = sqrt(Q(2,i)**2 + Q(3,i)**2)/c
               s = P/Q(1,i)**gamma - P_in/rho_in**gamma

               write(iunit,'(8ES)') Real(x),Real(y),Real(Q(1,i)),Real(Q(2,Real(Q(3,Real(Q(4,&
               & Real(MachNum),Real(s)
           elseif (opt == 2) then
               write(iunit,'(6ES)') Real(x),i))
           end if
     
       end do
       do i = 1,Mesh%numTri
           write(iunit,'(3I)') Mesh%trilist(1,i),Mesh%trilist(2,Mesh%trilist(3,i) 
       end do
   end if
   close(iunit)

end subroutine

编辑:

我试图提出一个最小的可验证示例:

program plotSolution                                                                                                        
!--------------------------------------------end of use--------------------------------------------!
integer       :: iter
character(80) :: filename
!------------------------------------------end of intents------------------------------------------!
filename = 'plt/Solution'
iter = 1
call plot_nodal_field(iter,filename)

contains


subroutine plot_nodal_field(iter,output_file)
!--------------------------------------------end of use--------------------------------------------!
integer,intent(in) :: iter
character(*),intent(in) :: output_file
!------------------------------------------end of intents------------------------------------------!
character(80) :: filename,file_num
integer       :: iunit
!------------------------------------------end of declare------------------------------------------!


iunit = 2

write(file_num,'(I6.6)') iter
write(*,output_file
filename = adjustl(trim(output_file)) // '_' // adjustl(trim(file_num)) // '.plt'
write(*,filename
filename = adjustl(trim(filename))
write(*,filename
print*,iter

open(unit = iunit,iostat = io)
if( io /= 0) then
   print*,'ERROR: opening 2nd order plt file'
else
end if
close(iunit)
end subroutine
end program

但这可行: 并返回:

输出名称plt /解决方

文件名1: plt / Solution_000001.plt
文件名2: plt / Solution_000001.plt
写入文件... 1 错误:正在打开二阶plt文件

这向我表明我的主代码中可能存在一些奇怪的内存错误。我不确定。感谢所有建议和帮助!

解决方法

我使用此测试程序尝试了发布的例程:

  • 我找不到gfortran> = 5.3.0的问题,但是根据我的经验,gfortran的字符串处理在过去的版本中一直存在很多错误;
  • 每当我有可变大小的字符串时,我的编码习惯是拥有固定长度的字符串缓冲区(例如character(len=BUFFER_SIZE) :: buffer),进行所有临时剪切和粘贴,然后将相关部分分配给变量-函数末尾的长度变量(选择最大长度加上盐粒!)
  • 在所附示例中,我发布了一个示例路径创建函数。可以将其通用化以便更灵活地使用(选择文件夹名称等);
program test_string
   implicit none
   
   integer :: counter
   character(len=:),allocatable :: fileName
   
   counter = 2
   
   ! Original routine   
   call create_string_orig(counter,'PLT/Solution')
   
   ! With path function
   fileName = output_fileName('PLT',counter)
   write(*,*) 'filename with function: ',fileName
   
   contains
   
   function output_fileName(folder,iter) result(fileName)
      character(*),intent(in) :: folder      
      integer,intent(in) :: iter
      character(len=:),allocatable :: fileName
      
      ! Local variables 
      character(*),parameter :: prefix = 'Solution'
      character(*),parameter :: ext    = '.plt'
      character(len=1024) :: buffer
      character(len=6)    :: fileID
      integer :: fullLen
      
      write(fileID,'(I6.6)') iter
      
      buffer = trim(adjustl(folder)) // filesep() // prefix // '_' // fileID // ext
      
      fullLen = len_trim(buffer)
      
      allocate(character(len=fullLen) :: fileName)
      fileName(1:fullLen) = buffer(1:fullLen)
      
   end function output_fileName
   
   ! Get current system's folder separator
   character function filesep()
      character(len=9999) :: system_path
      integer             :: i

      system_path = repeat(' ',len(system_path))
      call get_environment_variable('PATH',system_path)

      do i = 1,len(system_path)
         if (system_path(i:i) == '/') then
            filesep = '/'
            return
         elseif (system_path(i:i) == '\') then
            filesep = '\'
            return
         endif
      end do

      ! Use default if unable to find a separator
      filesep = '/'
      return

   end function filesep   
   
   
   subroutine create_string_orig(iter,output_file)
      integer,intent(in) :: iter
      character(*),intent(in) :: output_file
      
      character(80) :: filename,file_num
       
      write(file_num,'(I6.6)') iter
      write(*,*) 'output_name: ',output_file
      filename = adjustl(trim(output_file)) // '_' // adjustl(trim(file_num)) // '.plt'
      write(*,*) 'filename1: ',filename
      filename = adjustl(trim(filename))
      write(*,*) 'filename2: ',filename
      print*,'# Writing to File...',iter
      
      end subroutine create_string_orig
end program  

这将产生以下输出(在Windows上运行):

$ a.exe
 output_name: PLT/Solution
 filename1: PLT/Solution_000002.plt
 filename2: PLT/Solution_000002.plt
 # Writing to File...           2
 filename with function: PLT\Solution_000002.plt
,

我感谢你们提供的所有帮助,并决定今天回到这里并发现问题。据我所知,这是一个编译器标志问题,其中编译器/预处理器将连接运算符 (//) 作为 C++ 注释,而预处理器在连接后将所有内容注释掉。我不得不使用连续符号来拼凑一个修复程序,但它现在有效,然后我可以再次查看标志。如果有人有任何见解,我将不胜感激。