CUBIC version of NSK                         (c) J.Kolafa 5/1999, 4/2025
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Calculation of phase diagrams of binary mixtures using van der Waals and
Redlich-Kwong equations of state with parameters defined by one-fluid
approach.  For units, see CH4+CO2.def.

For combining rules and cross parameters, see section sys: in nsk.k99.

The basic principle if NSK is showing a scaled Gibbs function G(x) or its
derivative, where x is the composition (mole fraction).  For instance, the
phase equilibrium appears as a common tangent.  The typical style of work is
based on locating phenomena approximately on the G(x) graph, followed by
switching to accurate numerical calcuulation including line tracing.
More that 40 phenomena are supported, divided to 5 groups according to the
number of degrees of freedom, f:
  f=2:  vapor-liquid equilibria for p or T=const,..
  f=1:  lines in p-T diagrams: critical lines, heteroazeotropes, azeotropes,..
  f=0:  points in p-T diagrams: pure critical, azeotropic end-points,..
  f=-1: lines in global diagrams: tricritial point, critical end-points,..
  f=-2: points in global diagrams: tricritical end-point,..

Supported platforms:
  * Linux and X11
  * Historically: DOS, various unixes with X11

Supported precision:
  * this demo: long double (10 B real numbers)
  * also available: double, emulated any precision

GUI:
  * is ugly because Turbo C graphics originally used under DOS is emulated

CLI:
  * 25x80 text mode screen or window with ISO 6429 / ANSI color codes
  * any text editor, preferably with a possibility to sort lines according
    to selected columns

Installation of the DEMO (van der Waals and Redlich-Kwong):
  * unzip to a directory

Other EoSs and sources:
  * Send me an e-mail for the sources and help

Running NSK (see file cubic.k99):
  * nsk [OPTIONS] [IMAGE]
  * use OPTION=-a0 for broken terminal (no ANSI colors)
  * see below for examples

Getting help from NSK:
  * Full help
    - read file "nsk.k99"
    - from NSK data input type `key=99;' and press Enter
    - from GUI: click [key>] and select [99=help browser]
    - from GUI: click [key>] and click the [key] of interest
  * Brief GUI help: click [help] or type `h'


EXAMPLE 1 -- calculate P-T diagram of mixture CO + CO2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0.  If anything gets wrong:
    - try [Undo], hot key U
    - Type Ctrl-C in the terminal, select as needed
    - kill NSK and restart with history0.img:
      nsk history0.img -o CO+CO2.res
1.  See CO+CO2.def to calculate parameters a,b from compound critical data
    and how to set NSK to use real units.
2.  Run NSK with the respective i/o files:
    nsk -i CO+CO2.def -o your-CO+CO2.res
3.  Type
      ; <Enter>
4.  Click button [CP] or type C above black [x=0].  The critical point of CO2
    will be calculated.  To write it to file CO+CO2.res, type [res=0]
    (res=1 should appear) and recalculate. Type [EOL] to separate.
5.  Click [x] (bottom row) or type `x', select x=1 or type `1' and repeat
    step 4.
