Esempio n. 1
0
    def test_update(self):
        # dimension = 0
        dimension = 0 # len(samples) = 2**dimension
        Q=2.0
        list_values = [[[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],10],[[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],10],[[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],10],[[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],10],[[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],10],[[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],[10*Q],[10*Q*Q],10]]

        true_power_sums = [[120.0],[240.0],[120.0],[240.0],[120.0],[240.0],[120.0],[240.0],[120.0],[240.0]]

        # read parameters
        parametersPath = "parameters/parameters_test_combinedMomentEstimator.json"
        with open(parametersPath,'r') as parameter_file:
            parameters = json.load(parameter_file)

        for order in [1,5]:
            parameters["momentEstimatorInpuctDict"]["order"] = order
            # build momentEstimator class
            test_me = me.CombinedMomentEstimator(**parameters["momentEstimatorInpuctDict"])

            # update power sums
            for i in range (len(list_values)):
                test_me.update([[list_values[i]]])

            # test update sample number
            self.assertEqual(test_me._sampleCounter,60)

            # test update power sums
            for i in range (2*test_me.order):
                self.assertEqual(test_me.powerSums[i], true_power_sums[i])

            S1 = test_me.powerSums[0][0]
            S2 = test_me.powerSums[1][0]
            h1 = mdccm.computeCentralMomentsOrderOneDimensionZero(S1,test_me._sampleCounter)
            h2 = mdccm.computeCentralMomentsOrderTwoDimensionZeroBiased(S1,S2,test_me._sampleCounter)

            self.assertEqual(h1,2.0)
            self.assertEqual(h2,0.0)
Esempio n. 2
0
    def test_update(self):
        Q = 2.0
        list_values = [
            [
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                10,
            ],
            [
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                10,
            ],
            [
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                10,
            ],
            [
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                10,
            ],
            [
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                10,
            ],
            [
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                [10 * Q],
                [10 * Q * Q],
                10,
            ],
        ]

        true_power_sums = [
            [120.0],
            [240.0],
            [120.0],
            [240.0],
            [120.0],
            [240.0],
            [120.0],
            [240.0],
            [120.0],
            [240.0],
        ]

        # read parameters
        with open("parameters/parameters_test_combinedMomentEstimator.json",
                  "r") as parameter_file:
            parameters = load(parameter_file)

        for order in [1, 5]:
            parameters["momentEstimatorInputDict"]["order"] = order
            # build momentEstimator class
            test_me = CombinedMomentEstimator(
                **parameters["momentEstimatorInputDict"])

            # update power sums
            for value in list_values:
                test_me.update([[value]])

            # test update sample number
            self.assertEqual(test_me._sampleCounter, 60)

            # test update power sums
            for i in range(2 * test_me.order):
                self.assertEqual(test_me.powerSums[i], true_power_sums[i])

            S1 = test_me.powerSums[0][0]
            S2 = test_me.powerSums[1][0]
            h1 = mdccm.computeCentralMomentsOrderOneDimensionZero(
                S1, test_me._sampleCounter)
            h2 = mdccm.computeCentralMomentsOrderTwoDimensionZeroBiased(
                S1, S2, test_me._sampleCounter)

            self.assertEqual(h1, 2.0)
            self.assertEqual(h2, 0.0)
