|
17 | 17 | #
|
18 | 18 | import inspect # Don't worry about this, standard stuff plus file discovery
|
19 | 19 | import os
|
| 20 | +import tempfile |
20 | 21 |
|
21 | 22 | filename = inspect.getframeinfo(inspect.currentframe()).filename
|
22 | 23 | path = os.path.dirname(os.path.abspath(filename))
|
@@ -141,91 +142,88 @@ def myDataCallback(monteCarloData, retentionPolicy):
|
141 | 142 | @pytest.mark.slowtest
|
142 | 143 | def test_MonteCarloSimulation(show_plots):
|
143 | 144 | # Test a montecarlo simulation
|
144 |
| - dirName = os.path.abspath(os.path.dirname(__file__)) + "/tmp_montecarlo_test" |
145 |
| - monteCarlo = Controller() |
146 |
| - monteCarlo.setShouldDisperseSeeds(True) |
147 |
| - monteCarlo.setExecutionFunction(myExecutionFunction) |
148 |
| - monteCarlo.setSimulationFunction(myCreationFunction) |
149 |
| - monteCarlo.setExecutionCount(NUMBER_OF_RUNS) |
150 |
| - monteCarlo.setThreadCount(PROCESSES) |
151 |
| - monteCarlo.setVerbose(True) |
152 |
| - monteCarlo.setArchiveDir(dirName) |
153 |
| - |
154 |
| - # Add some dispersions |
155 |
| - disp1Name = 'TaskList[0].TaskModels[0].hub.sigma_BNInit' |
156 |
| - disp2Name = 'TaskList[0].TaskModels[0].hub.omega_BN_BInit' |
157 |
| - disp3Name = 'TaskList[0].TaskModels[0].hub.mHub' |
158 |
| - disp4Name = 'TaskList[0].TaskModels[0].hub.r_BcB_B' |
159 |
| - disp5Name = 'TaskList[0].TaskModels[0].hub.r_CN_NInit' |
160 |
| - disp6Name = 'TaskList[0].TaskModels[0].hub.v_CN_NInit' |
161 |
| - dispDict = {} |
162 |
| - dispDict["mu"] = 0.3986004415E+15 |
163 |
| - dispDict["a"] = ["normal", 42000 * 1E3, 2000 * 1E3] |
164 |
| - dispDict["e"] = ["uniform", 0, 0.5] |
165 |
| - dispDict["i"] = ["uniform", -80, 80] |
166 |
| - dispDict["Omega"] = None |
167 |
| - dispDict["omega"] = ["uniform", 80, 90] |
168 |
| - dispDict["f"] = ["uniform", 0, 359] |
169 |
| - monteCarlo.addDispersion(OrbitalElementDispersion(disp5Name, disp6Name, dispDict)) |
170 |
| - monteCarlo.addDispersion(UniformEulerAngleMRPDispersion(disp1Name)) |
171 |
| - monteCarlo.addDispersion(NormalVectorCartDispersion(disp2Name, 0.0, 0.75 / 3.0 * np.pi / 180)) |
172 |
| - monteCarlo.addDispersion(UniformDispersion(disp3Name, ([1300.0 - 812.3, 1500.0 - 812.3]))) |
173 |
| - monteCarlo.addDispersion( |
174 |
| - NormalVectorCartDispersion(disp4Name, [0.0, 0.0, 1.0], [0.05 / 3.0, 0.05 / 3.0, 0.1 / 3.0])) |
175 |
| - |
176 |
| - # Add retention policy |
177 |
| - retentionPolicy = RetentionPolicy() |
178 |
| - retentionPolicy.addMessageLog(retainedMessageName, [var1, var2]) |
179 |
| - retentionPolicy.addVariableLog("helloworldModule.GetTicker()") |
180 |
| - retentionPolicy.addVariableLog("bskSat.totOrbEnergy") |
181 |
| - retentionPolicy.setDataCallback(myDataCallback) |
182 |
| - monteCarlo.addRetentionPolicy(retentionPolicy) |
183 |
| - |
184 |
| - failures = monteCarlo.executeSimulations() |
185 |
| - |
186 |
| - assert len(failures) == 0, "No runs should fail" |
187 |
| - |
188 |
| - # Test loading data from runs from disk |
189 |
| - monteCarloLoaded = Controller.load(dirName) |
190 |
| - |
191 |
| - retainedData = monteCarloLoaded.getRetainedData(NUMBER_OF_RUNS-1) |
192 |
| - assert retainedData is not None, "Retained data should be available after execution" |
193 |
| - |
194 |
| - assert "messages" in retainedData, "Retained data should retain messages" |
195 |
| - assert retainedMessageName + ".r_BN_N" in retainedData["messages"], "Retained messages should exist" |
196 |
| - assert retainedMessageName + ".v_BN_N" in retainedData["messages"], "Retained messages should exist" |
197 |
| - |
198 |
| - assert "variables" in retainedData, "Retained data should retain variables" |
199 |
| - assert retainedVariableName + ".GetTicker()" in retainedData["variables"], "Retained variables should exist" |
200 |
| - |
201 |
| - # rerun the case and it should be the same, because we dispersed random seeds |
202 |
| - oldOutput = retainedData["messages"][retainedMessageName + ".r_BN_N"] |
203 |
| - |
204 |
| - failed = monteCarloLoaded.reRunCases([NUMBER_OF_RUNS-1]) |
205 |
| - assert len(failed) == 0, "Should rerun case successfully" |
206 |
| - |
207 |
| - retainedData = monteCarloLoaded.getRetainedData(NUMBER_OF_RUNS-1) |
208 |
| - newOutput = retainedData["messages"][retainedMessageName + ".r_BN_N"] |
209 |
| - for k1, v1 in enumerate(oldOutput): |
210 |
| - for k2, v2 in enumerate(v1): |
211 |
| - assert np.fabs(oldOutput[k1][k2] - newOutput[k1][k2]) < .001, \ |
212 |
| - "Outputs shouldn't change on runs if random seeds are same" |
213 |
| - |
214 |
| - # test the initial parameters were saved from runs, and they differ between runs |
215 |
| - params1 = monteCarloLoaded.getParameters(NUMBER_OF_RUNS-1) |
216 |
| - params2 = monteCarloLoaded.getParameters(NUMBER_OF_RUNS-2) |
217 |
| - assert "TaskList[0].TaskModels[0].RNGSeed" in params1, "random number seed should be applied" |
218 |
| - for dispName in [disp1Name, disp2Name, disp3Name, disp4Name]: |
219 |
| - assert dispName in params1, "dispersion should be applied" |
220 |
| - # assert two different runs had different parameters. |
221 |
| - assert params1[dispName] != params2[dispName], "dispersion should be different in each run" |
222 |
| - |
223 |
| - monteCarloLoaded.executeCallbacks() |
224 |
| - if show_plots: |
225 |
| - plt.show() |
226 |
| - |
227 |
| - shutil.rmtree(dirName) |
228 |
| - assert not os.path.exists(dirName), "No leftover data should exist after the test" |
| 145 | + with tempfile.TemporaryDirectory() as tempDirectory: |
| 146 | + monteCarlo = Controller() |
| 147 | + monteCarlo.setShouldDisperseSeeds(True) |
| 148 | + monteCarlo.setExecutionFunction(myExecutionFunction) |
| 149 | + monteCarlo.setSimulationFunction(myCreationFunction) |
| 150 | + monteCarlo.setExecutionCount(NUMBER_OF_RUNS) |
| 151 | + monteCarlo.setThreadCount(PROCESSES) |
| 152 | + monteCarlo.setVerbose(True) |
| 153 | + monteCarlo.setArchiveDir(tempDirectory) |
| 154 | + |
| 155 | + # Add some dispersions |
| 156 | + disp1Name = 'TaskList[0].TaskModels[0].hub.sigma_BNInit' |
| 157 | + disp2Name = 'TaskList[0].TaskModels[0].hub.omega_BN_BInit' |
| 158 | + disp3Name = 'TaskList[0].TaskModels[0].hub.mHub' |
| 159 | + disp4Name = 'TaskList[0].TaskModels[0].hub.r_BcB_B' |
| 160 | + disp5Name = 'TaskList[0].TaskModels[0].hub.r_CN_NInit' |
| 161 | + disp6Name = 'TaskList[0].TaskModels[0].hub.v_CN_NInit' |
| 162 | + dispDict = {} |
| 163 | + dispDict["mu"] = 0.3986004415E+15 |
| 164 | + dispDict["a"] = ["normal", 42000 * 1E3, 2000 * 1E3] |
| 165 | + dispDict["e"] = ["uniform", 0, 0.5] |
| 166 | + dispDict["i"] = ["uniform", -80, 80] |
| 167 | + dispDict["Omega"] = None |
| 168 | + dispDict["omega"] = ["uniform", 80, 90] |
| 169 | + dispDict["f"] = ["uniform", 0, 359] |
| 170 | + monteCarlo.addDispersion(OrbitalElementDispersion(disp5Name, disp6Name, dispDict)) |
| 171 | + monteCarlo.addDispersion(UniformEulerAngleMRPDispersion(disp1Name)) |
| 172 | + monteCarlo.addDispersion(NormalVectorCartDispersion(disp2Name, 0.0, 0.75 / 3.0 * np.pi / 180)) |
| 173 | + monteCarlo.addDispersion(UniformDispersion(disp3Name, ([1300.0 - 812.3, 1500.0 - 812.3]))) |
| 174 | + monteCarlo.addDispersion( |
| 175 | + NormalVectorCartDispersion(disp4Name, [0.0, 0.0, 1.0], [0.05 / 3.0, 0.05 / 3.0, 0.1 / 3.0])) |
| 176 | + |
| 177 | + # Add retention policy |
| 178 | + retentionPolicy = RetentionPolicy() |
| 179 | + retentionPolicy.addMessageLog(retainedMessageName, [var1, var2]) |
| 180 | + retentionPolicy.addVariableLog("helloworldModule.GetTicker()") |
| 181 | + retentionPolicy.addVariableLog("bskSat.totOrbEnergy") |
| 182 | + retentionPolicy.setDataCallback(myDataCallback) |
| 183 | + monteCarlo.addRetentionPolicy(retentionPolicy) |
| 184 | + |
| 185 | + failures = monteCarlo.executeSimulations() |
| 186 | + |
| 187 | + assert len(failures) == 0, "No runs should fail" |
| 188 | + |
| 189 | + # Test loading data from runs from disk |
| 190 | + monteCarloLoaded = Controller.load(tempDirectory) |
| 191 | + |
| 192 | + retainedData = monteCarloLoaded.getRetainedData(NUMBER_OF_RUNS-1) |
| 193 | + assert retainedData is not None, "Retained data should be available after execution" |
| 194 | + |
| 195 | + assert "messages" in retainedData, "Retained data should retain messages" |
| 196 | + assert retainedMessageName + ".r_BN_N" in retainedData["messages"], "Retained messages should exist" |
| 197 | + assert retainedMessageName + ".v_BN_N" in retainedData["messages"], "Retained messages should exist" |
| 198 | + |
| 199 | + assert "variables" in retainedData, "Retained data should retain variables" |
| 200 | + assert retainedVariableName + ".GetTicker()" in retainedData["variables"], "Retained variables should exist" |
| 201 | + |
| 202 | + # rerun the case and it should be the same, because we dispersed random seeds |
| 203 | + oldOutput = retainedData["messages"][retainedMessageName + ".r_BN_N"] |
| 204 | + |
| 205 | + failed = monteCarloLoaded.reRunCases([NUMBER_OF_RUNS-1]) |
| 206 | + assert len(failed) == 0, "Should rerun case successfully" |
| 207 | + |
| 208 | + retainedData = monteCarloLoaded.getRetainedData(NUMBER_OF_RUNS-1) |
| 209 | + newOutput = retainedData["messages"][retainedMessageName + ".r_BN_N"] |
| 210 | + for k1, v1 in enumerate(oldOutput): |
| 211 | + for k2, v2 in enumerate(v1): |
| 212 | + assert np.fabs(oldOutput[k1][k2] - newOutput[k1][k2]) < .001, \ |
| 213 | + "Outputs shouldn't change on runs if random seeds are same" |
| 214 | + |
| 215 | + # test the initial parameters were saved from runs, and they differ between runs |
| 216 | + params1 = monteCarloLoaded.getParameters(NUMBER_OF_RUNS-1) |
| 217 | + params2 = monteCarloLoaded.getParameters(NUMBER_OF_RUNS-2) |
| 218 | + assert "TaskList[0].TaskModels[0].RNGSeed" in params1, "random number seed should be applied" |
| 219 | + for dispName in [disp1Name, disp2Name, disp3Name, disp4Name]: |
| 220 | + assert dispName in params1, "dispersion should be applied" |
| 221 | + # assert two different runs had different parameters. |
| 222 | + assert params1[dispName] != params2[dispName], "dispersion should be different in each run" |
| 223 | + |
| 224 | + monteCarloLoaded.executeCallbacks() |
| 225 | + if show_plots: |
| 226 | + plt.show() |
229 | 227 |
|
230 | 228 |
|
231 | 229 | if __name__ == "__main__":
|
|
0 commit comments