Skip to content

[Codescan] Close each EDA-FF PQR file before reconnecting unit 10 #39

Description

@Stardust0831

Global scan provenance

This issue was found by the Codex global repository scan of non-unit-test files in Stardust0831/Multiwfn, pinned to commit d2b770a531eb7039153198d04520b5992f15c49d.

Problem

When EDA-FF atom-contribution output is enabled, the code opens atmint_tot.pqr on unit 10, writes it, and then reconnects the still-open unit to four different filenames. Only the final file is closed.

Multiwfn/EDA.f90

Lines 365 to 406 in d2b770a

!Output .pqr files
if (ioutatmpqr==1) then
open(10,file="atmint_tot.pqr",status="replace")
write(10,"('REMARK Generated by Multiwfn, totally',i10,' atoms')") ncenter
do i=1,ncenter
write(10,"(a6,i5,1x,a4,1x,a3, 1x,a1,i4,4x,3f8.3,f13.4,f9.4,a2)") &
"HETATM",i,' '//ind2name_up(a(i)%index)//' ',"MOL",'A',1,a(i)%x*b2a,a(i)%y*b2a,a(i)%z*b2a,totatm(i),vdwr(a(i)%index)*b2a,adjustr(ind2name_up(a(i)%index))
end do
write(10,"('END')")
open(10,file="atmint_ele.pqr",status="replace")
write(10,"('REMARK Generated by Multiwfn, totally',i10,' atoms')") ncenter
do i=1,ncenter
write(10,"(a6,i5,1x,a4,1x,a3, 1x,a1,i4,4x,3f8.3,f13.4,f9.4,a2)") &
"HETATM",i,' '//ind2name_up(a(i)%index)//' ',"MOL",'A',1,a(i)%x*b2a,a(i)%y*b2a,a(i)%z*b2a,eleatm(i),vdwr(a(i)%index)*b2a,adjustr(ind2name_up(a(i)%index))
end do
write(10,"('END')")
open(10,file="atmint_rep.pqr",status="replace")
write(10,"('REMARK Generated by Multiwfn, totally',i10,' atoms')") ncenter
do i=1,ncenter
write(10,"(a6,i5,1x,a4,1x,a3, 1x,a1,i4,4x,3f8.3,f13.4,f9.4,a2)") &
"HETATM",i,' '//ind2name_up(a(i)%index)//' ',"MOL",'A',1,a(i)%x*b2a,a(i)%y*b2a,a(i)%z*b2a,repatm(i),vdwr(a(i)%index)*b2a,adjustr(ind2name_up(a(i)%index))
end do
write(10,"('END')")
open(10,file="atmint_disp.pqr",status="replace")
write(10,"('REMARK Generated by Multiwfn, totally',i10,' atoms')") ncenter
do i=1,ncenter
write(10,"(a6,i5,1x,a4,1x,a3, 1x,a1,i4,4x,3f8.3,f13.4,f9.4,a2)") &
"HETATM",i,' '//ind2name_up(a(i)%index)//' ',"MOL",'A',1,a(i)%x*b2a,a(i)%y*b2a,a(i)%z*b2a,dispatm(i),vdwr(a(i)%index)*b2a,adjustr(ind2name_up(a(i)%index))
end do
write(10,"('END')")
open(10,file="atmint_vdW.pqr",status="replace")
write(10,"('REMARK Generated by Multiwfn, totally',i10,' atoms')") ncenter
do i=1,ncenter
write(10,"(a6,i5,1x,a4,1x,a3, 1x,a1,i4,4x,3f8.3,f13.4,f9.4,a2)") &
"HETATM",i,' '//ind2name_up(a(i)%index)//' ',"MOL",'A',1,a(i)%x*b2a,a(i)%y*b2a,a(i)%z*b2a,repatm(i)+dispatm(i),vdwr(a(i)%index)*b2a,adjustr(ind2name_up(a(i)%index))
end do
write(10,"('END')")
write(*,*)
write(*,"(a)") " atmint_tot.pqr, atmint_ele.pqr, atmint_rep.pqr, atmint_disp.pqr, atmint_vdW.pqr have been exported to current folder. &
&Their atomic charge fields record atom contribution to total/electrostatic/repulsive/dispersion/vdW interaction energy between all fragments, &
&respectively. the radius column corresponds to Bondi vdW radii"
close(10)

Standard Fortran does not permit changing the FILE= connection of an already connected unit this way. Runtime behavior is compiler-dependent: strict runtimes can reject the second OPEN, while permissive runtimes may implicitly close files and hide the portability defect.

Impact

The documented option to export five PQR contribution files can stop after the first file or produce an incomplete set, particularly in GNU Fortran cross-platform builds. Buffered output and error handling also become runtime-dependent.

Suggested direction

  • Close unit 10 after finishing each PQR file, or use distinct newunit= values with scoped open/write/close handling.
  • Add iostat/iomsg checks so filesystem failures identify the specific target file.
  • Add a functional test that enables PQR output and verifies all five files exist, end with END, and contain the expected number of atoms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions