0

I need to run several hundred iterations (or more) of a simulation that reads its parameters from an .xml file. This file has ~83k lines.

A sample of a parameter from this simulation is:

  <ScalarVariable
  name = "nr_resources_init"
  valueReference = "2721"
  description = "Initial available non-recoverable resources"
  variability = "parameter" isDiscrete = "true"
  causality = "internal" isValueChangeable = "true"
  alias = "noAlias"
  classIndex = "1415" classType = "rPar"
  isProtected = "false" hideResult = "false"
  fileName = "[path to model file]" startLine = "4602" startColumn = "9" endLine = "4602" endColumn = "119" fileWritable = "true">
  <Real start="1000000000000.0" fixed="true" useNominal="false" unit="ton" />
  </ScalarVariable>

Because of the following...:

  • The simulations are run with an executable that automatically reads the .xml for its initial state (parameters).
  • From Fortran I would need to search and replace the values ("1000000000000" from the "Real" subnode in the example) of the parameters ("nr_resources_init") and then call the executable with a system call.
  • The .xml is automatically generated (only once) before the simulations and it will always have the same structure. Therefore, I think that using regex for this search&replace won't be an issue.

... I considered that using an "XML heavy library" would be an overkill and it may not be the fastest option. Therefore, I tried using sed and perl but couldn't find the correct regex.

What's the best/fastest way to replace the value of a given parameter with a given new value programmatically from Fortran or using system calls?

Matt Hogan-Jones
  • 2,981
  • 1
  • 29
  • 35
Alechan
  • 817
  • 1
  • 10
  • 24
  • 1
    Are you using one of the available Fortran libraries for manipulating XML ? If so, be specific, and be clear about what you have tried and where you have failed. If not, you might want to choose a programming system which has better intrinsic XML-processing facilities than naked Fortran. But not regular expressions which lack the expressive power to handle XML. – High Performance Mark Jan 24 '17 at 17:26
  • *"Therefore, I tried using sed and perl but couldn't find the correct regex."* Have you read http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags by any chance? Or http://stackoverflow.com/questions/8577060/why-is-it-such-a-bad-idea-to-parse-xml-with-regex – Vladimir F Героям слава Jan 24 '17 at 18:43
  • @HighPerformanceMark, before researching about the available XML libraries for Fortran I decided to ask here to see if it was the right choice or it would have a lot of overhead. – Alechan Jan 24 '17 at 19:16
  • @VladimirF, the first one is full of offtopic answers and the second one is irrelevant. The XML that I deal with has a fixed structure so a regex would be enough. The problem is that I tried different regexes (regexs?) but none would wrap what I desired. – Alechan Jan 24 '17 at 19:19
  • 1
    It seems you're not planning to treat the file as XML at all. If that approach is satisfactory to you I wouldn't bother with a library to read and transform XML. At a minimum using one of the Fortran XML libraries will impose the overhead of you having to learn how to use it. It really seems to me that you ought to fix your regex-based approach. If I were you I'd delete this question and ask about that. – High Performance Mark Jan 24 '17 at 19:58
  • 1
    Just read the 83k lines into a array, like " CHARACTER(LEN=256), DIMENSION(83000) :I XML_lines ". Then root through each line and change the 10000ˆ? to whatever and then write out the new file. It should be about 30 lines in total. – Holmz Jan 25 '17 at 01:44

0 Answers0