RPA-C Scripting Utility

RPA-C Scripting Utility is available as a part of RPA-C and implements object-oriented binding from JavaScript (ECMAScript) to many internal objects and functions of RPA-C.

Key features of RPA-C Scripting Utility

For more information see also

Download

Scripting Utility is an integral part of RPA-C and available since v.1.0.

Examples

/***************************************************
 RPA-C - tool for the thermodynamic analysis.
  
 This script loads existing configuration file,
 solves the configured problem and prints out the results.
****************************************************/
// Load configuration file
c = new ConfigFile("examples/HMX.cfg");
c.read();
// Create and run combustion analysis
ca = new CombustionAnalysis();
ca.run(c);
if (ca.getCombustorsListSize()>0) {
    printf("Initial mixture:\n");
    ca.getMixture().print();
    tUnit = "K";
    printf("T   = %10.5f %s\n", ca.getCombustor(0).getEquilibrium().getT(tUnit), tUnit);
    hexUnit = "kJ/kg";
    printf("HEX = %10.5f %s\n", ca.getHEX(hexUnit), hexUnit);
} else {
    printf("Could not solve!\n");
}

/***************************************************
 RPA-C - tool for the thermodynamic analysis.
  
 This script demonstrates how to prepare the mixture of 
 ingredients and run the combustion problem using object Combustor.
****************************************************/
// Prepare the mixture of ingredients
mix = new Mixture();
mix.addSpecies("NH4CLO4(cr)", 0.7); 
mix.addSpecies("AL(cr)", 0.2); 
mix.addSpecies("HTPB+Curative", 0.9, "g/cm^3", 0.1); 
printf("Initial mixture:");
mix.print();
// Solve problem (p,H)=const using object Combustor
printf("Solve problem (p,H)=const");
c1 = new Combustor(mix, true, true);
c1.setP(20.7, "MPa"); 
c1.solve(true, false);
c1.getEquilibrium().print("SI");
c1.getDerivatives().print("SI");
// Solve problem (p,T)=const using object Combustor
p = 20.7;
T = 1400;
printf("Solve problem (p,T)=const at p=%4.1f MPa T=%8.3f K", p, T);
c2 = new Combustor(mix, true, true);
c2.setPT(p, "MPa", T, "K"); 
c2.solve(true, false);
c2.getEquilibrium().print("SI");
c2.getDerivatives().print("SI");
// Solve problem (p,H)=const using object Combustor
printf("Solve problem (p,H)=const at p=%4.1f MPa", p);
c3 = new Combustor(mix, true, true);
c3.setP(p, "MPa"); 
c3.solve(true, false);
c3.getEquilibrium().print("SI");
c3.getDerivatives().print("SI");

/***************************************************
 RPA-C - tool for the thermodynamic analysis.
  
 This script demonstrates how to prepare the mixture of 
 ingredients and run nested combustion problems using 
 object Combustor.
****************************************************/
// Prepare the mixture of ingredients
mix = new Mixture();
mix.addSpecies("NH4CLO4(cr)", 0.7); 
mix.addSpecies("AL(cr)", 0.2); 
mix.addSpecies("HTPB+Curative", 0.9, "g/cm^3", 0.1); 
printf("Initial mixture:\n");
mix.print();
// Nested analysis
// Array of pressure values in MPa
p = [10, 15, 20];
// Array of temperature values in K
// "-1" means the temperature won't be assigned 
// (see the code below)
T = [-1, 1000, 1400, 1800]; 
for (var i=0; i<p.length; i++) {
    printf("Pressure p=%10.5f MPa\n", p[i]);
    for (var j=0; j<T.length; j++) {
        c = new Combustor(mix, true, true);
        if (T[j]>0) {
            printf("Temperature T=%10.5f K\n", T[j]);
            c.setPT(p[i], "MPa", T[j], "K"); 
        } else {
            c.setP(p[i], "MPa"); 
        }
        c.solve(true, false);
        c.getEquilibrium().print("SI");
        c.getDerivatives().print("SI");
        printf("\n*************************************************************\n");
    }
}

