Updated copyright text/header in most source files.
[geda-gaf/peter-b.git] / gnetlist / scheme / gnet-mathematica.scm
blobaeceb44de776e6d73a6d58e9ae663f0c37345d4c
1 ;;; gEDA - GPL Electronic Design Automation
2 ;;; gnetlist - gEDA Netlist
3 ;;; Copyright (C) 2007-2010 John P. Doty
4 ;;;
5 ;;; This program is free software; you can redistribute it and/or modify
6 ;;; it under the terms of the GNU General Public License as published by
7 ;;; the Free Software Foundation; either version 2 of the License, or
8 ;;; (at your option) any later version.
9 ;;;
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;; GNU General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU General Public License
16 ;;; along with this program; if not, write to the Free Software
17 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ;; Netlister for symbolic circuit analysis using Mathematica.
20 ;; See the Mathematica notebook gEDA.nb (obtainable at www.noqsi.com)
21 ;; for usage.
23 (define (mathematica:quoted thing port)
24    (write-char #\" port)
25    (display thing port)
26    (write-char #\" port)
29 (define (mathematica:write-pin-voltages netname pins port)
30    (if (not (null? pins))
31       (let ((pin (car pins)))
32          (display "v[" port)
33          (mathematica:quoted (car pin) port)
34          (display "," port)
35          (mathematica:quoted (car (cdr pin)) port)
36          (display "]=v[" port)
37          (mathematica:quoted netname port)
38          (display "];" port)
39          (newline port)
40          (mathematica:write-pin-voltages netname (cdr pins) port)
41       )
42    )
46 (define (mathematica:write-voltages netnames port)
47   (if (not (null? netnames))
48       (let ((netname (car netnames)))
49          (mathematica:write-pin-voltages netname 
50             (gnetlist:get-all-connections netname) port)
51          (mathematica:write-voltages (cdr netnames) port))))
54 (define (mathematica:write-node-currents pins port)
55    (let ((pin (car pins)))
56       (display "i[" port)
57       (mathematica:quoted (car pin) port)
58       (display "," port)
59       (mathematica:quoted (car (cdr pin)) port)
60       (display "]" port)
61       (if (not (null? (cdr pins )))
62          (begin
63             (display "+" port)
64             (mathematica:write-node-currents (cdr pins) port)
65          )
66       )
67    )
70 (define (mathematica:,newline port)
71    (display "," port)
72    (newline port)
76 (define (mathematica:write-currents netnames first port)
77    (if (not (null? netnames))
78       (let ((netname (car netnames)))
79          (if (not (equal? netname "GND"))
80             (begin
81                (if (not first)
82                   (mathematica:,newline port)
83                 )
84                (mathematica:write-node-currents 
85                   (gnetlist:get-all-connections netname) port)
86                (display "==0" port)
87                (mathematica:write-currents (cdr netnames) #f port)
88              )
89             (mathematica:write-currents (cdr netnames) first port)
90          )
91       )
92    )
95 (define (mathematica:write-device-value device value refdes port)
96    (display (string-downcase device) port)
97    (display "[value->" port)
98    (display value port)
99    (display "][" port)
100    (mathematica:quoted refdes port)
101    (display "]" port)
104 (define (mathematica:write-device-model model refdes port)
105    (display model port)
106    (display "[" port)
107    (mathematica:quoted refdes port)
108    (display "]" port)
112 (define (mathematica:write-model refdes port)
113    (let ((device (gnetlist:get-package-attribute refdes "device"))
114          (value (gnetlist:get-package-attribute refdes "value"))
115          (model (gnetlist:get-package-attribute refdes "model")))
116       (if (equal? model "unknown")
117          (if (equal? value "unknown")
118             (mathematica:write-device-value device (string-downcase refdes)
119                refdes port)
120             (mathematica:write-device-value device value refdes port)
121          )
122          (mathematica:write-device-model model refdes port)
123       )
124    )
127 (define (mathematica:write-models refdeses first port)
128    (if (not (null? refdeses))
129       (let ((refdes (car refdeses)))
130          (if (not first)
131             (mathematica:,newline port)
132          )
133          (mathematica:write-model refdes port)
134          (mathematica:write-models (cdr refdeses) #f port)
135       )
136    )
139 (define (mathematica:list-voltages netnames first port)
140    (if (not (null? netnames))
141       (let ((netname (car netnames)))
142          (if (not (equal? netname "GND"))
143             (begin
144                (if (not first)
145                   (mathematica:,newline port)
146                 )
147                 (display "v[" port)
148                 (mathematica:quoted netname port)
149                 (display "]" port)
150                 (mathematica:list-voltages (cdr netnames) #f port)
151              )
152             (mathematica:list-voltages (cdr netnames) first port)
153          )
154       )
155    )
159 (define (mathematica:list-pin-currents pins port)
160    (if (not (null? pins))
161       (let ((pin (car pins)))
162          (mathematica:,newline port)
163          (display "i[" port)
164          (mathematica:quoted (car pin) port)
165          (display "," port)
166          (mathematica:quoted (car (cdr pin)) port)
167          (display "]" port)
168          (mathematica:list-pin-currents (cdr pins) port)
169       )
170    )
173                
174 (define (mathematica:list-currents netnames port)
175    (if (not (null? netnames))
176       (let ((netname (car netnames)))
177          (mathematica:list-pin-currents 
178             (gnetlist:get-all-connections netname) port)
179             (mathematica:list-currents (cdr netnames) port)
180       )
181    )
185 (define (mathematica output-filename)
186   (let ((port (open-output-file output-filename)) 
187         (nets (gnetlist:get-all-unique-nets "dummy")))
188      (mathematica:write-voltages nets port)
189      (display "nodeEquations={" port)
190      (newline port)
191      (mathematica:write-currents nets #t port)
192      (display "};" port)
193      (newline port)
194      (display "modelEquations={" port)
195      (newline port)
196      (mathematica:write-models packages #t port)
197      (display "};" port)
198      (newline port)
199      (display "variables={" port)
200      (newline port)
201      (mathematica:list-voltages nets #t port)
202      (mathematica:list-currents nets port)
203      (display "};" port)
204      (newline port)
205    )