Skip to content

Commit df1aed7

Browse files
authored
Merge pull request #86 from KratosMultiphysics/mlmc-update-examples
[MLMC] Update CPF example
2 parents d4922da + 495d549 commit df1aed7

11 files changed

Lines changed: 93 additions & 103 deletions

multilevel_monte_carlo/use_cases/compressible_potential_flow/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
**Kratos version:** 8.1
66

7-
**XMC version:** 2.0
7+
**XMC version:** Kratos default version
88

9-
**PyCOMPSs version:** 2.7
9+
**PyCOMPSs version:** Kratos default version to run in serial, >2.8 to run with `runcompss`
1010

1111
**Source files:** [Asynchronous Monte Carlo and Asynchronous Multilevel Monte Carlo](source)
1212

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"properties" : [{
3+
"model_part_name" : "model",
4+
"properties_id" : 1,
5+
"Material" : {
6+
"Variables" : {
7+
"DENSITY" : 1.225
8+
},
9+
"Tables" : {}
10+
}
11+
}]
12+
}
13+

multilevel_monte_carlo/use_cases/compressible_potential_flow/source/problem_settings/parameters_potential_naca_lev0.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"input_type" : "mdpa",
1515
"input_filename" : "problem_settings/naca0012Mesh4"
1616
},
17+
"material_import_settings": {
18+
"materials_filename": "problem_settings/materials.json"
19+
},
1720
"formulation" : {
1821
"element_type": "compressible"
1922
},

multilevel_monte_carlo/use_cases/compressible_potential_flow/source/problem_settings/parameters_potential_naca_lev1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"input_type" : "mdpa",
1515
"input_filename" : "problem_settings/CPS_MONTECARLO_MeshInterpError1e-2"
1616
},
17+
"material_import_settings": {
18+
"materials_filename": "problem_settings/materials.json"
19+
},
1720
"formulation" : {
1821
"element_type": "compressible"
1922
},

multilevel_monte_carlo/use_cases/compressible_potential_flow/source/problem_settings/parameters_potential_naca_lev2.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"input_type" : "mdpa",
1515
"input_filename" : "problem_settings/CPS_MONTECARLO_MeshInterpError5e-3"
1616
},
17+
"material_import_settings": {
18+
"materials_filename": "problem_settings/materials.json"
19+
},
1720
"formulation" : {
1821
"element_type": "compressible"
1922
},

