(part-type 'ce-amplifier		; type-name
           '()				; terminals
           '(gain swing input-impedance ; global parameters
		  min-input-impedance)
           '((any-model
              (vcc gnd c b e in out)	; model-specific nodes
					; model-specific parts
              ((power   dc-voltage-source   vcc gnd)
               (rb1     resistor            vcc b)
               (rb2     resistor            b   gnd)
               (rc      resistor            vcc c)
               (re      resistor            e   gnd)
               (q       npn-bjt             c   b  e)
               (cin     capacitor           in  b)
               (cout    capacitor           c   out)
               (sigin   voltage-signal-source  in  gnd)
               (sigout  open-circuit        out gnd))
              ()			; model-specific parameters
              ()))			; model-specific relations
           '((bias-ground		; inter-model relations
	      (= (>> potential gnd bias) 0)
	      (bias))			; models needed for this to be relevant
	     (incremental-ground
	      (= (>> potential gnd incremental) 0)
	      (incremental))
	     (input-impedance-definition
	      (= (/ (>> voltage sigin incremental)
		    (>> current t1 cin incremental))
		 (>> input-impedance))
	      (incremental))
	     (min-impedance
	      (> (>> input-impedance)
		 (>> min-input-impedance))
	      (incremental))	     
	     (gain-definition
	      (= (>> gain)
		 (/ (>> voltage sigout incremental)
		    (>> voltage sigin incremental)))
	      (incremental))
	     (swing-high
	      (< (+ (>> swing)
		    (- (>> potential c bias)
		       (>> potential gnd bias)))
		 (>> potential vcc bias))
	      (bias))
	     (swing-low
	      (> (- (>> potential collector q bias)
		    (>> potential emitter q bias))
		 (+ (>> swing) (>> vsat q bias)))
	      (bias))
	     (gain-heuristic
	      (try 
	       (simple-estimate
		(and (= (>> gain)
			(- (/ (>> resistance rc)
			      (>> resistance re))))
		     (> (>> resistance re)
			(* gain-estimator-safety-factor
			   (/ 1
			      (* q/kT
				 (>> current collector q bias)))))))
	       (actual-gain
		(= (>> gain)
		   (- (* (/ (>> resistance rc)
			    (>> resistance re))
			 (/ 1
			    (+ 1
			       (/ 1 (>> beta q)) 
			       (/ 1
				  (* (>> transconductance source q incremental)
				     (>> resistance re))))))))))
	      (bias incremental))))

(define gain-estimator-safety-factor 10.0)

