Technical Blog | All posts with tag: any tag , RPA, RPA-4, RPA-C, Release Notes, cycle analysis, news, scripting, thermodynamics

Using server mode of Scripting Utility with CURL, Scilab, and Octave

25-Oct-2021, 10:00 GMT | Tags: RPA-C, scripting

Since RPA-C v.2.0.0, the scripting utility supports a server mode which can be used to integrate the RPA-C with third-party tools via HTTP interface.


Table of contents:

Examples are available for download from RPA-C site:

Getting started

To start scripting utility in the server mode, execute one the following commands:

RPA-C-Script.exe --server

to listen on default server port 8080, or

RPA-C-Script.exe --server 80

to listen on specified server port (80 in this example).

Now you may connect to the server using Internet browser or any other tool that supports HTTP protocol, for example, CURL or custom tools.

To get the help screen with provided HTTP API, execute the command /about using any internet browser:

RPA-C-Script Server v.2.0.0
Web API
  /evaluate      POST
  /reset         POST
  /log/info      GET
  /log/warn      GET
  /log/error     GET
  /about         GET

Example of executing the example script “script-examples/mixture.js” file with CURL:

curl -i --request POST \
	--header "Content-Type: text/plain" \
	--header "Accept: text/plain" \
	-F "file=@script-examples/mixture.js" \
	http://localhost:8080/evaluate

Response:

HTTP/1.1 200 OK 
Content-Type: text/plain 
Content-Length: 967 

Initial mixture (built-in printout) 
********************************** 

Initial mixture (script printout) 
********************************** 
------------------------------------------------------------- 
 Propellant Mixture           Mass            Mole 
                         fractions       fractions 
------------------------------------------------------------- 
        NH4CLO4(cr)      0.7000000       0.4452445  
             AL(cr)      0.2000000       0.5539381  
      HTPB+Curative      0.1000000       0.0008174  
------------------------------------------------------------- 
             Total:      1.0000000       1.0000000 
------------------------------------------------------------- 
Exploded chemical formula: 
   based on 1 mole:     (N)0.449331 (H)2.580360 (CL)0.445245 (O)1.791604 (AL)0.553938 (C)0.536191  
    based on 100 g:     (N)0.006013 (H)0.034529 (CL)0.005958 (O)0.023974 (AL)0.007413 (C)0.007175  

Equivalence ratio: 0.630733 (omitted: CU) 
Equivalence ratio: 0.630733 (omitted: )

Example of executing the JavaScript code (2+5) file with CURL:

curl -i --request POST \
	--header "Content-Type: text/plain" \
	--header "Accept: text/plain" \
	--data '2+5' \
	http://localhost:8080/evaluate

Response (7=2+5):

HTTP/1.1 200 OK 
Content-Type: text/plain 
Content-Length: 1 

7

In consecutive calls you may use any object (both standard Java and RPA-C specific), created in one of the previous calls.

For example:

curl -i --request POST \
	--header "Content-Type: text/plain" \
	--header "Accept: text/plain" \
	-F "file=@script-examples/mixture.js" \
	http://localhost:8080/evaluate

curl -i --request POST \
	--header "Content-Type: text/plain" \
	--header "Accept: text/plain" \
	--data 'omit = ["CU"]' \
	http://localhost:8080/evaluate

curl -i --request POST \
	--header "Content-Type: text/plain" \
	--header "Accept: text/plain" \
	--data 'mix.getEquivalenceRatio(omit)' \
	http://localhost:8080/evaluate

Response, returning the equivalence ratio “0.6307330246120119” for the mixture object created after the first request:

HTTP/1.1 200 OK 
Content-Type: text/plain 
Content-Length: 18 

0.6307330246120119

To reset the current context removing all created JavaScript objects, use the command “/reset”:

curl -i --request POST \
	--header "Content-Type: text/plain" \
	--header "Accept: text/plain" \
	http://localhost:8080/reset

Example of using RPA-C scripting server from Scilab scripts

Scilab is a free and open source software for engineers & scientists, with a long history (first release in 1994) and a growing community.

Scilab provides the API to communicate with external services via HTTP interface.

To make the access to RPA-C server easier, helper custom Octave functions can be used:

rpa_url = "http://localhost:8080";

// Load script as a text from specified path
function [script]=rpa_load_script(file_name)
    details = fileinfo(file_name);
    len = details(1);
    fd = mopen(file_name, 'rt');
    script = mgetstr(len, fd);  
    mclose(fd);
endfunction

// Reset the server context
function []=rpa_reset()
    [res,code] = http_post(rpa_url+"/reset");
endfunction

// Execute the script passed as a text
// Returned values:
//    res - result; depends on the script
//    code - HTTP status code; has to be 200 on success
function [res,code]=rpa_execute(script)
    [res,code] = http_post(rpa_url+"/evaluate", script);
endfunction

Then the call using RPA-S scripting server can be performed from Octave as follows:

// Reset the current server context.
// All previously created objects will be removed.
rpa_reset();

// Load the script from file C:/SD/test.js
script = rpa_load_script("C:/SD/test.js");

// Execute the loaded script
[res,code] = rpa_execute(script);

