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
|
2001-12-02 11:03:11 +01:00
|
|
|
Modified: 2001 Paolo Nenzi (printnum)
|
2012-12-09 13:10:26 +01:00
|
|
|
Patched: 2010/2012 by Bill Swartz (hash table for vectors)
|
2000-04-27 22:03:57 +02:00
|
|
|
**********/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Do a 'diff' of two plots.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-11-27 02:59:56 +01:00
|
|
|
#include "ngspice/dstring.h"
|
2011-12-11 19:05:00 +01:00
|
|
|
#include "ngspice/dvec.h"
|
2019-11-27 02:59:56 +01:00
|
|
|
#include "ngspice/ftedefs.h"
|
|
|
|
|
#include "ngspice/hash.h"
|
|
|
|
|
#include "ngspice/ngspice.h"
|
2011-12-11 19:05:00 +01:00
|
|
|
#include "ngspice/sim.h"
|
2000-05-06 16:12:51 +02:00
|
|
|
|
2000-04-27 22:03:57 +02:00
|
|
|
#include "diff.h"
|
2000-06-27 18:09:02 +02:00
|
|
|
#include "variable.h"
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2019-11-27 02:59:56 +01:00
|
|
|
static bool nameeq(const char *n1, const char *n2);
|
|
|
|
|
static char *canonical_name(const char *name, DSTRINGPTR dbuf_p,
|
2019-05-27 22:30:10 +02:00
|
|
|
bool make_i_name_lower);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2012-12-09 13:10:26 +01:00
|
|
|
static char *
|
2019-11-27 02:59:56 +01:00
|
|
|
canonical_name(const char *name, DSTRINGPTR dbuf_p,
|
2019-05-27 22:30:10 +02:00
|
|
|
bool make_i_name_lower)
|
2012-12-09 13:10:26 +01:00
|
|
|
{
|
2019-11-27 02:59:56 +01:00
|
|
|
ds_clear(dbuf_p); /* Reset dynamic buffer */
|
2019-05-27 22:30:10 +02:00
|
|
|
|
|
|
|
|
/* "i(some_name)" -> "some_name#branch" */
|
|
|
|
|
/* "I(some_name)" -> "some_name#branch" */
|
|
|
|
|
if (ciprefix("i(", name)) {
|
|
|
|
|
static const char sz_branch[] = "#branch";
|
|
|
|
|
const char *p_start = name + 2;
|
|
|
|
|
size_t n = strlen(p_start) - 1; /* copy all but final ')' */
|
2019-11-27 02:59:56 +01:00
|
|
|
ds_case_t case_type = make_i_name_lower ?
|
|
|
|
|
ds_case_lower : ds_case_as_is;
|
|
|
|
|
bool f_ok = ds_cat_mem_case(dbuf_p, p_start, (int) n,
|
|
|
|
|
case_type) == DS_E_OK;
|
|
|
|
|
f_ok &= ds_cat_mem(dbuf_p, sz_branch,
|
|
|
|
|
sizeof sz_branch / sizeof *sz_branch - 1) == DS_E_OK;
|
|
|
|
|
if (!f_ok) {
|
|
|
|
|
controlled_exit(-1);
|
2019-05-27 22:30:10 +02:00
|
|
|
}
|
2019-11-27 02:59:56 +01:00
|
|
|
return ds_get_buf(dbuf_p);
|
2019-05-27 22:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Convert a name starting with a digit, such as a numbered node to
|
|
|
|
|
* something like v(33) */
|
|
|
|
|
if (isdigit_c(*name)) {
|
2019-11-27 02:59:56 +01:00
|
|
|
bool f_ok = ds_cat_mem(dbuf_p, "v(", 2) == DS_E_OK;
|
|
|
|
|
f_ok &= ds_cat_str(dbuf_p, name) == DS_E_OK;
|
|
|
|
|
f_ok &= ds_cat_char(dbuf_p, ')') == DS_E_OK;
|
|
|
|
|
if (!f_ok) {
|
|
|
|
|
controlled_exit(-1);
|
|
|
|
|
}
|
|
|
|
|
return ds_get_buf(dbuf_p);
|
2012-12-09 13:10:26 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-27 22:30:10 +02:00
|
|
|
/* Finally if neither of the special cases above occur, there is
|
|
|
|
|
* no need to do anything with the name. A slight improvement in
|
|
|
|
|
* performance could be achieved by simply returning the name
|
|
|
|
|
* argument. Making a copy ensures that it can be modified without
|
|
|
|
|
* changing the original, but in the current use cases that is
|
|
|
|
|
* not an issue. */
|
2019-11-27 02:59:56 +01:00
|
|
|
if (ds_cat_str(dbuf_p, name) != DS_E_OK) {
|
|
|
|
|
controlled_exit(-1);
|
|
|
|
|
}
|
|
|
|
|
return ds_get_buf(dbuf_p);
|
2019-05-27 22:30:10 +02:00
|
|
|
} /* end of function canonical_name */
|
2012-12-09 13:10:26 +01:00
|
|
|
|
|
|
|
|
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2019-05-27 22:30:10 +02:00
|
|
|
/* Determine if two vectors have the 'same' name. Note that this compare can
|
|
|
|
|
* be performed by using the "canonical" forms returned by
|
|
|
|
|
* canonical_name(). */
|
2000-04-27 22:03:57 +02:00
|
|
|
static bool
|
2019-05-27 22:30:10 +02:00
|
|
|
nameeq(const char *n1, const char *n2)
|
2000-04-27 22:03:57 +02:00
|
|
|
{
|
2019-05-27 22:30:10 +02:00
|
|
|
/* First compare them the way they came in, case insensitive.
|
|
|
|
|
* If they match nothing more to do */
|
|
|
|
|
if (cieq(n1, n2)) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2012-09-20 20:30:53 +02:00
|
|
|
|
2019-11-27 02:59:56 +01:00
|
|
|
/* Init the dynamic string buffers to build canonical names */
|
|
|
|
|
DS_CREATE(ds1, 100);
|
|
|
|
|
DS_CREATE(ds2, 100);
|
2008-11-20 11:47:23 +01:00
|
|
|
|
2019-05-27 22:30:10 +02:00
|
|
|
/* Compare canonical names */
|
|
|
|
|
const BOOL rc = (BOOL) cieq(canonical_name(n1, &ds1, FALSE),
|
|
|
|
|
canonical_name(n2, &ds2, FALSE));
|
2008-11-20 11:47:23 +01:00
|
|
|
|
2019-05-27 22:30:10 +02:00
|
|
|
/* Free the dynamic string buffers */
|
2019-11-27 02:59:56 +01:00
|
|
|
ds_free(&ds1);
|
|
|
|
|
ds_free(&ds2);
|
2019-05-27 22:30:10 +02:00
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
} /* end of function nameeq */
|
2008-11-20 11:47:23 +01:00
|
|
|
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2012-09-20 20:30:53 +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;
|
2001-12-02 11:03:11 +01:00
|
|
|
int i, j;
|
2019-05-27 22:30:10 +02:00
|
|
|
char *v1_name; /* canonical v1 name */
|
|
|
|
|
char *v2_name; /* canonical v2 name */
|
2012-12-09 13:10:26 +01:00
|
|
|
NGHASHPTR crossref_p; /* cross reference hash table */
|
2000-04-27 22:03:57 +02:00
|
|
|
wordlist *tw;
|
2012-09-20 20:30:53 +02:00
|
|
|
char numbuf[BSIZE_SP], numbuf2[BSIZE_SP], numbuf3[BSIZE_SP], numbuf4[BSIZE_SP]; /* For printnum */
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2018-07-21 18:22:18 +02:00
|
|
|
if (!cp_getvar("diff_vntol", CP_REAL, &vntol, 0))
|
2000-04-27 22:03:57 +02:00
|
|
|
vntol = 1.0e-6;
|
2018-07-21 18:22:18 +02:00
|
|
|
if (!cp_getvar("diff_abstol", CP_REAL, &abstol, 0))
|
2000-04-27 22:03:57 +02:00
|
|
|
abstol = 1.0e-12;
|
2018-07-21 18:22:18 +02:00
|
|
|
if (!cp_getvar("diff_reltol", CP_REAL, &reltol, 0))
|
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) {
|
2012-09-20 20:30:53 +02:00
|
|
|
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) &&
|
2012-09-20 20:30:53 +02:00
|
|
|
!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) {
|
2012-09-20 20:30:53 +02:00
|
|
|
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) {
|
2012-09-20 20:30:53 +02:00
|
|
|
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))
|
2012-09-20 20:30:53 +02:00
|
|
|
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))
|
2012-09-20 20:30:53 +02:00
|
|
|
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
|
|
|
|
2012-12-09 13:10:26 +01: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;
|
2012-12-09 13:10:26 +01:00
|
|
|
|
2019-11-27 02:59:56 +01:00
|
|
|
DS_CREATE(ibuf, 100); /* used to build canonical name */
|
2012-12-09 13:10:26 +01:00
|
|
|
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;
|
2019-05-27 22:30:10 +02:00
|
|
|
v2_name = canonical_name(v2->v_name, &ibuf, TRUE);
|
2012-12-09 13:10:26 +01:00
|
|
|
nghash_insert(crossref_p, v2_name, v2);
|
|
|
|
|
}
|
2000-04-27 22:03:57 +02:00
|
|
|
|
2012-12-09 13:10:26 +01:00
|
|
|
for (v1 = p1->pl_dvecs; v1; v1 = v1->v_next) {
|
2019-05-27 22:30:10 +02:00
|
|
|
v1_name = canonical_name(v1->v_name, &ibuf, TRUE);
|
2012-12-09 13:10:26 +01:00
|
|
|
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)) ==
|
2012-09-20 20:30:53 +02:00
|
|
|
(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;
|
|
|
|
|
}
|
2012-12-09 13:10:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 02:59:56 +01:00
|
|
|
ds_free(&ibuf);
|
2012-12-09 13:10:26 +01:00
|
|
|
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)
|
2012-09-20 20:30:53 +02:00
|
|
|
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",
|
2012-09-20 20:30:53 +02:00
|
|
|
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)
|
2012-09-20 20:30:53 +02:00
|
|
|
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",
|
2012-09-20 20:30:53 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-20 20:30:53 +02:00
|
|
|
|
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) {
|
2012-09-20 20:30:53 +02:00
|
|
|
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,
|
2012-09-20 20:30:53 +02:00
|
|
|
p1->pl_typename, v2->v_length, p2->pl_typename);
|
2000-04-27 22:03:57 +02:00
|
|
|
break;
|
|
|
|
|
} else if (v2->v_length <= i) {
|
2012-09-20 20:30:53 +02:00
|
|
|
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,
|
2012-09-20 20:30:53 +02:00
|
|
|
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 +
|
2012-09-20 20:30:53 +02:00
|
|
|
tol < fabs(d1 - d2)) {
|
|
|
|
|
printnum(numbuf, d1);
|
2000-04-27 22:03:57 +02:00
|
|
|
fprintf(cp_out,
|
2012-09-20 20:30:53 +02:00
|
|
|
"%s.%s[%d] = %-15s ",
|
|
|
|
|
p1->pl_typename, v1->v_name, i, numbuf);
|
2001-12-02 11:03:11 +01:00
|
|
|
printnum(numbuf, d2);
|
2000-04-27 22:03:57 +02:00
|
|
|
fprintf(cp_out,
|
2012-09-20 20:30:53 +02:00
|
|
|
"%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];
|
2012-02-07 20:53:12 +01:00
|
|
|
realpart(c3) = realpart(c1) - realpart(c2);
|
|
|
|
|
imagpart(c3) = imagpart(c1) - imagpart(c2);
|
2000-04-27 22:03:57 +02:00
|
|
|
/* Stupid evil PC compilers */
|
2012-02-07 20:49:31 +01:00
|
|
|
cm1 = cmag(c1);
|
|
|
|
|
cm2 = cmag(c2);
|
2000-04-27 22:03:57 +02:00
|
|
|
cmax = MAX(cm1, cm2);
|
2012-09-20 20:30:53 +02:00
|
|
|
if (cmax * reltol + tol < cmag(c3)) {
|
|
|
|
|
|
|
|
|
|
printnum(numbuf, realpart(c1));
|
2012-02-07 20:53:12 +01:00
|
|
|
printnum(numbuf2, imagpart(c1));
|
|
|
|
|
printnum(numbuf3, realpart(c2));
|
|
|
|
|
printnum(numbuf4, imagpart(c2));
|
2012-09-20 20:30:53 +02:00
|
|
|
|
2000-04-27 22:03:57 +02:00
|
|
|
fprintf(cp_out,
|
2012-09-20 20:30:53 +02:00
|
|
|
"%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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|