📄 spice3.c
字号:
cvt_ftof(&dcvt_in_buffer[i], CVT_BIG_ENDIAN_IEEE_T,
&fcvt_out_buffer[i],FLOAT_CVT_TARGET,
CVT_FORCE_ALL_SPECIAL_VALUES);
}
for(i=0; i<data_size*data->num_variables; i++ )
data->variable_array[i][j] = fcvt_out_buffer[i];
} else {
for(i=0; i<data_size*data->num_variables; i++ )
data->variable_array[i][j] = (float) dcvt_in_buffer[i];
}
}
}
}
if( fcvt_in_buffer != NULL ) free(fcvt_in_buffer);
if( fcvt_out_buffer != NULL ) free(fcvt_out_buffer);
if( dcvt_in_buffer != NULL ) free(dcvt_in_buffer);
if( dcvt_out_buffer != NULL ) free(dcvt_out_buffer);
return data;
error:
if( data != NULL ) {
if( data->variable_array != NULL ) {
for(i=0; i<data->num_variables; i++) {
if( data->variable_array[i] != NULL ) free(data->variable_array[i]);
}
free(data->variable_array);
}
if( data->variable_names != NULL ) {
for(i=0; i<data->num_variables; i++) {
if( data->variable_names[i] != NULL ) free(data->variable_names[i]);
}
free(data->variable_names);
}
if( data->variable_units != NULL ) {
for(i=0; i<data->num_variables; i++) {
if( data->variable_units[i] != NULL ) free(data->variable_units[i]);
}
free(data->variable_units);
}
free(data);
}
if( fcvt_in_buffer != NULL ) free(fcvt_in_buffer);
if( fcvt_out_buffer != NULL ) free(fcvt_out_buffer);
if( dcvt_in_buffer != NULL ) free(dcvt_in_buffer);
if( dcvt_out_buffer != NULL ) free(dcvt_out_buffer);
return NULL;
}
/************************************************************************/
/* */
/* Close the spice3 file */
/* */
/************************************************************************/
void spice3_close (Spice3File f)
{
fclose(f);
}
/************************************************************************/
/* */
/* Free the spice3 data structure */
/* */
/************************************************************************/
void spice3_free_data(Spice3Data data)
{
int i;
if( data != NULL ) {
if( data->variable_array != NULL ) {
for(i=0; i<data->num_variables; i++) {
if( data->variable_array[i] != NULL ) free(data->variable_array[i]);
}
free(data->variable_array);
}
if( data->variable_names != NULL ) {
for(i=0; i<data->num_variables; i++) {
if( data->variable_names[i] != NULL ) free(data->variable_names[i]);
}
free(data->variable_names);
}
if( data->variable_units != NULL ) {
for(i=0; i<data->num_variables; i++) {
if( data->variable_units[i] != NULL ) free(data->variable_units[i]);
}
free(data->variable_units);
}
free(data);
}
}
/************************************************************************/
/* */
/* Return the number of simulation points */
/* */
/************************************************************************/
int spice3_num_transient_points(Spice3Data data)
{
int num_points = 0;
if( data != NULL ) {
num_points = data->num_points;
}
return num_points;
}
/************************************************************************/
/* */
/* Return the number of variables */
/* */
/************************************************************************/
int spice3_num_variables(Spice3Data data)
{
int num_variables = 0;
if( data != NULL ) {
num_variables = data->num_variables;
}
return num_variables;
}
/************************************************************************/
/* */
/* Return the simulation data type found */
/* */
/************************************************************************/
int spice3_get_simulation_type(Spice3Data data)
{
int type = 0;
if( data != NULL ) {
if( data->complex_flag == TRUE ) {
type = 2; /* AC small signal */
} else {
type = 1; /* transient */
}
}
return type;
}
/************************************************************************/
/* */
/* Return a simulation variable array */
/* */
/* The user is responsible for allocating memory for the array */
/* */
/************************************************************************/
int spice3_get_quantity_data(Spice3Data data, char *name,
float *array)
{
int num_points = 0;
int error_code = 0;
int type, i, index, index_r, index_i;
if( data != NULL && data->variable_array != NULL ) {
num_points = data->num_points;
/* Lookup up name that is being requested, find index into
array data. */
spice3_lookup_name(data, name, &index, &type);
if( index == -1 ) {
error_code = 10;
} else {
if( array != NULL ) {
if( data->complex_flag == TRUE ) {
if( type == SPICE3_REAL_PART ) {
index = 2*index;
memcpy(array,data->variable_array[index],
sizeof(float)*num_points);
} else if ( type == SPICE3_IMAG_PART ) {
index = 2*index + 1;
memcpy(array,data->variable_array[index],
sizeof(float)*num_points);
} else if ( type == SPICE3_MAGNITUDE ) {
index_r = 2*index;
index_i = 2*index + 1;
for(i=0; i<num_points; i++ ) {
array[i] = sqrtf(data->variable_array[index_r][i]*
data->variable_array[index_r][i] +
data->variable_array[index_i][i]*
data->variable_array[index_i][i]);
}
} else if ( type == SPICE3_PHASE ) {
index_r = 2*index;
index_i = 2*index + 1;
for(i=0; i<num_points; i++ ) {
array[i] = atand2f(data->variable_array[index_i][i],
data->variable_array[index_r][i]);
}
} else {
index = 2*index; /* Real part is default */
memcpy(array,data->variable_array[index],
sizeof(float)*num_points);
}
} else {
memcpy(array,data->variable_array[index],
sizeof(float)*num_points);
}
} else {
error_code = 9;
}
}
} else {
error_code = 8;
}
return error_code;
}
/************************************************************************/
/* */
/* Return data->variable_array[index] */
/* Return a NULL pointer if index is invalid */
/* */
/************************************************************************/
char *spice3_get_name_by_index(Spice3Data data, int index)
{
int i;
char *p = NULL;
if( data != NULL && data->variable_array != NULL ) {
if( index >= 0 && index <= (data->num_variables)-1 ) {
p = data->variable_names[index];
}
}
return p;
}
/************************************************************************/
/* */
/* Return a simulation variable unit name */
/* */
/* The user is responsible for allocating memory for the array */
/* */
/************************************************************************/
int spice3_get_quantity_units(Spice3Data data, char *name,
char *units, int isize)
{
int error_code = 0;
int type, i, index;
if( data != NULL && data->variable_array != NULL ) {
/* Lookup up name that is being requested, find index into
array data. */
spice3_lookup_name(data, name, &index, &type);
if( index == -1 ) {
error_code = 10;
} else {
if( units != NULL ) {
strncpy(units, data->variable_units[index], isize);
} else {
error_code = 9;
}
}
} else {
error_code = 8;
}
return error_code;
}
/************************************************************************/
/* */
/* Lookup up a name to see if it exists in the Spice3 output */
/* Return an index into the variable_array where this quantity exists */
/* */
/************************************************************************/
static void spice3_lookup_name(Spice3Data data, char *name, int *index,
int *type)
{
int i, istart;
char *p, tmp[256];
/* We assume that "data" is a valid pointer */
*index = -1; /* Initialize to an invalid index */
*type = SPICE3_REAL_PART;
/* If data is complex, look for real/imag/mag/phase keywords */
if( data->complex_flag == TRUE ) {
if( strncasecmp(name,"REAL(",5) == 0 ) {
*type = SPICE3_REAL_PART;
istart = 5;
} else if( strncasecmp(name,"IMAG(",5) == 0 ) {
*type = SPICE3_IMAG_PART;
istart = 5;
} else if( strncasecmp(name,"MAG(",4) == 0 ) {
*type = SPICE3_MAGNITUDE;
istart = 4;
} else if( strncasecmp(name,"PHASE(",6) == 0 ) {
*type = SPICE3_PHASE;
istart = 6;
} else {
*type = SPICE3_REAL_PART;
istart = 0;
}
strncpy(tmp,&name[istart],255);
i = strlen(tmp);
if( istart > 0 && tmp[i-1] == ')' ) {
/* Clobber trailing ) */
tmp[i-1] = NUL;
}
for(i=0; i<data->num_variables; i++) {
if( strcasecmp(tmp,data->variable_names[i]) == 0 ) {
*index = i;
break;
}
}
} else {
for(i=0; i<data->num_variables; i++) {
if( strcasecmp(name,data->variable_names[i]) == 0 ) {
*index = i;
break;
}
}
}
return;
}
/************************************************************************/
/* */
/* Returns an error message associated with an error code */
/* */
/************************************************************************/
char *spice3_get_error_msg(int error_code)
{
switch ( error_code ) {
case 0: return "success";
case 1: return "failed to allocate dynamic memory (malloc)";
case 2: return "spice3 file open error";
case 3: return "spice3 file read error";
case 4: return "number of variables <= 0";
case 5: return "number of points <= 0";
case 8: return "analysis data not found";
case 9: return "invalid argument (NULL pointer)";
case 10: return "quantity not found";
case 11: return "error reading SPICE dictionary";
case 12: return "can't find end of SPICE dictionary";
case 13: return "invalid header -- may not be a SPICE3 file";
case 14: return "SPICE3 file contains unexpected data";
default: return "Unknown error code";
}
}
static void kill_nl(char *string)
{
char *p = string;
while( *p != '\0' ) {
if( *p == '\n' ) {
*p = ' ';
break;
}
p++;
}
}
static int lnblnk(char *string, int max_len)
{
int i;
for(i=max_len-1; i>=0; i--) {
if( string[i] != ' ' ) break;
}
if( i == -1 ) i=0;
return i+1; /* Fortran string indices at 1 */
}
#if !defined(__osf__) && !defined(__vms)
int cvt_ftof(void *input_val, int input_type,
void *output_val, int output_type, int options)
{
/* A limited functionality cvt_ftof for spice3.c */
unsigned char buffer[8], t;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -