Ejemplo n.º 1
0
def experiment(P, opt, expname):
    # First run with regular refinement
    opt["meshRefinement"] = 1
    df_uniform = solveProblem(P, opt)
    if WRITE_CSV:
        hlp.writeOutputToCsv(df_uniform, opt, opt['id'] + expname + '_uniform',
                             df_uniform)
def experiment(P, opt, expname):
    # First run with regular refinement
    opt["meshRefinement"] = 1
    df_uniform = solveProblem(P, opt)
    if WRITE_CSV:
        fname = '{}_{}_uniform'.format(opt['id'], expname)
        hlp.writeOutputToCsv(df_uniform, opt, fname)
Ejemplo n.º 3
0
def degreeStudy(P, opt, minDegree=1, maxDegree=4, filename=None):

    out = []

    for deg in range(minDegree, maxDegree + 1):
        opt["p"] = deg
        opt["q"] = deg
        df = solveProblem(P, opt)
        out.append(df)
        if WRITE_CSV:
            fname = '{}_degree_study_{}_deg_{}'.format(opt['id'], filename,
                                                       deg)
            hlp.writeOutputToCsv(df, opt, fname)

    return out
Ejemplo n.º 4
0
def experiment(P, opt, expname):
    # First run with regular refinement
    opt["meshRefinement"] = 1
    df_uniform = solveProblem(P, opt)
    if WRITE_CSV:
        fname = '{}_{}_uniform'.format(opt['id'], expname)
        hlp.writeOutputToCsv(df_uniform, opt, fname)

    # Second run with adaptive refinement
    opt["meshRefinement"] = 2
    opt["refinementThreshold"] = 0.9
    df_adaptive = solveProblem(P, opt)
    if WRITE_CSV:
        fname = '{}_{}_adaptive'.format(opt['id'], expname)
        hlp.writeOutputToCsv(df_adaptive, opt, fname)
def experiment(P, opt, expname):
    # First run with regular refinement
    opt["meshRefinement"]    = 1
    df_uniform = solveProblem(P, opt)
    if WRITE_CSV:
        hlp.writeOutputToCsv(CSV_DIR + opt['id'] + expname + '_uniform.csv', df_uniform)

    # Second run with adaptive refinement
    opt["meshRefinement"]      = 2
    opt["refinementThreshold"] = 0.95
    opt["plotMesh"] = 1
    opt["saveMesh"] = 1
    opt["plotSolution"] = 1
    df_adaptive = solveProblem(P, opt)
    if WRITE_CSV:
        hlp.writeOutputToCsv(CSV_DIR + opt['id'] + expname + '_adaptive.csv', df_adaptive)
Ejemplo n.º 6
0
    dg1stab = degreeStudy(P, hlp.opt_Own_DG_1_stab(global_opt), minDegree,
                          maxDegree, 'dg1stab')

    # Set up one large dataframe to store relevant output
    dfs = []
    keys = [
        'L2_error', 'H1_error', 'H2_error', 'H2h_error', 'L2_eoc', 'H1_eoc',
        'H2_eoc', 'H2h_eoc'
    ]

    for deg in range(maxDegree):
        df = pd.DataFrame(columns=['Ndofs', 'hmax'], dtype=float)

        df['Ndofs'] = Neilan[deg]['Ndofs']
        df['hmax'] = Neilan[deg]['hmax']
        for key in keys:
            df['neilan_' + key] = Neilan[deg][key]
            df['nsz_' + key] = NSZ[deg][key]
            df['cg0stab_' + key] = cg0stab[deg][key]
            df['cg1stab_' + key] = cg1stab[deg][key]
            df['cg2stab_' + key] = cg2stab[deg][key]
            df['dg0stab_' + key] = dg0stab[deg][key]
            df['dg1stab_' + key] = dg1stab[deg][key]

        dfs.append(df)

        if WRITE_CSV:
            fname = global_opt['id'] + '_deg_{}'.format(deg + 1)
            hlp.writeOutputToCsv(df, global_opt, fname)
