This project is read-only.

Using Nature from within Powershell

Installing Nature PS module

Important: Ensure that Windows PowerShell 4.0 (or higher) and Microsoft .NET Framework 4.5 (or higher) are installed on the computer.
If you are familiar with PowerShell modules, you can start working with Nature directly by importing it into PowerShell with the Import-Module cmdlet. To make the import easier, you can install the Nature module into your Modules directory so that importing will not require specifying the module file path. Additionally you can add the “Import-Module Nature” command into you profile.ps1 file so that Nature will be loaded automatically every time you start the new PS session.
If you are not familiar with Microsoft PowerShell, please use the NaturePS.Installer.v2 executable which will copy the module into the user-specific PS directory. Optionally you can also choose to load the module every time the new PS session started.
To be able using scrips stored on your file system, after installing the module, open PowerShell editor as Administrator and execute the following command
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned 
Note: it is often convenient to pin Windows PowerShell and Windows PowerShell ISE to either the Start Menu or the Taskbar

Importing the Nature module

The following is only important if you did not choose to import Nature for each PS session automatically. Otherwise, when starting the new PS session, in order to be able calling Nature from PowerShell, you need to import the module first using the following command
Import-Module Nature

To check whether the module is loaded you can use the following
Get-Module Nature | Format-Table -AutoSize
If the module is loaded successfully, you will see the command output displaying the module information.
Important: Verify that the Numbers Decimal symbol in the Current User Region and Language settings is ".". If not possible to change, you will need to add the following command at the begining of each Nature script you execute:
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::InvariantCulture

Loading the thermochemical model. New-ModelObject cmdlet

In order to start with actual computing, a new instance of the ModelObject class needs to be constructed. A new model can be created using the New-ModelObject cmdlet. To get a detailed help information on the New-ModelObject cmdlet, use the following command
help New-ModelObject -Full
At minimum the cmdlet requires the user to specify web Url(s) (or local file names) targeting a thermochemical model given in the CHEMKIN format. As an example consider the following operator creating the Nature model based the GRI 3 reaction mechanism
$model = New-ModelObject "",
Here the GRI 3 based model is created and stored in the $model variable. From now on all the properties and methods of the model can be accesses/called directly using this variable. For example the following operator displays all the chemical element names found the model
The PowerShell intellisense feature can be very handy in discovering all the properties and methods available. You can use tab for auto-completion.
If your computations do not require the complete set of elements or species found in the model files, you can use the -OptIn or -OptOut switch together with the -Elements and/or -Species parameters. For example the following code creates the GRI 3 based model where only the oxygen and the nitrogen species are included
$model = 
New-ModelObject "",
    "" -OptIn -Species o2, n2

"Elements: $($model.ElementIDs)"
"Species: $($model.SpeciesIDs)"
As the output you should see
Elements: o n
Species: o2 n2
Or, because of Nature module includes the predefined $GRI3 variable containing the mechanism URLs, the above example can be shortend to
$model = New-ModelObject $GRI3 -OptIn -Species o2, n2
"Elements: $($model.ElementIDs)"
"Species: $($model.SpeciesIDs)"

The complete list of methods and properties of ModelObject can be retrived by executing the following command
$model | Get-Member
Here the $model variable, created in the examples above, is piped into the Get-Member cmdlet that discovers the models properties and methods. The resulting list of members will be displayed on the screen in the alphabetic order.

Creating and working with Ideal Gas Mixture objects

