LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
runRemoteLQNS.m
1function result = runRemoteLQNS(self, filename, options)
2% RUNREMOTELQNS Execute LQNS via remote REST API
3%
4% @brief Executes LQNS solver via a remote Docker container running lqns-rest API
5% @param filename Path to the LQNX model file (without extension)
6% @param options Solver options including remote configuration
7% @return result Response from the remote server
8%
9% This method sends the LQNX model to a remote lqns-rest server for execution.
10% The server URL is configured via options.config.remote_url.
11% Upon successful execution, the LQXO response is written to disk for parsing.
12
13% Read LQNX model file
14modelFile = [filename, '.lqnx'];
15if ~exist(modelFile, 'file')
16 % Try without extension if already includes it
17 modelFile = filename;
18end
19modelContent = fileread(modelFile);
20
21% Build API URL
22baseUrl = options.config.remote_url;
23if ~endsWith(baseUrl, '/')
24 baseUrl = [baseUrl, '/'];
25end
26
27% Determine endpoint based on method
28switch options.method
29 case {'sim', 'lqsim'}
30 url = [baseUrl, 'api/v1/solve/lqsim'];
31 otherwise
32 url = [baseUrl, 'api/v1/solve/lqns'];
33end
34
35% Build request structure
36request = struct();
37request.model = struct();
38request.model.content = modelContent;
39request.model.base64 = false;
40
41% Add options
42request.options = struct();
43request.options.include_raw_output = true;
44
45% Add pragmas based on method
46switch options.method
47 case {'sim', 'lqsim'}
48 request.options.blocks = 30;
49 if options.samples > 0
50 request.options.run_time = options.samples;
51 end
52 otherwise
53 request.options.pragmas = struct();
54
55 % Multiserver pragma
56 switch options.config.multiserver
57 case 'default'
58 request.options.pragmas.multiserver = 'rolia';
59 otherwise
60 request.options.pragmas.multiserver = options.config.multiserver;
61 end
62
63 % Method-specific pragmas
64 switch options.method
65 case 'srvn'
66 request.options.pragmas.layering = 'srvn';
67 case 'exactmva'
68 request.options.pragmas.mva = 'exact';
69 case 'srvn.exactmva'
70 request.options.pragmas.layering = 'srvn';
71 request.options.pragmas.mva = 'exact';
72 end
73
74 request.options.pragmas.stop_on_message_loss = false;
75end
76
77% Set HTTP options
78webOpts = weboptions(...
79 'MediaType', 'application/json', ...
80 'Timeout', 300, ...
81 'ContentType', 'json');
82
83% Make HTTP request
84try
85 response = webwrite(url, request, webOpts);
86catch ME
87 line_error(mfilename, 'Remote LQNS execution failed: %s', ME.message);
88 result = struct('status', 'error', 'error', ME.message);
89 return;
90end
91
92% Check response status
93if isfield(response, 'status')
94 if strcmp(response.status, 'error') || strcmp(response.status, 'failed')
95 errorMsg = '';
96 if isfield(response, 'error')
97 errorMsg = response.error;
98 end
99 line_error(mfilename, 'Remote solver returned error: %s', errorMsg);
100 result = response;
101 return;
102 end
103end
104
105% Write LQXO response to file for parsing by existing code
106if isfield(response, 'raw_output') && isstruct(response.raw_output)
107 if isfield(response.raw_output, 'lqxo') && ~isempty(response.raw_output.lqxo)
108 lqxoFile = [filename, '.lqxo'];
109 fid = fopen(lqxoFile, 'w');
110 if fid ~= -1
111 fprintf(fid, '%s', response.raw_output.lqxo);
112 fclose(fid);
113 else
114 line_warning(mfilename, 'Could not write LQXO file: %s', lqxoFile);
115 end
116 end
117end
118
119result = response;
120end