multilevel_monte_carlo/use_cases/compressible_potential_flow/source/problem_settings/parameters_refinement.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"hessian_metric": {
2+
"metric": {
33
"hessian_strategy_parameters" :{
44
"metric_variable" : ["VELOCITY_X,VELOCITY_Y"],
55
"estimate_interpolation_error" : false,
@@ -11,7 +11,7 @@
1111
"anisotropy_remeshing" : true,
1212
"enforce_anisotropy_relative_variable" : false
1313
},
14-
"refinement_mmg" : {
14+
"remeshing" : {
1515
"initialize_entities" : false,
1616
"echo_level" : 0
1717
}

multilevel_monte_carlo/use_cases/compressible_potential_flow/source/problem_settings/parameters_xmc_asynchronous_mc_potentialFlow.json

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
"tolerancesForHierarchy": [0],
88
"positionMaxNumberIterationsCriterion": 2
99
},
10-
"costEstimatorInputDictionary": {
11-
"indexSetDimension": 0,
12-
"order": 1
13-
},
1410
"hierarchyOptimiserInputDictionary": {
1511
"defaultHierarchy": [[[],5]],
1612
"indexSpace": [1,1],
@@ -20,9 +16,17 @@
2016
},
2117
"monteCarloIndexInputDictionary": {
2218
"costEstimator": "xmc.momentEstimator.MomentEstimator",
19+
"costEstimatorInputDictionary": {
20+
"indexSetDimension": 0, "order": 1, "updatedPowerSums":"xmc.methodDefs_momentEstimator.updatePowerSums.updatePowerSumsOrder2Dimension0", "centralMomentComputer":"xmc.methodDefs_momentEstimator.computeCentralMoments.centralMomentWrapper", "centralMomentErrorComputer":"xmc.methodDefs_momentEstimator.computeErrorEstimation.centralMomentErrorWrapper"
21+
},
2322
"indexValue": null,
24-
"qoiEstimator": ["xmc.momentEstimator.MomentEstimator"],
25-
"combinedEstimator": ["xmc.momentEstimator.CombinedMomentEstimator"],
23+
"qoiEstimator": [
24+
"xmc.momentEstimator.MomentEstimator",
25+
"xmc.momentEstimator.MultiMomentEstimator"
26+
],
27+
"qoiEstimatorInputDictionary": [
28+
{"indexSetDimension": 0,"order": 5,"updatedPowerSums":"xmc.methodDefs_momentEstimator.updatePowerSums.updatePowerSumsOrder10Dimension0","centralMomentComputer":"xmc.methodDefs_momentEstimator.computeCentralMoments.centralMomentWrapper","centralMomentErrorComputer":"xmc.methodDefs_momentEstimator.computeErrorEstimation.centralMomentErrorWrapper"},
29+
{"variableDimension": 3432,"order": 1, "isEstimationParallel": false}],
2630
"sampler": "xmc.sampleGenerator.SampleGenerator",
2731
"eventGroupSize": 5
2832
},
@@ -36,14 +40,6 @@
3640
"qoiPredictor": [],
3741
"initialNumberBatches": 1
3842
},
39-
"qoiEstimatorInputDictionary": {
40-
"indexSetDimension": 0,
41-
"order": 5
42-
},
43-
"combinedEstimatorInputDictionary": {
44-
"indexSetDimension": 0,
45-
"order": 5
46-
},
4743
"randomGeneratorInputDictionary": {
4844
"generator": "xmc.methodDefs_randomGeneratorWrapper.generator.returnUniformAndTwoNormal",
4945
"parameters": [0,4294967295,0.3,0.003,-0.035,0.00035]
@@ -55,36 +51,35 @@
5551
},
5652
"solverWrapperInputDictionary": {
5753
"adaptiveRefinementJumpToFinestLevel": false,
54+
"analysisStage": "simulation_definition.SimulationScenario",
5855
"asynchronous": true,
5956
"fakeRandomVariable": [0,0.3,0.0],
6057
"index": [],
6158
"mappingOutputQuantities": false,
6259
"numberContributionsPerInstance": 1,
63-
"numberQoI": 3433,
64-
"numberCombinedQoi": 0,
65-
"outputBatchSize": 1717,
60+
"outputBatchSize": 1,
6661
"parameters": null,
6762
"printToFile": true,
68-
"problemId": "problem_airfoil",
6963
"projectParametersPath": "problem_settings/parameters_potential_naca_lev2.json",
7064
"refinementParametersPath": "problem_settings/parameters_refinement.json",
7165
"refinementStrategy": "stochastic_adaptive_refinement",
66+
"sizeMultiXMomentEstimator": 3432,
7267
"taskAllAtOnce": true
7368
},
7469
"monoCriteriaInpuctDictionary" :{
7570
"statisticalError": {
7671
"criteria": "xmc.methodDefs_monoCriterion.criterionFunctions.isLowerThanOrEqualTo",
77-
"tolerance": [0.01],
72+
"tolerance": 0.01,
7873
"input": "error0"
7974
},
8075
"minNumberIterations": {
8176
"criteria": "xmc.methodDefs_monoCriterion.criterionFunctions.isGreaterThanOrEqualTo",
82-
"tolerance": [0],
77+
"tolerance": 0.0,
8378
"input": "algorithmCost"
8479
},
8580
"maxNumberIterations": {
8681
"criteria": "xmc.methodDefs_monoCriterion.criterionFunctions.isGreaterThanOrEqualTo",
87-
"tolerance": [0],
82+
"tolerance": 0.0,
8883
"input": "algorithmCost"
8984
}
9085
},

multilevel_monte_carlo/use_cases/compressible_potential_flow/source/problem_settings/parameters_xmc_asynchronous_mlmc_potentialFlow.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
"tolerancesForHierarchy": [0],
88
"positionMaxNumberIterationsCriterion": 2
99
},
10-
"costEstimatorInputDictionary": {
11-
"indexSetDimension": 0,
12-
"order": 1
13-
},
1410
"hierarchyOptimiserInputDictionary": {
1511
"defaultHierarchy": [[[0],5],[[1],5],[[2],5]],
1612
"indexSpace": [10],
@@ -19,10 +15,23 @@
1915
"toleranceSplittingBounds": [0.5,0.5]
2016
},
2117
"monteCarloIndexInputDictionary": {
22-
"combinedEstimator": ["xmc.momentEstimator.CombinedMomentEstimator"],
2318
"costEstimator": "xmc.momentEstimator.MomentEstimator",
19+
"costEstimatorInputDictionary": {
20+
"indexSetDimension": 0,
21+
"order": 1,
22+
"updatedPowerSums":"xmc.methodDefs_momentEstimator.updatePowerSums.updatePowerSumsOrder2Dimension0",
23+
"centralMomentComputer":"xmc.methodDefs_momentEstimator.computeCentralMoments.centralMomentWrapper",
24+
"centralMomentErrorComputer":"xmc.methodDefs_momentEstimator.computeErrorEstimation.centralMomentErrorWrapper"
25+
},
2426
"indexValue": null,
25-
"qoiEstimator": ["xmc.momentEstimator.MomentEstimator"],
27+
"qoiEstimator": [
28+
"xmc.momentEstimator.MomentEstimator",
29+
"xmc.momentEstimator.MultiMomentEstimator"
30+
],
31+
"qoiEstimatorInputDictionary": [
32+
{"indexSetDimension": 1,"order": 1,"updatedPowerSums":"xmc.methodDefs_momentEstimator.updatePowerSums.updatePowerSumsOrder2Dimension1","centralMomentComputer":"xmc.methodDefs_momentEstimator.computeCentralMoments.centralMomentWrapper","centralMomentErrorComputer":"xmc.methodDefs_momentEstimator.computeErrorEstimation.centralMomentErrorWrapper"},
33+
{"variableDimension": 204,"order": 1, "isEstimationParallel": false}
34+
],
2635
"sampler": "xmc.sampleGenerator.SampleGenerator",
2736
"eventGroupSize": 5
2837
},
@@ -36,14 +45,6 @@
3645
"qoiPredictor": [],
3746
"initialNumberBatches": 1
3847
},
39-
"qoiEstimatorInputDictionary": {
40-
"indexSetDimension": 1,
41-
"order": 1
42-
},
43-
"combinedEstimatorInputDictionary": {
44-
"indexSetDimension": 1,
45-
"order": 1
46-
},
4748
"randomGeneratorInputDictionary": {
4849
"generator": "xmc.methodDefs_randomGeneratorWrapper.generator.returnUniformAndTwoNormal",
4950
"parameters": [0,4294967295,0.3,0.03,0.0,0.0087]
@@ -55,36 +56,35 @@
5556
},
5657
"solverWrapperInputDictionary": {
5758
"adaptiveRefinementJumpToFinestLevel": false,
59+
"analysisStage": "simulation_definition.SimulationScenario",
5860
"asynchronous": true,
5961
"fakeRandomVariable": [0,0.3,0.0],
6062
"index": [],
6163
"mappingOutputQuantities": true,
6264
"numberContributionsPerInstance": 1,
63-
"numberQoI": 205,
64-
"numberCombinedQoi": 0,
6565
"outputBatchSize": 1,
6666
"parameters": null,
6767
"printToFile": false,
68-
"problemId": "problem_airfoil",
6968
"projectParametersPath": ["problem_settings/parameters_potential_naca_lev0.json","problem_settings/parameters_potential_naca_lev1.json","problem_settings/parameters_potential_naca_lev2.json"],
7069
"refinementParametersPath": "problem_settings/parameters_refinement.json",
7170
"refinementStrategy": "reading_from_file",
71+
"sizeMultiXMomentEstimator": 204,
7272
"taskAllAtOnce": true
7373
},
7474
"monoCriteriaInpuctDict" :{
7575
"statisticalError": {
7676
"criteria": "xmc.methodDefs_monoCriterion.criterionFunctions.isLowerThanOrEqualTo",
77-
"tolerance": [0.15],
77+
"tolerance": 0.15,
7878
"input": "error0"
7979
},
8080
"minNumberIterations": {
8181
"criteria": "xmc.methodDefs_monoCriterion.criterionFunctions.isGreaterThanOrEqualTo",
82-
"tolerance": [0],
82+
"tolerance": 0.0,
8383
"input": "algorithmCost"
8484
},
8585
"maxNumberIterations": {
8686
"criteria": "xmc.methodDefs_monoCriterion.criterionFunctions.isGreaterThanOrEqualTo",
87-
"tolerance": [0],
87+
"tolerance": 0.0,
8888
"input": "algorithmCost"
8989
}
9090
},

