[Swift-user] Dynamic For Loops from Input File

Matthew Shaxted Matthew.Shaxted at som.com
Thu Aug 21 14:18:42 CDT 2014


Ah yes Mihael this is it...  attached my finished swift script for reference. Thanks again for your help.


# ----- TYPE DEFINITIONS

  type file;
  
  type param {
    string pname;
    string pvals;
  }
  
  type pval {
    string name;
    string[] values;
  }


# ----- APP DEFINITIONS

  app (file all) runEP ( file imf, file epw, string params[] )
  {
    RunEPlocal "--imf" @imf "--epw" @epw "--outall" @all "--params" params;
  }


# ----- EXTERNAL INPUTS
  
  file epconfig  <single_file_mapper; file=@arg("epconfig",  "epinput.imf")>;
  file epweather <single_file_mapper; file=@arg("epweather", "chicago.epw")>;
  string outdir=@arg("outdir","output");
  string count=@arg("count","true");
  param  pset[] = readData(@arg("sweep","test.sweep"));


# ----- INTERNAL FUNCTIONS

  (string[] r) extend(string[] a, string v,string n, int lastIndex) {
    foreach ov, i in a {r[i] = a[i];}
    string[] f;f[0]=n;f[1]=v;
    r[lastIndex]=@strjoin(f,",");
  }
  
  iterateEP(file c, file w, string[] parameters, string d) {
    string ap=@strjoin(parameters,",");
    string[] apx=@strsplit(ap,",");
    string[] aval;
    foreach v, vn in parameters {
      string[] an=@strsplit(v,",");
      aval[vn]=an[1];
    }
    string fileid = @strcat("ep+", at strjoin(aval,"+"));
    tracef("%s\n",fileid);
    file outall  <single_file_mapper; file=@strcat(d,"/",fileid,".tgz")>;  
    (outall) = runEP (c,w,apx);
  }

  dynamicFor(pval[] vars, string[] values, int index, int len, file epc, file epw, string od) {
    if (index < len) {
      foreach v in vars[index].values {dynamicFor(vars, extend(values, v,vars[index].name, index), index + 1, len, epc, epw, od);}
    }else {iterateEP(epc,epw,values,od);}
  }

  (int sz) countAllRecursive(pval[] pval, int index) {
    if (index < 0) {sz = 1;}
    else {sz = @length(pval[index].values) * countAllRecursive(pval, index - 1);}
  }

  (int sz) countAll(pval[] pval) {
    if (@length(pval) == 0) {sz = 0;}
    else {sz = countAllRecursive(pval, @length(pval) - 1);}
  }


# ----- PARAMETER SETUP & LAUNCH
  
  string[] empty;
  pval[] pvals;

  foreach p, pn in pset {
      string val[]=@strsplit(p.pvals,",");
      pvals[pn].name=p.pname;
      foreach v, vn in val {
        pvals[pn].values[vn]=v;
      }
  }
  
  int runs=countAll(pvals);
  tracef("%i Runs in Simulation\n\n",runs);
  
  if (count=="false"){
      dynamicFor(pvals, empty, 0, @length(pvals), epconfig, epweather, outdir);
  }







-----Original Message-----
From: Mihael Hategan [mailto:hategan at mcs.anl.gov] 
Sent: Thursday, August 21, 2014 1:53 PM
To: Matthew Shaxted
Cc: swift-user at ci.uchicago.edu; Jason Kirkpatrick
Subject: Re: [Swift-user] Dynamic For Loops from Input File

Hi,

Same idea I would think:

(int sz) countAllRecursive(sometype[] pval, int index) {
  if (index < 0) {
    sz = 1;
  }
  else {
    sz = length(pval[index]) * countAllRecursive(pval, index - 1);
  }
}

(int sz) countAll(sometype[] pval) {
  if (length(pval) == 0) {
    sz = 0;
  }
  else {
    sz = countAllRecursive(pval, length(pval) - 1);
  }
}

Mihael