6.  Let's calculate the critical line ending at the Cp of CO2.
    Check the calculated data by clicking [p:T] or hot key ; or :.
    Rescale this graph by `-' and select a higher pressure (~3e7) and
    T about 150.
7.  Alternatively, click/type ESC and type T=150, p=3e7 and continue
    by ; <Enter>.
8.  If needed, change T to locate a critical point (third derivative of G(x)
    is zero at certain x, try [G 0] and [G 1] or Space to show G'(x).
    Temperature change goes by typong t and T or * / next to [T]; the step is
    changed by clicking [*] and [/] next to [step].
    To see better G(x), rescale the graph by drawing a rectangle from top
    (this adds a linear function to G(x); drawing a rectangle from bottom just
    zooms in). Full range of x is requested by i or [in[0,1]].
    Click [cL] or type c. Re-adjust in case of error.
9.  We will now calculate the critical line down to one of the critical
    points. Type [f] next to [T] to select p as the independent variable.
    Select [BOL] (Beginning Of Line). Click [qInv] to go down. You may adjust
    the step by [*] [/] next to [q] or hot keys q,Q. Click [cL]: another point
    should be calculated. Check by [p:T]. Repeat; if you are lazy, selext
    higher [liNe] or -1 (until error) and recalculate by [cL]. [pred] should be
    1 (prediction is on).
10. Check the diagram by [p:T]. You may recalculate the critical point
    by x=0 and [CP] or copy the respective line (starting with key=3) to the
    file.
    Type [EOL] (End Of Line) - DO NOT FORGET THIS!
11. Let's calculate the VLE lines of pure compounds.  It is easier to start
    from low T (but not too much).  E.g., for CO2 (less volatile), you may
    start from about 150 K.
12. Decrease pressure (using pP and */ or buttons) until you can see two
    phases (yellow and cyan) close to x=0 (pure CO2); the cyan may be short and
    going up.  Type [vL] or hot key v. Select scanning for T ([f] or hot key
    f).  Select [BOL], perhaps [liNe 10] and retype [vL]. Recalculate CP or
    copy the line beginning with 3 after these data. Type [EOL]. Check by
    [p:T].
13. Repeat for CO (x=1) from about T=100.
14. Now there is a time to calculate the heteroazeotrope.
    Click [p:T] and select a point slightly above the CO vapor-liquid line
    (let us say around T=120). Change pressure until the vapor phase close to
    x=1 appears. Adjust finely to align all three minima to one common
    tangent. Now, by clicking [x], typing x or just clicking to existing x
    red vertical line, move/click it to approximately propagate the common
    tangent. Repeat for y. Repeat for z (gas); you may need rescaling and
    slider [<-<- fine move->->] in the bottom line.
15. Type [3PL]. Repeat the above point on error. Calculate the line towards
    the critical point.  Type several times until it fails because we are
    close to the critical point.
16. Put x to the critical point and y to the first minimum; from the command
    prompt (type ESC), this may be useful:
    w=x  x=(y+z)/2  y=w ;
    Click [CEP] (Critical EndPoint). Click [EOL].  It it fails, try 18. forst
    and repeat after you pass the CEP.
17. To extend the heteroazeotropic line also in the opposite direction,
    click [qInv] and repeat from 14.; you may also load the previous bol.img
    using key->[loadbol].
18. The short critical line between CEP and CP has not been calculated yet.
    Use [p:T], click in between, and watch G(x) and G'(x) properly rescaled
    (around x close to 1).  Fine adjust p to see a clear critical point, at best
    as G''(x)=0 visible on G'(x) (i.e., a horizontal "smooth step"). Click [x]
    or type x and click this x, type ESC and calculate [cL].  Calculate the
    whole line as usual.  In the CO+CO2 file, remove the metastable branch
    (beyond CEP) if necessary.
19. Edit the final CO+CO2.res file using your text editor, or using the
    embedded tool from [p:T] and hot key E. To get the manual, use section
    key=-55 of the manual, or type key=99; from the prompt, then -55.


DEMO 1 -- watch an already calculated T-x diagram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is a p=1.1 section of the diagram from DEMO 2
1. Run NSK with image `tx.img' and res-file `tx.res':
     nsk tx -o tx.res
2. Type
     ; <Enter>
   and you will see the Gibbs energy G(x) at a three-phase equilibrium
3. Click [T:x] to see the T-x diagram
   - Type ? for symbol help
   - Draw a rectangle by a left mouse button to zoom in a selected part
   - Escape back to the prompt by typing Esc
4. Type
     key=-99;
   and press Enter to quit.
   Alternatively, press Ctrl-D or Ctrl-C and select e+Enter


DEMO 2 -- watch an already calculated p-T diagram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Run NSK with image `4pp.img' and res-file `4pp.res':
     nsk 4pp -o 4pp.res
2. Type
     key=0; <Enter>
   and you will see the Gibbs energy G(x) at a four-phase equilibrium
