ngspice/src/frontend/plotting/agraf.c

337 lines
10 KiB
C
Raw Normal View History

2000-04-27 22:03:57 +02:00
/**********
Copyright 1990 Regents of the University of California. All rights reserved.
Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
**********/
/*
* Line-printer (ASCII) plots.
*/
#include "ngspice/ngspice.h"
#include "ngspice/cpdefs.h"
#include "ngspice/ftedefs.h"
#include "ngspice/dvec.h"
#include "ngspice/fteparse.h"
2000-04-27 22:03:57 +02:00
#include "agraf.h"
src/Makefile.am src/help.c src/main.c src/circuit/Makefile.am src/circuit/ifnewuid.c src/frontend/Makefile.am src/frontend/aspice.c src/frontend/circuits.h src/frontend/com_display.c src/frontend/com_hardcopy.c src/frontend/commands.c src/frontend/commands.h src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/device.c src/frontend/diff.c src/frontend/display.c src/frontend/dotcards.c src/frontend/fourier.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/mw_coms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/postcoms.c src/frontend/postsc.c src/frontend/rawfile.c src/frontend/resource.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/subckt.c src/frontend/vectors.c src/frontend/where.c src/frontend/plotting/Makefile.am src/frontend/plotting/agraf.c src/frontend/plotting/graf.c src/frontend/plotting/plotcurv.c src/frontend/plotting/plotit.c src/frontend/plotting/x11.c src/frontend/plotting/xgraph.c src/include/Makefile.am src/maths/cmaths/cmath4.c src/misc/terminal.c src/misc/terminal.h src/parser/cshpar.c src/parser/front.c src/parser/front.h src/parser/history.c src/parser/history.h src/parser/modify.c src/parser/var2.c src/parser/var2.h src/parser/variable.c: Refactoring of frontend code. * src/include/ftehelp.h src/include/variable.h: Moved into frontend directory. * src/include/cpdefs.h src/include/cpextern.h src/include/ftedefs.h src/include/plot.h: Updates.
2000-06-27 18:09:02 +02:00
#define FUDGE 7
#define MARGIN_BASE 11
#define LCHAR '.'
#define MCHAR 'X'
#define PCHARS "+*=$%!0123456789"
2000-04-27 22:03:57 +02:00
/* We should really deal with the xlog and delta arguments. This routine is
* full of magic numbers that make the formatting correct.
*/
void
ft_agraf(double *xlims, double *ylims, struct dvec *xscale, struct plot *plot, struct dvec *vecs, double xdel, double ydel, bool xlog, bool ylog, bool nointerp)
{
int height;
bool nobreakp, novalue;
int maxx, maxy, omaxy; /* The size of the plotting area. */
bool /* xlogscale = FALSE, */ ylogscale = FALSE;
char *field, buf[BSIZE_SP];
char *line1, *line2, c, cb;
double xrange[2], yrange[2], x1, x2, yy1, y2, x, y;
int mag, hmt, lmt, dst, spacing, nsp, ypt, upper, lower, curline;
double tenpowmag, diff;
double *values = NULL;
2000-04-27 22:03:57 +02:00
struct dvec *v;
int margin = MARGIN_BASE;
int omargin;
int i, j, k;
2000-04-27 22:03:57 +02:00
int shift;
2010-11-16 21:38:24 +01:00
NG_IGNORE(xdel);
NG_IGNORE(ydel);
NG_IGNORE(ylog);
2000-04-27 22:03:57 +02:00
/* ANSI C does not specify how many digits are in an exponent for %c
* We assumed it was 2. If it's more, shift starting position over.
*/
sprintf(buf, "%1.1e", 0.0); /* expect 0.0e+00 */
shift = (int) strlen(buf) - 7;
2000-04-27 22:03:57 +02:00
margin += shift;
/* Make sure the margin is correct */
omargin = margin;
novalue = cp_getvar("noasciiplotvalue", CP_BOOL, NULL, 0);
if (!novalue && !vec_eq(xscale, vecs))
2000-04-27 22:03:57 +02:00
margin *= 2;
else
2000-04-27 22:03:57 +02:00
novalue = TRUE;
if ((xscale->v_gridtype == GRID_YLOG) || (xscale->v_gridtype == GRID_LOGLOG))
2000-04-27 22:03:57 +02:00
ylogscale = TRUE;
if (!cp_getvar("width", CP_NUM, &maxy, 0))
maxy = DEF_WIDTH;
if (!cp_getvar("height", CP_NUM, &height, 0))
2000-04-27 22:03:57 +02:00
height = DEF_HEIGHT;
2000-04-27 22:03:57 +02:00
if (ft_nopage)
nobreakp = TRUE;
2000-04-27 22:03:57 +02:00
else
nobreakp = cp_getvar("nobreak", CP_BOOL, NULL, 0);
2000-04-27 22:03:57 +02:00
maxy -= (margin + FUDGE);
maxx = xscale->v_length;
2000-04-27 22:03:57 +02:00
xrange[0] = xlims[0];
xrange[1] = xlims[1];
yrange[0] = ylims[0];
yrange[1] = ylims[1];
if (maxx < 2) {
fprintf(cp_err,
"Error: asciiplot can't handle scale with length < 2\n");
2000-04-27 22:03:57 +02:00
return;
}
if (maxx <= 0) {
fprintf(cp_err, "Note: no points to plot\n");
return;
}
for (v = vecs, i = 0; v; v = v->v_link2)
2000-04-27 22:03:57 +02:00
v->v_linestyle = (PCHARS[i] ? PCHARS[i++] : '#');
2000-04-27 22:03:57 +02:00
/* Now allocate the field and stuff. */
field = TMALLOC(char, (maxy + 1) * (maxx + 1));
line1 = TMALLOC(char, maxy + margin + FUDGE + 1);
line2 = TMALLOC(char, maxy + margin + FUDGE + 1);
2000-04-27 22:03:57 +02:00
if (!novalue)
values = TMALLOC(double, maxx);
2000-04-27 22:03:57 +02:00
/* Clear the field, put the lines in the right places, and create
* the headers.
*/
for (i = 0, j = (maxx + 1) * (maxy + 1); i < j; i++)
field[i] = ' ';
for (i = 0, j = maxy + margin + FUDGE; i < j; i++) {
line1[i] = '-';
line2[i] = ' ';
}
line1[j] = line2[j] = '\0';
/* The following is similar to the stuff in grid.c */
if ((xrange[0] > xrange[1]) || (yrange[0] > yrange[1])) {
fprintf(cp_err,
"ft_agraf: Internal Error: bad limits %g, %g, %g, %g\n",
2000-04-27 22:03:57 +02:00
xrange[0], xrange[1], yrange[0], yrange[1]);
return;
}
/* gcc doesn't like !double */
if (ylims[1] == 0.0) {
mag = (int) floor(mylog10(- ylims[0]));
tenpowmag = pow(10.0, (double) mag);
} else if (ylims[0] == 0.0) {
2000-04-27 22:03:57 +02:00
mag = (int) floor(mylog10(ylims[1]));
tenpowmag = pow(10.0, (double) mag);
} else {
diff = ylims[1] - ylims[0];
mag = (int) floor(mylog10(diff));
tenpowmag = pow(10.0, (double) mag);
}
lmt = (int) floor(ylims[0] / tenpowmag);
yrange[0] = ylims[0] = lmt * tenpowmag;
hmt = (int) ceil(ylims[1] / tenpowmag);
yrange[1] = ylims[1] = hmt * tenpowmag;
dst = hmt - lmt;
/* This is a strange case; I don't know why it's here. */
if (dst == 11) {
2000-04-27 22:03:57 +02:00
dst = 12;
} else if (dst == 1) {
2000-04-27 22:03:57 +02:00
dst = 10;
mag++;
hmt *= 10;
lmt *= 10;
2000-04-27 22:03:57 +02:00
} else if (dst == 0) {
dst = 2;
lmt -= 1;
hmt += 1;
2000-04-27 22:03:57 +02:00
}
for (nsp = 4; nsp < 8; nsp++)
if (!(dst % nsp))
break;
if (nsp == 8)
for (nsp = 2; nsp < 4; nsp++)
if (!(dst % nsp))
break;
spacing = maxy / nsp;
/* Reset the max X coordinate to deal with round-off error. */
omaxy = maxy + 1;
maxy = spacing * nsp;
for (i = 0, j = lmt; j <= hmt; i += spacing, j += dst / nsp) {
for (k = 0; k < maxx; k++)
field[k * omaxy + i] = LCHAR;
line1[i + margin + 2 * shift] = '|';
(void) sprintf(buf, "%.2e", j * pow(10.0, (double) mag));
memcpy(&line2[i + margin - ((j < 0) ? 2 : 1) - shift], buf,
strlen(buf));
2000-04-27 22:03:57 +02:00
}
line1[i - spacing + margin + 1] = '\0';
for (i = 1; i < omargin - 1 && xscale->v_name[i - 1]; i++)
line2[i] = xscale->v_name[i - 1];
if (!novalue)
for (i = omargin + 1;
i < margin - 2 && (vecs->v_name[i - omargin - 1]);
i++)
2000-04-27 22:03:57 +02:00
line2[i] = vecs->v_name[i - omargin - 1];
/* Now the buffers are all set up properly. Plot points for each
* vector using interpolation. For each point on the x-axis, find the
* two bracketing points in xscale, and then interpolate their
* y values for each vector.
*/
upper = lower = 0;
for (i = 0; i < maxx; i++) {
if (nointerp)
x = isreal(xscale) ? xscale->v_realdata[i] :
realpart(xscale->v_compdata[i]);
2000-04-27 22:03:57 +02:00
else if (xlog && xrange[0] > 0.0 && xrange[1] > 0.0)
x = xrange[0] * pow(10.0, mylog10(xrange[1]/xrange[0])
* i / (maxx - 1));
2000-04-27 22:03:57 +02:00
else
x = xrange[0] + (xrange[1] - xrange[0]) * i /
(maxx - 1);
2000-04-27 22:03:57 +02:00
while ((isreal(xscale) ? (xscale->v_realdata[upper] < x) :
(realpart(xscale->v_compdata[upper]) < x)) &&
(upper < xscale->v_length - 1))
2000-04-27 22:03:57 +02:00
upper++;
while ((isreal(xscale) ? (xscale->v_realdata[lower] < x) :
(realpart(xscale->v_compdata[lower]) < x)) &&
(lower < xscale->v_length - 1))
2000-04-27 22:03:57 +02:00
lower++;
if ((isreal(xscale) ? (xscale->v_realdata[lower] > x) :
(realpart(xscale->v_compdata[lower]) > x)) &&
(lower > 0))
2000-04-27 22:03:57 +02:00
lower--;
x1 = (isreal(xscale) ? xscale->v_realdata[lower] :
realpart(xscale->v_compdata[lower]));
2000-04-27 22:03:57 +02:00
x2 = (isreal(xscale) ? xscale->v_realdata[upper] :
realpart(xscale->v_compdata[upper]));
2000-04-27 22:03:57 +02:00
if (x1 > x2) {
fprintf(cp_err, "Error: X scale (%s) not monotonic\n",
xscale->v_name);
return;
}
for (v = vecs; v; v = v->v_link2) {
yy1 = (isreal(v) ? v->v_realdata[lower] :
realpart(v->v_compdata[lower]));
2000-04-27 22:03:57 +02:00
y2 = (isreal(v) ? v->v_realdata[upper] :
realpart(v->v_compdata[upper]));
2000-04-27 22:03:57 +02:00
if (x1 == x2)
y = yy1;
else
y = yy1 + (y2 - yy1) * (x - x1) / (x2 - x1);
if (!novalue && (v == vecs))
values[i] = y;
ypt = ft_findpoint(y, yrange, maxy, 0, ylogscale);
c = field[omaxy * i + ypt];
if ((c == ' ') || (c == LCHAR))
field[omaxy * i + ypt] = (char) v->v_linestyle;
else
field[omaxy * i + ypt] = MCHAR;
}
}
2000-04-27 22:03:57 +02:00
out_init();
for (i = 0; i < omaxy + margin; i++)
out_send("-");
out_send("\n");
i = (omaxy + margin - (int) strlen(plot->pl_title)) / 2;
2000-04-27 22:03:57 +02:00
while (i-- > 0)
out_send(" ");
(void) strcpy(buf, plot->pl_title);
buf[maxy + margin] = '\0'; /* Cut off if too wide */
out_send(buf);
out_send("\n");
(void) sprintf(buf, "%s %s", plot->pl_name, plot->pl_date);
buf[maxy + margin] = '\0';
i = (omaxy + margin - (int) strlen(buf)) / 2;
2000-04-27 22:03:57 +02:00
while (i-- > 0)
out_send(" ");
out_send(buf);
out_send("\n\n");
curline = 7;
out_send("Legend: ");
i = 0;
j = (maxx + margin - 8) / 20;
2000-04-27 22:03:57 +02:00
if (j == 0)
j = 1;
2000-04-27 22:03:57 +02:00
for (v = vecs; v; v = v->v_link2) {
out_printf("%c = %-17s", (char) v->v_linestyle, v->v_name);
2000-04-27 22:03:57 +02:00
if (!(++i % j) && v->v_link2) {
out_send("\n ");
curline++;
}
}
out_send("\n");
2000-04-27 22:03:57 +02:00
for (i = 0; i < omaxy + margin; i++)
out_send("-");
out_send("\n");
i = 0;
out_printf("%s\n%s\n", line2, line1);
curline += 2;
2000-04-27 22:03:57 +02:00
for (i = 0; i < maxx; i++) {
if (nointerp)
x = isreal(xscale) ? xscale->v_realdata[i] :
realpart(xscale->v_compdata[i]);
2000-04-27 22:03:57 +02:00
else if (xlog && xrange[0] > 0.0 && xrange[1] > 0.0)
x = xrange[0] * pow(10.0, mylog10(xrange[1]/xrange[0])
* i / (maxx - 1));
else
x = xrange[0] + (xrange[1] - xrange[0]) * i / (maxx - 1);
if (x < 0.0)
out_printf("%.3e ", x);
else
out_printf(" %.3e ", x);
2000-04-27 22:03:57 +02:00
if (!novalue) {
if (values[i] < 0.0)
out_printf("%.3e ", values[i]);
else
out_printf(" %.3e ", values[i]);
2000-04-27 22:03:57 +02:00
}
2000-04-27 22:03:57 +02:00
cb = field[(i + 1) * omaxy];
field[(i + 1) * omaxy] = '\0';
out_send(&field[i * omaxy]);
field[(i + 1) * omaxy] = cb;
out_send("\n");
if (((curline++ % height) == 0) && (i < maxx - 1) && !nobreakp) {
out_printf("%s\n%s\n\014\n%s\n%s\n",
line1, line2, line2, line1);
2000-04-27 22:03:57 +02:00
curline += 5;
}
}
2000-04-27 22:03:57 +02:00
out_printf("%s\n%s\n", line1, line2);
txfree(field);
txfree(line1);
txfree(line2);
2000-04-27 22:03:57 +02:00
if (!novalue)
txfree(values);
2000-04-27 22:03:57 +02:00
}