From 0183964d6f6379b1bf6080080aff2448af145447 Mon Sep 17 00:00:00 2001 From: Jim Monte Date: Wed, 1 May 2019 16:20:06 +0200 Subject: [PATCH] [PATCH #41] Made constants agree across ngspice simulator. File const.h is the source of the values with all other files referencing it. --- src/frontend/cpitf.c | 38 +++++++------------- src/include/ngspice/const.h | 68 +++++++++++++++++++++++++++-------- src/include/ngspice/defines.h | 34 ++++++++---------- src/main.c | 27 +++++++------- 4 files changed, 95 insertions(+), 72 deletions(-) diff --git a/src/frontend/cpitf.c b/src/frontend/cpitf.c index e442d2f92..9d07e6fdf 100644 --- a/src/frontend/cpitf.c +++ b/src/frontend/cpitf.c @@ -4,6 +4,7 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group ***********/ #include "ngspice/ngspice.h" +#include "ngspice/const.h" #include "ngspice/cpdefs.h" #include "ngspice/ftedefs.h" #include "ngspice/dvec.h" @@ -23,6 +24,11 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group /* Set some standard variables and aliases, etc, and init the ccom stuff. Called by fcn main() */ +/* Macros to expand a macro to its value and then quote that value */ +#undef stringit +#undef stringit2 +#define stringit2(x) #x +#define stringit(x) stringit2(x) void ft_cpinit(void) { @@ -37,32 +43,14 @@ ft_cpinit(void) "TRUE", "1", "no", "0", "FALSE", "0", - "pi", "3.14159265358979323846", - "e", "2.71828182845904523536", - - /* https://physics.nist.gov/cgi-bin/cuu/Value?e - * value = 299 792 458 m s-1 (exact) */ - "c", "299792458", - + "pi", stringit(CONSTpi), + "e", stringit(CONSTnap), + "c", stringit(CONSTc), "i", "0,1", - - /* https://www.nist.gov/pml/weights-and-measures/si-units-temperature */ - "kelvin", "-273.15", - - /* https://physics.nist.gov/cgi-bin/cuu/Value?e - * value = 1.602 176 6208 x 10-19 C - * standard uncertainty = 0.000 000 0098 x 10-19 C */ - "echarge", "1.6021766208E-19", - - /* https://physics.nist.gov/cgi-bin/cuu/Value?k|search_for=physchem_in! - * value = 1.380 648 52 x 10-23 J K-1 - * standard uncertainty = 0.000 000 79 x 10-23 J K-1 */ - "boltz", "1.38064852E-23", - - /* https://physics.nist.gov/cgi-bin/cuu/Value?h - * value = 6.626 070 040 x 10-34 J s - * standard uncertainty = 0.000 000 081 x 10-34 J s */ - "planck", "6.626070040E-34" + "kelvin", stringit(CONSTKtoC_for_str), + "echarge", stringit(CHARGE), + "boltz", stringit(CONSTboltz), + "planck", stringit(CONSTplanck) }; static char *udfs[] = { diff --git a/src/include/ngspice/const.h b/src/include/ngspice/const.h index 1a96ef640..c688fa12c 100644 --- a/src/include/ngspice/const.h +++ b/src/include/ngspice/const.h @@ -7,17 +7,57 @@ Author: 1985 Thomas L. Quarles #ifndef ngspice_CONST_H #define ngspice_CONST_H -#define CHARGE (1.6021918e-19) -#define CONSTCtoK (273.15) -#define CONSTboltz (1.3806226e-23) -#define CONSTepsZero (8.854214871e-12) /* epsilon zero F/m */ -#define CONSTepsSiO2 (3.4531479969e-11) /* epsilon SiO2 F/m */ -#define CONSTmuZero (1.25663706143592e-6) /* MuZero H/m */ -#define REFTEMP 300.15 /* 27 degrees C */ - - extern double CONSTroot2; - extern double CONSTvt0; - extern double CONSTKoverQ; - extern double CONSTe; - -#endif +#define CONSTsqrt2 1.4142135623730950488016887242097 +#define CONSTpi 3.1415926535897932384626433832795 +#define CONSTnap 2.7182818284590452353602874713527 +#define CONSTlog10e 0.43429448190325182765112891891661 +#define CONSTlog2e 1.4426950408889634073599246810019 + + +/* https://physics.nist.gov/cgi-bin/cuu/Value?c + * value = 299 792 458 m s-1 (exact) */ +#define CONSTc 299792458 + +/* https://www.nist.gov/pml/weights-and-measures/si-units-temperature + * Note that for general use in an expression, the negative value must + * be guarded by (), so CONSTKtoC_for_str should only be used for building + * a string value */ +#define CONSTCtoK 273.15 +#define CONSTKtoC_for_str -CONSTCtoK +#define CONSTKtoC (CONSTKtoC_for_str) + +/* https://physics.nist.gov/cgi-bin/cuu/Value?e + * value = 1.602 176 6208 x 10-19 C + * standard uncertainty = 0.000 000 0098 x 10-19 C */ +#define CHARGE 1.6021766208e-19 + +/* https://physics.nist.gov/cgi-bin/cuu/Value?k + * value = 1.380 648 52 x 10-23 J K-1 + * standard uncertainty = 0.000 000 79 x 10-23 J K-1 */ +#define CONSTboltz 1.38064852e-23 + +/* https://physics.nist.gov/cgi-bin/cuu/Value?h + * value = 6.626 070 040 x 10-34 J s + * standard uncertainty = 0.000 000 081 x 10-34 J s */ +#define CONSTplanck 6.626070040e-34 + +#define CONSTmuZero (4.0 * CONSTpi * 1E-7) /* MuZero H/m */ + +/* epsilon zero e0*u0*c*c=1 */ +#define CONSTepsZero (1.0 / (CONSTmuZero * CONSTc * CONSTc)) /* F/m */ + +/* This value is not really constant over temperature and frequency, but + * 3.9 is the most common "all-purpose" value */ +#define CONSTepsrSiO2 3.9 + +#define CONSTepsSiO2 (CONSTepsrSiO2 * CONSTepsZero) /* epsilon SiO2 F/m */ +#define REFTEMP (27.0 + CONSTCtoK) /* 27 degrees C in K */ + + +/* Some global variables defining constant values */ +extern double CONSTroot2; +extern double CONSTvt0; +extern double CONSTKoverQ; +extern double CONSTe; + +#endif /* include guard */ diff --git a/src/include/ngspice/defines.h b/src/include/ngspice/defines.h index 99c28c302..e95f650ce 100644 --- a/src/include/ngspice/defines.h +++ b/src/include/ngspice/defines.h @@ -13,21 +13,26 @@ #ifndef ngspice_DEFINES_H #define ngspice_DEFINES_H +/* + * Physical constants (const.h) + */ + /* For definitions of CHARGE, CONSTCtoK CONSTboltz, CONSTepsZero, + * CONSTepsSi02, CONSTmuZero, REFTEMP */ +#include "ngspice/const.h" -#ifndef M_PI -# define M_PI 3.14159265358979323846 +#ifndef M_PI +#define M_PI CONSTpi #endif -#ifndef M_E -# define M_E 2.7182818284590452354 +#ifndef M_E +#define M_E CONSTnap #endif -#ifndef M_LOG2E -# define M_LOG2E 1.4426950408889634074 +#ifndef M_LOG2E +#define M_LOG2E CONSTlog2e #endif -#ifndef M_LOG10E -# define M_LOG10E 0.43429448190325182765 +#ifndef M_LOG10E +#define M_LOG10E CONSTlog10e #endif - /* * IEEE Floating point */ @@ -55,18 +60,7 @@ #define MAXPOSINT INT_MAX -/* - * Physical constants (const.h) - */ - -#define CHARGE (1.6021918e-19) -#define CONSTCtoK (273.15) -#define CONSTboltz (1.3806226e-23) -#define CONSTepsZero (8.854214871e-12) /* epsilon zero F/m */ -#define CONSTepsSiO2 (3.4531479969e-11) /* epsilon SiO2 F/m */ -#define CONSTmuZero (1.25663706143592e-6) /* MuZero H/m */ -#define REFTEMP 300.15 /* 27 degrees C */ /* Standard initialisation file name */ #define INITSTR ".spiceinit" diff --git a/src/main.c b/src/main.c index d994fe4cb..0d2c60e97 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ */ #include "ngspice/ngspice.h" +#include "ngspice/const.h" #include #include @@ -463,12 +464,13 @@ EVTfindvec(char *node) { char *hlp_filelist[] = { "ngspice", NULL }; -/* allocate space for global constants in 'CONST.h' */ +/* Allocate space for global constants declared in const.h + * and set their values */ +double CONSTroot2 = CONSTsqrt2; +double CONSTvt0 = CONSTboltz * REFTEMP / CHARGE; +double CONSTKoverQ = CONSTboltz / CHARGE; +double CONSTe = CONSTnap; -double CONSTroot2; -double CONSTvt0; -double CONSTKoverQ; -double CONSTe; IFfrontEnd *SPfrontEnd = NULL; int DEVmaxnum = 0; @@ -482,9 +484,10 @@ SIMinit(IFfrontEnd *frontEnd, IFsimulator **simulator) SIMinfo.numDevices = DEVmaxnum = num_devices(); SIMinfo.devices = devices_ptr(); SIMinfo.numAnalyses = spice_num_analysis(); - SIMinfo.analyses = (IFanalysis **)spice_analysis_ptr(); /* va: we recast, because we use - * only the public part - */ + + /* va: we recast, because we use only the public part */ + SIMinfo.analyses = (IFanalysis **)spice_analysis_ptr(); + #ifdef CIDER /* Evaluates limits of machine accuracy for CIDER */ @@ -495,12 +498,10 @@ SIMinit(IFfrontEnd *frontEnd, IFsimulator **simulator) SPfrontEnd = frontEnd; *simulator = &SIMinfo; - CONSTroot2 = sqrt(2.); - CONSTvt0 = CONSTboltz * (27 /* deg c */ + CONSTCtoK) / CHARGE; - CONSTKoverQ = CONSTboltz / CHARGE; - CONSTe = exp(1.0); + return OK; -} +} /* end of function SIMinit */ + /* -------------------------------------------------------------------------- */