/***************************************************
 RPA-C - tool for the thermodynamic analysis.
  
 This script demonstrates how to write results in required
 format into the file "log.txt".
****************************************************/
// Open the  file "log.txt" in the mode "w" ("write")
var f = new File("log.txt", "w"); 
// Define variable with the name of configuration file
configName = "examples/HMX.cfg";
// Open configuration file
c = new ConfigFile(configName);
c.read();
f.printf("# Configuration file: %s\n\n", configName);
// Prepare and run combustion analysis
ca = new CombustionAnalysis();
ca.run(c);
if (ca.getCombustorsListSize()>0) {
    combustor = ca.getCombustor(0);
    r = combustor.getEquilibrium();
    products = r.getResultingMixture();
    unit = "MPa";
    f.printf("p   = %10.5f %s\n", combustor.getEquilibrium().getP(unit), unit);
    unit = "K";
    f.printf("T   = %10.5f %s\n", combustor.getEquilibrium().getT(unit), unit);
    unit = "kJ/kg";
    f.printf("HEX = %10.5f %s\n", ca.getHEX(unit), unit);
   
    f.printf("\n# %13s %9s %9s %4s\n", "Name", "Mass Frac", "Mole Frac", "Cond");
    sum1 = 0;
    sum2 = 0; 
    for (i=0; i<products.size(); ++i) {
        // Reaction product
        s = products.getSpecies(i);
        massFraction = products.getFraction(i, "mass"); 
        moleFraction = products.getFraction(i, "mole");; 
        sum1 += massFraction;
        sum2 += moleFraction;
        // We are printing out mass fraction in  format "%9.7f",
        // so skip all products with massFraction<1e-7
        if (massFraction<1e-7) {
            continue;
        }
        f.printf("%15s %9.7f %9.7f %4d\n",
            s.getName(),
            massFraction,
            moleFraction,
            s.getCondensed()
        );
    }
    f.printf("%15s %9.7f %9.7f\n",
        "Summ:",
        sum1,
        sum2
    );
    
}
// Close the file
f.close();

More examples can be found in RPA-C distribution package.

RPA-C Scilab Plugin

Scilab Plugin is available as a part of RPA-C v.2 that enables users to use many features of RPA-C from custom Scilab scripts.

Key features of RPA-C Scilab Plugin

For more information see also

Download

Scilab Plugin is an integral part of RPA-C and available since v.2.0.

Examples

exec loader.sce
RPAInit()
// clear console
clc
// clear memory
clear
// close all plots
xdel(winsid())
tic()
//*************************************
// Load configuration from the file
cfg = ConstructConfigFile();
ConfigFile_read(cfg, "scilab-examples/ScilabTest.cfg")
// Create separate copy of mixture with ingredients to get some
// propertiesof the mixture: density and equivalence ratio
// We coud get it from CombustionAnalysis, but only after the analysis run,
// which is in the loop in this example. So, to improve the performance,
// we create another copy of mixtue outside of the loop
mix = ConstructMixture();
ingredients = ConfigFile_getIngredients(cfg);
for i=0:Ingredients_getSize(ingredients)-1
    ing = Ingredients_getComponent(ingredients, i);
    s = Mixture_add(mix, Component_getName(ing), Component_getMf(ing));
