Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-5-haiku-20241022· AnthropicHugging Face LMArena Human Preference 140K

write a modern fortran program to read a text file containing "variables = X, Y zone t =…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Amazon Novaamazon.nova-pro-v1:0’s answer.
  1. User

    write a modern fortran program to read a text file containing "variables = X, Y zone t = "outer box" -0.200000E+02 -0.200000E+02 0.200000E+02 -0.200000E+02 0.200000E+02 0.200000E+02 -0.200000E+02 0.200000E+02 -0.200000E+02 -0.200000E+02 zone t = "airfoil 1" 0.837657E+00 0.246750E-01 0.837216E+00 0.237010E-01 0.832049E+00 0.241980E-01 zone t = "airfoil 2" 0.331250E-01 -0.281000E-03 0.310020E-01 -0.239800E-02 0.289100E-01 -0.454700E-02 0.268130E-01 -0.669000E-02 0.247020E-01 -0.882000E-02 zone t = "airfoil 3" 0.936261E+00 -0.365980E-01 0.930591E+00 -0.313880E-01 0.934141E+00 -0.346230E-01 0.936261E+00 -0.365980E-01 zone t = "airfoil 4" 0.109274E+01 -0.191904E+00 0.109018E+01 -0.189040E+00 0.108684E+01 -0.185232E+00 0.106809E+01 -0.168725E+00 0.106330E+01 -0.166621E+00 0.105714E+01 -0.165049E+00 0.109274E+01 -0.191904E+00 zone t = "airfoil 5" 0.113387E+01 -0.283991E+00 0.113304E+01 -0.284458E+00 0.113191E+01 -0.282637E+00 0.112965E+01 -0.279000E+00 "

  2. Claude Assistant

    Here's a modern Fortran program to read and process the given text file:

    program read_zone_data
        use, intrinsic :: iso_fortran_env, only: real64, error_unit
        implicit none
    
        ! Type to store zone information
        type :: zone_data
            character(len=:), allocatable :: name
            real(real64), allocatable :: coordinates(:,:)
        end type zone_data
    
        ! File handling variables
        integer :: io_unit, io_status
        character(len=:), allocatable :: filename
        character(len=500) :: line
    
        ! Data processing variables
        type(zone_data), allocatable :: zones(:)
        integer :: num_zones, current_zone
        integer :: num_points
        real(real64) :: x, y
        integer :: line_count
    
        ! Prompt for filename
        filename = "zones.txt"
    
        ! Open the file
        open(newunit=io_unit, file=filename, status='old', action='read', iostat=io_status)
        if (io_status /= 0) then
            write(error_unit, *) "Error opening file: ", filename
            stop
        end if
    
        ! First pass: count zones
        num_zones = 0
        do
            read(io_unit, '(A)', iostat=io_status) line
            if (io_status /= 0) exit
            
            if (index(line, 'zone t =') > 0) then
                num_zones = num_zones + 1
            end if
        end do
    
        ! Allocate zones array
        allocate(zones(num_zones))
    
        ! Reset file position
        rewind(io_unit)
    
        ! Second pass: read zone data
        current_zone = 0
        do
            read(io_unit, '(A)', iostat=io_status) line
            if (io_status /= 0) exit
    
            ! Check for zone definition
            if (index(line, 'zone t =') > 0) then
                current_zone = current_zone + 1
                
                ! Extract zone name
                line = adjustl(line)
                zones(current_zone)%name = line(index(line, '"')+1 : len_trim(line)-1)
                
                ! Initialize coordinate storage
                allocate(zones(current_zone)%coordinates(0, 2))
                line_count = 0
            
            ! Read coordinate data
            elseif (current_zone > 0) then
                ! Skip lines with non-numeric content
                if (index(line, '"') > 0) cycle
                
                ! Try to read coordinates
                read(line, *, iostat=io_status) x, y
                if (io_status == 0) then
                    line_count = line_count + 1
                    
                    ! Resize coordinate array
                    call resize_coordinates(zones(current_zone)%coordinates, line_count)
                    
                    ! Store coordinates
                    zones(current_zone)%coordinates(line_count, 1) = x
                    zones(current_zone)%coordinates(line_count, 2) = y
                end if
            end if
        end do
    
        ! Close file
        close(io_unit)
    
        ! Output zone information
        call print_zone_data(zones)
    
    contains
    
        ! Subroutine to dynamically resize coordinate array
        subroutine resize_coordinates(coords, new_size)
            real(real64), allocatable, intent(inout) :: coords(:,:)
            integer, intent(in) :: new_size
            real(real64), allocatable :: temp(:,:)
    
            ! Allocate new array with increased size
            allocate(temp(new_size, 2))
            
            ! Copy existing data if array is not empty
            if (allocated(coords)) then
                temp(1:size(coords,1), :) = coords
                deallocate(coords)
            end if
            
            ! Move allocation
            call move_alloc(temp, coords)
        end subroutine resize_coordinates
    
        ! Subroutine to print zone data
        subroutine print_zone_data(zones)
            type(zone_data), intent(in) :: zones(:)
            integer :: i, j
    
            do i = 1, size(zones)
                print '(A, A, A, I0, A)', "Zone ", trim(zones(i)%name), &
                       " has ", size(zones(i)%coordinates, 1), " points:"
                
                do j = 1, size(zones(i)%coordinates, 1)
                    print '(2(F12.6, 2X))', &
                          zones(i)%coordinates(j, 1), &
                          zones(i)%coordinates(j, 2)
                end do
                print *, "" ! Blank line between zones
            end do
        end subroutine print_zone_data
    
    end program read_zone_data
    

    Key Features:

    1. Modern Fortran Practices:

      • Uses iso_fortran_env for precise real kind
      • Implicit none
      • Dynamic memory allocation
      • Type-bound procedures (custom type for zones)
    2. File Handling:

      • Two-pass reading strategy
      • Dynamic zone and coordinate storage
      • Error handling for file operations
    3. Coordinate Processing:

      • Dynamically resizable coordinate arrays
      • Handles scientific notation
      • Skips non-coordinate lines
    4. Output:

      • Prints zone names and coordinates
      • Formatted output for readability

    Compilation

    Compile with a modern Fortran compiler:

    gfortran -std=f2018 -O2 zone_reader.f90 -o zone_reader
    

    Usage

    1. Save the text you provided in a file named zones.txt
    2. Compile the program
    3. Run the executable

    The program will read the file and print out the coordinates for each zone.

    Would you like me to elaborate on any part of the code or explain any specific aspect of the implementation?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.