#! /usr/bin/gawk -f # Last edited on 2007-01-10 13:27:18 by stolfi BEGIN { abort = -1; usage = ( \ "make-test-field \\\n" \ " -v nx={NUM} -v ny=NUM \\\n" \ " [ -v debug={BOOL} ] \\\n" \ " > OUTFILE.txt" \ ); # Outputs a data file suitable for producing a 3D graph # of a bivariate function with {gnuplot}'s command # "splot '{OUTFILE}.txt'". # # The output has one datum per line, in the format # "{X} {Y} {F(X,Y)}" # where {X} and {Y} vary from 0 to 1. # # The function is sampled at {nx+1} # equally spaced points in X and {ny+1} in Y. # The X coordinate is the fastest varying one. # A blank line separates blocks with the same Y value. if (debug == "") { debug = 0; } if (nx == "") { arg_error("must define \"ini\""); } if (ny == "") { arg_error("must define \"fin\""); } for (iy = 0; iy <= ny; iy++) { y = (iy + 0.0)/(ny + 0.0); for (ix = 0; ix <= nx; ix++) { x = (ix + 0.0)/(nx + 0.0); fxy = eval_func(x,y); printf " %7.5f %7.5f %16.8e\n", x, y, fxy; } printf "\n"; } } function eval_func(x,y, r,s,pi) { # A function like the X-derivative of {sinc}. # Remap {x,y} to [-1_+1]: x = 2*x-1; y = 2*y-1; # Nice function on {[-1_+1]^2}. r = sqrt((x*x + y*y)/2); s = (x+y)/2; pi = 3.1415926; return -sin(2*pi*r)*cos(pi*s); } function check_value(x, v) { # Checks format of {x}. if (x !~ /^[ ]*[-+]?[0-9]*([.][0-9]*|[0-9])[ ]*$/) { data_error(("bad number = \"" x "\"\n")); } v = x + 0.000; return v; } function assert(cond) { if (! cond) { prog_error("bug"); } } function data_warning(msg) { printf "%s:%s: warning - %s\n", FILENAME, FNR, msg > "/dev/stderr"; } function arg_error(msg) { printf "** %s\n", msg > "/dev/stderr"; printf "usage: %s\n", usage; abort = 1; exit abort; } function data_error(msg) { printf "%s:%s: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; abort = 1; exit abort; } function prog_error(msg) { printf "** %s\n", msg > "/dev/stderr"; abort = 1; exit abort; } function debug_scalar(lab,fmt,V) { printf "%-20s = ", lab > "/dev/stderr"; printf fmt, V > "/dev/stderr"; printf "\n" > "/dev/stderr"; }