multilevel_monte_carlo/use_cases/compressible_potential_flow/source/run_mc_Kratos.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import KratosMultiphysics.MultilevelMonteCarloApplication
1212
import xmc
1313
import xmc.methodDefs_momentEstimator.computeCentralMoments as mdccm
14-
from exaqute.ExaquteTaskLocal import *
14+
from exaqute import get_value_from_remote
15+
1516

1617
if __name__ == "__main__":
1718

@@ -24,9 +25,8 @@
2425
with open(parametersPath,'r') as parameter_file:
2526
parameters = json.load(parameter_file)
2627

27-
# add path of the problem folder to python path
28-
problem_id = parameters["solverWrapperInputDictionary"]["problemId"]
29-
sys.path.append(os.path.join("..","xmc","classDefs_solverWrapper","problemDefs_KratosMultiphysics",problem_id))
28+
# SolverWrapper
29+
parameters["solverWrapperInputDictionary"]["qoiEstimator"] = parameters["monteCarloIndexInputDictionary"]["qoiEstimator"]
3030

3131
# SampleGenerator
3232
samplerInputDictionary = parameters["samplerInputDictionary"]
@@ -37,19 +37,6 @@
3737
monteCarloIndexInputDictionary = parameters["monteCarloIndexInputDictionary"]
3838
monteCarloIndexInputDictionary["samplerInputDictionary"] = samplerInputDictionary
3939