// Get the values "Combustion pressure [MPA]" and "Combustion temperature [K]"
// from the object "e" created in server context (see the code of example script "test.js" below)
[res,code] = rpa_execute("printf(''[%f, %f]'', e.getP(''MPa''), e.getT(''K''))");

// Note the format of parameters: [%f, %f]
// This will create a standard pair of Scilab values which can be immediately referenced as
// res(1) and res(2) and used in any calculations in Scilab.
// In this way you may also print out the vectors or matrices.

// Execute any JS code within the current context.
// It may use or may not use any objects created in the context.
// It may also create new objects within the context.
[res,code] = rpa_execute("(1+2) * 5");

// The script above returns a single value which can be referenced as res and used in any calculations in Scilab

// Get the oxygen balance of the object "mix" stored in the current context (see the code of the test script below)
[res,code] = rpa_execute("mix.getOxygenBalance()");

// The script above returns a single value which can be referenced as res and used in any calculations in Scilab

In all examples the functions returns the pair [res, code] where "res" is a result (depends on the executed code) and "code" is a standard HTTP status code. On success the code has to be equal to 200, so you may verify it in your own scripts.

The code of the example script

C:\SD\test.js

used in examples above:

/* Prepare the mixture of ingredients */
mix = new Mixture();
mix.addSpecies("H2O2(L)", 4.0, "g/cm^3", 0.02);
mix.addSpecies("CuO(cr)", 6.315, "g/cm^3", 0.62);
mix.addSpecies("Zr(a)", 6.52, "g/cm^3", 0.36);

/* Solve problem (p,H)=const using object Combustor */
c = new Combustor(mix, true, true);
c.setP(20.7, "MPa");
c.solve(true, false);
e = c.getEquilibrium();
d = c.getDerivatives();
 
// You may print the results, so that script execution will return them.
// Otherwise it returns the result of the last statement (object "d" in this case)
printf("%f %f", 1, 1/3);

Note that the server context keeps all created JavaScript objects. On the one hand, this is very helpful as you may use such objects in subsequent calls. On the other hand, the previously stored objects may affect the execution results of the next JavaScript script. To avoid this, call the reset function (e.g. using helper function "rpa_reset()") where appropriate.

Examples for Scilab are available for download from RPA-C site: server-mode-scilab.zip.

Example of using RPA-C scripting server from Octave scripts

GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with Matlab. It may also be used as a batch-oriented language.

Octave provides the API to communicate with external services via HTTP interface.

This API can be used to create additional helper custom Octave functions to make the execution of RPA-C scripts easier, for example:

rpa_url = "http://localhost:8080";
global evaluate_url = [rpa_url "/evaluate"];
global reset_url = [rpa_url "/reset"];

function result = rpa_execute(script)
    global evaluate_url;
    result = urlread(evaluate_url, "post", {"script", script});
endfunction

function rpa_reset()
    global reset_url;
    urlread(reset_url, "post", "");
endfunction

function script = rpa_load_script(fname)
    script = fileread(fname);
endfunction

Such helper functions can then be used to call the RPA-C scripts:

rpa_reset();

s = rpa_load_script("script-examples/mixture.js");
rpa_execute(s);

rpa_execute("omit = ['CU']");
er = rpa_execute("mix.getEquivalenceRatio(omit)");

In addition to helper Octave functions, the helper RPA-C JavaScript functions can be used to assist in the preparing the combustion problem for the analysis from the Octave scripts.

For example, here is the JavaScript file “helper.js” with helper JavaScript functions for preparing the mixture and solve the combustion problem:

function prepareMixture() {
    var mix = new Mixture();
    for (var i=0; i<arguments.length; i++) {
        var arg = arguments[i];
        mix.addSpecies(arg[0], arg[1]);
    }
    return mix;
}

function solve(mix, p, p_units) {
    var c = new Combustor(mix, true, true);
    c.setP(p, p_units);
    c.solve(true, false);
    return {"e":c.getEquilibrium(), "d":c.getDerivatives()};
}

Such JavaScript helper functions can be used then together with Octave helper functions:

rpa_reset();

s2=rpa_load_script("helper.js");
rpa_execute(s2);

# Prepare mixture ans store it in server context as variable "mix"
rpa_execute("mix = prepareMixture(['NH4CLO4(cr)', 0.7], ['AL(cr)', 0.2], ['HTPB+Curative', 0.1])");

p_array = [1, 2, 3, 4, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25, 27.5, 30]; 
T_array = []; # Vector to collect calculated temperatures

for p = p_array
    # Result "res" is stored in the server context
    # and can be reused to obtain calculated values
    rpa_execute(sprintf("res = solve(mix, %f, 'MPa')", p));
    # Obtain required value from stored result
    T_array(end+1) = str2num(rpa_execute("res.e.getT('K')"));
end

plot(p_array, T_array);
xlabel ("p, MPa");
ylabel ("T, K");

Note that the server context keeps all created JavaScript objects. On the one hand, this is very helpful as you may use such objects in subsequent calls. On the other hand, the previously stored objects may affect the execution results of the next JavaScript script. To avoid this, call the reset function (e.g. using helper function "rpa_reset()") where appropriate.

Examples for Octave are available for download from RPA-C site: server-mode-octave.zip.

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