TABLE OF CONTENTS


::pwtk::nebi::setImageCharge

SYNOPSIS

proc ::pwtk::nebi::setImageCharge {index charge} {

PURPOSE

Set the TOTAL_CHARGE card for index-th image.

REMARK

This command is not related to the "num_of_images" variable, but to the POSITIONS card (aka BEGIN_POSITIONS / END_POSITIONS).

ARGUMENTS

SOURCE

    if { ! [string is double $charge] } {
        ::pwtk::error "expected a floating point number for the total_charge, but got $charge" 1
    }

    set nimages [getNImages]
    if { $nimages == 0 } {
        # no image specified
        ::pwtk::error "cannot set any image charge because POSITIONS/ATOMIC_POSITIONS card is undefined" 1
    }    

    set index [image_in_range_ $index $nimages]    

    set iimage 0
    set chargeSet 0
    set positions {}
    set skip    0

    foreach line [split [::pwtk::input::cardGetContent POSITIONS] \n] {        
        if { [regexp {IMAGE} $line] } {
            if { $iimage == $index && ! $chargeSet } {
                # TOTAL_CHARGE card was missing & charge was not yet written
                append positions "TOTAL_CHARGE\n$charge\n"
            }
            incr iimage
            set skip 0
        }
        if { [regexp {ATOMIC_POSITIONS} $line] } {
            set skip 0
        }
        if { [regexp {TOTAL_CHARGE} $line] } {
            if { $iimage == $index } {
                append positions $line\n$charge\n
                set skip 1
                set chargeSet 1
                continue
            } else {
                set skip 0
            }
        }
        if { ! $skip } {
            append positions $line\n
        }
    }
    # take care of LAST_IMAGE if TOTAL_CHARGE record is missing
    if { $iimage == $index && ! $chargeSet } {
        # TOTAL_CHARGE card was missing & charge was not yet written
        append positions "TOTAL_CHARGE\n$charge\n"
    }
        
    POSITIONS $positions

    return $positions
}