ngspice/src/frontend/diff.c

328 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
Modified: 2001 Paolo Nenzi (printnum)
Patched: 2010/2012 by Bill Swartz (hash table for vectors)
2000-04-27 22:03:57 +02:00
**********/
/*
* Do a 'diff' of two plots.
*/
#include "ngspice/ngspice.h"
#include "ngspice/ftedefs.h"
#include "ngspice/dvec.h"
#include "ngspice/sim.h"
* src/Makefile.am src/main.c src/sconvert.c src/analysis/cktdisto.c src/analysis/cktnoise.c src/analysis/noisean.c: Updates for the new header files. * src/maths/cmaths/cmath1.c src/maths/cmaths/cmath2.c src/maths/cmaths/cmath3.c src/maths/cmaths/cmath4.c: Updates for the new header files. * src/frontend/.cvsignore src/frontend/Makefile.am: Updates for the new files. * src/frontend/agraf.c src/frontend/aspice.c src/frontend/breakp.c src/frontend/breakp2.c src/frontend/circuits.c src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/define.c src/frontend/diff.c src/frontend/dimens.c src/frontend/display.c src/frontend/doplot.c src/frontend/dotcards.c src/frontend/evaluate.c src/frontend/fourier.c src/frontend/graf.c src/frontend/grid.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/interp.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/misccoms.h src/frontend/miscvars.c src/frontend/mw_coms.c src/frontend/newcoms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/plotcurv.c src/frontend/points.c src/frontend/postcoms.c src/frontend/rawfile.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/typesdef.c src/frontend/vectors.c src/frontend/where.c src/frontend/postcoms.c: Updates for the new header files. Some commands have moved into the new files below. * src/frontend/README src/frontend/com_compose.c src/frontend/com_compose.h src/frontend/com_display.c src/frontend/com_display.h src/frontend/com_let.c src/frontend/com_let.h src/frontend/com_setscale.c src/frontend/com_setscale.h src/frontend/commands.c src/frontend/commands.h src/frontend/completion.h src/frontend/streams.h src/frontend/testcommands.c: Separation into different com_* commands. This is a start. The rest of the subdirectory needs doing. * src/include/complex.h src/include/cpdefs.h src/include/cpextern.h src/include/cpstd.h src/include/fteconst.h src/include/ftedata.h src/include/ftedev.h src/include/fteext.h src/include/ftegraph.h src/include/fteparse.h src/include/dvec.h src/include/grid.h src/include/plot.h src/include/pnode.h src/include/sim.h src/include/variable.h src/include/wordlist.h src/include/bool.h: Separation of header files into smaller pieces. This limits recompilation to only the affected source files. The original header files have a warning message embedded to flag obsoleted use. * src/frontend/compose.c src/frontend/compose.h src/frontend/nutctab.c src/frontend/nutctab.h src/frontend/plot5.c src/frontend/plot5.h src/frontend/spcmdtab.c src/frontend/x11.c src/frontend/x11.h src/frontend/xgraph.c src/frontend/xgraph.h: Moved these files into src/frontend/plotting subdirectory. * src/frontend/plotting/.cvsignore src/frontend/plotting/Makefile.am src/frontend/plotting/plot5.c src/frontend/plotting/plot5.h src/frontend/plotting/plotting.c src/frontend/plotting/plotting.h src/frontend/plotting/pvec.c src/frontend/plotting/pvec.h src/frontend/plotting/x11.c src/frontend/plotting/x11.h src/frontend/plotting/xgraph.c src/frontend/plotting/xgraph.h: The new libplotting library with automake and CVS infrastructure.
2000-05-06 16:12:51 +02:00
2000-04-27 22:03:57 +02:00
#include "diff.h"
#include <ngspice/hash.h>
#include <ngspice/dstring.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
#include "variable.h"
2000-04-27 22:03:57 +02:00
static char *
cannonical_name(char *name, SPICE_DSTRINGPTR dbuf_p)
{
char *tmp; /* position in string */
char *ptr; /* current position in string */
spice_dstring_reinit(dbuf_p);
if (ciprefix("i(",name)) {
tmp = name;
while (*tmp != '(')
tmp++;
tmp++;
for (ptr = tmp; *ptr; ptr++)
if (isupper(*ptr))
tmp = spice_dstring_append_char(dbuf_p, (char)tolower(*ptr));
else
tmp = spice_dstring_append_char(dbuf_p, *ptr);
while (*tmp != ')')
tmp++;
*tmp = '\0';
tmp = spice_dstring_append(dbuf_p, "#branch", -1);
} else if (isdigit(*name)) {
spice_dstring_append(dbuf_p, "v(", -1);
spice_dstring_append(dbuf_p, name, -1);
tmp = spice_dstring_append_char(dbuf_p, ')');
} else {
tmp = spice_dstring_append(dbuf_p, name, -1);
}
return(tmp);
}
2000-04-27 22:03:57 +02:00
/* Determine if two vectors have the 'same' name. */
static bool
nameeq(char *n1, char *n2)
{
char buf1[BSIZE_SP], buf2[BSIZE_SP];
char *tmp;
2000-04-27 22:03:57 +02:00
if (eq(n1, n2))
return (TRUE);
/* n1 or n2 is in the form i(...) or I(...)
* This happens in the saved rawfile
*/
if (ciprefix("i(", n1)) {
tmp = n1;
while (*tmp != '(')
tmp++;
tmp++;
(void) strcpy(buf1, tmp);
tmp = buf1;
while (*tmp != ')')
tmp++;
*tmp = '\0';
(void) strcat(buf1, "#branch");
} else if (isdigit(*n1)) {
(void) sprintf(buf1, "v(%s)", n1);
} else {
(void) strcpy(buf1, n1);
}
if (ciprefix("i(", n2)) {
tmp = n2;
while (*tmp != '(')
tmp++;
tmp++;
(void) strcpy(buf2, tmp);
tmp = buf2;
while (*tmp != ')')
tmp++;
*tmp = '\0';
(void) strcat(buf2, "#branch");
} else if (isdigit(*n2)) {
(void) sprintf(buf2, "v(%s)", n2);
} else {
(void) strcpy(buf2, n2);
}
2010-11-06 18:43:52 +01:00
return (cieq(buf1, buf2) ? TRUE : FALSE);
2000-04-27 22:03:57 +02:00
}
2000-04-27 22:03:57 +02:00
void
com_diff(wordlist *wl)
{
double vntol, abstol, reltol, tol, cmax, cm1, cm2;
struct plot *p1, *p2 = NULL;
struct dvec *v1, *v2;
double d1, d2;
2010-10-24 14:45:05 +02:00
ngcomplex_t c1, c2, c3;
int i, j;
char *v1_name; /* cannonical v1 name */
char *v2_name; /* cannonical v2 name */
NGHASHPTR crossref_p; /* cross reference hash table */
SPICE_DSTRING ibuf; /* used to build cannonical name */
2000-04-27 22:03:57 +02:00
wordlist *tw;
char numbuf[BSIZE_SP], numbuf2[BSIZE_SP], numbuf3[BSIZE_SP], numbuf4[BSIZE_SP]; /* For printnum */
2000-04-27 22:03:57 +02:00
2011-04-30 14:29:19 +02:00
if (!cp_getvar("diff_vntol", CP_REAL, &vntol))
2000-04-27 22:03:57 +02:00
vntol = 1.0e-6;
2011-04-30 14:29:19 +02:00
if (!cp_getvar("diff_abstol", CP_REAL, &abstol))
2000-04-27 22:03:57 +02:00
abstol = 1.0e-12;
2011-04-30 14:29:19 +02:00
if (!cp_getvar("diff_reltol", CP_REAL, &reltol))
2000-04-27 22:03:57 +02:00
reltol = 0.001;
/* Let's try to be clever about defaults. This code is ugly. */
if (!wl || !wl->wl_next) {
if (plot_list && plot_list->pl_next && !plot_list->pl_next->pl_next) {
2000-04-27 22:03:57 +02:00
p1 = plot_list;
p2 = plot_list->pl_next;
if (wl && !eq(wl->wl_word, p1->pl_typename) &&
!eq(wl->wl_word, p2->pl_typename)) {
2000-04-27 22:03:57 +02:00
fprintf(cp_err, "Error: no such plot \"%s\"\n",
wl->wl_word);
return;
}
fprintf(cp_err, "Plots are \"%s\" and \"%s\"\n",
plot_list->pl_typename,
plot_list->pl_next->pl_typename);
if (wl)
wl = NULL;
} else {
fprintf(cp_err, "Error: plot names not given.\n");
return;
}
} else {
for (p1 = plot_list; p1; p1 = p1->pl_next)
if (eq(wl->wl_word, p1->pl_typename))
break;
if (!p1) {
fprintf(cp_err, "Error: no such plot %s\n", wl->wl_word);
2000-04-27 22:03:57 +02:00
return;
}
wl = wl->wl_next;
}
if (!p2) {
for (p2 = plot_list; p2; p2 = p2->pl_next)
if (eq(wl->wl_word, p2->pl_typename))
break;
if (!p2) {
fprintf(cp_err, "Error: no such plot %s\n", wl->wl_word);
2000-04-27 22:03:57 +02:00
return;
}
wl = wl->wl_next;
}
/* Now do some tests to make sure these plots are really the
* same type, etc.
*/
if (!eq(p1->pl_name, p2->pl_name))
fprintf(cp_err,
"Warning: plots %s and %s seem to be of different types\n",
p1->pl_typename, p2->pl_typename);
2000-04-27 22:03:57 +02:00
if (!eq(p1->pl_title, p2->pl_title))
fprintf(cp_err,
"Warning: plots %s and %s seem to be from different circuits\n",
p1->pl_typename, p2->pl_typename);
2000-04-27 22:03:57 +02:00
/* This may not be the best way to do this. It wasn't :). The original
* was O(n2) - not good. Now use a hash table to reduce it to O(n). */
2000-04-27 22:03:57 +02:00
for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next)
v1->v_link2 = NULL;
spice_dstring_init(&ibuf);
crossref_p = nghash_init(NGHASH_MIN_SIZE);
nghash_unique(crossref_p, FALSE);
for (v2 = p2->pl_dvecs; v2; v2 = v2->v_next) {
2000-04-27 22:03:57 +02:00
v2->v_link2 = NULL;
v2_name = cannonical_name(v2->v_name, &ibuf);
nghash_insert(crossref_p, v2_name, v2);
}
2000-04-27 22:03:57 +02:00
for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) {
v1_name = cannonical_name(v1->v_name, &ibuf);
for (v2 = nghash_find(crossref_p, v1_name);
v2;
v2 = nghash_find_again(crossref_p, v1_name))
{
if (!v2->v_link2 &&
2000-04-27 22:03:57 +02:00
((v1->v_flags & (VF_REAL | VF_COMPLEX)) ==
(v2->v_flags & (VF_REAL | VF_COMPLEX))) &&
(v1->v_type == v2->v_type))
{
2000-04-27 22:03:57 +02:00
v1->v_link2 = v2;
v2->v_link2 = v1;
break;
}
}
}
spice_dstring_free(&ibuf);
nghash_free(crossref_p, NULL, NULL);
2000-04-27 22:03:57 +02:00
for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next)
if (!v1->v_link2)
fprintf(cp_err,
">>> %s vector %s in %s not in %s, or of wrong type\n",
2000-04-27 22:03:57 +02:00
isreal(v1) ? "real" : "complex",
v1->v_name, p1->pl_typename, p2->pl_typename);
2000-04-27 22:03:57 +02:00
for (v2 = p2->pl_dvecs; v2; v2 = v2->v_next)
if (!v2->v_link2)
fprintf(cp_err,
">>> %s vector %s in %s not in %s, or of wrong type\n",
2000-04-27 22:03:57 +02:00
isreal(v2) ? "real" : "complex",
v2->v_name, p2->pl_typename, p1->pl_typename);
2000-04-27 22:03:57 +02:00
/* Throw out the ones that aren't in the arg list */
if (wl && !eq(wl->wl_word, "all")) { /* Just in case */
for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next)
if (v1->v_link2) {
for (tw = wl; tw; tw = tw->wl_next)
if (nameeq(v1->v_name, tw->wl_word))
break;
if (!tw)
v1->v_link2 = NULL;
}
for (v2 = p2->pl_dvecs; v2; v2 = v2->v_next)
if (v2->v_link2) {
for (tw = wl; tw; tw = tw->wl_next)
if (nameeq(v2->v_name, tw->wl_word))
break;
if (!tw)
v2->v_link2 = NULL;
}
}
2000-04-27 22:03:57 +02:00
/* Now we have all the vectors linked to their twins. Travel
* down each one and print values that differ enough.
*/
for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) {
if (!v1->v_link2)
continue;
v2 = v1->v_link2;
if (v1->v_type == SV_VOLTAGE)
tol = vntol;
else
tol = abstol;
j = MAX(v1->v_length, v2->v_length);
for (i = 0; i < j; i++) {
if (v1->v_length <= i) {
fprintf(cp_out,
">>> %s is %d long in %s and %d long in %s\n",
2000-04-27 22:03:57 +02:00
v1->v_name, v1->v_length,
p1->pl_typename, v2->v_length, p2->pl_typename);
2000-04-27 22:03:57 +02:00
break;
} else if (v2->v_length <= i) {
fprintf(cp_out,
">>> %s is %d long in %s and %d long in %s\n",
2000-04-27 22:03:57 +02:00
v2->v_name, v2->v_length,
p2->pl_typename, v1->v_length, p1->pl_typename);
2000-04-27 22:03:57 +02:00
break;
} else {
if (isreal(v1)) {
d1 = v1->v_realdata[i];
d2 = v2->v_realdata[i];
if (MAX(fabs(d1), fabs(d2)) * reltol +
tol < fabs(d1 - d2)) {
printnum(numbuf, d1);
2000-04-27 22:03:57 +02:00
fprintf(cp_out,
"%s.%s[%d] = %-15s ",
p1->pl_typename, v1->v_name, i, numbuf);
printnum(numbuf, d2);
2000-04-27 22:03:57 +02:00
fprintf(cp_out,
"%s.%s[%d] = %s\n",
p2->pl_typename, v2->v_name, i, numbuf);
2000-04-27 22:03:57 +02:00
}
} else {
c1 = v1->v_compdata[i];
c2 = v2->v_compdata[i];
realpart(c3) = realpart(c1) - realpart(c2);
imagpart(c3) = imagpart(c1) - imagpart(c2);
2000-04-27 22:03:57 +02:00
/* Stupid evil PC compilers */
cm1 = cmag(c1);
cm2 = cmag(c2);
2000-04-27 22:03:57 +02:00
cmax = MAX(cm1, cm2);
if (cmax * reltol + tol < cmag(c3)) {
printnum(numbuf, realpart(c1));
printnum(numbuf2, imagpart(c1));
printnum(numbuf3, realpart(c2));
printnum(numbuf4, imagpart(c2));
2000-04-27 22:03:57 +02:00
fprintf(cp_out,
"%s.%s[%d] = %-10s, %-10s %s.%s[%d] = %-10s, %s\n",
p1->pl_typename, v1->v_name, i,
numbuf,
numbuf2,
p2->pl_typename, v2->v_name, i,
numbuf3,
numbuf4);
2000-04-27 22:03:57 +02:00
}
}
}
}
}
}