Advertisement
Guest User

sharedspice.h

a guest
Oct 16th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.95 KB | None | 0 0
  1. /* header file for shared ngspice */
  2. /* Copyright 2013 Holger Vogt */
  3. /* Modified BSD license */
  4.  
  5. /*
  6. Interface between a calling program (caller) and ngspice.dll (ngspice.so)
  7.  
  8. **
  9. ngSpice_Init(SendChar*, SendStat*, ControlledExit*,
  10.              SendData*, SendInitData*, BGThreadRunning*, void*)
  11. After caller has loaded ngspice.dll, the simulator has to be initialized
  12. by calling ngSpice_Init(). Address pointers of several callback functions
  13. defined in the caller are sent to ngspice.dll.
  14.  
  15. Callback funtion typedefs
  16. SendChar       typedef of callback function for reading printf, fprintf, fputs
  17. SendStat       typedef of callback function for reading status string and precent value
  18. ControlledExit typedef of callback function for tranferring a signal upon
  19.                ngspice controlled_exit to caller. May be used by caller
  20.                to detach ngspice.dll.
  21. SendData       typedef of callback function for sending an array of structs containing
  22.                data values of all vectors in the current plot (simulation output)
  23. SendInitData   typedef of callback function for sending an array of structs containing info on
  24.                all vectors in the current plot (immediately before simulation starts)
  25. BGThreadRunning typedef of callback function for sending a boolean signal (true if thread
  26.                 is running)
  27.  
  28. The void pointer may contain the object address of the calling
  29. function ('self' or 'this' pointer), so that the answer may be directed
  30. to a calling object. Callback functions are defined in the global section.
  31.  
  32. **
  33. ngSpice_Command(char*)
  34. Send a valid command (see the control or interactive commands) from caller
  35. to ngspice.dll. Will be executed immediately (as if in interactive mode).
  36. Some commands are rejected (e.g. 'plot', because there is no graphics interface).
  37. Command 'quit' will remove internal data, and then send a notice to caller via
  38. ngexit().
  39.  
  40. **
  41. ngGet_Vec_Info(char*)
  42. receives the name of a vector (may be in the form 'vectorname' or
  43. <plotname>.vectorname) and returns a pointer to a vector_info struct.
  44. The caller may then directly assess the vector data (but probably should
  45. not modify them).
  46.  
  47. **
  48. ngSpice_Circ(char**)
  49. sends an array of null-terminated char* to ngspice.dll. Each char* contains a
  50. single line of a circuit (each line like in an input file **.sp). The last
  51. entry to char** has to be NULL. Upon receiving the arry, ngspice.dll will
  52. immediately parse the input and set up the circuit structure (as if received
  53. the circuit from a file by the 'source' command.
  54.  
  55. **
  56. char* ngSpice_CurPlot();
  57. returns to the caller a pointer to the name of the current plot
  58.  
  59. **
  60. char** ngSpice_AllPlots()
  61. returns to the caller a pointer to an array of all plots (by their typename)
  62.  
  63. **
  64. char** ngSpice_AllVecs(char*);
  65. returns to the caller a pointer to an array of vector names in the plot
  66. named by the string in the argument.
  67.  
  68. **
  69. Additional basics:
  70. No memory mallocing and freeing across the interface:
  71. Memory allocated in ngspice.dll has to be freed in ngspice.dll.
  72. Memory allocated in the calling program has to be freed only there.
  73.  
  74. ngspice.dll should never call exit() directly, but handle either the 'quit'
  75. request to the caller or an request for exiting upon error,
  76. done by callback function ngexit().
  77. */
  78.  
  79. #ifndef NGSPICE_DLL_H
  80. #define NGSPICE_DLL_H
  81.  
  82. #ifdef __cplusplus
  83. extern "C" {
  84. #endif
  85.  
  86. #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__CYGWIN__)
  87.   #ifdef SHARED_MODULE
  88.     #define IMPEXP __declspec(dllexport)
  89.   #else
  90.     #define IMPEXP __declspec(dllimport)
  91.   #endif
  92. #else
  93.   /* use with gcc flag -fvisibility=hidden */
  94.   #if __GNUC__ >= 4
  95.     #define IMPEXP __attribute__ ((visibility ("default")))
  96.     #define IMPEXPLOCAL  __attribute__ ((visibility ("hidden")))
  97.   #else
  98.     #define IMPEXP
  99.     #define IMPEXP_LOCAL
  100.   #endif
  101. #endif
  102.  
  103. /* required only if header is used by the caller,
  104.    is already defined in ngspice.dll */
  105. #ifndef ngspice_NGSPICE_H
  106. /* Complex numbers. */
  107. struct ngcomplex {
  108.     double cx_real;
  109.     double cx_imag;
  110. } ;
  111.  
  112. typedef struct ngcomplex ngcomplex_t;
  113. #endif
  114.  
  115. /* vector info obtained from any vector in ngspice.dll.
  116.    Allows direct access to the ngspice internal vector structure,
  117.    as defined in include/ngspice/devc.h . */
  118. typedef struct vector_info {
  119.     char *v_name;       /* Same as so_vname. */
  120.     int v_type;         /* Same as so_vtype. */
  121.     short v_flags;      /* Flags (a combination of VF_*). */
  122.     double *v_realdata;     /* Real data. */
  123.     ngcomplex_t *v_compdata;    /* Complex data. */
  124.     int v_length;       /* Length of the vector. */
  125. } vector_info, *pvector_info;
  126.  
  127. typedef struct vecvalues {
  128.     char* name;        /* name of a specific vector */
  129.     double creal;      /* actual data value */
  130.     double cimag;      /* actual data value */
  131.     bool is_scale;     /* if 'name' is the scale vector */
  132.     bool is_complex;   /* if the data are complex numbers */
  133. } vecvalues, *pvecvalues;
  134.  
  135. typedef struct vecvaluesall {
  136.     int veccount;      /* number of vectors in plot */
  137.     int vecindex;      /* index of actual set of vectors. i.e. the number of accepted data points */
  138.     pvecvalues *vecsa; /* values of actual set of vectors, indexed from 0 to veccount - 1 */
  139. } vecvaluesall, *pvecvaluesall;
  140.  
  141. /* info for a specific vector */
  142. typedef struct vecinfo
  143. {
  144.     int number;     /* number of vector, as postion in the linked list of vectors, starts with 0 */
  145.     char *vecname;  /* name of the actual vector */
  146.     bool is_real;   /* TRUE if the actual vector has real data */
  147.     void *pdvec;    /* a void pointer to struct dvec *d, the actual vector */
  148.     void *pdvecscale; /* a void pointer to struct dvec *ds, the scale vector */
  149. } vecinfo, *pvecinfo;
  150.  
  151. /* info for the current plot */
  152. typedef struct vecinfoall
  153. {
  154.     /* the plot */
  155.     char *name;
  156.     char *title;
  157.     char *date;
  158.     char *type;
  159.     int veccount;
  160.  
  161.     /* the data as an array of vecinfo with length equal to the number of vectors in the plot */
  162.     pvecinfo *vecs;
  163.  
  164. } vecinfoall, *pvecinfoall;
  165.  
  166.  
  167. /* callback functions
  168. addresses received from caller with ngSpice_Init() function
  169. */
  170. /* sending output from stdout, stderr to caller */
  171. typedef int (SendChar)(char*, int, void*);
  172. /*
  173.    char* string to be sent to caller output
  174.    int   identification number of calling ngspice shared lib
  175.    void* return pointer received from caller, e.g. pointer to object having sent the request
  176. */
  177. /* sending simulation status to caller */
  178. typedef int (SendStat)(char*, int, void*);
  179. /*
  180.    char* simulation status and value (in percent) to be sent to caller
  181.    int   identification number of calling ngspice shared lib
  182.    void* return pointer received from caller
  183. */
  184. /* asking for controlled exit */
  185. typedef int (ControlledExit)(int, bool, bool, int, void*);
  186. /*
  187.    int   exit status
  188.    bool  if true: immediate unloading dll, if false: just set flag, unload is done when function has returned
  189.    bool  if true: exit upon 'quit', if false: exit due to ngspice.dll error
  190.    int   identification number of calling ngspice shared lib
  191.    void* return pointer received from caller
  192. */
  193. /* send back actual vector data */
  194. typedef int (SendData)(pvecvaluesall, int, int, void*);
  195. /*
  196.    vecvaluesall* pointer to array of structs containing actual values from all vectors
  197.    int           number of structs (one per vector)
  198.    int           identification number of calling ngspice shared lib
  199.    void*         return pointer received from caller
  200. */
  201.  
  202. /* send back initailization vector data */
  203. typedef int (SendInitData)(pvecinfoall, int, void*);
  204. /*
  205.    vecinfoall* pointer to array of structs containing data from all vectors right after initialization
  206.    int         identification number of calling ngspice shared lib
  207.    void*       return pointer received from caller
  208. */
  209.  
  210. /* indicate if background thread is running */
  211. typedef int (BGThreadRunning)(bool, int, void*);
  212. /*
  213.    bool        true if background thread is running
  214.    int         identification number of calling ngspice shared lib
  215.    void*       return pointer received from caller
  216. */
  217.  
  218. /* callback functions
  219.    addresses received from caller with ngSpice_Init_Sync() function
  220. */
  221.  
  222. /* ask for VSRC EXTERNAL value */
  223. typedef int (GetVSRCData)(double*, double, char*, int, void*);
  224. /*
  225.    double*     return voltage value
  226.    double      actual time
  227.    char*       node name
  228.    int         identification number of calling ngspice shared lib
  229.    void*       return pointer received from caller
  230. */
  231.  
  232. /* ask for ISRC EXTERNAL value */
  233. typedef int (GetISRCData)(double*, double, char*, int, void*);
  234. /*
  235.    double*     return current value
  236.    double      actual time
  237.    char*       node name
  238.    int         identification number of calling ngspice shared lib
  239.    void*       return pointer received from caller
  240. */
  241.  
  242. /* ask for new delta time depending on synchronization requirements */
  243. typedef int (GetSyncData)(double, double*, double, int, int, int, void*);
  244. /*
  245.    double      actual time (ckt->CKTtime)
  246.    double*     delta time (ckt->CKTdelta)
  247.    double      old delta time (olddelta)
  248.    int         redostep (as set by ngspice)
  249.    int         identification number of calling ngspice shared lib
  250.    int         location of call for synchronization in dctran.c
  251.    void*       return pointer received from caller
  252. */
  253.  
  254. /* ngspice initialization,
  255. printfcn: pointer to callback function for reading printf, fprintf
  256. statfcn: pointer to callback function for the status string and percent value
  257. ControlledExit: pointer to callback function for setting a 'quit' signal in caller
  258. SendData: pointer to callback function for returning data values of all current output vectors
  259. SendInitData: pointer to callback function for returning information of all output vectors just initialized
  260. BGThreadRunning: pointer to callback function indicating if workrt thread is running
  261. userData: pointer to user-defined data, will not be modified, but
  262.           handed over back to caller during Callback, e.g. address of calling object */
  263. IMPEXP int  ngSpice_Init(SendChar* printfcn, SendStat* statfcn, ControlledExit* ngexit,
  264.                   SendData* sdata, SendInitData* sinitdata, BGThreadRunning* bgtrun, void* userData);
  265.  
  266. /* initialization of synchronizing functions
  267. vsrcdat: pointer to callback function for retrieving a voltage source value from caller
  268. isrcdat: pointer to callback function for retrieving a current source value from caller
  269. syncdat: pointer to callback function for synchronization
  270. ident: pointer to integer unique to this shared library (defaults to 0)
  271. userData: pointer to user-defined data, will not be modified, but
  272.           handed over back to caller during Callback, e.g. address of calling object.
  273.           If NULL is sent here, userdata info from ngSpice_Init() will be kept, otherwise
  274.           userdata will be overridden by new value from here.
  275. */
  276. IMPEXP int  ngSpice_Init_Sync(GetVSRCData *vsrcdat, GetISRCData *isrcdat, GetSyncData *syncdat, int *ident, void *userData);
  277.  
  278. /* Caller may send ngspice commands to ngspice.dll.
  279. Commands are executed immediately */
  280. IMPEXP int  ngSpice_Command(char* command);
  281.  
  282.  
  283. /* get info about a vector */
  284. IMPEXP pvector_info ngGet_Vec_Info(char* vecname);
  285.  
  286.  
  287. /* send a circuit to ngspice.dll
  288.    The circuit description is a dynamic array
  289.    of char*. Each char* corresponds to a single circuit
  290.    line. The last entry of the array has to be a NULL */
  291. IMPEXP int ngSpice_Circ(char** circarray);
  292.  
  293.  
  294. /* return to the caller a pointer to the name of the current plot */
  295. IMPEXP char* ngSpice_CurPlot(void);
  296.  
  297.  
  298. /* return to the caller a pointer to an array of all plots created
  299. so far by ngspice.dll */
  300. IMPEXP char** ngSpice_AllPlots(void);
  301.  
  302.  
  303. /* return to the caller a pointer to an array of vector names in the plot
  304. named by plotname */
  305. IMPEXP char** ngSpice_AllVecs(char* plotname);
  306.  
  307. /* returns TRUE if ngspice is running in a second (background) thread */
  308. IMPEXP bool ngSpice_running(void);
  309.  
  310. /* set a breakpoint in ngspice */
  311. IMPEXP bool ngSpice_SetBkpt(double time);
  312.  
  313.  
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317.  
  318. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement