Generator for Tube Parameter Tools


Author: Dmitry Nizhegorodov (dmitrynizh@hotmail.com). My other projects and articles



1.   Model Parameter finders must be On-Demand Generated

It is very convenient to use a model and a set of parameter we wish to be able to adjust as"input" and a turn-key, standalone java code as output.

One approach is to use model-view-contrroller style of coding. Combined with swing, this allows to create a model "container" with slider and model "plug-ins". However, the result code looks difficult to tweak by a novice.

Here we examine a different approach. I can quickly generate any number of model parameter finders because I use a macro-expand style code generator for the task. For example, the macro sources for class Paint_Kit in paint_kit.jar is this code:

 package tube.model.finder;

 import java.awt.event.*;
 import java.applet.*;
 import java.awt.*;
 import java.util.*;
 import java.io.FileInputStream;
 import java.io.StringBufferInputStream;
 import java.io.InputStream;

 EXTEND Macros;
 EXTEND TopLevelMacros;

 WITH_MODEL // Koren Improved Triode
  /** 
   * *********************************************************************
   * * Created on ?DATE? using ?TOOL?
   * .SUBCKT TRIODE_?NAME? 1 2 3 ; P G K ;  
   * + PARAMS: CCG=3P  CGP=1.4P CCP=1.9P RGI=2000
   * + MU=?MU? EX=?EX? KG1=?KG1? KP=?KP? KVB=?KVB? VCT=?VCT? 
   * * Vp_MAX=?Vp_MAX? Ip_MAX=?Ip_MAX? Vg_step=?Vg_step?
   * E1 7 0 VALUE={V(1,3)/KP*LOG(1+EXP(KP*(1/MU+V(2,3)/SQRT(KVB+V(1,3)*V(1,3)))))} 
   * RE1 7 0 1G 
   * G1 1 3 VALUE={(PWR(V(7),EX)+PWRS(V(7),EX))/KG1} 
   * RCP 1 3 1G ; TO AVOID FLOATING NODES IN MU-FOLLOWER 
   * C1 2 3 {CCG} ; CATHODE-GRID 
   * C2 2 1 {CGP} ; GRID=PLATE 
   * C3 1 3 {CCP} ; CATHODE-PLATE 
   * D3 5 3 DX ; FOR GRID CURRENT 
   * R1 2 5 {RGI} ; FOR GRID CURRENT 
   * .MODEL DX D(IS=1N RS=1 CJO=10PF TT=1N) 
   * .ENDS 
   * *$
   * 
   */
 final private double model(double Ep, double Eg) {
   double E1 = 
     (Ep /KP) * Math.log(1.0 + Math.exp(KP * (1.0/MU + (Eg + VCT)/Math.sqrt(KVB + Ep*Ep))));
   int sgn_E1 = (E1 >= 0.0) ? 1 : -1;
   return (Math.pow(E1, EX)/KG1) * (double)(1 + sgn_E1);
 }
 WITH_SLIDER MU 4.2
 WITH_SLIDER KG1 1500
 WITH_SLIDER KP 600.0
 WITH_SLIDER KVB 300.0
 WITH_SLIDER VCT 0.2
 WITH_SLIDER EX 1.4
 DEF_MODEL_PARAM_FINDER PaintKIT;

  
this generates the complete text of PaintKIT.java. How doe it works? Each macro expands into something specific -a template, and those that take parameters expand into a variant of its template (parameterized expansion). That's about all to it. DEF_MODEL_PARAM_FINDER XYZ generates class XYZ with all the guts of the applet that displays tube curves; macro WITH_SLIDER adds an instance variable and a slider control to it. WITH_MODEL extracts SPICE code from java doc comment that follows it and adds it to the class.

The method double model(double,double) is something that is used by the engine created by DEF_MODEL_PARAM_FINDER and can be any computation using, in particular, the variables defined with WITH_SLIDER. it is invoked by the engine with various value pairs of grid and plate voltages, as defined by the ranges.

The following is a completely trivial parameter finder (nothing related to tubes!):

 package tube.model.finder;

 import java.awt.event.*;
 import java.applet.*;
 import java.awt.*;
 import java.util.*;
 import java.io.FileInputStream;
 import java.io.StringBufferInputStream;
 import java.io.InputStream;

 EXTEND Macros;
 EXTEND TopLevelMacros;

 WITH_MODEL // Just test
  /** 
   * 
   * *** Created on ?DATE? using ?TOOL? *****
   * .SUBCKT ?NAME? 1 2 3 PARAMS: A=?A? B=?B? C=?C?
   * G1 1 3 VALUE={(A * V(1,2) + B)C }
   * RCP 1 3 1G 
   * 
   */
 final private double model(double Ep, double Eg) {
   return (A * Eg + B) / C;
 }
 WITH_SLIDER A 10.0
 WITH_SLIDER B 2.0
 WITH_SLIDER C 2000.0
 DEF_MODEL_PARAM_FINDER TrivialModel;

  
yet generates a comnplete model finder

2.   Try it online.

The following link opens up a java applet that is an online interface to the code generator. The input is in the top window, the output is below. The 1st button creates a template of a tube param finder. One can try to change it slightly, generate java code, copy and paste it into a text file, compile using "javac" command and then run using "java" command.

Java Code Generator


Author: Dmitry Nizhegorodov (dmitrynizh@hotmail.com). My other projects and articles