The IdealGasMixture class plays the key role in the Nature library, since objects of this class are the primary source of the all the information associated with the ideal gas state. The following shown an example of creating a new object of this class, using the $model variable (see ModelObject examples above)
$mixture = $model.CreateIdealGasMixture("C3H8")
Here the $mixture object represents an ideal gas mixture consisting of a single component: C3H8. By default, the mixtures temperature and pressure are 300K and 1atm respectively. To verify that, you can convert it to string displaying the Temperature – Pressure – Mole Fractions state with
$mixture = $model.ToString()
The IdealGasMixture class is designed to allow for the ultimately simple, strait forward querying of the ideal gas properties without the need to call any particular functions. In fact the only functions callable on IdealGasMixture objects are there to change the mixture state by e.g. altering its temperature, pressure, by simulating adiabatic compression etc.
The basic principal of the class functioning is described as follows. Every time the user accesses for the first time a property of the IdealGasMixture class, Nature automatically figures out which internal functions it has to call in order to compute the requested property. As an example consider the following request that displays the specific internal energy of the earlier created $mixture
Executing this statement should display -2408607.72995265. Prior to this call, the specific internal energy value was not pre-computed. Instead, due to the property is requested explicitly, the ideal gas mixture object is triggered to make all the necessary function calls for evaluating the requested property. Once computed, the gas property is cached inside the ideal gas mixture object, so that any repetitive requests to the same property do not trigger the computational algorithm again and again, but instead return the value computed with the primary request.
When the mixture state changes, the IdealGasMixture object drops all the values populated from the user requests, and is prepared to compute the new values. The following example alters the mixture temperature by executing the Set-Temperature cmdlet for the temperature values of 1000K, 2000K, 3000K, 4000K and 5000K
foreach($temperature in 1000, 2000, 3000, 4000, 5000){
    Set-Temperature -Mixture $mixture -Temperature $temperature -AtFixedPressure
    "Temperature: $temperature [K], Speed of sound: $($mixture.SpeedOfSound) [m/s]"
The output of this loop is
Temperature: 1000 [K], Speed of sound: 444.947553107775 [m/s]
Temperature: 2000 [K], Speed of sound: 625.900257735061 [m/s]
Temperature: 3000 [K], Speed of sound: 765.638826579957 [m/s]
Temperature: 4000 [K], Speed of sound: 883.837779518238 [m/s]
Temperature: 5000 [K], Speed of sound: 987.817984690689 [m/s]
Creating complex mixtures. Gmix script
In Nature, the primary way to construct IdealGasMixture objects representing complex gas mixtures is by using the mixture definition scripts (Gmix scripts). To build a new mixture object, the GMix script needs to be supplied is a parameter for the CreateIdealGasMixture method of the ModelObject. Here is an example of creating a mixture representing air, based on the GRI3 model:
$model = New-ModelObject $GRI3 -OptIn -Species o2, n2
$mixture = 
$model.CreateIdealGasMixture("  O2 = 0.21   N2 = ?  [temperature] = 800.0 [pressure] = 1  ")

First the new ModelObject is built including only the O2 and the N2 chemical species. Next the air mixture at the temperature of 800K and the pressure of 1 atmosphere is created using the Gmix script: " O2 = 0.21 N2 = ? temperature = 800.0 pressure = 1 ". In this script the oxygen mole fraction is set to the value of 21% while the nitrogen concentration is delegated to be evaluated by the Gmix engine. In this particular case, the engine will simply deduct the value from the mole fractions total of 1, so that N2 = 0.79. Temperature and pressure are enclosed in the square brackets since both temperature and pressure are reserved key words. From the moment the $mixture object is constructed, it is ready to be used for the querying.
Below is the complete example creating and using the air mixture to compute the temperature/speed of sound map for the shock compression from pressure of 1 atmosphere to the pressures of 10, 100, 200 and 300 atmosphere respectively.
$model = New-ModelObject $GRI3 -OptIn -Species o2, n2
$mixture = 
$model.CreateIdealGasMixture("  O2 = 0.21   N2 = ?  [temperature] = 800.0 [pressure] = 1")
foreach($pressure in 10, 100, 200, 300)
    Set-Pressure -Mixture $mixture -Pressure $pressure -Units Atmosphere -Shock
    "Temperature: $($mixture.Temperature) [K], Speed of sound: $($mixture.SpeedOfSound) [m/s]"
Running this code in PowerShell should produce
Temperature: 1872.34526076953 [K], Speed of sound: 837.235690936563 [m/s]
Temperature: 4153.48946981238 [K], Speed of sound: 1236.45979565387 [m/s]
Temperature: 4866.22639127899 [K], Speed of sound: 1337.42841368153 [m/s]
Temperature: 5320.39847285319 [K], Speed of sound: 1398.41473145363 [m/s]
Sometimes the concentrations of the chemical species are defined not explicitly, but rather via the fuel/oxidizer characteristic ratios. In that case Gmix allows to delegate computing the species concentrations to the Gmix engine (by setting the correspondent values to '?' in the mixture definition script). The characteristic ratios need to be specified explicitly. The next example creates the stoichiometrically balanced mixture of methane and oxygen (equivalence ratio = 1) at temperature of 300K and pressure of 1 atmosphere
$model = New-ModelObject $GRI3 -OptIn -Species o2, ch4
$mixture = $model.CreateIdealGasMixture("
    CH4 = ?
    O2  = ?    
    [equivalence ratio] = 1")
"The mixture state is: $($mixture)"
Running the script results in
The mixture state is:  [T] = 300
 [P] = 1
 o2 = 0.666666666666667
 ch4 = 0.333333333333333
In a more complex scenario, if the mixture consists not of just the pure chemical components, but of some other complex components (other mixtures), a specialized Gmix scripting construct needs to be used. For example, when required to build an IdealGasMixture object representing the stoichiometric mixture of the 50%CH4 by 50%C3H8 fuel with pure air, it is easier to define the fuel and the air components in their own Gmix blocks:
[fuel] { CH4 = 0.5   C3H8 = ? }
[air]  { O2 = 0.21   N2 = ? }
Note that enclosing fuel and air in square brackets reserves both as dynamic keywords and also names the corresponding components definitions. Now both components can be referenced from the main/root mixture definition block by the reserved names. The main mixture definition is identified with the *main – name. Here is the complete example creating the stoichiometrically balanced mixture of both complex components
$model = New-ModelObject $GRI3 -OptIn -Species o2, ch4, n2, c3h8
$mixture = $model.CreateIdealGasMixture("
        [fuel] = ?
        [air] = ?
        [equivalence ratio] = 1
    [fuel] { CH4 = 0.5   C3H8 = ? }
    [air]  { O2  = 0.21  N2   = ? } ")

"The mixture state is: $($mixture)"
Running this code displays
The mixture state is:  [T] = 300
 [P] = 1
 o2 = 0.19811320754717
 ch4 = 0.0283018867924528
 n2 = 0.745283018867925
 c3h8 = 0.0283018867924528
The result can be validated by additionally displaying the CHOFormula property of the constructed IdealGasMixture object:
that results in
Carbon        : 0.113207547169811
Hydrogen      : 0.339622641509434
Oxygen        : 0.39622641509434
OxygenBalance : 0
IsOxydizer    : False
IsFuel        : False
IsBalanced    : True

The displayed result shows that the computed compositions is indeed stoichiometrically balanced.
The examples shown above demonstrate the most basic use of the Gmix scripting for constructing objects of the IdealGasMixture type. The following resources contain further information on the Gmix scripting

Cmdlets updating the mixtures thermodynamic state

Displaying computational results

Exporting results to Excel

Last edited Nov 30, 2013 at 1:12 PM by AlexeyE, version 11


No comments yet.