(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
              ((ground			; model-specific relations
		(= (>> potential gnd) 0)))))
           '((input-impedance-definition ; inter-model relations
	      (= (/ (>> voltage sigin incremental)
		    (>> current t1 cin incremental))
		 (>> input-impedance))
	      (incremental))		; models needed for this to be relevant
	     (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 can use gain = -Rc/Re heuristic
(node-assume! (referent ce '(simple-estimate gain-heuristic)))

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

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

;; Assume can use vbe=0.6 heuristic
(node-assume! (referent ce '(emitter-follows vbe-heuristic q bias)))

(propagate (constraint-network ce))

;;; Student-specified assumptions

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

(propagate (constraint-network ce))
; (Contradiction! #[uninterned-symbol 19 CEP94])

(explain (car (cn-contradictions (constraint-network ce))))
;(CEP94 contradiction found by (<swing-high) (CEP93 CEP79))
;(CEP93 (v:lhs:swing-high) = 16 set by (+lhs:swing-high) (CEP68 CEP91))
;(CEP79 (potential vcc bias) = 15 set by (-rhs:kvl power bias) (CEP75 CEP35))
;(CEP68 (swing) = 6 set by assumption (CEP69))
;(CEP91 (v:1:lhs:swing-high) = 10 set by (-1:lhs:swing-high) (CEP89 CEP35))
;(CEP75 (voltage power bias) = 15 set by (=rhs:voltage-source power bias) (CEP64))
;(CEP35 (potential gnd bias) = 0 set by (v:rhs:ground bias) ())
;(CEP89 (potential c bias) = 10 set by assumption (CEP90))
;(CEP64 (strength power) = 15 set by assumption (CEP65))
;Value: QED

(support ce (car (cn-contradictions (constraint-network ce))))
;Value: ((swing) (potential c bias) (strength power))

(retract-assumed-value ce '(potential c bias))

(propagate (constraint-network ce))

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

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

(propagate (constraint-network ce))
; (Contradiction! #[uninterned-symbol 19 CEP125])

(explain (car (cn-contradictions (constraint-network ce))))
;(CEP125 contradiction found by (>swing-low) (CEP122 CEP72))
;(CEP122 (v:lhs:swing-low) = 4. set by (-lhs:swing-low) (CEP94 CEP119))
;(CEP72 (v:rhs:swing-low) = 6.2 set by (+rhs:swing-low) (CEP67 CEP26))
;(CEP94 (potential c bias) = 5 set by assumption (CEP95))
;(CEP119 (potential e bias) = 1. set by (-rhs:kvl re bias) (CEP118 CEP49))
;(CEP67 (swing) = 6 set by assumption (CEP68))
;(CEP26 (vsat q bias) = .2 set by (v:rhs:vsat-typical q bias) ())
;(CEP118 (voltage re bias) = 1. set by (*rhs:ohm re bias) (CEP110 CEP115))
;(CEP49 (potential gnd bias) = 0 set by (v:rhs:bias-ground) ())
;(CEP110 (resistance re) = 100. set by (/0:rhs:lhs:R:0:gain-heuristic) (CEP108 CEP84))
;(CEP115 (current t1 re bias) = .01 set by (kcl-node e bias) (CEP104))
;(CEP108 (resistance rc) = 1000. set by (*rhs:ohm rc bias) (CEP98 CEP105))
;(CEP84 (v:0:rhs:lhs:R:0:gain-heuristic) = 10 set by (-rhs:lhs:R:0:gain-heuristic) (CEP73))
;(CEP104 (current emitter q bias) = -.01 set by (-rhs:kcl q bias) (CEP103))
;(CEP98 (voltage rc bias) = 10 set by (-rhs:kvl rc bias) (CEP75 CEP94))
;(CEP105 (current t1 rc bias) = .01 set by (-rhs:kcl rc bias) (CEP102))
;(CEP73 (v:rhs:lhs:R:0:gain-heuristic) = -10 set by (=lhs:R:0:gain-heuristic) (CEP65 CEP53))
;(CEP103 (=kcl q bias) = .01 set by (+lhs:kcl q bias) (CEP96 CEP71))
;(CEP75 (potential vcc bias) = 15 set by (-rhs:kvl power bias) (CEP74 CEP49))
;(CEP102 (current t2 rc bias) = -.01 set by (kcl-node c bias) (CEP28 CEP96))
;(CEP65 (gain) = -10 set by assumption (CEP66))
;(CEP53 and-a1:R:0:gain-heuristic set by (and:R:0:gain-heuristic) (CEP51))
;(CEP96 (current collector q bias) = .01 set by assumption (CEP97))
;(CEP71 (current base q bias) = 0 set by (=R:0:beta-heuristic q bias) (CEP17 CEP16))
;(CEP74 (voltage power bias) = 15 set by (=rhs:voltage-source power bias) (CEP63))
;(CEP28 (current t1 cout bias) = 0 set by (v:rhs:opened cout bias) ())
;(CEP51 T:0:gain-heuristic PREMISE)
;(CEP17 (v:rhs:R:0:beta-heuristic q bias) = 0 set by (c:rhs:R:0:beta-heuristic q bias) ())
;(CEP16 T:0:beta-heuristic PREMISE)
;(CEP63 (strength power) = 15 set by assumption (CEP64))
;Value: QED

(pp (support ce (car (cn-contradictions (constraint-network ce)))))
;((swing)
; (beta-infinite beta-heuristic q bias)
; (simple-estimate gain-heuristic)
; (gain)
; (current collector q bias)
; (strength power)
; (potential c bias))

(retract-assumed-value ce '(potential c bias))

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

(propagate (constraint-network ce))

(the-value ce '(resistance rc))
; ((resistance rc) = 650.)
;   (set by (*rhs:ohm rc bias))
;     (because ((voltage rc bias) (current t1 rc bias)))

(the-value ce '(resistance re))
; ((resistance re) = 65.)
;   (set by (/0:rhs:rhs:lhs:gain-heuristic))
;     (because ((resistance rc) (v:0:rhs:rhs:lhs:gain-heuristic)))

(support ce '(resistance rc))
;Value: ((strength power) (potential c bias) (current collector q bias))

(the-value ce '(resistance rb1))
;((resistance rb1) is not assigned)

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

(propagate (constraint-network ce))

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

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

(propagate (constraint-network ce))
; (Contradiction! #[uninterned-symbol 19 CEP213])
; (Contradiction! #[uninterned-symbol 20 CEP212])
; (Contradiction! #[uninterned-symbol 21 CEP208])

(pp (support ce (car (cn-contradictions (constraint-network ce)))))
;((min-input-impedance)
; (strength sigin)
; (resistance rb2)
; (emitter-follows vbe-heuristic q bias)
; (beta-infinite beta-heuristic q bias)
; (simple-estimate gain-heuristic)
; (gain)
; (current collector q bias)
; (strength power)
; (potential c bias))

(pp (support ce (cadr (cn-contradictions (constraint-network ce)))))
;((strength sigin) 
; (resistance rb2)
; (emitter-follows vbe-heuristic q bias)
; (beta-infinite beta-heuristic q bias)
; (simple-estimate gain-heuristic)
; (gain)
; (current collector q bias)
; (strength power)
; (potential c bias))

(pp (support ce (caddr (cn-contradictions (constraint-network ce)))))
;((strength sigin)
; (simple-estimate gain-heuristic)
; (gain)
; (current collector q bias)
; (strength power)
; (potential c bias))

(node-retract! (referent ce '(simple-estimate gain-heuristic)))

(propagate (constraint-network ce))
; (Contradiction! #[uninterned-symbol 30 CEP251])

(pp (support ce (car (cn-contradictions (constraint-network ce)))))
;((min-input-impedance)
; (resistance rb2)
; (emitter-follows vbe-heuristic q bias)
; (beta-infinite beta-heuristic q bias)
; (strength sigin)
; (gain)
; (current collector q bias)
; (strength power)
; (potential c bias))

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

(retract-assumed-value ce '(resistance rb2))

(propagate (constraint-network ce))

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

(propagate (constraint-network ce))
; (Contradiction! #[uninterned-symbol 32 CEP272])

(retract-assumed-value ce '(current collector q bias))

(propagate (constraint-network ce))

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

(propagate (constraint-network ce))

(the-value ce '(resistance re))
; ((resistance re) = 617.9681613101286)
;   (set by (*rhs:ohm re incremental))
;     (because ((voltage re incremental) (current t1 re incremental)))

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

(retract-assumed-value ce '(current collector q bias))

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

(propagate (constraint-network ce))

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

(the-value ce '(resistance re))
; ((resistance re) = 123.59363226202572)
;   (set by (*rhs:ohm re incremental))
;     (because ((voltage re incremental) (current t1 re incremental)))

;;; Now, let beta be finite!
;; retract beta=infinity heuristic
(node-retract! (referent ce '(beta-infinite beta-heuristic q bias)))

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

(propagate (constraint-network ce))
; (Contradiction! #[uninterned-symbol 33 CEP375])

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

(retract-assumed-value ce '(current collector q bias))

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

(propagate (constraint-network ce))

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

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

(the-value ce '(resistance rb2))
; ((resistance rb2) = 50000)
;   (set by assumption)

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

(the-value ce '(resistance re))
; ((resistance re) = 205.9893871033762)
;   (set by (*rhs:ohm re incremental))
;     (because ((voltage re incremental) (current t1 re incremental)))

;;; Now, relax the vbe=0.6 heuristic assumption:
(node-retract! (referent ce '(emitter-follows vbe-heuristic q bias)))

(node-assume! (referent ce '(exponential vbe-heuristic q bias)))

(propagate (constraint-network ce))

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

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

(the-value ce '(resistance rb2))
; ((resistance rb2) = 50000)
;   (set by assumption)

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

(the-value ce '(resistance re))
; ((resistance re) = 205.9893871033762)
;   (set by (*rhs:ohm re incremental))
;     (because ((voltage re incremental) (current t1 re incremental)))