Esempio n. 3
0
    def mpi_test_mlmc_Kratos_ParMmg(self):

        # read parameters
        parametersList = [
            "problem_settings/parameters_xmc_asynchronous_mlmc_SAR.json",
            "problem_settings/parameters_xmc_asynchronous_mlmc_DAR.json",
        ]

        with WorkFolderScope("caarc_wind_mpi/", __file__, add_to_path=True):
            for parametersPath in parametersList:
                with open(parametersPath, "r") as parameter_file:
                    parameters = json.load(parameter_file)
                # SolverWrapper
                parameters["solverWrapperInputDictionary"][
                    "qoiEstimator"] = parameters[
                        "monteCarloIndexInputDictionary"]["qoiEstimator"]
                # SampleGenerator
                samplerInputDictionary = parameters["samplerInputDictionary"]
                samplerInputDictionary[
                    "randomGeneratorInputDictionary"] = parameters[
                        "randomGeneratorInputDictionary"]
                samplerInputDictionary[
                    "solverWrapperInputDictionary"] = parameters[
                        "solverWrapperInputDictionary"]
                # MonteCarloIndex Constructor
                monteCarloIndexInputDictionary = parameters[
                    "monteCarloIndexInputDictionary"]
                monteCarloIndexInputDictionary[
                    "samplerInputDictionary"] = samplerInputDictionary
                # MonoCriterion
                criteriaArray = []
                criteriaInputs = []
                for monoCriterion in parameters["monoCriteriaInputDictionary"]:
                    criteriaArray.append(
                        xmc.monoCriterion.MonoCriterion(
                            parameters["monoCriteriaInputDictionary"]
                            [monoCriterion]["criteria"],
                            parameters["monoCriteriaInputDictionary"]
                            [monoCriterion]["tolerance"],
                        ))
                    criteriaInputs.append([
                        parameters["monoCriteriaInputDictionary"]
                        [monoCriterion]["input"]
                    ])
                # MultiCriterion
                multiCriterionInputDictionary = parameters[
                    "multiCriterionInputDictionary"]
                multiCriterionInputDictionary["criteria"] = criteriaArray
                multiCriterionInputDictionary[
                    "inputsForCriterion"] = criteriaInputs
                criterion = xmc.multiCriterion.MultiCriterion(
                    **multiCriterionInputDictionary)
                # ErrorEstimator
                MSEErrorEstimator = xmc.errorEstimator.ErrorEstimator(
                    **parameters["errorEstimatorInputDictionary"])
                # HierarchyOptimiser
                hierarchyCostOptimiser = xmc.hierarchyOptimiser.HierarchyOptimiser(
                    **parameters["hierarchyOptimiserInputDictionary"])
                # MonteCarloSampler
                monteCarloSamplerInputDictionary = parameters[
                    "monteCarloSamplerInputDictionary"]
                monteCarloSamplerInputDictionary[
                    "indexConstructorDictionary"] = monteCarloIndexInputDictionary
                monteCarloSamplerInputDictionary["errorEstimators"] = [
                    MSEErrorEstimator
                ]
                # EstimationAssembler
                monteCarloSamplerInputDictionary["assemblers"] = []
                for key, dicArgs in parameters[
                        "estimationAssemblerInputDictionary"].items():
                    monteCarloSamplerInputDictionary["assemblers"].append(
                        xmc.estimationAssembler.EstimationAssembler(**dicArgs))
                mcSampler = xmc.monteCarloSampler.MonteCarloSampler(
                    **monteCarloSamplerInputDictionary)
                # XMCAlgorithm
                XMCAlgorithmInputDictionary = parameters[
                    "XMCAlgorithmInputDictionary"]
                XMCAlgorithmInputDictionary["monteCarloSampler"] = mcSampler
                XMCAlgorithmInputDictionary[
                    "hierarchyOptimiser"] = hierarchyCostOptimiser
                XMCAlgorithmInputDictionary["stoppingCriterion"] = criterion

                algo = xmc.XMCAlgorithm(**XMCAlgorithmInputDictionary)

                if parameters["solverWrapperInputDictionary"][
                        "asynchronous"] is True:
                    algo.runAsynchronousXMC()
                else:
                    algo.runXMC()

                # test
                # such results are not accurate, since we run the problem for few decimals
                # and coarse meshes instead of hundreds of seconds and finer meshes
                estimations = get_value_from_remote(algo.estimation())
                self.assertGreater(sum(estimations), 0)
                for level in algo.hierarchy():
                    self.assertEqual(level[1], 5)
                # check moment estimator - level 0
                sample_counter = (algo.monteCarloSampler.indices[0].
                                  qoiEstimator[0]._sampleCounter)
                S1 = get_value_from_remote(algo.monteCarloSampler.indices[0].
                                           qoiEstimator[0].powerSums[0][0])
                h1 = get_value_from_remote(
                    ccm.computeCentralMomentsOrderOneDimensionZero(
                        S1, sample_counter))
                self.assertGreater(-h1, 0)
                self.assertEqual(sample_counter, 5)
                # check multi moment estimator - level 1
                sample_counter = (algo.monteCarloSampler.indices[1].
                                  qoiEstimator[1]._sampleCounter)
                self.assertEqual(sample_counter, 5)
                # check combined moment estimator - level 2
                sample_counter = (algo.monteCarloSampler.indices[2].
                                  qoiEstimator[2]._sampleCounter)
                S1 = get_value_from_remote(algo.monteCarloSampler.indices[2].
                                           qoiEstimator[2].powerSums[0][0])
                h1 = get_value_from_remote(
                    ccm.computeCentralMomentsOrderOneDimensionZero(
                        S1, sample_counter))
                self.assertEqual(sample_counter, 5)
                # check multi combined moment estimator - level 2
                sample_counter = (algo.monteCarloSampler.indices[2].
                                  qoiEstimator[3]._sampleCounter)
                self.assertEqual(sample_counter, 5)