DECLARE SUB qwlogcalcgl (logfile$, gl!, sgl!) DECLARE SUB qwcalcgl (nx%, x0!(), dx!, vtvsx!(), svtvsx!(), gl!, sgl!) DECLARE SUB qwlinfit (n%, x!(), y!(), m!, sm!, b!, sb!) DECLARE SUB qwlogqw (logfile$, nmeas%, nx%, imag!, x0!(), dx!, vt!(), svt!(), gl!, sgl!) DECLARE SUB qwlogvtvsx (logfile$, nx%, x0!(), dx!, vt!(), svt!()) DECLARE SUB qwmeasvtvsx (nx%, x0!(), dx!, vtvsx!(), svtvsx!()) DECLARE SUB qwpltvtvsx (logfile$, magname$, run$, nx%, x!(), vt!(), svt!()) DECLARE SUB wiregetvtav (x0!, dx!, vt!, svt!) DECLARE SUB wiremoveabs (x!) DECLARE SUB imaggeti (imag!) DECLARE SUB imagmeas (imag!, simag!) '**************************************************************************** 'Module QW.BAS ' 'These subroutines perform calculations related to quadrupole magnet 'stretched wire measurements. ' 'Zachary Wolf '1/14/95 '**************************************************************************** 'Open the parameter file REM $INCLUDE: 'param.inc' SUB qw '**************************************************************************** 'This subroutine supervises the wire measurements and integrate gradient 'calculation. ' 'Zachary Wolf '10/31/94 '**************************************************************************** 'Simplify the notation nx% = nx0P% dx! = dxcmP! DIM x0!(1 TO nx%) FOR i% = 1 TO nx% x0!(i%) = x0P!(i%) NEXT i% nmeas% = nmeasaveP% 'Initialize data arrays DIM vtvsx!(1 TO nx%) 'average voltage integral at each position DIM svtvsx!(1 TO nx%) 'standard deviation of the voltage integral measurements 'Message PRINT PRINT "Beginning the stretched wire measurement cycle..." 'Measure the magnet current CALL imagmeas(imag!, simag!) 'Measure the voltage integral at points across the magnet aperture CALL qwmeasvtvsx(nx%, x0!(), dx!, vtvsx!(), svtvsx!()) 'Calculate the integradient of the quadrupole CALL qwcalcgl(nx%, x0!(), dx!, vtvsx!(), svtvsx!(), gl!, sgl!) 'Write the result to the dat file CALL qwlogqw(datfileP$, nmeas%, nx%, imag!, x0!(), dx!, vtvsx!(), svtvsx!(), gl!, sgl!) 'Message PRINT PRINT "Moving back to the home position..." 'Move the wire back to the home position CALL wiremoveabs(0!) END SUB SUB qwcalcgl (nx%, x0!(), dx!, vtvsx!(), svtvsx!(), gl!, sgl!) '**************************************************************************** 'This subroutine calculates the integrated gradient from the measurements 'of the integrated voltage as a function of x. ' 'Input: ' nx%, the number of x positions ' x0!(1 to nx%), the position of the center of each wire motion ' dx!, the distance the wire moves for each motion ' vtvsx!(1 to nx%), the integrated voltage measurements ' svtvsx!(1 to nx%), the standard deviation on each vt ' 'Output: ' gl!, the integrated gradient of the magnet ' sgl!, the standard deviation on gl ' 'Zachary Wolf '1/14/95 '**************************************************************************** 'Fit the vt vs x data points with a line CALL qwlinfit(nx%, x0!(), vtvsx!(), m!, sm!, b!, sb!) 'm=dVT/dX0 (Vs/cm) 'Compute the integrated gradient 'gl = (1/delX)*(dVT/dX0) m! = m! * 100! '(Vs/m) gl! = m! / dx! 'gl = (1/delx)(dVT/dX0), ((1/cm)(Tm^2/m)) gl! = gl! * 100! '(T) 'Error sm! = sm! * 100! '(Vs/m) sgl! = sm! / dx! 'gl = (1/delx)(dVT/dX0), ((1/cm)(Tm^2/m)) sgl! = sgl! * 100! '(T) 'Log the result CALL qwlogcalcgl(logfileP$, gl!, sgl!) END SUB SUB qwlinfit (n%, x!(), y!(), m!, sm!, b!, sb!) '**************************************************************************** 'This subroutine does a least squares linear fit to the (x,y) data points. 'It returns the slope and intercept of the line with errors. 'See Bevington, pp. 104, 114 ' 'Input: ' n%, the number of (x,y) points ' x!(1 to n%), the x values ' y!(1 to n%), the y values ' 'Output: ' m!, the fitted line's slope ' sm!, error estimate for m ' b!, the fitted line's intercept ' sb!, error estimate for b ' 'Zachary Wolf '5/28/95 '**************************************************************************** 'Compute various sums SUMxy! = 0! SUMxx! = 0! SUMx! = 0! SUMy! = 0! FOR i% = 1 TO n% SUMxy! = SUMxy! + x!(i%) * y!(i%) SUMxx! = SUMxx! + x!(i%) * x!(i%) SUMx! = SUMx! + x!(i%) SUMy! = SUMy! + y!(i%) NEXT i% 'Compute the determinant d! = n% * SUMxx! - SUMx! * SUMx! 'Compute the slope and intercept m! = (n% * SUMxy! - SUMx! * SUMy!) / d! b! = (SUMxx! * SUMy! - SUMxy! * SUMx!) / d! 'Compute the errors 'Estimate the error on each y value sigsq! = 0! FOR i% = 1 TO n% sigsq! = sigsq! + (y!(i%) - (m! * x!(i%) + b!)) ^ 2 NEXT i% sigsq! = sigsq! / (n% - 2) 'Use the estimated y error to find sb! and sm! sb! = SQR(sigsq! * SUMxx / d!) sm! = SQR(n% * sigsq! / d!) END SUB SUB qwlogcalcgl (logfile$, gl!, sgl!) '**************************************************************************** 'This subroutine writes the calculated integrated gradient of the magnet. ' 'Input: ' logfile$, the name of the log file ' gl!, the integrated gradient of the magnet ' sgl!, the standard deviation of GL ' 'Zachary Wolf '1/14/95 '**************************************************************************** 'Open the log file filenum% = FREEFILE OPEN logfile$ FOR APPEND AS filenum% 'Calculated Results PRINT #filenum%, PRINT #filenum%, "The integrated gradient is "; gl!; " +- "; sgl!; " T" 'Close the log file CLOSE filenum% END SUB SUB qwlogqw (logfile$, nmeas%, nx%, imag!, x0!(), dx!, vt!(), svt!(), gl!, sgl!) '**************************************************************************** 'This subroutine writes a summary of the VT vs X measurements. ' 'Input: ' logfile$, the name of the log file ' nmeas%, the number of measurements for average and standard deviation ' nx%, the number of x-positions ' imag!, the magnet current ' x0!(1 to nx%), the x-position of the center of each wire motion ' dx!, the distance the wire is moved for each measurement ' vt!(1 to nx%), the voltage integral for the wire motion ' svt!(1 to ni%), the standard deviation of VT ' gl!, the integrated gradient of the magnet ' sgl!, the standard deviation of GL ' 'Zachary Wolf '11/25/94 '**************************************************************************** 'Open the log file filenum% = FREEFILE OPEN logfile$ FOR APPEND AS filenum% 'Write a header PRINT #filenum%, PRINT #filenum%, PRINT #filenum%, " STRETCHED WIRE" PRINT #filenum%, " INTEGRATED VOLTAGE VS X" PRINT #filenum%, PRINT #filenum%, "# Measurements For Average, Nmeas = "; nmeas% PRINT #filenum%, "# x-positions, Nx = "; nx% PRINT #filenum%, "Distance wire moved, Delta X = "; dx!; " cm" PRINT #filenum%, PRINT #filenum%, "Magnet Current, Imag = "; imag!; " A" PRINT #filenum%, PRINT #filenum%, " X0 VT sigVT " PRINT #filenum%, " (cm) (VS) (VS) " PRINT #filenum%, " -------- ----------+----------" 'Write the results of the measurements FOR i% = 1 TO nx% PRINT #filenum%, USING " ##.#####"; x0!(i%); PRINT #filenum%, USING " ##.#######"; vt!(i%); PRINT #filenum%, USING " ##.#######"; svt!(i%) NEXT i% 'Calculated Results PRINT #filenum%, PRINT #filenum%, PRINT #filenum%, "The integrated gradient is" PRINT #filenum%, USING "###.######"; gl!; PRINT #filenum%, " +- "; PRINT #filenum%, USING " ###.######"; sgl!; PRINT #filenum%, " T" 'Close the log file CLOSE filenum% END SUB SUB qwlogvtvsx (logfile$, nx%, x0!(), dx!, vt!(), svt!()) '**************************************************************************** 'This subroutine writes a summary of the VT vs X measurements. ' 'Input: ' logfile$, the name of the log file ' nx%, the number of x-positions ' x0!(1 to nx%), the x-position of the center of each wire motion ' dx!, the distance the wire is moved for each measurement ' vt!(1 to nx%), the voltage integral for the wire motion ' svt!(1 to ni%), the standard deviation of VT ' 'Zachary Wolf '11/25/94 '**************************************************************************** 'Open the log file filenum% = FREEFILE OPEN logfile$ FOR APPEND AS filenum% 'Write a header PRINT #filenum%, PRINT #filenum%, PRINT #filenum%, "STRETCHED WIRE MEASUREMENT SUMMARY" PRINT #filenum%, PRINT #filenum%, "Distance wire moved, Delta X = "; dx!; " cm" PRINT #filenum%, "X0 = center position of the wire motion" PRINT #filenum%, "VT = integrated voltage" PRINT #filenum%, PRINT #filenum%, " X0 VT sigVT " PRINT #filenum%, " (cm) (VS) (VS) " PRINT #filenum%, " -------- ----------+----------" 'Write the results of the measurements FOR i% = 1 TO nx% PRINT #filenum%, USING " ##.#####"; x0!(i%); PRINT #filenum%, USING " ##.#######"; vt!(i%); PRINT #filenum%, USING " ##.#######"; svt!(i%) NEXT i% 'Close the log file CLOSE filenum% END SUB SUB qwmeasvtvsx (nx%, x0!(), dx!, vtvsx!(), svtvsx!()) '**************************************************************************** 'This subroutine measures the integrated voltage as a function of position 'across the magnet aperture. ' 'Input: ' nx%, the number of x positions ' x0!(1 to nx%), the center position of each wire motion ' dx!, the distance the wire moves ' 'Output: ' vtvsx!(1 to nx%), the integrated voltage at each x positions ' svtvsx!(1 to nx%), the standard deviation of each vt measurement ' 'Zachary Wolf '1/14/95 '**************************************************************************** 'Loop over x positions FOR i% = 1 TO nx% 'Measure the magnet current 'The value is recorded in the log file CALL imaggeti(imag!) 'Message PRINT PRINT "Measuring at X0 = "; x0!(i%); " cm..." 'Get the voltage integral at this position CALL wiregetvtav(x0!(i%), dx!, vt!, svt!) 'Save the results vtvsx!(i%) = vt! svtvsx!(i%) = svt! 'End loop over x positions NEXT i% 'Write the results to the log file CALL qwlogvtvsx(logfileP$, nx%, x0!(), dx!, vtvsx!(), svtvsx!()) 'Plot VT vs X CALL qwpltvtvsx(pltfileP$, magnameP$, runP$, nx%, x0!(), vtvsx!(), svtvsx!()) END SUB SUB qwpltvtvsx (logfile$, magname$, run$, nx%, x!(), vt!(), svt!()) '**************************************************************************** 'This subroutine write a summary of the VT vs X measurements for plotting. ' 'Input: ' logfile$, the name of the log file ' magname$, the magnet name ' run$, the run number ' nx%, the number of x positions ' x!(1 to nx%), the x positions ' vt!(1 to nx%), the integrated voltage at each x position ' svt!(1 to nx%), the standard deviation of vt ' 'Zachary Wolf '12/27/94 '**************************************************************************** 'Open the log file filenum% = FREEFILE OPEN logfile$ FOR APPEND AS filenum% 'Write the title for plotting PRINT #filenum%, "/et g '" + magname$ + ", Run " + run$ + "'" PRINT #filenum%, ";x vt svt" 'Write the values FOR i% = 1 TO nx% PRINT #filenum%, USING "##.####"; x!(i%); PRINT #filenum%, USING " ###.#######"; vt!(i%); PRINT #filenum%, USING " ###.#######"; svt!(i%) NEXT i% 'Close the log file CLOSE filenum% END SUB