On Thu, 2014-08-21 at 14:33 -0400, Matthew Shaxted wrote:
> Thanks Mihael,
> 
> I'm pleased to say this dynamic for loop code works well. Below is how my iterating script turned out.
> 
> One thing I was not yet able to replicate is the counting of the total iterations in the Swift run. 
> 
> When I was previously hard coding the values, I was doing something similar to below - any chance you know of an easy to do this in the dynamic way? I was thinking I could iterate in the PARAMETER SETUP & LAUNCH section, but not quite sure how to best implement.
> 
> 	int countRuns=@length(pval[0])*@length(pval[1])*@length(pval[2])*@length(pval[3])*@length(pval[4])*@length(pval[5])*@length(pval[6])*@length(pval[7])*@length(pval[8]);
> 	tracef("%i Runs in Simulation\n\n",countRuns);
> 
> Thanks,
> Matthew
> 
> 
> 
> # -----  ENERGYPLUS SWEEP SWIFT SCRIPT ----- #
> 
> # ----- TYPE DEFINITIONS
> 
>   type file;
>   
>   type param {
>     string pname;
>     string pvals;
>   }
>   
>   type pval {
>     string name;
>     string[] values;
>   }
> 
> 
> # ----- APP DEFINITIONS
> 
>   app (file all) runEP ( file imf, file epw, string params[] )
>   {
>     RunEPlocal "--imf" @imf "--epw" @epw "--outall" @all "--params" params;
>   }
> 
> 
> # ----- EXTERNAL INPUTS
>   
>   file epconfig  <single_file_mapper; file=@arg("epconfig",  "epinput.imf")>;
>   file epweather <single_file_mapper; file=@arg("epweather", "chicago.epw")>;
>   string outdir=@arg("outdir","output");
>   string count=@arg("count","false");
>   param  pset[] = readData(@arg("sweep","test.sweep"));
> 
> 
> # ----- INTERNAL FUNCTIONS
> 
>   (string[] r) extend(string[] a, string v,string n, int lastIndex) {
>     foreach ov, i in a {r[i] = a[i];}
>     string[] f;f[0]=n;f[1]=v;
>     r[lastIndex]=@strjoin(f,",");
>   }
>   
>   iterateEP(file c, file w, string[] parameters, string d) {
>     string ap=@strjoin(parameters,",");
>     string[] apx=@strsplit(ap,",");
>     string[] aval;
>     foreach v, vn in parameters {
>       string[] an=@strsplit(v,",");
>       aval[vn]=an[1];
>     }
>     string fileid = @strcat("ep+", at strjoin(aval,"+"));
>     tracef("%s\n",fileid);
>     file outall  <single_file_mapper; file=@strcat(d,"/",fileid,".tgz")>;  
>     (outall) = runEP (c,w,apx);
>   }
> 
>   dynamicFor(pval[] vars, string[] values, int index, int len, file epc, file epw, string od) {
>     if (index < len) {
>       foreach v in vars[index].values {dynamicFor(vars, extend(values, v,vars[index].name, index), index + 1, len, epc, epw, od);}
>     }else {iterateEP(epc,epw,values,od);}
>   }
> 
> 
> # ----- PARAMETER SETUP & LAUNCH
>   
>   string[] empty;
>   pval[] pvals;
>   
>   foreach p, pn in pset {
>       string val[]=@strsplit(p.pvals,",");
>       pvals[pn].name=p.pname;
>       foreach v, vn in val {
>         pvals[pn].values[vn]=v;
>       }
>   }
>   
>   dynamicFor(pvals, empty, 0, @length(pvals), epconfig, epweather, outdir);
> 
> 
> 
> 
> 
> 
> -----Original Message-----
> From: Matthew Shaxted 
> Sent: Wednesday, August 20, 2014 5:51 PM
> To: 'Mihael Hategan'
> Cc: swift-user at ci.uchicago.edu; Jason Kirkpatrick
> Subject: RE: [Swift-user] Dynamic For Loops from Input File
> 
> Hi Mihael,
> 
> This looks like it may work well - many thanks for the quick response.
> 
> I'll see if I can integrate this into my existing sweep scripts now.
> 
> Thanks again,
> Matthew
> 
> 
> MATTHEW SHAXTED
> SKIDMORE, OWINGS & MERRILL LLP
> 224 SOUTH MICHIGAN AVENUE
> CHICAGO, IL 60604
> T  (312) 360-4368
> MATTHEW.SHAXTED at SOM.COM
> 
> 
> The information contained in this communication may be confidential, is intended only for the use of the recipient(s) named above, and may be legally privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication, or any of its contents, is strictly prohibited and may be unlawful. If you have received this communication in error, please return it to the sender immediately and delete the original message and any copy of it from your computer system. If you have any questions concerning this message, please contact the sender.
> 
> 
> 
> -----Original Message-----
> From: Mihael Hategan [mailto:hategan at mcs.anl.gov] 
> Sent: Wednesday, August 20, 2014 5:41 PM
> To: Matthew Shaxted
> Cc: swift-user at ci.uchicago.edu; Jason Kirkpatrick
> Subject: Re: [Swift-user] Dynamic For Loops from Input File
> 
> Hi Matthew,
> 
> I'm thinking something like this:
> 
> type var {
>   string name;
>   float[] values;
> }
> 
> (float[] r) extend(float[] a, float v, int lastIndex) {
>   foreach ov, i in a {
>     r[i] = a[i];
>   }
>   r[lastIndex] = v;
> }
> 
> doSomething(float[] a) {
>   trace(a);
> }
> 
> dynamicFor(var[] vars, float[] values, int index, int len) {
>   if (index < len) {
>     tracef("Iterating over %s\n", vars[index].name);
>     foreach v in vars[index].values {
>       dynamicFor(vars, extend(values, v, index), index + 1, len);
>     }
>   }
>   else {
>     doSomething(values);
>   }
> }
> 
> var[] vars;
> vars[0].name = "a";
> vars[0].values = [0.1, 0.2, 0.3];
> vars[1].name = "b";
> vars[1].values = [0.4, 0.5];
> 
> float[] empty;
> 
> dynamicFor(vars, empty, 0, 2);
> 
> Mihael
> 
> 
> On Wed, 2014-08-20 at 17:59 -0400, Matthew Shaxted wrote:
> > Hi Swift Users,
> > 
> > I have a swift script that reads an input file of parameters and variables, and iterates through the variables in one for loop per parameter:
> > 
> > Input Sweep File Example:
> > pnum    pname                                  pvals
> > 1              InfilRate                               0.0015,0.025
> > 2              PreheatCoilSetpoint       15,15.5,16.0
> > 3              KitchenGasPD                   200,250
> > 
> > Swift File For Loops:
> > foreach v0 in pval[0] {
> > foreach v1 in pval[1] {
> > foreach v2 in pval[2] {
> > 
> > As of now, I need to hard code in the number of for loops in the Swift script to match those of the input file parameters, and I'm thinking there must be a better way.
> > 
> > I'm wondering if there is a more dynamic way to have Swift loop through the parameters without having to hard code in the for loops? For example, if I define 5 parameters in the input file, the Swift script would automatically know to add 5 for loops.
> > 
> > I was thinking I could create a shell script to read the input file and dynamically create the swift script with the correct number of for loops before running swift, but perhaps there is a better way?
> > 
> > Thanks,
> > Matthew
> > 
> > 
> > MATTHEW SHAXTED
> > SKIDMORE, OWINGS & MERRILL LLP
> > 224 SOUTH MICHIGAN AVENUE
> > CHICAGO, IL 60604
> > T  (312) 360-4368
> > MATTHEW.SHAXTED at SOM.COM<mailto:MATTHEW.SHAXTED at SOM.COM>
> > 
> > [cid:image004.png at 01CFBC98.297A1030]<http://www.som.com/>
> > The information contained in this communication may be confidential, is intended only for the use of the recipient(s) named above, and may be legally privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution, or copying of this communication, or any of its contents, is strictly prohibited and may be unlawful. If you have received this communication in error, please return it to the sender immediately and delete the original message and any copy of it from your computer system. If you have any questions concerning this message, please contact the sender.
> > 
> > [cid:image003.gif at 01CFBC96.E755A620]
> > 
> > _______________________________________________
> > Swift-user mailing list
> > Swift-user at ci.uchicago.edu
> > https://lists.ci.uchicago.edu/cgi-bin/mailman/listinfo/swift-user
> 
> 
> 




More information about the Swift-user mailing list