mirror of
https://git.code.sf.net/p/ngspice/ngspice
synced 2026-08-22 13:57:32 +02:00
Add analog code model astate.
It reports the previous state (delayed by 1 to 3 time steps) of the input node. Single or differential voltage or current.
This commit is contained in:
@@ -0,0 +1,188 @@
|
||||
/*.......1.........2.........3.........4.........5.........6.........7.........8
|
||||
================================================================================
|
||||
|
||||
FILE astate/cfunc.mod
|
||||
|
||||
3-clause BSD
|
||||
|
||||
Copyright 2025
|
||||
The ngspice team
|
||||
|
||||
AUTHORS
|
||||
|
||||
20 September 2025 Holger Vogt
|
||||
|
||||
|
||||
MODIFICATIONS
|
||||
|
||||
|
||||
|
||||
SUMMARY
|
||||
|
||||
This file contains the functional description of the analog
|
||||
state code model.
|
||||
|
||||
|
||||
INTERFACES
|
||||
|
||||
FILE ROUTINE CALLED
|
||||
|
||||
|
||||
|
||||
REFERENCED FILES
|
||||
|
||||
Inputs from and outputs to ARGS structure.
|
||||
|
||||
|
||||
NON-STANDARD FEATURES
|
||||
|
||||
NONE
|
||||
|
||||
===============================================================================*/
|
||||
|
||||
/*=== INCLUDE FILES ====================*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
/*=== CONSTANTS ========================*/
|
||||
|
||||
|
||||
|
||||
|
||||
/*=== MACROS ===========================*/
|
||||
|
||||
|
||||
|
||||
|
||||
/*=== LOCAL VARIABLES & TYPEDEFS =======*/
|
||||
|
||||
typedef struct {
|
||||
double state1; /* first state value */
|
||||
double state2; /* second state value */
|
||||
double state3; /* third state value */
|
||||
double outval; /* output value */
|
||||
double xval1; /* first x value */
|
||||
double xval2; /* second x value */
|
||||
double xval3; /* third x value */
|
||||
} stLocal_Data_t;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*=== FUNCTION PROTOTYPE DEFINITIONS ===*/
|
||||
|
||||
static void cm_astate_callback(ARGS, Mif_Callback_Reason_t reason);
|
||||
|
||||
/*=== CM_STATE ROUTINE ===*/
|
||||
|
||||
void cm_astate(ARGS)
|
||||
{
|
||||
int state_number = 0;
|
||||
double outval = 0.0;
|
||||
|
||||
stLocal_Data_t *loc; /* Pointer to local static data, not to be included
|
||||
in the state vector */
|
||||
|
||||
|
||||
if (ANALYSIS != MIF_AC) { /**** only Transient Analysis and dc ****/
|
||||
|
||||
/** INIT: allocate storage **/
|
||||
|
||||
if (INIT==1) {
|
||||
|
||||
CALLBACK = cm_astate_callback;
|
||||
|
||||
/*** allocate static storage for *loc ***/
|
||||
if ((loc = (stLocal_Data_t *) (STATIC_VAR(locdata) = calloc(1,
|
||||
sizeof(stLocal_Data_t)))) == (stLocal_Data_t *) NULL) {
|
||||
cm_message_send("Unable to allocate Local_Data_t "
|
||||
"in cm_astate()");
|
||||
return;
|
||||
}
|
||||
loc->state1 = 0;
|
||||
loc->state2 = 0;
|
||||
loc->state3 = 0;
|
||||
loc->outval = 0;
|
||||
loc->xval1 = 0;
|
||||
loc->xval2 = 0;
|
||||
loc->xval3 = 0;
|
||||
}
|
||||
|
||||
/* retrieve previous values */
|
||||
|
||||
loc = STATIC_VAR (locdata);
|
||||
|
||||
state_number = PARAM(astate_no);
|
||||
|
||||
if (state_number == 0) {
|
||||
OUTPUT(out) = INPUT(in);
|
||||
return;
|
||||
}
|
||||
|
||||
if (TIME == loc->xval1) {
|
||||
switch(state_number) {
|
||||
case 1:
|
||||
loc->outval = loc->state1;
|
||||
break;
|
||||
case 2:
|
||||
loc->outval = loc->state2;
|
||||
break;
|
||||
case 3:
|
||||
loc->outval = loc->state3;
|
||||
break;
|
||||
default:
|
||||
loc->outval = INPUT(in);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (TIME > loc->xval1) {
|
||||
loc->state3 = loc->state2;
|
||||
loc->state2 = loc->state1;
|
||||
loc->state1 = INPUT(in);
|
||||
|
||||
loc->xval3 = loc->xval2;
|
||||
loc->xval2 = loc->xval1;
|
||||
loc->xval1 = TIME;
|
||||
}
|
||||
/* initial time iteration */
|
||||
else if (TIME == 0.0) {
|
||||
loc->state1 = loc->outval = INPUT(in);
|
||||
loc->xval1 = 0.0;
|
||||
}
|
||||
/* time step rejected ? */
|
||||
else if (TIME < loc->xval1){
|
||||
loc->state1 = INPUT(in);
|
||||
loc->xval1 = TIME;
|
||||
}
|
||||
/* output */
|
||||
if (ANALYSIS == MIF_TRAN) {
|
||||
OUTPUT(out) = loc->outval;
|
||||
}
|
||||
else { /* dc */
|
||||
OUTPUT(out) = INPUT(in);
|
||||
}
|
||||
}
|
||||
else {
|
||||
OUTPUT(out) = INPUT(in);
|
||||
}
|
||||
}
|
||||
|
||||
/* free the memory created locally */
|
||||
static void cm_astate_callback(ARGS, Mif_Callback_Reason_t reason)
|
||||
{
|
||||
switch (reason) {
|
||||
case MIF_CB_DESTROY: {
|
||||
stLocal_Data_t *loc = (stLocal_Data_t *) STATIC_VAR(locdata);
|
||||
if (loc == (stLocal_Data_t *) NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
free(loc);
|
||||
|
||||
STATIC_VAR(locdata) = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} /* end of function cm_astate_callback */
|
||||
@@ -0,0 +1,58 @@
|
||||
/*.......1.........2.........3.........4.........5.........6.........7.........8
|
||||
================================================================================
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
Copyright 2025
|
||||
The ngspice team
|
||||
3 - Clause BSD license
|
||||
(see COPYING or https://opensource.org/licenses/BSD-3-Clause)
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
AUTHORS
|
||||
|
||||
20 September 2025 Holger Vogt
|
||||
|
||||
|
||||
SUMMARY
|
||||
|
||||
This file contains the interface specification file for the
|
||||
astate code model.
|
||||
|
||||
===============================================================================*/
|
||||
|
||||
NAME_TABLE:
|
||||
|
||||
C_Function_Name: cm_astate
|
||||
Spice_Model_Name: astate
|
||||
Description: "analog state return"
|
||||
|
||||
|
||||
PORT_TABLE:
|
||||
|
||||
Port_Name: in out
|
||||
Description: "input" "output"
|
||||
Direction: in out
|
||||
Default_Type: v v
|
||||
Allowed_Types: [v,vd,vnam,i,id] [v,vd,i,id]
|
||||
Vector: no no
|
||||
Vector_Bounds: - -
|
||||
Null_Allowed: no no
|
||||
|
||||
|
||||
PARAMETER_TABLE:
|
||||
|
||||
Parameter_Name: astate_no
|
||||
Description: "state to be returned"
|
||||
Data_Type: int
|
||||
Default_Value: 1
|
||||
Limits: [0 3]
|
||||
Vector: no
|
||||
Vector_Bounds: -
|
||||
Null_Allowed: yes
|
||||
|
||||
|
||||
STATIC_VAR_TABLE:
|
||||
|
||||
Static_Var_Name: locdata
|
||||
Description: "local static data"
|
||||
Data_Type: pointer
|
||||
@@ -20,3 +20,4 @@ triangle
|
||||
file_source
|
||||
delay
|
||||
pwlts
|
||||
astate
|
||||
|
||||
Reference in New Issue
Block a user