* check_mdl.cpp - iterate an IC-CAP MDL file
*
* Copyright (C) 2006, 2007 Stefan Jahn <stefan@lkcc.org>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this package; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* $Id$
*
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <assert.h>
#include <float.h>
#include <ctype.h>
#include "logging.h"
#include "strlist.h"
#include "object.h"
#include "complex.h"
#include "vector.h"
#include "dataset.h"
#include "sweep.h"
#include "valuelist.h"
#include "constants.h"
#include "check_mdl.h"
#include "parse_mdl.hpp"
using namespace qucs;
dataset * mdl_result = NULL;
struct mdl_link_t * mdl_root = NULL;
struct mdl_sync_t * mdl_sync_root = NULL;
static void mdl_create_depdataset (sweep * data, char * name) {
vector v (name);
for (int i = 0; i < data->getSize (); i++) v.add (data->get (i));
mdl_result->appendDependency (new vector (v));
}
static void mdl_create_condataset (double val, char * name) {
vector v (name);
v.add (val);
mdl_result->appendDependency (new vector (v));
}
static void mdl_create_vardataset (struct mdl_point_t * point,
struct mdl_datasize_t * dsize,
const char * name, const char * type,
strlist * deps) {
vector * v = new vector[dsize->x * dsize->y] ();
if (!strcmp (type, "MEAS"))
type = ".M";
else if (!strcmp (type, "SIMU"))
type = ".S";
else if (!strcmp (type, "COMMON"))
type = "";
for (struct mdl_point_t * p = point; p != NULL; p = p->next) {
int n = (p->y - 1) * 2 + p->x - 1;
v[n].add (nr_complex_t (p->r, p->i));
}
for (int x = 1; x < dsize->x + 1; x++) {
for (int y = 1; y < dsize->y + 1; y++) {
int n = (y - 1) * 2 + x - 1;
char * txt = (char *) malloc (strlen (name) + strlen (type) + 4 + 2 * 3);
if (dsize->x > 1 || dsize->y > 1)
sprintf (txt, "%s%s[%d,%d]", name, type, x, y);
else
sprintf (txt, "%s%s", name, type);
v[n].setName (txt);
free (txt);
if (v[n].getSize () > 1) {
v[n].setDependencies (new strlist (*deps));
mdl_result->appendVariable (new vector (v[n]));
} else {
v[n].setDependencies (new strlist ());
mdl_result->appendDependency (new vector (v[n]));
}
}
}
delete[] v;
}
static char * mdl_find_helement (struct mdl_element_t * root,
const char * name) {
for (; root != NULL; root = root->next) {
if (!strcmp (root->name, name)) return root->value;
}
return NULL;
}
static char * mdl_find_telement (struct mdl_element_t * root,
const char * name) {
for (; root != NULL; root = root->next) {
if (!strcmp (root->name, "Name") && !strcmp (root->value, name)) {
if (root->next && !strcmp (root->next->name, "Value")) {
return root->next->value;
}
}
}
return NULL;
}
static double mdl_convert_factor (char * end) {
double f = 1.0;
if (end) {
switch (*end) {
case 'K': f = 1e+03; break;
case 'M': f = 1e+06; break;
case 'G': f = 1e+09; break;
case 'T': f = 1e+12; break;
case 'm': f = 1e-03; break;
case 'u': f = 1e-06; break;
case 'n': f = 1e-09; break;
case 'p': f = 1e-12; break;
case 'f': f = 1e-15; break;
case 'a': f = 1e-18; break;
}
}
return f;
}
static double mdl_telement_dvalue (struct mdl_link_t *, struct mdl_element_t *,
const char *);
static int mdl_resolve_variable (struct mdl_link_t * link, char * name,
double &val) {
int done = 0;
val = 0.0;
struct mdl_lcontent_t * root;
for (root = link->content; !done && root != NULL; root = root->next) {
if (root->type == t_TABLE) {
struct mdl_element_t * eroot = root->table->data;
if (mdl_find_telement (eroot, name)) {
val = mdl_telement_dvalue (link, eroot, name);
done++;
}
}
}
if (!done && link->parent) {
done = mdl_resolve_variable (link->parent, name, val);
}
return done;
}
static double mdl_variable_value (struct mdl_link_t * link, char * txt) {
double val = 0.0;
char * end = NULL;
if (txt != NULL) {
char * t, * p = txt;
while (*p) {
if (isspace (*p)) {
t = p;
while (*t) { *t = *(t + 1); t++; }
p--;
}
p++;
}
val = strtod (txt, &end);
if (end == txt) {
double f = 1.0;
if (*txt == '-') { f = -1.0; txt++; }
else if (*txt == '+') { f = +1.0; txt++; }
if (!mdl_resolve_variable (link, txt, val)) {
if (!strcmp (txt, "PI")) {
val = pi;
}
else {
logprint (LOG_ERROR,
"checker error, unable to resolve `%s' variable in '%s'\n",
txt, link->name);
val = 0.0;
}
}
val = f * val;
}
else {
val *= mdl_convert_factor (end);
}
}
return val;
}
static double mdl_helement_dvalue (struct mdl_link_t * link,
struct mdl_element_t * eroot,
const char * name) {
char * txt = mdl_find_helement (eroot, name);
return mdl_variable_value (link, txt);
}
static double mdl_telement_dvalue (struct mdl_link_t * link,
struct mdl_element_t * eroot,
const char * name) {
char * txt = mdl_find_telement (eroot, name);
return mdl_variable_value (link, txt);
}
static int mdl_helement_ivalue (struct mdl_link_t * link,
struct mdl_element_t * eroot,
const char * name) {
return (int) mdl_helement_dvalue (link, eroot, name);
}
static void mdl_find_vardataset (struct mdl_dcontent_t * droot, char * name,
strlist * deps) {
struct mdl_dcontent_t * root;
for (root = droot; root != NULL; root = root->next) {
if (root->type == t_DATASET) {
struct mdl_dataset_t * dset = root->data;
if (dset->data1)
mdl_create_vardataset (dset->data1, dset->dsize, name, dset->type1,
deps);
if (dset->data2)
mdl_create_vardataset (dset->data2, dset->dsize, name, dset->type2,
deps);
}
}
}
valuelist<int> * mdl_find_depdataset (struct mdl_link_t * link,
struct mdl_dcontent_t * droot,
char * name) {
char * stype = NULL;
double val, start, stop, step;
int nof = 0, order = 0;
valuelist<int> * deps = new valuelist<int> ();
struct mdl_dcontent_t * root;
for (root = droot; root != NULL; root = root->next) {
if (root->type == t_HYPTABLE) {
struct mdl_hyptable_t * hyptab = root->hyptable;
if (!strcmp (hyptab->name, "Edit Sweep Def")) {
if (!strcmp (stype, "LIN")) {
order = mdl_helement_ivalue (link, hyptab->data, "Sweep Order");
start = mdl_helement_dvalue (link, hyptab->data, "Start");
stop = mdl_helement_dvalue (link, hyptab->data, "Stop");
nof = mdl_helement_ivalue (link, hyptab->data, "# of Points");
step = mdl_helement_dvalue (link, hyptab->data, "Step Size");
if (nof <= 0) nof = (int) fabs ((stop - start) / step) + 1;
deps->insert({{name,order}});
linsweep * sw = new linsweep ();
sw->create (start, stop, nof);
mdl_create_depdataset (sw, name);
delete sw;
}
else if (!strcmp (stype, "CON")) {
val = mdl_helement_dvalue (link, hyptab->data, "Value");
mdl_create_condataset (val, name);
}
else if (!strcmp (stype, "LOG")) {
order = mdl_helement_ivalue (link, hyptab->data, "Sweep Order");
start = mdl_helement_dvalue (link, hyptab->data, "Start");
stop = mdl_helement_dvalue (link, hyptab->data, "Stop");
nof = mdl_helement_ivalue (link, hyptab->data, "Total Pts");
if (nof <= 0)
nof = mdl_helement_ivalue (link, hyptab->data, "# of Points");
if (start * stop == 0.0) {
if (start == 0.0) start = 1.0;
if (stop == 0.0) stop = 1.0;
}
deps->insert({{name,order}});
logsweep * sw = new logsweep ();
sw->create (start, stop, nof);
mdl_create_depdataset (sw, name);
delete sw;
}
else if (!strcmp (stype, "LIST")) {
order = mdl_helement_ivalue (link, hyptab->data, "Sweep Order");
nof = mdl_helement_ivalue (link, hyptab->data, "# of Values");
deps->insert({{name,order}});
}
else if (!strcmp (stype, "SYNC")) {
struct mdl_sync_t * sync = (struct mdl_sync_t *)
calloc (sizeof (struct mdl_sync_t), 1);
sync->ratio = mdl_helement_dvalue (link, hyptab->data, "Ratio");
sync->offset = mdl_helement_dvalue (link, hyptab->data, "Offset");
sync->master = mdl_find_helement (hyptab->data, "Master Sweep");
sync->master = strdup (sync->master);
sync->name = strdup (name);
sync->next = mdl_sync_root;
mdl_sync_root = sync;
}
}
else if (!strcmp (hyptab->name, "Edit Sweep Info")) {
stype = mdl_find_helement (hyptab->data, "Sweep Type");
}
else if (!strcmp (hyptab->name, "List Table")) {
lstsweep * sw = new lstsweep ();
sw->create (nof);
char txt[16];
for (int i = 0; i < nof; i++) {
sprintf (txt, "Value %d", i + 1);
val = mdl_helement_dvalue (link, hyptab->data, txt);
sw->set (i, val);
}
mdl_create_depdataset (sw, name);
delete sw;
}
}
}
return deps;
}
static char * mdl_create_linkname (char * base, char * name) {
char * txt = (char *) malloc (strlen (base) + 2 + strlen (name));
sprintf (txt, "%s.%s", base, name);
return txt;
}
static void mdl_find_deplink (struct mdl_link_t * link, char * name,
valuelist<int> * deps) {
struct mdl_lcontent_t * root;
const valuelist<int> * d;
for (root = link->content; root != NULL; root = root->next) {
if (root->type == t_DATA) {
d = mdl_find_depdataset (link, root->data->content, name);
if (d != NULL) {
valuelist<int> copy = *d;
delete d;
copy.insert(deps->begin(),deps->end());
*deps=copy;
}
}
else if (root->type == t_LINK && !strcmp (root->link->type, "SWEEP")) {
char * txt = mdl_create_linkname (name, root->link->name);
root->link->parent = link;
mdl_find_deplink (root->link, txt, deps);
free (txt);
}
}
}
static void mdl_find_varlink (struct mdl_link_t * link, char * name,
strlist * deps) {
struct mdl_lcontent_t * root;
for (root = link->content; root != NULL; root = root->next) {
if (root->type == t_DATA) {
mdl_find_vardataset (root->data->content, name, deps);
}
else if (root->type == t_LINK && (!strcmp (root->link->type, "OUT") ||
!strcmp (root->link->type, "XFORM"))) {
char * txt = mdl_create_linkname (name, root->link->name);
root->link->parent = link;
mdl_find_varlink (root->link, txt, deps);
free (txt);
}
}
}
static strlist * mdl_sort_deps (valuelist<int> * d) {
strlist * deps = new strlist ();
for (int i = 0; i < d->size(); i++) {
for (auto &val: *d) {
if (val.second == i + 1) {
deps->append (val.first.c_str());
}
}
}
return deps;
}
static void mdl_find_link (struct mdl_link_t * link, char * name) {
struct mdl_lcontent_t * root;
valuelist<int> * vdeps = new valuelist<int> ();
mdl_find_deplink (link, name, vdeps);
strlist * deps = mdl_sort_deps (vdeps);
delete vdeps;
mdl_find_varlink (link, name, deps);
delete deps;
for (root = link->content; root != NULL; root = root->next) {
if (root->type == t_LINK &&
strcmp (root->link->type, "OUT") &&
strcmp (root->link->type, "SWEEP") &&
strcmp (root->link->type, "XFORM")) {
char * txt = mdl_create_linkname (name, root->link->name);
root->link->parent = link;
mdl_find_link (root->link, txt);
free (txt);
}
}
}
static void mdl_create_syndataset (vector * v, char * name) {
v->setName (name);
mdl_result->appendDependency (v);
}
void mdl_find_syncdatasets (struct mdl_sync_t * root) {
struct mdl_sync_t * sync;
for (sync = root; sync != NULL; sync = sync->next) {
char * link = sync->name;
int i = strlen (link) - 1;
while (i > 0 && link[i] != '.') i--;
if (link[i] == '.') {
link[i] = '\0';
char * txt = (char *) malloc (i + 2 + strlen (sync->master));
sprintf (txt, "%s.%s", link, sync->master);
link[i] = '.';
free (sync->master);
sync->master = txt;
}
vector * v = mdl_result->findDependency (sync->master);
if (v != NULL) {
vector * s = new vector ((*v) * sync->ratio + sync->offset);
mdl_create_syndataset (s, sync->name);
}
}
}
static void mdl_free_element (struct mdl_element_t * e) {
free (e->name);
free (e->value);
free (e->attr);
free (e);
}
static void mdl_free_datasize (struct mdl_datasize_t * d) {
free (d->type);
free (d);
}
static void mdl_free_hyptable (struct mdl_hyptable_t * h) {
free (h->name);
struct mdl_element_t * e, * next;
for (e = h->data; e != NULL; e = next) {
next = e->next;
mdl_free_element (e);
}
free (h);
}
static void mdl_free_table (struct mdl_table_t * t) {
free (t->name);
struct mdl_element_t * e, * next;
for (e = t->data; e != NULL; e = next) {
next = e->next;
mdl_free_element (e);
}
free (t);
}
static void mdl_free_dataset (struct mdl_dataset_t * d) {
free (d->type1);
struct mdl_point_t * p, * next;
for (p = d->data1; p != NULL; p = next) {
next = p->next;
free (p);
}
free (d->type2);
for (p = d->data2; p != NULL; p = next) {
next = p->next;
free (p);
}
if (d->dsize) mdl_free_datasize (d->dsize);
}
static void mdl_free_dcontent (struct mdl_dcontent_t * c) {
switch (c->type) {
case t_DATASET: mdl_free_dataset (c->data); break;
case t_HYPTABLE: mdl_free_hyptable (c->hyptable); break;
}
}
static void mdl_free_data (struct mdl_data_t * d) {
struct mdl_dcontent_t * c, * next;
for (c = d->content; c != NULL; c = next) {
next = c->next;
mdl_free_dcontent (c);
}
}
static void mdl_free_link (struct mdl_link_t *);
static void mdl_free_lcontent (struct mdl_lcontent_t * c) {
switch (c->type) {
case t_LINK: mdl_free_link (c->link); break;
case t_DATA: mdl_free_data (c->data); break;
case t_TABLE: mdl_free_table (c->table); break;
}
free (c);
}
static void mdl_free_link (struct mdl_link_t * l) {
free (l->name);
free (l->type);
struct mdl_lcontent_t * c, * next;
for (c = l->content; c != NULL; c = next) {
next = c->next;
mdl_free_lcontent (c);
}
}
static void mdl_free_sync (struct mdl_sync_t * s) {
struct mdl_sync_t * next;
for (; s != NULL; s = next) {
next = s->next;
free (s->name);
free (s->master);
free (s);
}
}
static int mdl_get_depsize (strlist * deps) {
char * n;
vector * v;
int res = 1;
for (int i = 0; i < deps->length (); i++) {
if ((n = deps->get (i)) != NULL)
if ((v = mdl_result->findDependency (n)) != NULL)
res *= v->getSize ();
}
return res;
}
static void mdl_check_xform_deplen (void) {
vector * v, * next;
for (v = mdl_result->getVariables (); v; v = next) {
next = (vector *) v->getNext ();
strlist * deps = v->getDependencies ();
if (deps->length () <= 0) {
vector * d = new vector (*v);
mdl_result->delVariable (v);
mdl_result->addDependency (d);
}
}
}
static void mdl_check_xform_dep (void) {
vector * v, * d;
strlist * deps;
for (v = mdl_result->getVariables (); v; v = (vector *) v->getNext ()) {
deps = v->getDependencies ();
int s = mdl_get_depsize (deps);
if (v->getSize () != s) {
int found = 0;
for (int i = 0; i < deps->length (); i++) {
char * n = deps->get (i);
if (n != NULL) {
d = mdl_result->findDependency (n);
if (d != NULL && v->getSize () == d->getSize ()) {
strlist * dep = new strlist ();
dep->add (n);
v->setDependencies (dep);
found++;
break;
}
}
}
if (!found) v->setDependencies (new strlist ());
}
}
}
static void mdl_check_xforms (void) {
mdl_check_xform_dep ();
mdl_check_xform_deplen ();
}
success, non-zero otherwise. */
int mdl_check (void) {
int errors = 0;
mdl_result = new dataset ();
struct mdl_link_t * root;
for (root = mdl_root; root; root = root->next) {
char * name = root->name;
mdl_find_link (root, name);
}
mdl_find_syncdatasets (mdl_sync_root);
mdl_check_xforms ();
return errors ? -1 : 0;
}
void mdl_destroy (void) {
if (mdl_result != NULL) {
delete mdl_result;
mdl_result = NULL;
}
if (mdl_root != NULL) {
struct mdl_link_t * root, * next;
for (root = mdl_root; root; root = next) {
next = root->next;
mdl_free_link (root);
}
mdl_root = NULL;
}
if (mdl_sync_root != NULL) {
mdl_free_sync (mdl_sync_root);
mdl_sync_root = NULL;
}
}
void mdl_init (void) {
mdl_root = NULL;
mdl_result = NULL;
mdl_sync_root = NULL;
}