40-
# Moment Estimators
41-
qoiEstimatorInputDictionary = parameters["qoiEstimatorInputDictionary"]
42-
combinedEstimatorInputDictionary = parameters["combinedEstimatorInputDictionary"]
43-
costEstimatorInputDictionary = parameters["costEstimatorInputDictionary"]
44-
# qoi estimators
45-
monteCarloIndexInputDictionary["qoiEstimator"] = [monteCarloIndexInputDictionary["qoiEstimator"][0] for _ in range (0,parameters["solverWrapperInputDictionary"]["numberQoI"])]
46-
monteCarloIndexInputDictionary["qoiEstimatorInputDictionary"] = [qoiEstimatorInputDictionary]*parameters["solverWrapperInputDictionary"]["numberQoI"]
47-
# combined estimators
48-
monteCarloIndexInputDictionary["combinedEstimator"] = [monteCarloIndexInputDictionary["combinedEstimator"][0] for _ in range (0,parameters["solverWrapperInputDictionary"]["numberCombinedQoi"])]
49-
monteCarloIndexInputDictionary["combinedEstimatorInputDictionary"] = [combinedEstimatorInputDictionary]*parameters["solverWrapperInputDictionary"]["numberCombinedQoi"]
50-
# cost estimator
51-
monteCarloIndexInputDictionary["costEstimatorInputDictionary"] = costEstimatorInputDictionary
52-
5340
# MonoCriterion
5441
criteriaArray = []
5542
criteriaInputs = []
@@ -153,25 +140,20 @@
153140
qoi_dict["qoi_id_"+str(qoi_counter)]["index_"+str(index)] = {"qoi_id":qoi_counter, "index": index, "instances": sample_counter, "S1": S1, "S2": S2, "S3": S3, "S4": S4, "S5": S5, "S6": S6, "S7": S7, "S8": S8, "S9": S9, "S10": S10, "h1": h1, "h2": h2,"type":"scalar_quantity","tag":"lift_coefficient"}
154141

