Generate multiple output files starting from a single “seed” file
-
27-06-2021 - |
質問
Good Morning everybody,
it's sometime I wonder if it is possible to do something close to what I'm gonna describe by means of Matlab:
Using an external tool (i.e. Ansys, Abaqus or other software) I engender a "seed" listed (file extension .inp, .db or others) file which will be used as reference for the next steps;
Starting from this seed listed file, I would like to get, let's say, 200 similar project files, containing some slight variations compared to the seed: I mean, for instance, simulation time or any other characteristic.
I will give a short example: I'm currently working on Bladed, software perfoming aero-elastic simulation for wind energy applications; Blade gives me, for instance, the chance to generate turbulent wind fields. The seed code would be the one shown below:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<BladedProject version="4.2.0.46">
<BladedData dataFormat="project">
<![CDATA[
VERSION 4.2.0.46
MULTIBODY 1
CALCULATION 3
OPTIONS 0
PROJNAME
DATE
ENGINEER
NOTES ""
PASSWORD
MSTART WINDND
SPMODEL 7
NLAT 31
NVER 45
LATDIM 150
VERDIM 220
LONGLS 340.2
LATLS 42.1482
VERTLS 42.1467
XLV 113.4
YLV 66.3117
ZLV 33.1546
XLW 27.72
YLW 25.228
ZLW 50.4542
LAMBDA1 0
CohScale 340.2
COHDEC 12
SCALE 0
GAMMA 0
YDIML 0
N2 0
YDIMS 0
K1MIN 3
LENGTH 1830
STEP .2233905
UBAR 3
SEED 3
OUTFILE l:\02_turb_dev\50-1\loads\50-1_D116_Validation_adapted_to_AV07\wind\DLC1-2_Kaimal\s1\3.wnd
DIAM 0
HUBHT 0
TURBHTTYPE 0
TURBBOTTOM 0
GUSTAVT 0
GUSTSPEED 0
TOLERANCE 0
DLONGMIN 0
DLONGMAX 0
Z0MIN 0
Z0MAX 0
MAXITER 14
MAXSEED 100
NFILES 1
UseWindShear 0
UseShearToGust 0
WVMODEL 0
MATCHFILE ''
SPACING 0
SAMPLEFREQ 0
MEANSPEED 0
ILAT 0
IVERT 0
GUSTMETHOD 0
DLONG 0
ILAT 0
IVERT 0
LONGGUST 0
LATGUST 0
VERTGUST 0
iLONGGUST 0
iLATGUST 0
iVERTGUST 0
PEAKINESS 0
MAXFRAN 0
MEND
0WINDND
]]>
</BladedData>
</BladedProject>
By means of matlab I would like to be able to generate similar project files for different wind speeds and random seeds (UBAR,SEED) and to save these files in a predetermined sub-folder.
Finally, I only would be glad to know if any of you has a clue/advice to give me. Then, it will be my task to find out a proper architecture for coding.
I thank you all in advance for supporting.
Best regards, Francesco
解決
So here are some bits and pieces of an answer, read more about the functions used in the Matlab documentation. The code below is only an example, but you shouldn't have any trouble expanding it. This code will write 5 files with UBAR
values 1..5
. I'll suppose that, at the start of execution, your current working directory is a suitable place for those 5 files to reside, at least temporarily
for ubar = 1:5 %using the name of the variable as the iteration variable here
fname = ['bladefile' num2str(ubar) '.txt'] % filenames will be bladefile1.txt etc
fid = fopen(fname,'w') % open the file for writing, keep the file id for later reference
fprintf(fid, '<?xml version="1.0" encoding="ISO-8859-1" ?>\n<BladedProject version="4.2.0.46">\n <BladedData dataFormat="project">\n <![CDATA[\n')
This fprintf
call writes to the output file (using the file id fid
); it writes your XML header. Note the embedded \n
s which will be turned into new lines in the output file.
fprintf(fid, 'VERSION 4.2.0.46\n') % note, again, the trailing newline so the next line of output starts on a new line
...
fprintf(fid, 'ubar %i\n', ubar)
This call to fprintf
writes the string ubar
to the file, then the %i
indicates that an integer will be written, and the value of that integer will be taken from the variable ubar
.
And so on until the end of the loop
fclose(fid)
end % and loop around
This should get you started and, if your aim is only to write a set of output files once, is good enough for throwaway use.
If you expect to re-use the code do as you would in any programming language and wrap it up into a function. For example, you could write a function which takes a value for ubar
and a value for seed
as inputs and writes the file, something like this;
for ubar = 1:5
for seed = 1:7
write_inp_file(ubar,seed)
end
end