3. Click [p:T] to see a p-T diagram from the shield region
   - Type ? for symbol help
   - Draw a rectangle by a left mouse button to zoom in a selected part
   - Escape back to the prompt by typing Esc
4. Type
     key=-99;
   and press Enter to quit.
   Alternatively, press Ctrl-D or Ctrl-C and select e+Enter


Example -- calculate a simple x-T diagram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.  Run NSK:
      nsk
    The default file "cubic.def" will be read and
    a NSK `command prompt' will appear
2.  type
      key=0; <Enter>
    and you should see the Gibbs function G(x) for given p and T
3.  Type `x' (as hot key) or click [x] button (by left mouse) on the
    bottom bar. Click (by left mouse button) close to one of the G(x)
    minima (called `bumps'), drag it and see the tangent, place this
    tangent so that it is, approximately, a common tangent of both bumps
    (to place it more precisely, use the [<<fine move>>] button as a
    scrollbar), and click the right mouse button (or type Esc) to return
    back
4.  Type `y' (as hot key) or click [y] button and place it in the same
    way to the second G(x) bump
5.  Type `=' or click [res=0]: [res=1] should appear denoting that the
    calculated data will be written to a file (called "nsk.res")
6.  Click [2ph] or press key `2': a phase equilibrium should be
    calculated
7.  Click [BOL] or press `B': this starts line tracing. Click [ q ] or
    type `q': you will see number 1.01 which is a quotient by which T
    will be multiplied for calculating more points
8.  Click [2ph] or press `2' again: one point on a line will be
    calculated
9.  Click [liNe 1] or press [N], [liNe10] should appear
10. Click [2ph] or press `2' again: 10 points will be calculated. Repeat
    and watch what G(x) is doing
11. When you repeat this several times, the two bumps merge together and
    the calculation fails. At this moment click [2ph] several times more
    to reach this limit more accurately
12. Click [ q ] or type Ctrl-q: the quotient decreased as the
    consequence of unsuccessful calculations. You may also change the
    value of the quotient by [*] and [/] or `q' and `Q'
13. Click [cL] or type `c': a point on a critical line (CL) will be
    calculated (if not, repeat 11. above)
14. Click [EOL] or type `E' to end the line
15. There is time to see what we have calculated: click [T:x] or type
    `:'. Return back by right button or Esc
16. Click [*] next to [step] several times to reach value of about 1.06
    (equivalent hot key is `J'). This is the multiplier of T for the
    next step.
17. Click [*] next to [ T ] (or type `T') several times (= increase
    temperature) until two bumps appear again. Be precise: click [/] or
    type `t' to return back, and/or use finer search (click [/] next to
    [step] or type `j').
18. Enlarge the area around emerging bumps: draw a rectangle by the left
    mouse button from top to bottom. The G(x) curve will be rescaled,
    i.e., a linear function will be added to see better the phenomena of
    interest
19. Click [G 0] or type Space to see dG/dx instead of G
20. Repeat step 17 until you see an inflection point with a horizontal
    tangent. This is another critical point
21. Calculate the critical point: set [res=1] (see step 5) and use [cL]
    or `c'
22. Set [G 0] (see step 19) to see G(x) again, increase T a bit,
    eventually change scaling (use [++ zoom --] and [<< move>>] as the
    scrollbar, and/or type `+' and `-' and cursors, eventually click
    [in<0,1>] or type `i' ...)
23. Place x and y to the bumps as in steps 3 and 4
24. Calculate the equilibrium in the same way as in steps 7--10.
    Change scaling (step 22) if the equilibrium runs out of screen
25. After a while, the calculation fails because x and y are close to 0
26. Click [x] or type `x', then click [x=0] or type `0' to set x=0
27. The menu changed. Click [vL] or type `v' and a vapor-liquid
    equilibrium for a pure compound 1 should be evaluated.
28. Have a look at the diagram (step 15)
29. To exit NSK click [key>] and select [-99="exit.img"+exit]
    or type Esc and type command key=-99;
    or type Alt-F4 (DOS only)


More -- calculate a p-T diagram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.  Run NSK as follows
      NSK exit -o pt.res
    where image file "exit.img" was saved in step 29 above and "pt.res"
    is the name of the res-file
2.  Click [Diag] to reach [Diag2], button left to [Diag2] should be [p:T]
3.  Calculate critical points of pure species: select [res=1], select x=0
    (use [x] and [x=0]), then click [CP].  Repeat with x=1
4.  Now click [help], browse it, and find an example on how to calculate
    critical lines -- you have two starting points from the Example
5.  Calculate the CL's
6.  Follow the low-temperature liquid-liquid CL to low pressures and
    watch how a second bump appears. To speed up, click [^] or type ^ to
    get 1.02 or 1.07 (see the help on variable qq to know what it does).
    You may need to move slowly up and down along this line -- use
    [qInv] or `I' and [/] next to [ q ] or `q' and click [cL] again
7.  When you locate the second bump so that it touches, approximately,
    the tangent to the critical point, place y there (you will need
    <<fine move>> with even shorter step: click [/] next to [step] or
    type `j').
8.  Click [CEP] or type `C': the critical end-point will be calculated.
    Save the image (use [key>] and [-1=save image]). If you are in
    trouble, load image "cep.img" (click [key>], select [-2=load image],
    type cep, and press Enter)
9.  Read the help how to calculate other lines: vapor-liquid,
    three-phase, ...


Even more -- calculate a global phase diagram
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.  Load point from step 6 above
2.  Click [Diag] to have [GPD] [Diag3]
3.  Check that the trace global variable is  [ s2 ]
4.  Start line by [BOL] (if there is [EOL], click [EOL] first, then
    [BOL])
5.  Click [CEP]. It will fail because quotient q is too long, but the
    line (auxiliary in this case) should be started (= button [pred]
    must be [pred 1])
6.  Re-try with lower q
7.  Have a look at T: you will see that it decreased, what we do not
    want. Hence, click [qInv]
8.  Click [^], select [liNe-1] (-1=infinity) and click CEP
9.  It will fail after a while. Decrease 1/q (click [/] next to [ q ])
    and repeat several times to locate the end of CEP. Note that, after
    any failure, 1/q is automatically decreased so that just by clicking
    [CEP] you can get close to the end of CEP. Rescale G(x) as
    appropriate
10. Save the image and have a look what is happening along the critical
    line -- do not forget to select [p:T] [Diag2]
11. Here is what is happening: the critical line becomes parallel to the
    T axis
12. Thus, load the image saved in step 10, click [f] or type `f' so
    that [f] appears next to [ T ]
13. Continue tracing of [CEP] until possible: you should see the normal
    bump coalescing with the critical bump. We are close to the
    tricritical point
14. Calculate the tricritical point: use [key>] and
    [14=TriCritical Point]
15. Watch the minimization and after the calculation is done, have a
    look at G(x) and dG/dx
16. Eventually, read the help for key=14 on how to use a faster method
    and try it
17. Check variables solve and trace (escape to the prompt using [Esc],
    check, and return back to GUI), click [BOL], and calculate the line
    of tricritical points (TCP)
18. And what about the double critical endpoint accompanying this TCP?
    It is on the same side of the TCP line as the CEP we started from.
    Increase very slightly s2, or use one of the CEP's calculated in
    step 13. Do not forget to set [p:T] [Diag2], then find another
    branch of CL and a CEP. Note that the CEP from step 13 has x<y
    (x=mole fraction of the critical phase, y=of the normal phase) while
    this CEP must have x>y.
19. Trace this CEP again in variable s2 (steps 2--); if x moves out of
    y, you are in bad direction. Try to locate precisely the end of CEP
    existence (i.e., until q is very close to 1). You should be very
    close to a double critical endpoint (DCEP)
20. Check that you are really close to it by tracing the critical line:
    the normal bump (y-bump) should cross a bit the x-tangent but then
    to retreat back
21. Calculate this DCEP: use [key>] and [19=Double Cr.EndP.]
