static void xtend(struct dvec *v, int length) { int i; if (v->v_length == length) return; if (v->v_length > length) { dvec_trunc(v, length); return; } i = v->v_length; dvec_realloc(v, length, NULL); if (isreal(v)) { double d = NAN; if (i > 0) d = v->v_realdata[i - 1]; while (i < length) v->v_realdata[i++] = d; } else { ngcomplex_t c = {NAN, NAN}; if (i > 0) c = v->v_compdata[i - 1]; while (i < length) v->v_compdata[i++] = c; } }
static void compress(struct dvec *d, double *xcomp, double *xind) { int cfac, ihi, ilo, newlen, i; if (xind) { ilo = (int) xind[0]; ihi = (int) xind[1]; if ((ihi >= ilo) && (ilo > 0) && (ilo < d->v_length) && (ihi > 1) && (ihi <= d->v_length)) { newlen = ihi - ilo; if (isreal(d)) { double *dd = TMALLOC(double, newlen); memcpy(dd, d->v_realdata + ilo, (size_t) newlen * sizeof(double)); dvec_realloc(d, newlen, dd); } else { ngcomplex_t *cc = TMALLOC(ngcomplex_t, newlen); memcpy(cc, d->v_compdata + ilo, (size_t) newlen * sizeof(ngcomplex_t)); dvec_realloc(d, newlen, cc); } }
void vec_transpose(struct dvec *v) { int dim0, dim1, nummatrices; int i, j, k, joffset, koffset, blocksize; double *newreal, *oldreal; ngcomplex_t *newcomp, *oldcomp; if (v->v_numdims < 2 || v->v_length <= 1) return; dim0 = v->v_dims[v->v_numdims-1]; dim1 = v->v_dims[v->v_numdims-2]; v->v_dims[v->v_numdims-1] = dim1; v->v_dims[v->v_numdims-2] = dim0; /* Assume length is a multiple of each dimension size. * This may not be safe, in which case a test should be * made that the length is the product of all the dimensions. */ blocksize = dim0*dim1; nummatrices = v->v_length / blocksize; /* Note: * olda[i,j] is at data[i*dim0+j] * newa[j,i] is at data[j*dim1+i] * where j is in [0, dim0-1] and i is in [0, dim1-1] * Since contiguous data in the old array is scattered in the new array * we can't use bcopy :(. There is probably a BLAS2 function for this * though. The formulation below gathers scattered old data into * consecutive new data. */ if (isreal(v)) { newreal = TMALLOC(double, v->v_length); oldreal = v->v_realdata; koffset = 0; for (k = 0; k < nummatrices; k++) { joffset = 0; for (j = 0; j < dim0; j++) { for (i = 0; i < dim1; i++) { newreal[ koffset + joffset + i ] = oldreal[ koffset + i*dim0 + j ]; } joffset += dim1; /* joffset = j*dim0 */ } koffset += blocksize; /* koffset = k*blocksize = k*dim0*dim1 */ } dvec_realloc(v, v->v_length, newreal); } else {
static struct plot * oldread(char *name) { struct plot *pl; char buf[BSIZE_SP]; struct dvec *v, *end = NULL; short nv; /* # vars */ long np; /* # points/var. */ long i, j; short a; /* The magic number. */ float f1, f2; FILE *fp; if (!(fp = fopen(name, "r"))) { perror(name); return (NULL); } pl = alloc(struct plot); tfread(buf, 1, 80, fp); buf[80] = '\0'; for (i = (int) strlen(buf) - 1; (i > 1) && (buf[i] == ' '); i--) ; buf[i + 1] = '\0'; pl->pl_title = copy(buf); tfread(buf, 1, 16, fp); buf[16] = '\0'; pl->pl_date = copy(fixdate(buf)); tfread(&nv, sizeof (short), 1, fp); tfread(&a, sizeof (short), 1, fp); if (a != 4) fprintf(cp_err, "Warning: magic number 4 is wrong...\n"); for (i = 0; i < nv; i++) { v = dvec_alloc(NULL, SV_NOTYPE, 0, 0, NULL); if (end) end->v_next = v; else pl->pl_scale = pl->pl_dvecs = v; end = v; tfread(buf, 1, 8, fp); buf[8] = '\0'; v->v_name = copy(buf); } for (v = pl->pl_dvecs; v; v = v->v_next) { tfread(&a, sizeof (short), 1, fp); v->v_type = a; } /* If the first output variable is type FREQ then there is complex * data, otherwise the data is real. */ i = pl->pl_dvecs->v_type; if ((i == SV_FREQUENCY) || (i == SV_POLE) || (i == SV_ZERO)) for (v = pl->pl_dvecs; v; v = v->v_next) v->v_flags |= VF_COMPLEX; else for (v = pl->pl_dvecs; v; v = v->v_next) v->v_flags |= VF_REAL; /* Check the node indices -- this shouldn't be a problem ever. */ for (i = 0; i < nv; i++) { tfread(&a, sizeof(short), 1, fp); if (a != i + 1) fprintf(cp_err, "Warning: output %d should be %ld\n", a, i); } tfread(buf, 1, 24, fp); buf[24] = '\0'; pl->pl_name = copy(buf); /* Now to figure out how many points of data there are left in * the file. */ i = ftell(fp); (void) fseek(fp, 0L, SEEK_END); j = ftell(fp); (void) fseek(fp, i, SEEK_SET); i = j - i; if (i % 8) { /* Data points are always 8 bytes... */ fprintf(cp_err, "Error: alignment error in data\n"); (void) fclose(fp); return (NULL); } i = i / 8; if (i % nv) { fprintf(cp_err, "Error: alignment error in data\n"); (void) fclose(fp); return (NULL); } np = i / nv; for (v = pl->pl_dvecs; v; v = v->v_next) { dvec_realloc(v, (int) np, NULL); } for (i = 0; i < np; i++) { /* Read in the output vector for point i. If the type is * complex it will be float and we want double. */ for (v = pl->pl_dvecs; v; v = v->v_next) { if (v->v_flags & VF_REAL) { tfread(&v->v_realdata[i], sizeof (double), 1, fp); } else { tfread(&f1, sizeof (float), 1, fp); tfread(&f2, sizeof (float), 1, fp); realpart(v->v_compdata[i]) = f1; imagpart(v->v_compdata[i]) = f2; } } } (void) fclose(fp); return (pl); }
struct dvec * vec_get(const char *vec_name) { struct dvec *d, *end = NULL, *newv = NULL; struct plot *pl; char buf[BSIZE_SP], *s, *wd, *word, *whole, *name = NULL, *param; int i = 0; struct variable *vv; wd = word = copy(vec_name); /* Gets mangled below... */ if (strchr(word, '.')) { /* Snag the plot... */ for (i = 0, s = word; *s != '.'; i++, s++) buf[i] = *s; buf[i] = '\0'; if (cieq(buf, "all")) { word = ++s; pl = NULL; /* NULL pl signifies a wildcard. */ } else { for (pl = plot_list; pl && !plot_prefix(buf, pl->pl_typename); pl = pl->pl_next) ; if (pl) { word = ++s; } else { /* This used to be an error... */ pl = plot_cur; } } } else { pl = plot_cur; } if (pl) { d = vec_fromplot(word, pl); if (!d) d = vec_fromplot(word, &constantplot); } else { for (pl = plot_list; pl; pl = pl->pl_next) { if (cieq(pl->pl_typename, "const")) continue; d = vec_fromplot(word, pl); if (d) { if (end) end->v_link2 = d; else newv = d; for (end = d; end->v_link2; end = end->v_link2) ; } } d = newv; if (!d) { fprintf(cp_err, "Error: plot wildcard (name %s) matches nothing\n", word); tfree(wd); /* MW. I don't want core leaks here */ return (NULL); } } if (!d && (*word == SPECCHAR)) { /* This is a special quantity... */ if (ft_nutmeg) { fprintf(cp_err, "Error: circuit parameters only available with spice\n"); tfree(wd); /* MW. Memory leak fixed again */ return (NULL); /* va: use NULL */ } whole = copy(word); name = ++word; for (param = name; *param && (*param != '['); param++) ; if (*param) { *param++ = '\0'; for (s = param; *s && *s != ']'; s++) ; *s = '\0'; } else { param = NULL; } if (ft_curckt) { /* * This is what is done in case of "alter r1 resistance = 1234" * r1 resistance, 0 * if_setparam(ft_curckt->ci_ckt, &dev, param, dv, do_model); */ /* vv = if_getparam (ft_curckt->ci_ckt, &name, param, 0, 0); */ vv = if_getparam(ft_curckt->ci_ckt, &name, param, 0, 0); if (!vv) { tfree(whole); tfree(wd); return (NULL); } } else { fprintf(cp_err, "Error: No circuit loaded.\n"); tfree(whole); tfree(wd); return (NULL); } d = dvec_alloc(copy(whole), /* MW. The same as word before */ SV_NOTYPE, VF_REAL, /* No complex values yet... */ 1, NULL); /* In case the represented variable is a REAL vector this takes * the actual value of the first element of the linked list which * does not make sense. * This is an error. */ /* This will copy the contents of the structure vv in another structure * dvec (FTEDATA.H) that do not have INTEGER so that those parameters * defined as IF_INTEGER are not given their value when using * print @pot[pos_node] * To fix this, it is necessary to define: * OPU( "pos_node", POT_QUEST_POS_NODE, IF_REAL,"Positive node of potenciometer"), * int POTnegNode; // number of negative node of potenciometer (Nodo_3) * case POT_QUEST_POS_NODE: * value->rValue = (double)fast->POTposNode; * return (OK); * Works but with the format 1.00000E0 */ /* We must make a change in format between the data that carries a variable to * put in a dvec. */ /* * #define va_bool va_V.vV_bool * #define va_num va_V.vV_num * #define va_real va_V.vV_real * #define va_string va_V.vV_string * #define va_vlist va_V.vV_list * enum cp_types { * CP_BOOL, * CP_NUM, * CP_REAL, * CP_STRING, * CP_LIST ° }; */ /* The variable is a vector */ if (vv->va_type == CP_LIST) { /* Compute the length of the vector, * used with the parameters of isrc and vsrc */ struct variable *nv; i = 0; for (nv = vv->va_vlist; nv; nv = nv->va_next) i++; dvec_realloc(d, i, NULL); i = 0; for (nv = vv->va_vlist; nv; nv = nv->va_next) d->v_realdata[i++] = nv->va_real; /* To be able to identify the vector to represent * belongs to a special "conunto" and should be printed in a * special way. */ d->v_dims[1] = 1; } else if (vv->va_type == CP_NUM) { /* Variable is an integer */ *d->v_realdata = (double) vv->va_num; } else if (vv->va_type == CP_REAL) { /* Variable is a real */ if (!(vv->va_next)) { /* Only a real data * usually normal */ *d->v_realdata = vv->va_real; } else { /* Real data set * When you print a model @ [all] * Just print numerical values, not the string */ struct variable *nv; /* We go to print the list of values * nv->va_name = Parameter description * nv->va_string = Parameter * nv->va_real= Value */ nv = vv; for (i = 1; ; i++) { switch (nv->va_type) { case CP_REAL: fprintf(stdout, "%s=%g\n", nv->va_name, nv->va_real); break; case CP_STRING: fprintf(stdout, "%s=%s\n", nv->va_name, nv->va_string); break; case CP_NUM: fprintf(stdout, "%s=%d\n", nv->va_name, nv->va_num); break; default: { fprintf(stderr, "ERROR: enumeration value `CP_BOOL' or `CP_LIST' not handled in vec_get\nAborting...\n"); controlled_exit(EXIT_FAILURE); } } nv = nv->va_next; if (!nv) break; } /* To distinguish those does not take anything for print screen to * make a print or M1 @ @ M1 [all] leaving only the correct data * and not the last */ d->v_rlength = 1; } } tfree(vv->va_name); tfree(vv); /* va: tfree vv->va_name and vv (avoid memory leakages) */ tfree(wd); vec_new(d); tfree(whole); return (d); } tfree(wd); return (sortvecs(d)); }