155142
# save pressure coefficient
143+
qoi_counter = qoi_counter + 1
144+
qoi_dict["qoi_id_"+str(qoi_counter)] = {"member_"+str(member): {} for member in range (algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter]._variableDimension)}
145+
member = 0
156146
for node in current_model.GetModelPart(model_part_of_interest).Nodes:
157-
qoi_counter = qoi_counter + 1
158-
qoi_dict["qoi_id_"+str(qoi_counter)] = {"index_"+str(index): {} for index in range (len(algo.monteCarloSampler.indices))}
147+
qoi_dict["qoi_id_"+str(qoi_counter)]["member_"+str(member)] = {"index_"+str(index): {} for index in range (len(algo.monteCarloSampler.indices))}
159148
for index in range (len(algo.monteCarloSampler.indices)):
160149
algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter] = get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter])
161150
sample_counter = algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter]._sampleCounter
162-
S1 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[0][0]))
163-
S2 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[1][0]))
164-
S3 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[2][0]))
165-
S4 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[3][0]))
166-
S5 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[4][0]))
167-
S6 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[5][0]))
168-
S7 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[6][0]))
169-
S8 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[7][0]))
170-
S9 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[8][0]))
171-
S10 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter].powerSums[9][0]))
151+
S1 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter]._powerSums["1"][member]))
152+
S2 = float(get_value_from_remote(algo.monteCarloSampler.indices[index].qoiEstimator[qoi_counter]._powerSums["2"][member]))
172153
h1 = float(get_value_from_remote(mdccm.computeCentralMomentsOrderOneDimensionZero(S1,sample_counter)))
173154
h2 = float(get_value_from_remote(mdccm.computeCentralMomentsOrderTwoDimensionZero(S1,S2,sample_counter)))
174-
qoi_dict["qoi_id_"+str(qoi_counter)]["index_"+str(index)] = {"qoi_id":qoi_counter, "index": index, "instances": sample_counter, "S1": S1, "S2": S2, "S3": S3, "S4": S4, "S5": S5, "S6": S6, "S7": S7, "S8": S8, "S9": S9, "S10": S10, "h1": h1, "h2": h2,"type":"scalar_quantity","tag":"pressure coefficent","node_id":node.Id,"node_coordinates":[node.X,node.Y,node.Z]}
155+
qoi_dict["qoi_id_"+str(qoi_counter)]["member_"+str(member)]["index_"+str(index)] = {"qoi_id":qoi_counter, "member":member, "index": index, "instances": sample_counter, "S1": S1, "S2": S2, "S3": S3, "S4": S4, "S5": S5, "S6": S6, "S7": S7, "S8": S8, "S9": S9, "S10": S10, "h1": h1, "h2": h2,"type":"scalar_quantity","tag":"pressure coefficent","node_id":node.Id,"node_coordinates":[node.X,node.Y,node.Z]}
156+
member += 1
175157

176158
# save to file
177159
with open('power_sums_outputs/MC_asynchronous_power_sums_' +str(time.time()) + '.json', 'w') as f:

0 commit comments

Comments
 (0)