(define ce (create-circuit 'CE 'ce-amplifier '(bias incremental)))


;;; Teacher-specified assumptions

(assume-value ce '(strength power) 15)

(assume-value ce '(gain) -10)

(assume-value ce '(swing) 6)

(assume-value ce '(min-input-impedance) 10000)

;; Assume cannot use gain = -Rc/Re heuristic
(node-assume! (referent ce '(actual-gain gain-heuristic)))

;; Assume transistor is amplifying
(node-assume! (referent ce '(amplifying operation q bias)))

;; Assume cannot use beta=infinity heuristic
(node-assume! (referent ce '(beta-finite beta-heuristic q bias)))

;; Assume cannot use vbe=0.6 heuristic
(node-assume! (referent ce '(exponential vbe-heuristic q bias)))

(propagate (constraint-network ce))

(assume-value ce '(potential c bias) 8.5)

(propagate (constraint-network ce))

(assume-value ce '(current collector q bias) 0.004)

(propagate (constraint-network ce))

(assume-value ce '(resistance rb2) 50000)

(propagate (constraint-network ce))

(assume-value ce '(strength sigin) .01)

(propagate (constraint-network ce))

(the-value ce '(resistance rb1))
; ((resistance rb1) = 215980.11120444624)
;   (set by (*rhs:ohm rb1 bias))
;     (because ((voltage rb1 bias) (current t1 rb1 bias)))
;Value: 215980.11120444624

(the-value ce '(input-impedance))
; ((input-impedance) = 11605.166586256135)
;   (set by (/lhs:input-impedance-definition))
;     (because ((voltage sigin incremental) (current t1 cin incremental)))
;Value: 11605.166586256135

(part-type 'ce-amplifier-pnp		; type-name
           '()				; terminals
           '(gain swing input-impedance ; global parameters
		  min-input-impedance)
           '((any-model
              (vee gnd c b e in out)	; model-specific nodes
					; model-specific parts
              ((power   dc-voltage-source   vee gnd)
               (rb2     resistor            vee b)
               (rb1     resistor            b   gnd)
               (rc      resistor            c   gnd)
               (re      resistor            vee e)
               (q       pnp-bjt             c   b  e)
               (cin     capacitor           in  b)
               (cout    capacitor           c   out)
               (sigin   voltage-signal-source  in  gnd)
               (sigout  open-circuit        out gnd))
              ()			; model-specific parameters
              ()))			; model-specific relations
           '((bias-ground		; inter-model relations
	      (= (>> potential gnd bias) 0)
	      (bias))			; models needed for this to be relevant
	     (incremental-ground
	      (= (>> potential gnd incremental) 0)
	      (incremental))
	     (input-impedance-definition
	      (= (/ (>> voltage sigin incremental)
		    (>> current t1 cin incremental))
		 (>> input-impedance))
	      (incremental))
	     (min-impedance
	      (> (>> input-impedance)
		 (>> min-input-impedance))
	      (incremental))	     
	     (gain-definition
	      (= (>> gain)
		 (/ (>> voltage sigout incremental)
		    (>> voltage sigin incremental)))
	      (incremental))
	     (swing-low
	      (> (- (>> potential c bias)
		    (>> potential gnd bias))
		 (>> swing))
	      (bias))
	     (swing-high
	      (> (- (>> potential emitter q bias)
		    (>> potential collector q bias))
		 (- (>> swing) (>> vsat q bias)))
	      (bias))
	     (gain-heuristic
	      (try 
	       (simple-estimate
		(and (= (>> gain)
			(- (/ (>> resistance rc)
			      (>> resistance re))))
		     (> (>> resistance re)
			(* gain-estimator-safety-factor
			   (/ -1
			      (* q/kT
				 (>> current collector q bias)))))))
	       (actual-gain
		(= (>> gain)
		   (- (* (/ (>> resistance rc)
			    (>> resistance re))
			 (/ 1
			    (+ 1
			       (/ 1 (>> beta q)) 
			       (/ 1
				  (* (>> transconductance source q incremental)
				     (>> resistance re))))))))))
	      (bias incremental))))

(define gain-estimator-safety-factor 10.0)

(define ce (create-circuit 'CE 'ce-amplifier-pnp '(bias incremental)))


;;; Teacher-specified assumptions

(assume-value ce '(strength power) 15)

(assume-value ce '(gain) -10)

(assume-value ce '(swing) 6)

(assume-value ce '(min-input-impedance) 10000)


;; Assume cannot use gain = -Rc/Re heuristic
(node-assume! (referent ce '(actual-gain gain-heuristic)))

;; Assume transistor is amplifying
(node-assume! (referent ce '(amplifying operation q bias)))

;; Assume cannot use beta=infinity heuristic
(node-assume! (referent ce '(beta-finite beta-heuristic q bias)))

;; Assume cannot use vbe=0.6 heuristic
(node-assume! (referent ce '(exponential vbe-heuristic q bias)))


(propagate (constraint-network ce))

(assume-value ce '(potential c bias) 6.5)

(propagate (constraint-network ce))

(assume-value ce '(current collector q bias) -0.004)

(propagate (constraint-network ce))

(assume-value ce '(resistance rb2) 50000)

(propagate (constraint-network ce))

(assume-value ce '(strength sigin) .01)

(propagate (constraint-network ce))

(the-value ce '(resistance rb1))
; ((resistance rb1) = 215980.11120444624)
;   (set by (*rhs:ohm rb1 bias))
;     (because ((voltage rb1 bias) (current t1 rb1 bias)))
;Value: 215980.11120444624

(the-value ce '(input-impedance))
; ((input-impedance) = 11605.166586256135)
;   (set by (/lhs:input-impedance-definition))
;     (because ((voltage sigin incremental) (current t1 cin incremental)))
;Value: 11605.166586256135
