Mercurial > forge
changeset 4813:9882766657e1 octave-forge
Improved help in PRS. Added doc folder.
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/Mcapacitors.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,25 @@ +function [a,b,c] =... + Mcapacitors(string,parameters,parameternames,extvar,intvar,t) + + ## Mcapacitors(string,parameters,parameternames,extvar,intvar,t) + +if isempty(intvar) + intvar = 0; +end + + switch string + + case "LIN" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + q = intvar(1); + + a = [0 0 1; 0 0 -1; 0 0 0]; + b = [0 0 0;0 0 0;C -C -1]; + c = [0 0 0]'; + break + otherwise + error (["unknown section:" string]) + end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/Mcurrentsources.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,31 @@ +function [a,b,c] =... + Mcurrentsources(string,parameters,parameternames,extvar,intvar,t) + + ## [a,b,c] = ... + ## Mcurrentsources(string,parameters,parameternames,extvar,intvar,t) + + switch string + + case "DC" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + a = zeros(2); + b = a; + c = [I -I]'; + break + + case "sinwave" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + I = shift+Ampl * sin(2*pi*(t+delay)*f ); + a = zeros(2); + b = a; + c = [I -I]'; + break + otherwise + error (["unknown section:" string]) + end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/Minductors.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,25 @@ +function [a,b,c] =... + Minductorstors(string,parameters,parameternames,extvar,intvar,t) + + ## Minductors(string,parameters,parameternames,extvar,intvar,t) + +if isempty(intvar) + intvar = [0 0]; +end + + switch string + + case "LIN" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + phi = intvar(1); + jl = intvar(2); + + a = [0 0 0 0; 0 0 0 0; 0 0 0 0; 0 0 0 -L]; + b = [0 0 -1 0; 0 0 0 1; 1 -1 -1 0; 0 0 1 0]; + c = [0 0 0 0]'; + otherwise + error (["unknown section:" string]) + end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/Mnmosfet.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,62 @@ +function [a,b,c]=Mnmosfet... + (string,parameters,parameternames,extvar,intvar,t) + + ## [a,b,c]=Mnmosfet... + ## (string,parameters,parameternames,extvar,intvar,t) + + switch string + case 'simple', + + rd = 1e6; + + for ii=1:length(parameternames) + eval([parameternames{ii} "=",... + num2str(parameters(ii)) " ;"]) + end + + vg = extvar(1); + vs = extvar(2); + vd = extvar(3); + vb = extvar(4); + + vgs = vg-vs; + vds = vd-vs; + + if (vgs < Vth) + + + gm = 0; + gd = 1/rd; + id = vds*gd; + + elseif ((vgs-Vth)>=(vds))&(vds>=0) + + id = k*((vgs-Vth)*vds-(vds^2)/2)+vds/rd; + gm = k*vds; + gd = k*(vgs-Vth-vds)+1/rd; + + elseif ((vgs-Vth)>=(vds))&(vds<0) + + gm = 0; + gd = 1/rd; + id = vds*gd; + + else # (i.e. if 0 <= vgs-vth <= vds) + + id = k*(vgs-Vth)^2/2+vds/rd; + gm = k*(vgs-Vth); + gd = 1/rd; + + end + a = zeros(4); + + b = [0 0 0 0; + -gm (gm+gd) -gd 0; + gm -(gm+gd) gd 0; + 0 0 0 0]; + + c = [0 -id id 0]'; + break; + otherwise + error(["unknown option:" string]); + end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/Mpmosfet.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,61 @@ +function [a,b,c]=Mpmosfet... + (string,parameters,parameternames,extvar,intvar,t) + + ## [a,b,c]=Mpmosfet... + ## (string,parameters,parameternames,extvar,intvar,t) + + switch string + case 'simple', + + rd = 1e6; + + for ii=1:length(parameternames) + eval([parameternames{ii} "=",... + num2str(parameters(ii)) " ;"]); + end + + vg = extvar(1); + vs = extvar(2); + vd = extvar(3); + vb = extvar(4); + + vgs = vg-vs; + vds = vd-vs; + + if (vgs > Vth) + + gm = 0; + gd = 1/rd; + id = vds*gd; + + elseif ((vgs-Vth)<=(vds))&(vds<=0) + + id = k*((vgs-Vth)*vds-(vds^2)/2)+vds/rd; + gm = k*vds; + gd = k*(vgs-Vth-vds)+1/rd; + + elseif ((vgs-Vth)<=(vds))&(vds>0) + + gm = 0; + gd = 1/rd; + id = vds*gd; + + else + + id = k*(vgs-Vth)^2/2+vds/rd; + gm = k*(vgs-Vth); + gd = 1/rd; + + end + a = zeros(4); + + b = [0 0 0 0; + -gm (gm+gd) -gd 0; + gm -(gm+gd) gd 0; + 0 0 0 0 ]; + + c =[0 -id id 0]'; + break; + otherwise + error(["unknown option:" string]); + end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/Mresistors.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,26 @@ +function [a,b,c] =... + Mresistors(string,parameters,parameternames,extvar,intvar,t) + + ## [a,b,c] = Mresistors(string,parameters,parameternames,extvar,intvar,t) + + switch string + + case "LIN" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + vp = extvar(1); + vm = extvar(2); + + a = zeros(2); + b = [1 -1 ;-1 1]/R; + c = -[0; 0]; + + break + + otherwise + + error (["unknown section:" string]) + + end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/Mvoltagesources.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,61 @@ +function [a,b,c] =... + Mvoltagesources(string,parameters,parameternames,extvar,intvar,t) + + ## Mvoltagesources(string,parameters,parameternames,extvar,intvar,t) + +if isempty(intvar) + intvar = 0; +end + + switch string + + case "DC" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + j = intvar(1); + + a = zeros(3); + b = [0 0 1;0 0 -1;1 -1 0]; + c = [0 0 -V]; + break + + case "sinwave" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + DV = shift+Ampl * sin(2*pi*(t+delay)*f ); + j = intvar(1); + + a = zeros(3); + b = [0 0 1;0 0 -1;1 -1 0]; + c = [0 0 -DV]' + b * [extvar;intvar]; + break + + case "squarewave" + for ii=1:length(parameternames) + eval([parameternames{ii} "=" num2str(parameters(ii)) ";"]) + end + + if t<delay + DV=low; + else + T = tlow+thigh; + t = mod(t-delay,T); + if t<tlow + DV = low; + else + DV = high; + end + end + j = intvar(1); + + a = zeros(3); + b = [0 0 1;0 0 -1;1 -1 0]; + c = [0 0 -DV]' - b * [extvar;intvar]; + break + otherwise + error (["unknown section:" string]) + end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/RC_chain.cir Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,26 @@ +% 0.1b1 +% Rc chain with 3 +% resistors and 3 capacitors + +END +% Resistors +Mresistors LIN 2 1 +3 1 +R +1e3 +1e3 +1e3 +1 2 +2 3 +3 4 +% Capacitors +Mcapacitors LIN 2 1 +3 1 +C +1e-12 +1e-12 +1e-12 +1 2 +2 5 +5 3 +END \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/RC_chain.nms Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,3 @@ +% 0.1b1 +1 V1 +4 V4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/TLelement.cir Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,29 @@ +% 0.1b1 +% A transmition line element +% Input voltage source +Mvoltagesources sinwave 2 4 +1 4 +Ampl f delay shift +.5 1 0.75 0.5 +1 0 +END +% Resistors +Mresistors LIN 2 1 +1 1 +R +1e6 +2 0 +% Capacitors +%Mcapacitors LIN 2 1 +%1 1 +%C +%1e-7 +%3 0 +% Inductors +Minductors LIN 2 1 +1 1 +L +1e5 +1 2 +END +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/TLelement.nms Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,4 @@ +% 0.1b1 +1 Vin +2 Vrl +3 Vout \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/and.cir Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,43 @@ +% 0.1b1 +% A Simple CMOS AND GATE +% +% N-Mosfets +% There are 3 N-Mosfets +Mnmosfet simple 4 3 +3 3 +k Vth rd +1e-4 0.1 1e7 +1e-4 0.1 1e7 +1e-4 0.1 1e7 +1 3 4 0 +2 0 3 0 +4 0 5 0 +% +% P-Mosfets +Mpmosfet simple 4 3 +3 3 +k Vth rd +-1e-4 -0.1 1e7 +-1e-4 -0.1 1e7 +-1e-4 -0.1 1e7 +1 6 4 6 +2 6 4 6 +4 6 5 6 +% +% Input voltage sources +Mvoltagesources sinwave 2 4 +2 4 +Ampl f delay shift +0.5 1 0.0 0.5 +0.5 2 0.25 0.5 +1 0 +2 0 +END +% +% Power supply +Mvoltagesources DC 2 1 +1 1 +V +1 +6 0 +END \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/and.nms Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,8 @@ +% 0.1b1 +1 Va +2 Vb +5 Va_and_b +6 Vdd +7 I1 +8 I2 +9 I3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/buildsystemIFF.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,62 @@ +function [varargout] = buildsystemIFF(instruct,x,t); + + ## [varargout] = buildsystemIFF(instruct,x,t) + + n = instruct.totextvar+instruct.totintvar; + A = spalloc(n,n,0); + Jac = spalloc(n,n,0); + res = spalloc(n,1,0); + + ## NLC section + + + nblocks = length(instruct.NLC); + + for ibl = 1:nblocks + for iel = 1:instruct.NLC(ibl).nrows + + ## evaluate element + + if instruct.NLC(ibl).nintvar(iel) + intvars = instruct.totextvar+instruct.NLC(ibl).osintvar(iel)+... + [1:instruct.NLC(ibl).nintvar(iel)]'; + else + intvars=[]; + end + + il = instruct.NLC(ibl).vnmatrix(iel,:)'; + nzil = find(il!=0); + + y = zeros(size(il)); + y(nzil)=x(il(nzil)); + + z = x(intvars); + + [a,b,c] = feval(instruct.NLC(ibl).func,... + instruct.NLC(ibl).section,... + instruct.NLC(ibl).pvmatrix(iel,:),... + instruct.NLC(ibl).parnames,... + y,z,t); + + ## assemble matrices + + vars = [il;intvars]; + + for irow=1:rows(a) + if (vars(irow)) + for icol=1:columns(a) + if (vars(icol)) + A(vars(irow),vars(icol)) += a(irow,icol); + Jac(vars(irow),vars(icol)) += b(irow,icol); + end ## endif + end ## endfor + res(vars(irow)) += c(irow); + + end ## endif + end ## endfor + end ## endfor + end ## endfor + + varargout{1}=A; + varargout{2}=Jac; + varargout{3}=res;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/funjac.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,5 @@ +function jac = funjac(x,xdot,T,d) + global A_MATRIX_IFF B_MATRIX_IFF C_MATRIX_IFF OUTSTRUCT_IFF + [A,B,res] = buildsystemIFF(OUTSTRUCT_IFF,x,T); + jac = (B+B_MATRIX_IFF) + d * (A+A_MATRIX_IFF); +endfunction \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/funres.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,5 @@ +function res = funres(x,xdot,T) + global A_MATRIX_IFF B_MATRIX_IFF C_MATRIX_IFF OUTSTRUCT_IFF + [A,Jac,res] = buildsystemIFF(OUTSTRUCT_IFF,x,T); + res += (A+A_MATRIX_IFF) * xdot + B_MATRIX_IFF * x + C_MATRIX_IFF; +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/iffspec_ocs.tex Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,589 @@ +\documentclass{scrartcl} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\usepackage{hyperref} +\usepackage[pdftex]{graphicx} +\usepackage[usenames]{xcolor} +\usepackage{amssymb} +\usepackage{amsmath} +\usepackage{amsfonts} +\usepackage{listings} +\newcommand{\Iff}{{\tt IFF}} +\newcommand{\nlc}{{\tt NLC}} +\newcommand{\cir}{{\tt CIR}} +\newcommand{\lcr}{{\tt LCR}} +\newcommand{\nms}{{\tt NMS}} +\newcommand{\sbn}{{\tt SBN}} +\newcommand{\ord}{{\tt ORD}} +\newcommand{\dem}{{\tt DEM}} +\newcommand{\nl}{{\tt \backslash n}} +\newcommand{\oct}{{\tt Octave}} +\newcommand{\mylink}[2]{\hyperlink{#1}{#2}} +\newcommand{\mylisting}[1]{\hypertarget{listing:#1}% +{{\sffamily\bfseries\large file : #1 \normalfont}}% +\null\lstinputlisting[]{#1}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\title{IFF version 0.1b1 File Format Specification} +\author{Carlo de Falco} + +\begin{document} +\maketitle + +\section*{Introduction} + +This document contains the specification of an {\bf I}ntermediate +{\bf F}ile {\bf F}ormat ({\Iff}) for describing coupled electrical circuits, +devices and systems. +The present version of this documents refers to the {\Iff} version 0.1b1. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\subsection{List of Files} +A circuit description is comprised of a set of files of three different types: +\begin{itemize} +\item 1 {\cir} ({\bf Cir}cuit) file: an {\tt ASCII } text file with filename +{$<$circuitname$>$.cir} +\item 1 {\nms} ({\bf N}a{\bf m}e{\bf s}) file: an {\tt ASCII } text file with +filename {$<$circuitname$>$.nms} +\item $N \geq 1$ {\sbn} ({\bf S}u{\bf bn}et) files: a set of M-functions or +DLD-functions following the template described in \autoref{ssecsbn} below. +%\item 1 {\ord} (Re{\bf ord}ering ) file: an M-function or DLD-function +%following the template described in \autoref{ssecord} below. +\end{itemize} +{\sbn} files are not necessarily specific to one circuit and can be grouped in +\emph{libraries} as long as the directory containing the library is added to +the path when the +{\Iff} parser is run. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\subsection{File Formats} +Below we describe the purpose and the syntax of each of file categories. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\subsubsection{{\cir} files} +The {\cir} file is divided into two sections describing the linear +time--independent ({\lcr} = {\bf l}inear {\bf c}i{\bf r}cuit) and the +non--linear and/or time--dependent ({\nlc} = {\bf n}on--{\bf l}inear +{\bf c}ircuit) partitions of the circuit respectively. +The syntax for the {\lcr} and {\nlc} section is identical. +{\nlc} can also contain linear elements, in fact the whole circuit could be +described only by the {\nlc} +section but this could result in the evaluator unnecessarily recomputing local +matrices for linear +time--independent elements +The content of {\cir} files is organized as follows\footnote{ This description +makes use of Extended Backus-Naur Form (EBNF) see this +\hyperref{http://www.garshol.priv.no/download/text/bnf.html}{}{}{article} +or the EBNF entry in the \hyperref{http://en.wikipedia.org}{}{}{wikipedia} +for an explanation of (E)BNF }: + +\begin{lstlisting}[language=Mathematica,mathescape=true,backgroundcolor={}] + cir := header nlc separator lcr separator ; + header := '%' version_id '$\nl$' + comment* ; + comment:= '%' text '$\nl$' ; + nlc := block* ; + block := blockcomment? blockheader pv_matrix vnum_matrix ; + block_comment := '%' text '$\nl$' ; + block_header := func section n_extvar n_par '$\nl$' + n_rows n_parnames '$\nl$' + par_name*; + section := string ; + n_extvar := number ; + n_par := number ; + n_rows := number ; + n_parnames := number ; + par_name := string ; + pv_matrix := matrix ; + vnum_matrix := matrix ; + matrix := number+ ; + separator := 'END $\nl$' ; + lcr := block* ; +\end{lstlisting} + +where + +\begin{description} +\item {\tt version\_id} is a string identifying the version on {\Iff} + in which the file is encoded +\item $\nl$ is the new-line character +\item {\tt string} represents anything that the {\oct} command + {\tt s=fscanf(file,\%s) } would parse as a string {\it i.e.} any sequence of + chars without white-space +\item {\tt text} is any sequence of chars without a $\nl$, + this differs from {\tt string} because it can contain white--space +\item {\tt number} represents anything that the {\oct} command + {\tt s=fscanf(file,\%g) } would parse as a number +\item {\tt func} is the name of a function to evaluate the elements + described in the block +\item {\tt n\_extvar} Is the number of {\it external variables} + for the elements of a block +\item {\tt n\_par} Is the number of {\it parameters} for the elements + of a block +\item {\tt n\_rows} Is the number of {\it elements} in a block +\item {\tt n\_parnames} Is the number of {\it parameter names} for the + elements of a block, it corresponds to the number of {\tt par\_name} entries. + If {\tt n\_parnames} is 0 the line with the {\tt par\_name}s is missing. +\item {\tt pv\_matrix} Is a list of {\tt n\_rows} $\times$ + {\tt n\_par} numbers separated by any character the + {\oct} command {\tt s=fscanf(file,\%g) } would consider whitespace + (including $\nl$). +\item [] every row (a set of {\tt n\_par} contiguous entries) + in {\tt pv\_matrix} refers to an element of the circuit. + The {\tt n\_par} numbers in a row represent the values of the + parameters to be passed to the function that evaluates that element. +\item {\tt vnum\_matrix} Is a list of {\tt n\_rows} $\times$ {\tt n\_extvar } + numbers separated by any character the {\oct} command {\tt s=fscanf(file,\%g) } + would consider white-space (including $\nl$). +\item [] every row (a set of {\tt n\_extvar} contiguous entries) + in {\tt vnum\_matrix} refers to an element of the circuit. + The {\tt n\_extvar} numbers in the row represent the global numbering of the element + external variables. +\end{description} + +\subsubsection{{\nms} files} + +{\nms} files are meant to contain the names of the circuit variables, +the format of {\nms} is just a list +of variable names one on each row preceded by the variable number: + +\begin{lstlisting}[language=Mathematica,mathescape=true,backgroundcolor={}] +nms := version_id '$\nl$' comment* line* ; +line := var_number var_name ; +var_number := number ; +var_name := string ; +\end{lstlisting} + +the variable are ordered as follows: +\begin{itemize} +\item first all external variables of all elements +in the order given by the global numbering of external variables as +explicitly written in the {\cir} +files +\item then the internal variables of the elements in the same order as the +corresponding elements appear in the {\cir} file +( internal variables of non-linear elements first, then those of linear elements) +\end{itemize} + +Notice that the number of internal variables of each element is not included +in the {\Iff} files. +This is because elements with a number of internal variables that is huge, that depends on the value of some parameter, or even that changes in time (for example distributed elements treated with a FEM with adaptive meshing, a large linear sub-circuit that is reduce via MOR...) and therefore it is more convenient to compute the number of internal variables when initializing the system. + +\subsubsection{{\sbn} files} \label{ssecsbn} + +{\sbn} files are {\oct} functions, implemented as (M-scripts or as +DLD functions) with a header as follows: + +\begin{lstlisting} +function [a,b,c,] =... + func (string, m(i,:), extvar, intvar, t) +\end{lstlisting} + +i.e. it should get as inputs: + +\begin{itemize} +\item the string entry of the block header +\item one row of the {\tt pv\_matrix} entry of the block +\item the current values of all internal and external variables +\item the current time +\end{itemize} + +and it should produce as outputs three matrices: +\begin{itemize} +\item $a,b \in \mathbb{R}^{ +\mbox{\tt (n\_extvar + n\_intvar})\times \mbox{\tt (n\_extvar + n\_intvar)}}$ +\item $c\in \mathbb{R}^{\mbox{\tt (n\_extvar + n\_intvar)}} $ +\end{itemize} + + +where {\tt n\_intvar} is the number of \emph{internal variables} +(see \autoref{sec:appintext} for explanation of \emph{internal variables} and + \emph{external variables} ) +that can be assembled in the complete system matrices. +We assume that the full DAE system can be written in the form +\begin{equation}\label{eqdaesystem} +\left(A(t) \mathbf{x}\right)_{,t} + +B \mathbf{x} + \mathbf{C} + \mathbf{f}(\mathbf{x},t)= 0 +\end{equation} + +The local matrices $a$,$b$,$c$ of a linear element $E_{l}$, +will contain the contributions to $A$, $B$, $C$ due to +$E_{l}$. + +At a given instant of time $t=\tilde{t}$ and for a given set of state +variables $\mathbf{x}=\mathbf{\tilde{x}}$, +the local matrices of a non-linear element $E_{nl}$, +will contain the local contributions to: +\begin{itemize} +\item $a$ to $A(\tilde{t})$ +\item $b$ to the Jacobian matrix +$\left.\frac{\partial \mathbf{f}}{\partial\mathbf{x}}\right|_{\mathbf{x}= + \mathbf{\tilde{x}},t=\tilde{t}}$ +\item $c$ to $\mathbf{f}(\mathbf{\tilde{x}},\tilde{t})$ +\end{itemize} +due to $E_{nl}$. + + +\subsection{Internal and External Variables}\label{sec:appintext} +To better understand what is intended by \emph{internal} and \emph{external} +variables we consider below an example with a linear system, +For another example see~\cite{freund99}, Pag.398. + +Consider the system below, where, for sake of simplicity, +we assume all coefficient matrices to be independent of time and +of the state vector $\mathbf{x}$ + +\begin{equation}\label{eq:coupledlinsys} +S: \,\, A \mathbf{x}_{,t} + B \mathbf{x} + \mathbf{C} = 0. +\end{equation} + +We assume that the system $S$ obeying~\eqref{eq:coupledlinsys} can be +partitioned into two subsystems $S1$ and $S2$ and therefore the state +vector $\mathbf{x}$ can be written as: +\newcommand{\xprtd}{\left[\begin{array}{c}% +\mathbf{x}_{c} \\ +\mathbf{x}_{1} \\ +\mathbf{x}_{2} \\ +\end{array}\right]} +\begin{equation}\label{eq:staepart} +\mathbf{x} = \xprtd +\end{equation} +where $\mathbf{x}_{1}$ represents the vector of the unknowns belonging only to +$S1$, $\mathbf{x}_{2}$ represents the vector of the unknowns belonging only to +$S2$ and $\mathbf{x}_{c}$ represents the vector of the unknowns common to +$S1$ and $S2$. +If $\mathbf{x}_{1}$ and $\mathbf{x}_{2}$ depend on each other only through +$\mathbf{x}_{c}$, then the \emph{flat} representation~\eqref{eq:coupledlinsys} +takes the form + +\newcommand{\MTT}[9]{\left[ + \begin{array}{ccc}#1 & #2 & #3 \\#4 & #5 & #6 \\#7 & #8 & #9 + \end{array} + \right]} + +\begin{equation}\label{eq:partitionedcoupledlinsys} +S: \,\, \MTT{A_{cc1}+A_{cc2}}{A_{c1}}{A_{c2}} +{A_{1c}}{A_{11}}{0} +{A_{2c}}{0}{A_{22}} +\left(\xprtd\right)_{,t} + +\MTT{B_{cc1}+B_{cc2}}{B_{c1}}{B_{c2}}{B_{1c}}{B_{11}}{0}{B_{2c}}{0}{B_{22}} +\xprtd + \left[ + \begin{array}{c}% + \mathbf{C}_{c1}+\mathbf{C}_{c2} \\ + \mathbf{C}_{1} \\ + \mathbf{C}_{2} \\ + \end{array}\right] += 0. +\end{equation} + +which is equivalent to + +$$ +\begin{array}{ll} +(A_{cc1}+A_{cc2}) \mathbf{x}_{c,t} + A_{c1} \mathbf{x}_{1,t} + +A_{c2} \mathbf{x}_{2,t} + (B_{cc1}+B_{cc2}) \mathbf{x}_{c} + +B_{c1} \mathbf{x}_{1} + B_{c2} \mathbf{x}_{2} + (C_{c1} + C_{c2}) &= 0\\ +% +A_{1c} \mathbf{x}_{c,t} + A_{11} \mathbf{x}_{1,t} + +B_{1c} \mathbf{x}_{c} + B_{11} \mathbf{x}_{1} + C_{1} &= 0\\ +% +A_{2c} \mathbf{x,t}_{c} + A_{22} \mathbf{x}_{2,t} + +B_{2c} \mathbf{x}_{c} + B_{22} \mathbf{x}_{1} + C_{2} &= 0\\ +\end{array} +$$ + +Then $S$ can be represented as the coupling of two systems $S1$ and $S2$ in +the following way: + +\newcommand {\matrow}[2]{\left[ \begin{array}{cc} #1 & #2 \end{array}\right]} +\newcommand {\matcol}[2]{\left[ \begin{array}{c} #1 \\ #2 \end{array}\right]} + +\begin{eqnarray} +\label{eq:s1} +S1 : & & \,\, \matrow{A_{1c}}{A_{11}} \matcol{ \mathbf{x}_{1,t}^{(e)}}{ \mathbf{x}_{1,t}} + \matrow{B_{1c}}{B_{11}} \matcol{ \mathbf{x}_{1}^{(e)}}{ \mathbf{x}_{1}} + C_{1} = 0 \\ +% +\label{eq:s2} +S2 : & &\,\, \matrow{A_{2c}}{A_{22}} \matcol{ \mathbf{x}_{2,t}^{(e)}} +{ \mathbf{x}_{2,t}} + \matrow{B_{2c}}{B_{22}} +\matcol{ \mathbf{x}_{2}^{(e)}}{ \mathbf{x}_{2}} + C_{2} = 0 \\ +% +\label{eq:s1s2coupling} +COUPLING : & & \,\, +\left\{\begin{array}{l} +\mathbf{x}_{1}^{(e)} = \mathbf{x}_{2}^{(e)} = \mathbf{x}_{c} \\[.5cm] +\matrow{A_{cc1}}{A_{c1}} +\matcol{ \mathbf{x}_{1,t}^{(e)}}{ \mathbf{x}_{1,t}} + +\matrow{A_{cc2}}{A_{c2}} +\matcol{ \mathbf{x}_{2,t}^{(e)}}{ \mathbf{x}_{2,t}} + \\ ++ \matrow{B_{cc1}}{B_{c1}} +\matcol{ \mathbf{x}_{1}^{(e)}}{ \mathbf{x}_{1}} + +\matrow{B_{cc2}}{B_{c2}} +\matcol{ \mathbf{x}_{2}^{(e)}}{ \mathbf{x}_{2}} + +C_{c1} + C_{c2} = 0 +\end{array}\right. +\end{eqnarray} + +It is easy to see that, reversing the procedure shown above, it is possible +to reconstruct the full system $S$ from the subsystems $S1$ and $S2$ the +information that is needed for this purpose is the following: + +\begin{itemize} +\item the local matrices + \begin{eqnarray} + \label{eq:locmati} + A_{i} = & & + \left[ \begin{array}{cc} A_{cci} & A_{ci} \nonumber \\ + A_{ic} & A_{ii} \end{array} \right]\\ + B_{i} = & & + \left[ \begin{array}{cc} B_{cci} & B_{ci} \\ + B_{ic} & B_{ii} \end{array} \right] \nonumber \\ + C_{i} = & & + \left[ \begin{array}{c} C_{ci} \\ + C_{i} \end{array} \right] \nonumber \\ + i=1,2 && \nonumber + \end{eqnarray} +\item a partitioning of the state variables of each subsystem in + \emph{internal} ($\mathbf{x}_{i}$) and \emph{external} + ($\mathbf{x}_{i}^{(e)}$) variables: +\item a \emph{global numbering} of external variables {\it i.e.} an + equation of the form of~\eqref{eq:s1s2coupling} or more generally, + in the case the ordering of the external variables is different in + the subsystems, a set of identities of the form + $$ x_{i}^{(e)} (j) = x_c (j)$$ +\end{itemize} + + +\subsection{Examples} + +In this section a set of examples is provided to show how to represent some +circuit topologies in the {\Iff} format, how to parse {\Iff} files and how +to build the systems of equations to be solved at each linearization step +within each time-step of a transient simulation. +The programs shown are simplified versions of thos included in the OCS package. + +\begin{table} +\begin{tabular}{|p{.15\linewidth}|l|l|l|l|l|} +\hline +description & script & flag & {\cir} file & {\nms} file & {\sbn} file(s) \\ +\hline \hline +%% +An inverting amplifier with one n-channel MOSFET and a resistive load & +\mylink{listing:runme.m}{\tt runme.m} & +\mylink{listing:runme.m}{\tt nmos} & +\mylink{listing:nmos.cir}{\tt nmos.cir} & +\mylink{listing:nmos.nms}{\tt nmos.nms} & +\begin{minipage}{.2\linewidth} +\mylink{listing:Mnmosfet.m}{\tt Mnmosfet.m}\\ +\mylink{listing:Mvoltagesources.m}{\tt Mvoltagesources.m}\\ +\mylink{listing:Mresistors.m}{\tt Mresistors.m} +\end{minipage}\\ \hline +%% +An inverting amplifier with one p-channel MOSFET and a resistive load & +\mylink{listing:runme.m}{\tt runme.m} & +\mylink{listing:runme.m}{\tt pmos} & +\mylink{listing:pmos.cir}{\tt pmos.cir} & +\mylink{listing:pmos.nms}{\tt pmos.nms} & +\begin{minipage}{.2\linewidth} +\mylink{listing:Mpmosfet.m}{\tt Mpmosfet.m}\\ +\mylink{listing:Mvoltagesources.m}{\tt Mvoltagesources.m}\\ +\mylink{listing:Mresistors.m}{\tt Mresistors.m} +\end{minipage}\\ \hline +%% +A CMOS inverter with one n-channel and one p-channel MOSFET & +\mylink{listing:runme.m}{\tt runme.m} & +\mylink{listing:runme.m}{\tt inverter} & +\mylink{listing:inverter.cir}{\tt inverter.cir} & +\mylink{listing:inverter.nms}{\tt inverter.nms} & +\begin{minipage}{.2\linewidth} +\mylink{listing:Mpmosfet.m}{\tt Mpmosfet.m}\\ +\mylink{listing:Mvoltagesources.m}{\tt Mvoltagesources.m}\\ +\mylink{listing:Mnmosfet.m}{\tt Mnmosfet.m} +\end{minipage}\\ \hline +%% +A CMOS AND gate with three n-channel and three p-channel MOSFETs & +\mylink{listing:runme.m}{\tt runme.m} & +\mylink{listing:runme.m}{\tt and} & +\mylink{listing:and.cir}{\tt and.cir} & +\mylink{listing:and.nms}{\tt and.nms} & +\begin{minipage}{.2\linewidth} +\mylink{listing:Mpmosfet.m}{\tt Mpmosfet.m}\\ +\mylink{listing:Mvoltagesources.m}{\tt Mvoltagesources.m}\\ +\mylink{listing:Mnmosfet.m}{\tt Mnmosfet.m} +\end{minipage}\\ \hline +%% +An inverting amplifier with one n-channel MOSFET and a capacitive load & +\mylink{listing:runmedaspk.m}{\tt runmedaspk.m} & +\mylink{listing:runmedaspk.m}{\tt nmos2} & +\mylink{listing:nmos2.cir}{\tt nmos2.cir} & +\mylink{listing:nmos2.nms}{\tt nmos2.nms} & +\begin{minipage}{.2\linewidth} +\mylink{listing:Mvoltagesources.m}{\tt Mvoltagesources.m}\\ +\mylink{listing:Mnmosfet.m}{\tt Mnmosfet.m} +\mylink{listing:Mcapacitors.m}{\tt Mcapacitors.m} +\end{minipage}\\ \hline +%% +A small element of a (lossy) transmission line modeled with lumped parameters & +\mylink{listing:runmedaspk.m}{\tt runmedaspk.m} & +\mylink{listing:runmedaspk.m}{\tt TLelement} & +\mylink{listing:TLelement.cir}{\tt TLelement.cir} & +\mylink{listing:TLelement.nms}{\tt TLelement.nms} & +\begin{minipage}{.2\linewidth} +\mylink{listing:Mvoltagesources.m}{\tt Mvoltagesources.m}\\ +\mylink{listing:Mcapacitors.m}{\tt Mcapacitors.m} +\mylink{listing:Minductors.m}{\tt Minductors.m} +\end{minipage}\\ \hline +\end{tabular} +\caption{List of examples}\label{tab:exmpl} +\end{table} + +\subsubsection{A CMOS AND Gate} + +In this section we discuss in slightly deeper detail the CMOS AND gate example, +to point out more clearly some features of the {\Iff} format. + +\autoref{fig:and} depicts the schematic of the circuit, while \autoref{fig:andout} +represents the output of a transient simulation. + +The file \mylink{listing:runme.m}{\tt runme.m} is a script that performs all the operations +needed to simulate transient behavior of the circuit at hand. + +Notice that a time independent model is used for the MOSFETs (for the description of the adopted model see~\cite{neamen} Sec.11.3) and that no energy storing +elements are present in the circuit so no time discretization is used and at each time-step +a non-linear system of equations is solved via a damped Newton method. + +The main steps of the simulation are the following: + +\begin{itemize} +\item Parsing the {\cir} and {\nms} files. This is accomplished by the call +\begin{lstlisting} + outstruct = parseIFF("and"); +\end{lstlisting} +it produces as output the structure {\tt outstruct } which will be described below. +\item Initializing the system. This is accomplished by the call +\begin{lstlisting} + [A,Jac,res,B,C,outstruct] = initsystemIFF(outstruct,x,t(0)); +\end{lstlisting} +which builds the linear and time independent matrices {\tt A}, {\tt B} +and {\tt C} and the initial value +of the Jacobian matrix {\tt Jac} and of the residual {\tt res}. +\item Cycle over time-steps and Newton steps +\begin{lstlisting} +for it=1:length(t) + for ii=1:maxit + ... + end +end +\end{lstlisting} +\item Rebuild time or state dependent components of the system +\begin{lstlisting} +[A,Jac,res] = buildsystemIFF(outstruct,x,t(it)); +\end{lstlisting} +\item Update the state vector +\begin{lstlisting} + xnew = (B+Jac)\(-res - C + Jac*x); + x = (1-damp)*x+damp*xnew; +\end{lstlisting} +\item Plot the solution +\begin{lstlisting} + plotbynameIFF(t(1:it),out,outstruct,pltvars) +\end{lstlisting} +\end{itemize} + +The run-time representation of the circuit that {\tt parseIFF} builds +from the {\Iff} file is the {\tt outstruct} structure which is composed as follows: +\begin{lstlisting} + +outstruct = +{ + LCR: struct % the fields of LCR are shown below + NLC: struct % NLC has the same fields as LCR + namesn: matrix % numbers of vars that are assigned a name in and.nms + namess: cell % the names corresponding to the vars above + totextvar: scalar % the total number of external variables + totintvar: scalar % the total number of internal variables +} + +outstruct.LCR = +{ + 1x2 struct array containing the fields: % array has one element per block + + func % name of the sbn file corresponding to each block + section % string parameter to be passed to the sbn files + nextvar % number of external variables for each element of the block + vnmatrix % numbers of the external variables of each element + nintvar % number of internal variables for each element of the block + osintvar % number of the first internal variable + npar % number of parameters + nparnames% number of parameter names + nrows % number of rows in the block + parnames % list of parameter names + pvmatrix % list of parameter values for each element + +} + + +\end{lstlisting} + + + +\begin{figure} +\begin{center} +\includegraphics[width=.99\linewidth]{AND_BW.png} +\end{center} +\caption{Schematic of a CMOS AND Gate}\label{fig:and} +\end{figure} + +\begin{figure} +\begin{center} +\includegraphics[width=.66\linewidth]{and_output.png} +\end{center} +\caption{Result of the CMOS AND Gate transient simulation}\label{fig:andout} +\end{figure} + +%\part*{Appendices} + +%\appendix + + +\subsection{Source Code of the Examples}\label{app:source} + +\subsubsection{{\sbn} Files } + +\mylisting{Mvoltagesources.m} +\mylisting{Mcurrentsources.m} +\mylisting{Mresistors.m} +\mylisting{Mnmosfet.m} +\mylisting{Mpmosfet.m} +\mylisting{Minductors.m} +\mylisting{Mcapacitors.m} + +\subsubsection{{\cir} and {\nms} Files} + +\mylisting{nmos.cir} +\mylisting{nmos.nms} +\mylisting{pmos.cir} +\mylisting{pmos.nms} +\mylisting{inverter.cir} +\mylisting{inverter.nms} +\mylisting{and.cir} +\mylisting{and.nms} +\mylisting{nmos2.cir} +\mylisting{nmos2.nms} +\mylisting{TLelement.cir} +\mylisting{TLelement.nms} + +\subsubsection{Example Parser and Matrix Asembler} + +\mylisting{parseIFF.m} +\mylisting{initsystemIFF.m} +\mylisting{buildsystemIFF.m} + +\subsubsection{Example Transient Simulations} + +\mylisting{runme.m} +\mylisting{runmedaspk.m} +\mylisting{plotbynameIFF.m} +\mylisting{funres.m} +\mylisting{funjac.m} + + +\end{document} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/initsystemIFF.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,192 @@ +function [varargout] = initsystemIFF(instruct,x); + + ## Check number of internal variables + + intvar = 0; + + ## NLC section + + + nblocks = length(instruct.NLC); + + for ibl = 1:nblocks + for iel = 1:instruct.NLC(ibl).nrows + + ## evaluate element + + il = instruct.NLC(ibl).vnmatrix(iel,:)'; + nzil = find(il!=0); + + y = zeros(size(il)); + y(nzil)=x(il(nzil)); + + + [a,b,c] = feval(instruct.NLC(ibl).func,... + instruct.NLC(ibl).section,... + instruct.NLC(ibl).pvmatrix(iel,:),... + instruct.NLC(ibl).parnames,... + y,[],0); + + instruct.NLC(ibl).nintvar(iel) = columns(a)-instruct.NLC(ibl).nextvar; + instruct.NLC(ibl).osintvar(iel) = intvar; + intvar += instruct.NLC(ibl).nintvar(iel); + + end ## endfor + end ## endfor + + ## LCR section + + nblocks = length(instruct.LCR); + + for ibl = 1:nblocks + for iel = 1:instruct.LCR(ibl).nrows + + ## evaluate element + il = instruct.LCR(ibl).vnmatrix(iel,:)'; + nzil = find(il!=0); + + y = zeros(size(il)); + y(nzil)=x(il(nzil)); + + + [a,b,c] = feval(instruct.LCR(ibl).func,... + instruct.LCR(ibl).section,... + instruct.LCR(ibl).pvmatrix(iel,:),... + instruct.LCR(ibl).parnames,... + y,[],0); + + instruct.LCR(ibl).nintvar(iel) = columns(a)-instruct.LCR(ibl).nextvar; + instruct.LCR(ibl).osintvar(iel) = intvar; + intvar += instruct.LCR(ibl).nintvar(iel); + + end ## endfor + end ## endfor + + + instruct.totintvar = intvar; + + ##################################### + ## + ## Build initial system + ## + ##################################### + + n = instruct.totextvar+instruct.totintvar; + A = spalloc(n,n,0); + + ###### + ## LCR section + ###### + + B = spalloc(n,n,0); + C = spalloc(n,1,0); + + nblocks = length(instruct.LCR); + + for ibl = 1:nblocks + for iel = 1:instruct.LCR(ibl).nrows + + ## evaluate element + + if instruct.LCR(ibl).nintvar(iel) + intvars = instruct.totextvar+instruct.LCR(ibl).osintvar(iel)+... + [1:instruct.LCR(ibl).nintvar(iel)]'; + else + intvars=[]; + end + + il = instruct.LCR(ibl).vnmatrix(iel,:)'; + nzil = find(il!=0); + + y = zeros(size(il)); + y(nzil)=x(il(nzil)); + + z = x(intvars); + + [a,b,c] = feval(instruct.LCR(ibl).func,... + instruct.LCR(ibl).section,... + instruct.LCR(ibl).pvmatrix(iel,:),... + instruct.LCR(ibl).parnames,... + y,z,0); + + ## assemble matrices + + vars = [il;intvars]; + + for irow=1:rows(a) + if (vars(irow)) + for icol=1:columns(a) + if (vars(icol)) + A(vars(irow),vars(icol)) += a(irow,icol); + B(vars(irow),vars(icol)) += b(irow,icol); + end ## endif + end ## endfor + C(vars(irow)) += c(irow); + end ## endif + end ## endfor + end ## endfor + end ## endfor + + varargout{4}=B; + varargout{5}=C; + +end ## endif + +###### +## NLC section +###### + +Jac = spalloc(n,n,0); +res = spalloc(n,1,0); + +nblocks = length(instruct.NLC); + +for ibl = 1:nblocks + for iel = 1:instruct.NLC(ibl).nrows + + ## evaluate element + + if instruct.NLC(ibl).nintvar(iel) + intvars = instruct.totextvar+instruct.NLC(ibl).osintvar(iel)+... + [1:instruct.NLC(ibl).nintvar(iel)]'; + else + intvars=[]; + end + + il = instruct.NLC(ibl).vnmatrix(iel,:)'; + nzil = find(il!=0); + + y = zeros(size(il)); + y(nzil)=x(il(nzil)); + + z = x(intvars); + + [a,b,c] = feval(instruct.NLC(ibl).func,... + instruct.NLC(ibl).section,... + instruct.NLC(ibl).pvmatrix(iel,:),... + instruct.NLC(ibl).parnames,... + y,z,0); + + ## assemble matrices + + vars = [il;intvars]; + + for irow=1:rows(a) + if (vars(irow)) + for icol=1:columns(a) + if (vars(icol)) + A(vars(irow),vars(icol)) += a(irow,icol); + Jac(vars(irow),vars(icol)) += b(irow,icol); + end ## endif + end ## endfor + res(vars(irow)) +=... + c(irow); + end ## endif + end ## endfor + end ## endfor +end ## endfor + +varargout{1}=A; +varargout{2}=Jac; +varargout{3}=res; +varargout{6}=instruct;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/inverter.cir Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,28 @@ +%0.1b1 +% A Simple MOSFET inverter +% Input voltage sources +Mvoltagesources sinwave 3 4 +1 4 +Ampl f delay shift +0.5 1 0.0 0.5 +1 0 4 +% P-Mosfet +Mpmosfet simple 4 3 +1 3 +k Vth rd +-1e-4 -0.1 1e6 +1 3 2 3 +% N-Mosfet +Mnmosfet simple 4 3 +1 3 +k Vth rd +1e-4 0.1 1e6 +1 0 2 0 +END +% Power supply +Mvoltagesources DC 3 1 +1 1 +V +1 +3 0 5 +END
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/inverter.nms Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,3 @@ +% 0.1b1 +1 Vgate +2 Vdrain
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/nmos.cir Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,29 @@ +% 0.1b1 +% A Simple MOSFET analog amplifier +% Input voltage sources +Mvoltagesources sinwave 2 4 +1 4 +Ampl f delay shift +.5 1 0.25 0.5 +1 0 +% N-Mosfet +Mnmosfet simple 4 3 +1 3 +k Vth rd +1e-4 0.1 1e6 +1 0 2 0 +END +% Power supply +Mvoltagesources DC 2 1 +1 1 +V +1 +3 0 +% Resistors +Mresistors LIN 2 1 +1 1 +R +1e5 +3 2 +END +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/nmos.nms Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,3 @@ +% 0.1b1 +1 Vgate +2 Vdrain
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/nmos2.cir Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,35 @@ +% 0.1b1 +% A Simple MOSFET analog amplifier +% Input voltage sources +Mvoltagesources sinwave 2 4 +1 4 +Ampl f delay shift +.5 1 0.75 0.5 +1 0 +% N-Mosfet +Mnmosfet simple 4 3 +1 3 +k Vth rd +1e-4 0.1 1e6 +1 0 2 0 +END +% Power supply +Mvoltagesources DC 2 1 +1 1 +V +1 +3 0 +% Resistors +Mresistors LIN 2 1 +1 1 +R +1e5 +3 2 +% Capacitors +Mcapacitors LIN 2 1 +1 1 +C +1e-6 +3 2 +END +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/nmos2.nms Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,3 @@ +% 0.1b1 +1 Vgate +2 Vdrain
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/parseIFF.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,227 @@ +function outstruct = parseIFF(name) + + ###### + ## init + ###### + + version ="0.1b1"; + outstruct = struct("NLC",[],... + "LCR",[],... + "totextvar",0); + + ###### + ## open cir file + ###### + + filename = [name ".cir"]; + fid = fopen(filename,"r"); + + ###### + ## Check version + ###### + + line = fgetl(fid); + + if line(1)~="%" + error(["missing version number in file " filename]); + end + + if ~strcmp(version,sscanf(line(2:end),'%s')); + error(["conflicting version number in file " filename]); + end + + ###### + ## NLC section + ###### + NLCcount = 0; + while ~strcmp(line,"END") + line + ###### + ## skip comments + ###### + + while line(1)=="%" + line = fgetl(fid) + end + + if strcmp(line,"END") + break + else + NLCcount++; + end + + ###### + ## parse block header + ###### + [outstruct]=parseNLCblockheader(fid,line,outstruct,NLCcount); + + ###### + ## parse block par-value matrix + ###### + [outstruct.NLC(NLCcount).pvmatrix]=... + fscanf(fid,"%g",[outstruct.NLC(NLCcount).npar,... + outstruct.NLC(NLCcount).nrows])'; + + ###### + ## parse block var-number matrix + ###### + [outstruct.NLC(NLCcount).vnmatrix]=... + fscanf(fid,"%g",[outstruct.NLC(NLCcount).nextvar,... + outstruct.NLC(NLCcount).nrows])'; + + outstruct.totextvar = max([max(outstruct.NLC(NLCcount).vnmatrix(:)) + outstruct.totextvar]); + + ###### + ## skip the newline char after the matrix + ###### + line = fgetl(fid) + + ###### + ## proceed to next line + ###### + line = fgetl(fid) + end + + + + ###### + ## LCR section + ###### + LCRcount = 0; + line = fgetl(fid) + + while ~strcmp(line,"END") + ###### + ## skip comments + ###### + + while line(1)=="%" + line = fgetl(fid) + end + + if strcmp(line,"END") + break + else + LCRcount++; + end + + ###### + ## parse block header + ###### + [outstruct]=parseLCRblockheader(fid,line,outstruct,LCRcount); + + ###### + ## parse block par-value matrix + ###### + [outstruct.LCR(LCRcount).pvmatrix]=... + fscanf(fid,"%g",[outstruct.LCR(LCRcount).npar,... + outstruct.LCR(LCRcount).nrows])'; + + ###### + ## parse block var-number matrix + ###### + [outstruct.LCR(LCRcount).vnmatrix]=... + fscanf(fid,"%g",[outstruct.LCR(LCRcount).nextvar,... + outstruct.LCR(LCRcount).nrows])'; + + outstruct.totextvar = max([max(outstruct.LCR(LCRcount).vnmatrix(:)) + outstruct.totextvar]); + + ###### + ## skip the newline char after the matrix + ###### + line = fgetl(fid) + + ###### + ## proceed to next line + ###### + line = fgetl(fid) + + end + + ###### + ## fclose cir file + ###### + fclose(fid); + + ###### + ## open nms file + ###### + + filename = [name ".nms"]; + fid = fopen(filename,"r"); + + + ###### + ## Check version + ###### + + line = fgetl(fid) + + if line(1)~="%" + error(["missing version number in file " filename]); + end + + if ~strcmp(version,sscanf(line(2:end),'%s')); + error(["conflicting version number in file " filename]); + end + + ###### + ## Init + ###### + + cnt = 1; + outstruct.namesn = []; + outstruct.namess = {}; + nnames = 0; + + while cnt + [nn,cnt] = fscanf(fid,"%d","C"); + [ns,cnt] = fscanf(fid,"%s","C"); + if cnt + outstruct.namesn(++nnames)=nn; + outstruct.namess(nnames)=ns; + end + end + + ###### + ## fclose cir file + ###### + +endfunction + + +############################################## +function [outstruct]=parseNLCblockheader(fid,line,outstruct,NLCcount); + + [func,section,nextvar,npar]=sscanf(line,"%s %s %g %g","C"); + outstruct.NLC(NLCcount).func = func; + outstruct.NLC(NLCcount).section = section; + outstruct.NLC(NLCcount).nextvar = nextvar; + outstruct.NLC(NLCcount).npar = npar; + [nrows,nparnames]=fscanf(fid,"%g %g","C"); + outstruct.NLC(NLCcount).nrows = nrows; + outstruct.NLC(NLCcount).nparnames = nparnames; + outstruct.NLC(NLCcount).parnames = {}; + for ii=1:nparnames + outstruct.NLC(NLCcount).parnames{ii}=fscanf(fid,"%s","C"); + end +endfunction + +############################################## +function [outstruct]=parseLCRblockheader(fid,line,outstruct,LCRcount); + + [func,section,nextvar,npar]=sscanf(line,"%s %s %g %g","C"); + outstruct.LCR(LCRcount).func = func; + outstruct.LCR(LCRcount).section = section; + outstruct.LCR(LCRcount).nextvar = nextvar; + outstruct.LCR(LCRcount).npar = npar; + [nrows,nparnames]=fscanf(fid,"%g %g","C"); + outstruct.LCR(LCRcount).nrows = nrows; + outstruct.LCR(LCRcount).nparnames = nparnames; + outstruct.LCR(LCRcount).parnames = {}; + for ii=1:nparnames + outstruct.LCR(LCRcount).parnames{ii}=fscanf(fid,"%s","C"); + end +endfunction
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/plotbynameIFF.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,16 @@ +function plotbynameIFF(t,out,outstruct,namelist) + + ## plotbynameIFF(t,out,outstruct,namelist) + + nn = length(outstruct.namesn); + + for ip = 1:nn + for in = 1:length(namelist) + if strcmp(namelist{in},outstruct.namess{ip}) + plot(t,out(outstruct.namesn(ip),:),[";" outstruct.namess{ip} ";"]); + hold on + end + end + end + + hold off \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/pmos.cir Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,29 @@ +% 0.1b1 +% A Simple MOSFET analog amplifier +% Input voltage sources +Mvoltagesources sinwave 2 4 +1 4 +Ampl f delay shift +.5 1 0.40 0.5 +1 0 4 +% P-Mosfet +Mpmosfet simple 4 3 +1 3 +k Vth rd +-1e-4 -0.1 1e6 +1 3 2 3 +END +% Power supply +Mvoltagesources DC 2 1 +1 1 +V +1 +3 0 5 +% Resistors +Mresistors LIN 2 1 +1 1 +R +1e5 +2 0 +END +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/pmos.nms Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,3 @@ +% 0.1b1 +1 Vgate +2 Vdrain
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/runm.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,70 @@ +exmpl ="nmos" +out=[]; +nrm=[]; + + +switch exmpl + case "nmos" + outstruct = parseIFF("nmos"); + x = [1 .03 1 0 0]'; + t = linspace(0,1,50); + dmp = .1; + pltvars={"Vgate","Vdrain"}; + case "pmos" + outstruct = parseIFF("pmos"); + x = [1 .1 1 0 -1e-6]'; + t = linspace(0,.5,50) + dmp = .1; + pltvars={"Vgate","Vdrain"}; + case "inverter" + outstruct = parseIFF("inverter"); + x = [.5 .5 1 0 0]'; + t = linspace(0,1,50); + dmp=.1; + pltvars={"Vgate","Vdrain"}; + case "and" + outstruct = parseIFF("and"); + x = [.5 .5 .33 .66 .5 1 0 0 1]'; + t = linspace(0,.5,50); + dmp = .1; + pltvars = {"Va","Vb","Va_and_b"}; +end + +tol = 1e-6; +dtol= 1e-5; +maxit = 200; + +[A,Jac,res,B,C,outstruct] = initsystemIFF(outstruct,x,0); + +for it=1:length(t) + for ii=1:maxit + + if (t(it)>0)|(ii>1) + [A,Jac,res] = buildsystemIFF(outstruct,x,t(it)); + end + + xnew = (B+Jac)\(-res - C + Jac*x); + nrm(it) = norm(x-xnew,inf); + + damp = dmp; + if (ii>2)&(nrm(it)<dtol), damp=1; end + x = (1-damp)*x+damp*xnew; + + if nrm(it)<tol + fprintf(1,'timestep %d converged in %d iterations\n',it,ii); + x = xnew; + out(:,it) = x; + break + elseif ii==maxit + fprintf(1,'timestep %d did not converge, nrm=%g\n',it,nrm(it)) + x = xnew; + out(:,it) = x; + break + end + + end + if it>1 + axis([min(t) max(t) 0 1]); + plotbynameIFF(t(1:it),out,outstruct,pltvars) + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/runme.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,70 @@ +exmpl ="nmos" +out=[]; +nrm=[]; + + +switch exmpl + case "nmos" + outstruct = parseIFF("nmos"); + x = [1 .03 1 0 0]'; + t = linspace(0,1,50); + dmp = .1; + pltvars={"Vgate","Vdrain"}; + case "pmos" + outstruct = parseIFF("pmos"); + x = [1 .1 1 0 -1e-6]'; + t = linspace(0,.5,50) + dmp = .1; + pltvars={"Vgate","Vdrain"}; + case "inverter" + outstruct = parseIFF("inverter"); + x = [.5 .5 1 0 0]'; + t = linspace(0,1,50); + dmp=.1; + pltvars={"Vgate","Vdrain"}; + case "and" + outstruct = parseIFF("and"); + x = [.5 .5 .33 .66 .5 1 0 0 1]'; + t = linspace(0,.5,50); + dmp = .1; + pltvars = {"Va","Vb","Va_and_b"}; +end + +tol = 1e-6; +dtol= 1e-5; +maxit = 200; + +[A,Jac,res,B,C,outstruct] = initsystemIFF(outstruct,x,0); + +for it=1:length(t) + for ii=1:maxit + + if (t(it)>0)|(ii>1) + [A,Jac,res] = buildsystemIFF(outstruct,x,t(it)); + end + + xnew = (B+Jac)\(-res - C + Jac*x); + nrm(it) = norm(x-xnew,inf); + + damp = dmp; + if (ii>2)&(nrm(it)<dtol), damp=1; end + x = (1-damp)*x+damp*xnew; + + if nrm(it)<tol + fprintf(1,'timestep %d converged in %d iterations\n',it,ii); + x = xnew; + out(:,it) = x; + break + elseif ii==maxit + fprintf(1,'timestep %d did not converge, nrm=%g\n',it,nrm(it)) + x = xnew; + out(:,it) = x; + break + end + + end + if it>1 + axis([min(t) max(t) 0 1]); + plotbynameIFF(t(1:it),out,outstruct,pltvars) + end +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extra/ocs/doc/runmedaspk.m Mon Apr 28 09:58:56 2008 +0000 @@ -0,0 +1,33 @@ +exmpl ="nmos" +out=[]; +nrm=[]; + + +switch exmpl + case "nmos" + outstruct = parseIFF("nmos2"); + x = [0 1 1 0 0 0 ]'; + t = linspace(0,10,200); + pltvars={"Vgate","Vdrain"}; + case "TLelement" + outstruct = parseIFF("TLelement"); + x = [0 0 0 0 0 ]'; + t = linspace(0,1,200); + pltvars={"Vin","Vout"}; +end + +global A_MATRIX_IFF B_MATRIX_IFF C_MATRIX_IFF OUTSTRUCT_IFF + +[A_MATRIX_IFF,Jac,res,B_MATRIX_IFF,C_MATRIX_IFF,OUTSTRUCT_IFF] =... + initsystemIFF(outstruct,x,0); + + +daspk_options("algebraic variables",[1 1 1 1 1 0 ]'); +daspk_options("compute consistent initial condition",2); +#daspk_options("relative tolerance",1e-); + +[x,xdot,istate,msg]= daspk(["funres";"funjac"],x,zeros(size(x)),t); + + +axis([min(t) max(t) 0 1]); +plotbynameIFF(t,x',OUTSTRUCT_IFF,pltvars)
--- a/extra/ocs/inst/PRS/PRSIFFparse.m Sun Apr 27 03:07:59 2008 +0000 +++ b/extra/ocs/inst/PRS/PRSIFFparse.m Mon Apr 28 09:58:56 2008 +0000 @@ -25,7 +25,41 @@ ## @var{name} is the basename of the CIR and NMS files to ## be parsed. ## -## See the @cite{IFF file format specifications} for more details +## See the @cite{IFF file format specifications} (distributed together +## with the OCS package) for more details on the file format +## +## The returned structure @var{outstruct} has the following fields: +## +## @example +## outstruct = +## @{ +## LCR: struct % the fields of LCR are shown below +## NLC: struct % NLC has the same fields as LCR +## namesn: matrix % numbers of vars named in .nms file +## namess: cell % the names corresponding to the vars above +## totextvar: scalar % the total number of external variables +## totintvar: scalar % the total number of internal variables +## @} +## +## outstruct.LCR = +## @{ +## struct array containing the fields: % array has one element per block +## +## func % name of the sbn file corresponding to each block +## section % string parameter to be passed to the sbn files +## nextvar % number of external variables for each element of the block +## vnmatrix % numbers of the external variables of each element +## nintvar % number of internal variables for each element of the block +## osintvar % number of the first internal variable +## npar % number of parameters +## nparnames% number of parameter names +## nrows % number of rows in the block +## parnames % list of parameter names +## pvmatrix % list of parameter values for each element +## +## @} +## @end example +## ## @end deftypefn function outstruct = PRSIFFparse(name)