Ejemplo n.º 7
0
    opt["NdofsThreshold"] = 70000
    # Don't write all the information
    opt["writeToCsv"] = 0
    # Set tolerance to 10^-8
    opt["gmresTolRes"] = 1e-8
    opt["gmresWarmStart"] = False
    # Prepare dataframe for results
    out = pd.DataFrame()

    # First experiment: Without stabilization
    for i, kappa in enumerate(kappa_list):
        for j, eta in enumerate(eta_list):
            opt["stabilizationFlag"] = 0 if eta < 1e-15 else 1
            opt["stabilizationConstant1"] = eta
            opt["stabilizationConstant2"] = 0
            opt["id"] = "Cinfty_eta_{}_kappa_{}_".format(eta, kappa)
            P = Cinfty(kappa)
            df = solveProblem(P, opt)
            if i == 0 and j == 0:
                out['h'] = [
                    '2^{{-{}}}'.format(j + 3) for j in range(df.shape[0])
                ]
            out['eta_{}_kappa_{}'.format(
                eta, kappa)] = df.loc[:, 'N_iter'].astype('int')

    # Write csv file
    if WRITE_CSV:
        opt['id'] = 'gmres_iter'
        writeOutputToCsv(out, opt)
    print(out)
Ejemplo n.º 8
0
def solveProblem(P, opt):

    set_log_level(opt['dolfinLogLevel'])

    # parameters["reorder_dofs_serial"] = False
    # parameters['form_compiler']['quadrature_rule'] = 'vertex'
    parameters["form_compiler"]["quadrature_degree"] = 15
    # parameters['ghost_mode'] = 'shared_facet'
    parameters['krylov_solver']['absolute_tolerance'] = 1e-10
    parameters['krylov_solver']['relative_tolerance'] = 1e-10
    parameters['krylov_solver']['maximum_iterations'] = 1000
    parameters['krylov_solver']['monitor_convergence'] = True

    df = hlp.initDataframe()

    if opt["meshRefinement"]:
        n_range = range(opt["numberMeshLevels"])
    else:
        n_range = [opt["initialMeshResolution"]]

    print('-' * 50 + '\n')

    n = opt["initialMeshResolution"]

    # Initialize mesh
    P.initMesh(n)
    P.meshLevel = 1

    for (k, n) in enumerate(n_range):

        # updateFunctionSpaces also, since it depends on the mesh
        P.updateFunctionSpaces(opt)

        if P.getType().find('HJB') > -1:
            # initControl has to be run on each mesh once
            P.initControl()

        # update Coefficients needs control, therefore after initControl
        P.updateCoefficients()
        # checkVariables sets things like hasSolution, hasDrift etc.
        P.checkVariables()

        h = P.mesh.hmax()
        ndofs = P.solDofs(opt)
        ndofsMixed = P.totalDofs(opt)

        if k == 0:
            print(hlp.getSummary(P, opt))

        print("Solve problem with %d (%d) dofs." % (ndofs, ndofsMixed))

        if opt["printCordesInfo"]:
            P.printCordesInfo()

        N_iter = P.solve(opt)

        # Save solution as uold
        P.uold = P.u.copy()

        P.doPlots(opt)

        df.loc[k, ['hmax', 'Ndofs', 'NdofsMixed', 'N_iter']] = (h, ndofs,
                                                                ndofsMixed,
                                                                N_iter)

        # Check error estimation option during the first iteration
        if k == 0 and not P.hasSolution and opt["errorEstimationMethod"] == 1:
            print("""WARNING: Switch to errorEstimationMethod 2;
            no explicit solution is available.""")

            opt["errorEstimationMethod"] = 2

        if k == 0 and opt["errorEstimationMethod"] == 2:
            # Initialize list to store all solutions
            U = []

        if isinstance(P, PNVP.PNVP_WorstOfTwoAssetsPut):
            if k == 0:
                qoi_val_list = []
            qoi_val = P.u([40, 40])
            qoi_val_list.append(qoi_val)

            print('--------------------------------------------------\n')
            print('Value at qoi: %10.8f\n' % qoi_val)
            print('--------------------------------------------------\n')

        # Method 1: Determine norms of residual on current mesh
        if opt["errorEstimationMethod"] == 1:

            # Expression for difference between discrete
            # and analytical solution
            if hasattr(P, 'loc'):
                u_diff = (P.u - P.u_) * P.loc
            else:
                u_diff = P.u - P.u_
            # Determine error norms
            l2err = L2_norm(u_diff)
            h1semierr = H10_norm(u_diff)
            h2semierr = H20_norm(u_diff)
            jumperr = EdgeJump_norm(u_diff, P.hE, P.nE)

            # Write errors to dataframe
            df.loc[k, 'L2_error'] = l2err
            df.loc[k, 'H1_error'] = h1semierr
            df.loc[k, 'H2_error'] = h2semierr
            df.loc[k, 'EdgeJump_error'] = jumperr
            df.loc[k,
                   'H2h_error'] = np.sqrt(pow(h2semierr, 2) + pow(jumperr, 2))

            # Method 2: Collect the current solution,
            # compute the norms of residual on finest mesh
        if opt["errorEstimationMethod"] == 2:
            uerr = Function(P.V)
            assign(uerr, P.u)
            U.append(uerr)

        if opt["plotSolution"]:
            P.plotSolution()

        # Determine error estimates
        print('WARNING: Introduce flag to estimate errors')
        print('Estimate errors')
        eta = P.determineErrorEstimates(opt)
        eta_est = np.sum(np.power(eta, 2))
        df.loc[k, 'Eta_global'] = eta_est

        # Mesh refinement
        if opt["meshRefinement"] > 0:
            if ndofs < opt["NdofsThreshold"]:
                P.meshLevel += 1
                # Refine mesh depending on the strategy
                if opt["meshRefinement"] == 1:
                    print('Refine mesh uniformly')
                    P.refineMesh()

                elif opt["meshRefinement"] == 2:
                    print("Refine mesh adaptively")
                    cell_markers = P.markCells(opt, eta)
                    P.refineMesh(cell_markers)

            else:
                print("Reached maximum number of dofs")
                break

        if opt["holdOn"]:
            inp = input('Press enter to continue, "x" to abort: ')

            if inp == 'x':
                break

    if opt["errorEstimationMethod"] == 2:
        """ This method computes the error norms all on the finest mesh.
        Since the errornorm command is not available for the H^2_h norm,
        we compute the respective norms directly.
        """

        u_exact = P.u_ if P.hasSolution else interpolate(P.u, P.V)

        for (k, u) in enumerate(U):

            print("Compute errors on mesh level %i / %i" % (k + 1, len(U)))

            # Interpolate u_k on the finest mesh
            u = interpolate(u, P.V)
            if hasattr(P, 'loc'):
                u_diff = (u - u_exact) * P.loc
            else:
                u_diff = u - u_exact

            # Determine norms of residual
            EdgeJumperr = EdgeJump_norm(u_diff, P.hE, P.nE)
            L2err = L2_norm(u_diff)
            H1err = H10_norm(u_diff)
            H2err = H20_norm(u_diff)
            H2herr = np.sqrt(pow(H2err, 2) + pow(EdgeJumperr, 2))

            # Write errors to dataframe
            df.loc[k, 'L2_error'] = L2err
            df.loc[k, 'H1_error'] = H1err
            df.loc[k, 'H2_error'] = H2err
            df.loc[k, 'H2h_error'] = H2herr
            df.loc[k, 'EdgeJump_error'] = EdgeJumperr

    if len(n_range) > 1:
        df = hlp.determineEOCforDataframe(P.dim(), df)

        if opt["plotConvergenceRates"]:
            hlp.plotConvergenceRates(df)

    if isinstance(P, PNVP.PNVP_WorstOfTwoAssetsPut):
        df['qoi_val'] = qoi_val_list

    if opt["writeToCsv"]:
        hlp.writeOutputToCsv(df, opt)

    # xf = XDMFFile("mesh.xdmf")
    # xf.write(P.u)
    return df
Ejemplo n.º 9
0
        # Threshold for dofs
        opt["NdofsThreshold"] = 50000
        opt["initialMeshResolution"] = 10
        opt["timeSteps"] = 10
        opt["timeStepFactor"] = 2

        # Fix polynomial degree
        opt["p"] = 2
        opt["q"] = 2

        opt["plotSolution"] = 0
        opt["plotErrorEstimates"] = 0
        opt["plotConvergenceRates"] = 0
        opt["plotMesh"] = 0
        opt["meshRefinement"] = 1  # Uniform refinement
        opt["stabilizationFlag"] = 1
        # Stability constant for first-order term
        opt["stabilityConstant1"] = 2
        # Stability constant for second-order term
        opt["stabilityConstant2"] = 0
        opt["solutionMethod"] = 'NeilanSalgadoZhang'

        df = solveProblem(P, opt)

        # Determine method to estimate error norms
        opt["errorEstimationMethod"] = 1

        if WRITE_CSV:
            hlp.writeOutputToCsv(df, opt, opt['id'])