end
// Get the density of the mixture in kg/m3
rho = Mixture_getRho(mix, "kg/m^3");
// Equivalence ratio
er = Mixture_getEquivalenceRatio(mix, Ingredients_getOmitAtomsER(ingredients));
//*************************************
// Run amalysis
p=logspace(-1,2,30)
for i=1:size(p,'*')
    cc = ConfigFile_getCombustionConditions(cfg);
    CombustionConditions_setP(cc, p(i), "MPa"); // Update main combustion conditions
    ca = ConstructCombustionAnalysis();
    CombustionAnalysis_setPrintResults(ca, %F);
    CombustionAnalysis_run(ca, cfg);
    if CombustionAnalysis_getCombustorsListSize(ca)>0 then
        e = CombustionAnalysis_getEquilibrium(ca, 0);
        // Pressure
        _p = Equilibrium_getP(e, "MPa");
        // Flame Temperature in K
        T = Equilibrium_getT(e, "K");
        // Gas Yield (mol/kg)
        products = Equilibrium_getResultingMixture(e);
        v = 1000 / Mixture_getM(products);	// Mole number in 100 gm of whole mixture
        v_c = 0;				// Mole number in 100 gm of condenced mixture
        for j=0:Mixture_size(products)-1
            s = Mixture_getSpecies(products, j);
            if Species_isCondensed(s) then
                v_c = v_c + (Mixture_getFraction(products, j, "mole") * v);
            end
        end
        g = v - v_c;
        // Condensed fraction
        vc = 0;
        d = CombustionAnalysis_getDerivatives(ca, 0);
        vc = Derivatives_getZ(d);
        mprintf("%2d: p=%f T=%f rho=%f er=%f g=%f vc=%f
", i, _p, T, rho, er, g, vc);
        data(:,i) = [T, rho, er, g, vc];
    end
    DeleteCombustionAnalysis(ca);
end
d=toc()
disp(d)
//*************************************
// Plot diagrams
xdel()
f=figure("background",-2,"figure_position", [0 0],"figure_size",[1800 1000]);
drawlater()
subplot(1,3,1)
plot(p,data(1,:),'bo-','thickness',2)
xgrid(33,1,8)
title('Flame Temperature',"font_style",8,"fontsize",3)
xlabel("Pressure (MPa)","font_style",8,"fontsize",2)
ylabel("Flame Temperature (K)","font_style",8,"fontsize",2)
a=gca();a.log_flags="lnn"
subplot(1,3,2)
plot(p,data(5,:),'bo-','thickness',2)
xgrid(33,1,8)
title('Gas Yield',"font_style",8,"fontsize",3)
xlabel("Pressure (MPa)","font_style",8,"fontsize",2)
ylabel("Gas yield (mol/kg)","font_style",8,"fontsize",2)
a=gca();a.log_flags="lnn"
subplot(1,3,3)
plot(p,data(5,:).*data(1,:)/1000,'bo-','thickness',2)
xgrid(33,1,8)
title('Massic Sifx',"font_style",8,"fontsize",3)
xlabel("Pressure (MPa)","font_style",8,"fontsize",2)
ylabel("Massic Sifx (mol.K/g)","font_style",8,"fontsize",2)
a=gca();a.log_flags="lnn"
drawnow()

exec loader.sce
RPAInit()
// clear console
clc
// clear memory
clear
// close all plots
xdel(winsid())
tic()
//*************************************
// Create configuration in memory
cfg = ConstructConfigFile();
ConfigFile_setName(cfg, "Test")
// We will change mass fractions for these two ingredients in the loop
// so assign them to separate variables to access later easier
c_al = ConstructComponent("AL(cr)", 0.20);
c_htpb = ConstructComponent("HTPB+Curative", 0.12);
c_total_mf = Component_getMf(c_al) + Component_getMf(c_htpb);
ingredients = ConfigFile_getIngredients(cfg);
Ingredients_addComponent(ingredients, ConstructComponent("NH4CLO4(cr)", 0.68));
Ingredients_addComponent(ingredients, c_al);
Ingredients_addComponent(ingredients, c_htpb);
gopt = ConfigFile_getGeneralOptions(cfg);
GeneralOptions_setMultiphase(gopt, %T);
GeneralOptions_setIons(gopt, %T);
cc = ConfigFile_getCombustionConditions(cfg);
CombustionConditions_setP(cc, 20.7, "MPa");               // Will be changed in the loop below
hexc = ConfigFile_getHexConditions(cfg);
HEXConditions_setType(hexc, "none");
//*************************************
// Run analysis
y1=.4
y2=.6
x1=1
x2=30
for i=x1:x2
    txg(i)=((y1-y2)/(x1-x2))*i+(-(x2*y1-x1*y2)/(x1-x2))
    txb(i)=c_total_mf-txg(i)
    // Re-assign the mass fractions
    Component_setMf(c_al, txg(i));
    Component_setMf(c_htpb, txb(i));
    ca = ConstructCombustionAnalysis();
    CombustionAnalysis_setPrintResults(ca, %F);
    CombustionAnalysis_run(ca, cfg);
    if CombustionAnalysis_getCombustorsListSize(ca)>0 then
        // Get current initial mixture (ingredients)
        mix = CombustionAnalysis_getMixture(ca);
        // Get the density of the mixture in kg/m3
        rho = Mixture_getRho(mix, "kg/m^3");
        // Equivalence ratio
        er = Mixture_getEquivalenceRatio(mix, Ingredients_getOmitAtomsER(ingredients));
        e = CombustionAnalysis_getEquilibrium(ca, 0);
        // Pressure
        p = Equilibrium_getP(e, "MPa");
        // Flame Temperature in K
        T = Equilibrium_getT(e, "K");
        // Gas Yield (mol/kg)
        products = Equilibrium_getResultingMixture(e);
        v = 1000 / Mixture_getM(products);	// Mole number in 100 gm of whole mixture
        v_c = 0;				// Mole number in 100 gm of condenced mixture
        for j=0:Mixture_size(products)-1
            s = Mixture_getSpecies(products, j);
            if Species_isCondensed(s) then
                v_c = v_c + (Mixture_getFraction(products, j, "mole") * v);
            end
        end
        g = v - v_c;
        // Condensed fraction
        vc = 0;
        d = CombustionAnalysis_getDerivatives(ca, 0);
        vc = Derivatives_getZ(d);
        mprintf("%2d: Mf=%f+%f=%f p=%f T=%f rho=%f er=%f g=%f vc=%f
", i, txg(i), txb(i), c_total_mf, p, T, rho, er, g, vc);
        data(:,i) = [T, rho, er, g, vc];
    end
    DeleteCombustionAnalysis(ca);
end
d=toc()
disp(d)
//*************************************
// Plot diagrams
xdel()
f=figure("background",-2,"figure_position", [0 0],"figure_size",[1800 1000]);
drawlater()
subplot(2,3,1)
plot(txg',data(1,:),'bo-','thickness',2)
xgrid(33,1,8)
title('Flame Temperature',"font_style",8,"fontsize",3)
xlabel("Guni rate","font_style",8,"fontsize",2)
ylabel("Flame Temperature (K)","font_style",8,"fontsize",2)
subplot(2,3,2)
plot(txg',data(4,:),'bo-','thickness',2)
xgrid(33,1,8)
title('Gas Yield',"font_style",8,"fontsize",3)
xlabel("Guni rate","font_style",8,"fontsize",2)
ylabel("Gas yield (mol/kg)","font_style",8,"fontsize",2)
subplot(2,3,3)
plot(txg',data(4,:).*data(1,:)/1000,'bo-','thickness',2)
xgrid(33,1,8)
title('Massic Sifx',"font_style",8,"fontsize",3)
xlabel("Guni rate","font_style",8,"fontsize",2)
ylabel("Massic Sifx (mol.K/g)","font_style",8,"fontsize",2)
subplot(2,3,4)
plot(txg',data(2,:),'bo-','thickness',2)
xgrid(33,1,8)
title('Density',"font_style",8,"fontsize",3)
xlabel("Guni rate","font_style",8,"fontsize",2)
ylabel("Density (g/m3)","font_style",8,"fontsize",2)
subplot(2,3,5)
plot(txg',data(3,:),'bo-','thickness',2)
xgrid(33,1,8)
title('Equivalent Ratio',"font_style",8,"fontsize",3)
xlabel("Guni rate","font_style",8,"fontsize",2)
ylabel("Equivalent ratio","font_style",8,"fontsize",2)
subplot(2,3,6)
plot(txg',data(5,:),'bo-','thickness',2)
xgrid(33,1,8)
title('Condensed phase',"font_style",8,"fontsize",3)
xlabel("Guni rate","font_style",8,"fontsize",2)
ylabel("Condensed Phase (%)","font_style",8,"fontsize",2)
drawnow()

More examples can be found in RPA-C distribution package.

RP Software+Engineering UG

53819 Neunkirchen-Seelscheid, Germany

About us  |  Impressum

Customers

Contact information

RP Software+Engineering UG

53819 Neunkirchen-Seelscheid, Germany

About us  |  Impressum

Customers

Contact information


Copyright © 2009 - 2024 RP Software+Engineering UG