sahbazhsdt commited on
Commit
727a8f2
·
verified ·
1 Parent(s): cbde030

Add files using upload-large-folder tool

Browse files
LIDE_generator_script/default.xml ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!-- -->
3
+ <configuration>
4
+ <domain>
5
+ <nodeSize> 0.2 e-5 </nodeSize>
6
+ <nodeRatio> 16 ,16 , 1 </nodeRatio>
7
+ <centeredAlignment> False, False, False </centeredAlignment>
8
+ <boundaryConditions>
9
+ <material>
10
+ <west> Symmetry </west>
11
+ <east> ZeroGradient </east>
12
+ <south> Symmetry </south>
13
+ <north> ZeroGradient </north>
14
+ <bottom> ZeroGradient </bottom>
15
+ <top> ZeroGradient </top>
16
+ </material>
17
+ </boundaryConditions>
18
+
19
+ <initialConditions>
20
+ <treeInitializationType> LevelBased </treeInitializationType>
21
+ <topologyInitializationType> BottomUp </topologyInitializationType>
22
+
23
+ <material1>
24
+ // air
25
+ if ((x &lt; 0.35e-6) and (sqrt(x^2+y^2) &lt; 1e-5)) {
26
+ density := 998.2;
27
+ velocityX := 0.0;
28
+ velocityY := 0.0;
29
+ velocityZ := 0.0;
30
+ pressure := 10e9;
31
+ } else {
32
+ density := 0.74;
33
+ velocityX := 0.0;
34
+ velocityY := 0.0;
35
+ velocityZ := 0.0;
36
+ pressure := 1e5;
37
+ }
38
+ </material1>
39
+
40
+ <material2>
41
+ // water
42
+ density := 998.2;
43
+ velocityX := 0.0;
44
+ velocityY := 0.0;
45
+ velocityZ := 0.0;
46
+ pressure := 1e5;
47
+ </material2>
48
+
49
+ <interface1>
50
+ <levelset>
51
+ <type> precompiled </type>
52
+ <operator> or </operator>
53
+ <!-- <data>
54
+ <function> Sphere </function>
55
+ <center> 0.0, 0.0, 0.0 </center>
56
+ <centerSign> -1 </centerSign>
57
+ <innerRadius> 0.0 </innerRadius>
58
+ <outerRadius> 1e-5 </outerRadius>
59
+ </data> -->
60
+ <data>
61
+ <function> Cylinder </function>
62
+ <center> 0.0, 0.0, 0.0 </center>
63
+ <centerSign> 1 </centerSign>
64
+ <rotation> 1.570796326794897, 0, 0 </rotation>
65
+ <innerRadius> 0.0 </innerRadius>
66
+ <outerRadius> 0.2e-6 </outerRadius>
67
+ <height> 4e-5 </height>
68
+ </data>
69
+ <data>
70
+ <function> Ellipsoid </function>
71
+ <center> 0.0, 0.0, 0.0 </center>
72
+ <centerSign> -1 </centerSign>
73
+ <rotation> 0.0, 0.0, 0.0 </rotation>
74
+ <semiAxis> 1.5e-5, 1e-5, 1e-5 </semiAxis>
75
+ </data>
76
+ </levelset>
77
+ </interface1>
78
+ </initialConditions>
79
+ </domain>
80
+
81
+ <materials>
82
+ <numberOfMaterials> 2 </numberOfMaterials>
83
+ <numberOfPositiveMaterials> 2 </numberOfPositiveMaterials>
84
+ <material1>
85
+ <type> Fluid </type>
86
+ <equationOfState>
87
+ <type> StiffenedGas </type>
88
+ <gamma> 1.33 </gamma>
89
+ <backgroundPressure> 0.0 </backgroundPressure>
90
+ </equationOfState>
91
+ <properties>
92
+ <specificHeatCapacity> 0.0 </specificHeatCapacity>
93
+ <thermalConductivity> 0.0 </thermalConductivity>
94
+ <shearViscosity> 0.0 </shearViscosity>
95
+ <bulkViscosity> 0.0 </bulkViscosity>
96
+ </properties>
97
+ </material1>
98
+
99
+ <material2>
100
+ <type> Fluid </type>
101
+ <equationOfState>
102
+ <type> StiffenedGas </type>
103
+ <gamma> 4.4 </gamma>
104
+ <backgroundPressure> 6.0e8 </backgroundPressure>
105
+ </equationOfState>1
106
+ <properties>
107
+ <specificHeatCapacity> 0.0 </specificHeatCapacity>
108
+ <thermalConductivity> 0.0 </thermalConductivity>
109
+ <shearViscosity> 0.0 </shearViscosity>
110
+ <bulkViscosity> 0.0 </bulkViscosity>
111
+ </properties>
112
+ </material2>
113
+ </materials>
114
+
115
+ <materialPairings>
116
+ <material1_2>
117
+ <surfaceTensionCoefficient> 0.0 </surfaceTensionCoefficient>
118
+ </material1_2>
119
+ </materialPairings>
120
+
121
+ <sourceTerms>
122
+ <gravity> 0, 0, 0 </gravity>
123
+ </sourceTerms>
124
+
125
+ <multiResolution>
126
+ <maximumLevel> 0 </maximumLevel>
127
+
128
+ <refinementCriterion>
129
+ <epsilonReference> 0.01 </epsilonReference>
130
+ <levelOfEpsilonReference> 0 </levelOfEpsilonReference>
131
+ </refinementCriterion>
132
+ </multiResolution>
133
+
134
+ <timeControl>
135
+ <startTime> 0.0 </startTime>
136
+ <endTime> 20e-9 </endTime>
137
+ <CFLNumber> 0.35 </CFLNumber>
138
+ </timeControl>
139
+
140
+ <dimensionalization>
141
+ <lengthReference> 1.0 </lengthReference>
142
+ <velocityReference> 1.0 </velocityReference>
143
+ <densityReference> 1.0 </densityReference>
144
+ <temperatureReference> 1.0 </temperatureReference>
145
+ <amountOfSubstanceReference> 1.0 </amountOfSubstanceReference>
146
+ </dimensionalization>
147
+
148
+ <restart>
149
+ <restore>
150
+ <mode> Off </mode>
151
+ <fileName> ./Alpaca/ALPACA_WORKSHOP/build/droplet_RDEMIC/restart/restart_.h5 </fileName>
152
+ </restore>
153
+ <snapshots>
154
+ <mode> Macro </mode>
155
+ <type> Interval </type>
156
+ <interval> 50e-9 </interval>
157
+ <intervalsToKeep> 2 </intervalsToKeep>
158
+ <stamps> </stamps>
159
+ </snapshots>
160
+ </restart>
161
+
162
+ <output>
163
+ <timeNamingFactor> 1.0e6 </timeNamingFactor>
164
+ <standard>
165
+ <type> Interval </type>
166
+ <interval> 1e-10 </interval>
167
+ <stamps> </stamps>
168
+ <minimalLevel> 0 </minimalLevel>
169
+ </standard>
170
+ </output>
171
+
172
+ </configuration>
LIDE_generator_script/generator.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This script generates input files for ALPACA simulations of LIDE by varying key parameters using a Sobol sequence for sampling.
3
+ Change the parameters in the 'param_bounds' array to modify the ranges for high pressure, low pressure, laser width, and droplet radii.
4
+ First creates a set of .xml input files using "create_alpaca_input" function, then runs ALPACA on each generated input file using "run_alpaca" function.
5
+ For this change the number of simualtions and paths at the end of the script.
6
+ Keep the default.xml file in the same directory as this script as the code overwrites it to create new input files.
7
+ """
8
+
9
+
10
+
11
+ import os
12
+ import subprocess
13
+ import logging
14
+ import time
15
+ import math
16
+ import html
17
+ import re
18
+ from scipy.stats.qmc import Sobol, scale
19
+ from scipy.stats import qmc
20
+ import xmltodict
21
+ import numpy as np
22
+ import warnings
23
+
24
+
25
+ def run_alpaca(
26
+ xml_file,
27
+ output_dir: str = None,
28
+ mpiexec_path: str = None,
29
+ exec_file_path: str = None,
30
+ num_workers: int = 10,
31
+ ):
32
+ """
33
+ Runs ALPACA with different parameters in .xml file.
34
+
35
+ Args:
36
+ xml_file (str): Path to the input .xml file to be processed.
37
+ mpiexec_path (str): Path to the mpiexec executable. Find using command "which mpiexec" in terminal.
38
+ exec_file_path (str): Path to the ALPACA executable file. Default is "./build/ALPACA".
39
+ num_workers (int): Number of cpu workers for parallel processing.
40
+ output_dir (str): Directory where output files will be saved.
41
+ """
42
+
43
+ # Setup logging
44
+ logging.basicConfig(
45
+ filename=os.path.join(str(output_dir), 'data_generator.log'),
46
+ level=logging.INFO,
47
+ format='%(asctime)s [%(levelname)s] %(message)s'
48
+ )
49
+
50
+ # Check for executable ALAPACA location in command
51
+ command = [str(mpiexec_path), "-n", str(num_workers), str(exec_file_path), str(xml_file), str(output_dir)]
52
+
53
+
54
+ logging.info(f"Starting: {' '.join(command)}")
55
+ start_time = time.time()
56
+ result = subprocess.run(command, capture_output=True, text=True)
57
+ end_time = time.time()
58
+
59
+ elapsed = end_time - start_time
60
+ length = math.ceil(elapsed / 60)
61
+
62
+
63
+ if result.returncode == 0:
64
+ logging.info(f"Completed {xml_file} successfully in {length:.2f} minutes; [{elapsed:.2f} seconds.]")
65
+ logging.debug(f"Output:\n{result.stdout}")
66
+ else:
67
+ logging.error(f"ALPACA failed on {xml_file} with return code {result.returncode}")
68
+ logging.error(f"stderr:\n{result.stderr}")
69
+
70
+ def round_sig(x, sig=5):
71
+ return float(f"{x:.{sig}g}")
72
+
73
+ def create_alpaca_input(
74
+ count: int,
75
+ base_path: str ,
76
+ output_path: str
77
+ ):
78
+
79
+ """
80
+ Generates a set of ALPACA input files with varying parameters for high pressure, low pressure, laser width, and the two radii of the droplet.
81
+
82
+ Args:
83
+ count (int): Number of samples to generate.
84
+ base_path (str): Path to the base .xml file that will be modified.
85
+ output_path (str): Directory where the generated .xml files will be saved. Create the directory if it does not exist.
86
+ """
87
+
88
+ warnings.warn(f"[WARNING] Make sure the default base_input file {base_path} exists and untouched !!!.")
89
+
90
+
91
+ # Define the parameters
92
+ param_bounds = np.array([
93
+ [1e10, 8e10], # high_pressure
94
+ [1e5, 1e6], # low_pressure
95
+ [2e-7, 15e-7], # laser_width
96
+ [1e-5, 1.6e-5], #drop_radius_1
97
+ [1e-5, 1.6e-5] #drop_radius_2
98
+ ])
99
+
100
+ n_samples = count
101
+ n_dims = param_bounds.shape[0]
102
+
103
+ sampler = qmc.Sobol(d=n_dims, scramble=True, seed=10)
104
+ samples_unit = sampler.random(n=n_samples)
105
+
106
+ params = qmc.scale(samples_unit, param_bounds[:, 0], param_bounds[:, 1]) #[N, D] = [128, 5]
107
+ params = np.vectorize(round_sig)(params, sig=5)
108
+
109
+ width = params[:, 2] + 0.15e-6
110
+
111
+
112
+ # Modify default.xml file
113
+ for i in range(n_samples):
114
+
115
+ with open(base_path) as f:
116
+ data = xmltodict.parse(f.read())
117
+
118
+
119
+ # Air
120
+ air_0 = data["configuration"]["domain"]["initialConditions"]["material1"]
121
+
122
+ air_1 = air_0.replace("0.35e-6", f"{width[i]}")
123
+ air_2 = air_1.replace("1e-5", f"{params[i, 4]}")
124
+ air_3 = air_2.replace("pressure := 10e9;", f"pressure := {params[i, 0]};")
125
+
126
+ match = re.search(r'else\s*{(.*?)}', air_0, flags=re.DOTALL)
127
+ else_block = match.group(1)
128
+ modified_else = re.sub(
129
+ r'pressure\s*:=\s*1e5;',
130
+ f'pressure := {params[i, 1]};',
131
+ match.group(1)
132
+ )
133
+ air_4 = air_3.replace(else_block, modified_else)
134
+
135
+ data["configuration"]["domain"]["initialConditions"]["material1"] = air_4
136
+
137
+ # Water
138
+ water_0 = data["configuration"]["domain"]["initialConditions"]["material2"]
139
+ water_1 = water_0.replace("pressure := 1e5;", f"pressure := {params[i, 1]};")
140
+ data["configuration"]["domain"]["initialConditions"]["material2"] = water_1
141
+
142
+
143
+ # Interface
144
+ geometry_list = data["configuration"]["domain"]["initialConditions"]["interface1"]["levelset"]["data"]
145
+ # print(geometry_list)
146
+ for geo in geometry_list:
147
+ func = geo["function"].strip()
148
+
149
+ if func == "Cylinder":
150
+ geo["outerRadius"] = f"{params[i, 2]}"
151
+
152
+ if func == "Ellipsoid":
153
+ geo["semiAxis"] = f"{params[i, 3]}, {params[i, 4]}, 1e-5"
154
+
155
+
156
+
157
+ formatted_params = ["{:.4e}".format(params[i, j]) for j in range(params.shape[1])]
158
+
159
+
160
+ with open(output_path+ f"/hp{formatted_params[0]}_lp{formatted_params[1]}_laser{formatted_params[2]}_dropradx{formatted_params[3]}_droprady{formatted_params[4]}.xml", 'w') as f:
161
+ f.write(xmltodict.unparse(data, pretty=True))
162
+
163
+
164
+ count = 2 # Number of samples to generate. keep it multiple of 2 for Sobol sequence
165
+ inputs_output_path = "."
166
+ data_output_path = "."
167
+
168
+
169
+ create_alpaca_input(count=count,
170
+ base_path="./default.xml",
171
+ output_path=inputs_output_path)
172
+
173
+
174
+ inputs = []
175
+ for file in os.listdir(inputs_output_path):
176
+ if file.endswith(".xml"):
177
+ inputs.append(os.path.join(".", file))
178
+ run_alpaca(xml_file=os.path.join(str(inputs_output_path), str(inputs[-1])),
179
+ output_dir=str(data_output_path),
180
+ mpiexec_path="mpiexec",
181
+ exec_file_path="./build/ALPACA",
182
+ num_workers=10)
183
+
184
+