| 1 | function solve(md, solutionstring) { //{{{
|
|---|
| 2 | /**
|
|---|
| 3 | * SOLVE - apply solution sequence for this model
|
|---|
| 4 | *
|
|---|
| 5 | * Usage:
|
|---|
| 6 | * solve(md, solutionstring, varargin);
|
|---|
| 7 | * where varargin is a list of paired arguments of string OR enums
|
|---|
| 8 | *
|
|---|
| 9 | * solution types available comprise:
|
|---|
| 10 | * - 'Stressbalance' or 'sb'
|
|---|
| 11 | * - 'Masstransport' or 'mt'
|
|---|
| 12 | * - 'Thermal' or 'th'
|
|---|
| 13 | * - 'Steadystate' or 'ss'
|
|---|
| 14 | * - 'Transient' or 'tr'
|
|---|
| 15 | * - 'Balancethickness' or 'mc'
|
|---|
| 16 | * - 'Balancevelocity' or 'bv'
|
|---|
| 17 | * - 'BedSlope' or 'bsl'
|
|---|
| 18 | * - 'SurfaceSlope' or 'ssl'
|
|---|
| 19 | * - 'Hydrology' or 'hy'
|
|---|
| 20 | * - 'DamageEvolution' or 'da'
|
|---|
| 21 | * - 'Gia' or 'gia'
|
|---|
| 22 | * - 'Sealevelrise' or 'slr'
|
|---|
| 23 | *
|
|---|
| 24 | * extra options:
|
|---|
| 25 | * - loadonly : do not solve, only load results
|
|---|
| 26 | * - runtimename : true or false (default is true), makes name unique
|
|---|
| 27 | * - checkconsistency : 'yes' or 'no' (default is 'yes'), ensures checks on consistency of model
|
|---|
| 28 | * - restart : directory name (relative to the execution directory) where the restart file is located
|
|---|
| 29 | * - successCallback : callback function to be called on success
|
|---|
| 30 | * - errorCallback : callback function to be called on error
|
|---|
| 31 | *
|
|---|
| 32 | * reporting:
|
|---|
| 33 | * With no optional arguments for reporting, progress and error reporting is written to the DOM element with ID 'solve-button'.
|
|---|
| 34 | * - solveButtonId : overrides default solve button ID
|
|---|
| 35 | * - callout : Callout to report progress/errors to; overrides reporting to solve button
|
|---|
| 36 | * - withProgressBar : reports progress of certain solution stages with a progress bar; will not display if a Callout has not been provided
|
|---|
| 37 | *
|
|---|
| 38 | * Examples:
|
|---|
| 39 | * md = solve(md, 'Stressbalance');
|
|---|
| 40 | * md = solve(md, 'sb');
|
|---|
| 41 | */
|
|---|
| 42 | if (typeof solutionstring !== 'string') {
|
|---|
| 43 | throw Error(sprintf("%s\n", "ISSM's solve function only accepts strings for solution sequences. Type help solve to get a list of supported solutions."));
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | //recover and process solve options
|
|---|
| 47 | if ((solutionstring.toLowerCase() === 'sb') || (solutionstring.toLowerCase() === 'stressbalance')) {
|
|---|
| 48 | solutionstring = 'StressbalanceSolution';
|
|---|
| 49 | } else if ((solutionstring.toLowerCase() === 'mt') || (solutionstring.toLowerCase() === 'masstransport')) {
|
|---|
| 50 | solutionstring = 'MasstransportSolution';
|
|---|
| 51 | } else if ((solutionstring.toLowerCase() === 'th') || (solutionstring.toLowerCase() === 'thermal')) {
|
|---|
| 52 | solutionstring = 'ThermalSolution';
|
|---|
| 53 | } else if ((solutionstring.toLowerCase() === 'st') || (solutionstring.toLowerCase() === 'steadystate')) {
|
|---|
| 54 | solutionstring = 'SteadystateSolution';
|
|---|
| 55 | } else if ((solutionstring.toLowerCase() === 'tr') || (solutionstring.toLowerCase() === 'transient')) {
|
|---|
| 56 | solutionstring = 'TransientSolution';
|
|---|
| 57 | } else if ((solutionstring.toLowerCase() === 'mc') || (solutionstring.toLowerCase() === 'balancethickness')) {
|
|---|
| 58 | solutionstring = 'BalancethicknessSolution';
|
|---|
| 59 | } else if ((solutionstring.toLowerCase() === 'bv') || (solutionstring.toLowerCase() === 'balancevelocity')) {
|
|---|
| 60 | solutionstring = 'BalancevelocitySolution';
|
|---|
| 61 | } else if ((solutionstring.toLowerCase() === 'bsl') || (solutionstring.toLowerCase() === 'bedslope')) {
|
|---|
| 62 | solutionstring = 'BedSlopeSolution';
|
|---|
| 63 | } else if ((solutionstring.toLowerCase() === 'ssl') || (solutionstring.toLowerCase() === 'surfaceslope')) {
|
|---|
| 64 | solutionstring = 'SurfaceSlopeSolution';
|
|---|
| 65 | } else if ((solutionstring.toLowerCase() === 'hy') || (solutionstring.toLowerCase() === 'hydrology')) {
|
|---|
| 66 | solutionstring = 'HydrologySolution';
|
|---|
| 67 | } else if ((solutionstring.toLowerCase() === 'da') || (solutionstring.toLowerCase() === 'damageevolution')) {
|
|---|
| 68 | solutionstring = 'DamageEvolutionSolution';
|
|---|
| 69 | } else if ((solutionstring.toLowerCase() === 'gia') || (solutionstring.toLowerCase() === 'gia')) {
|
|---|
| 70 | solutionstring = 'GiaSolution';
|
|---|
| 71 | } else if ((solutionstring.toLowerCase() === 'slr') || (solutionstring.toLowerCase() === 'sealevelrise')) {
|
|---|
| 72 | solutionstring = 'SealevelriseSolution';
|
|---|
| 73 | } else {
|
|---|
| 74 | throw Error(sprintf("%s%s%s\n",'solutionstring ',solutionstring,' not supported!'));
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | //Process options
|
|---|
| 78 | var args = Array.prototype.slice.call(arguments);
|
|---|
| 79 | var options = new pairoptions(args.slice(2,args.length));
|
|---|
| 80 | options.addfield('solutionstring',solutionstring);
|
|---|
| 81 |
|
|---|
| 82 | //recover some fields
|
|---|
| 83 | md.priv.solution=solutionstring;
|
|---|
| 84 | cluster=md.cluster;
|
|---|
| 85 |
|
|---|
| 86 | //check model consistency
|
|---|
| 87 | if (options.getfieldvalue('checkconsistency','yes') == 'yes'){
|
|---|
| 88 | if (md.verbose.solution){
|
|---|
| 89 | console.log('checking model consistency');
|
|---|
| 90 | }
|
|---|
| 91 | ismodelselfconsistent(md);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | //If we are restarting, actually use the provided runtime name:
|
|---|
| 95 | restart=options.getfieldvalue('restart','');
|
|---|
| 96 |
|
|---|
| 97 | //First, build a runtime name that is unique
|
|---|
| 98 | if (restart==1 ){
|
|---|
| 99 | //Leave the runtimename as is
|
|---|
| 100 | }
|
|---|
| 101 | else{
|
|---|
| 102 | if (!(restart == '')){
|
|---|
| 103 | md.priv.runtimename=restart;
|
|---|
| 104 | }
|
|---|
| 105 | else if (options.getfieldvalue('runtimename',true)){
|
|---|
| 106 | c=new Date().getTime();
|
|---|
| 107 | md.priv.runtimename=sprintf('%s-%g',md.miscellaneous.name,c);
|
|---|
| 108 | }
|
|---|
| 109 | else{
|
|---|
| 110 | md.priv.runtimename=md.miscellaneous.name;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | //if running qmu analysis, some preprocessing of dakota files using models
|
|---|
| 115 | //fields needs to be carried out.
|
|---|
| 116 | if (md.qmu.isdakota){
|
|---|
| 117 | throw Error("solve error message: qmu runs not supported yet!");
|
|---|
| 118 | //md.preqmu(options);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | //Do we load results only?
|
|---|
| 123 | if (options.getfieldvalue('loadonly',false)){
|
|---|
| 124 | loadresultsfromcluster(md);
|
|---|
| 125 | return;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | //Marshall into a binary array (fid) all the fields of model.
|
|---|
| 129 | var fid = marshall(md); // bin file
|
|---|
| 130 |
|
|---|
| 131 | //deal with toolkits options:
|
|---|
| 132 | toolkitsstring= md.toolkits.ToolkitsFile(md.miscellaneous.name + '.toolkits'); // toolkits file
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | /*
|
|---|
| 136 | Set success callback function
|
|---|
| 137 | */
|
|---|
| 138 | //{{{
|
|---|
| 139 |
|
|---|
| 140 | // Default: do nothing if no success callback function requested
|
|---|
| 141 | function successCallbackDefault() {
|
|---|
| 142 | solving = false;
|
|---|
| 143 | };
|
|---|
| 144 |
|
|---|
| 145 | let successCallback = options.getfieldvalue('successCallback', successCallbackDefault);
|
|---|
| 146 | //}}}
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | /*
|
|---|
| 150 | Set error callback function
|
|---|
| 151 | */
|
|---|
| 152 | //{{{
|
|---|
| 153 |
|
|---|
| 154 | // Default: do nothing if no error callback function requested
|
|---|
| 155 | function errorCallbackDefault() {
|
|---|
| 156 | solving = false;
|
|---|
| 157 | };
|
|---|
| 158 |
|
|---|
| 159 | let errorCallback = options.getfieldvalue('errorCallback', errorCallbackDefault);
|
|---|
| 160 | //}}}
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | /*
|
|---|
| 164 | Set solve button ID
|
|---|
| 165 | */
|
|---|
| 166 | //{{{
|
|---|
| 167 |
|
|---|
| 168 | // Default: update #solve-button element with progress updates
|
|---|
| 169 | let solveButtonId = options.getfieldvalue('solveButtonId', '#solve-button');
|
|---|
| 170 | //}}}
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 | /*
|
|---|
| 174 | Set Callout
|
|---|
| 175 | */
|
|---|
| 176 | //{{{
|
|---|
| 177 | var callout = {};
|
|---|
| 178 |
|
|---|
| 179 | // Default: Callout is an empty object
|
|---|
| 180 | callout = options.getfieldvalue('callout', {});
|
|---|
| 181 | //}}}
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 | /*
|
|---|
| 185 | Set progress bar display boolean
|
|---|
| 186 | */
|
|---|
| 187 | //{{{
|
|---|
| 188 | // Default: no progress bar; NOTE: must have supplied a callout for progress bar to display
|
|---|
| 189 | let withProgressBar = options.getfieldvalue('withProgressBar', false);
|
|---|
| 190 | //}}}
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 | if (cluster.classname() == 'local'){ //{{{
|
|---|
| 194 |
|
|---|
| 195 | /*We are running locally on the machine, using the issm module:*/
|
|---|
| 196 | console.log('running issm locally');
|
|---|
| 197 |
|
|---|
| 198 | //Call issm:
|
|---|
| 199 | var outputs = issm(fid, toolkitsstring, solutionstring, md.miscellaneous.name);
|
|---|
| 200 |
|
|---|
| 201 | //Recover output arguments:
|
|---|
| 202 | var outputbuffer = outputs[0]; var outputbuffersize = outputs[1];
|
|---|
| 203 |
|
|---|
| 204 | //Load results:
|
|---|
| 205 | md = loadresultsfrombuffer(md, outputbuffer, outputbuffersize);
|
|---|
| 206 |
|
|---|
| 207 | // Call success callback
|
|---|
| 208 | successCallback();
|
|---|
| 209 |
|
|---|
| 210 | return md;
|
|---|
| 211 | //}}}
|
|---|
| 212 | } else { //{{{
|
|---|
| 213 | // We are running somewhere else on a computational server. Send the buffer to that server and retrieve output.
|
|---|
| 214 | console.log('running issm remotely');
|
|---|
| 215 |
|
|---|
| 216 | cluster.UploadAndRun(
|
|---|
| 217 | md,
|
|---|
| 218 | fid,
|
|---|
| 219 | toolkitsstring,
|
|---|
| 220 | solutionstring,
|
|---|
| 221 | md.miscellaneous.name,
|
|---|
| 222 | md.priv.runtimename,
|
|---|
| 223 | successCallback,
|
|---|
| 224 | errorCallback,
|
|---|
| 225 | solveButtonId,
|
|---|
| 226 | callout,
|
|---|
| 227 | withProgressBar
|
|---|
| 228 | );
|
|---|
| 229 |
|
|---|
| 230 | return md;
|
|---|
| 231 | //}}}
|
|---|
| 232 | }
|
|---|
| 233 | //}}}
|
|---|
| 234 | }
|
|---|