예제 #1
0
                        'mesh2d/terminusdriven{0:04d}.pvtu.tar.gz'.format(i))):
                os.chdir(DIRM + "mesh2d")
                os.system('tar -czf terminusdriven{0:04d}.pvtu.tar.gz '.format(i)+\
                        'terminusdriven*{0:04d}.'.format(i)+'*vtu')
                os.system('rm terminusdriven*{0:04d}.'.format(i) + '*vtu')
                os.chdir(DIRM)

    job = sched.add_job(check_files, 'interval', minutes=5)
    sched.start()

###################
# Run Elmersolver #
###################

returncode = elmerrunlib.run_elmer(DIRM + solverfile_out + '.sif',
                                   n=partitions,
                                   email=True)

if solverfile_in.startswith('terminusdriven') or (solverfile_in
                                                  == 'checkmeshes'):

    job.remove()
    print "Stopped unzipping/zipping mesh files and vtu."

    # Remove all remaining mesh and tar remaining vtu files
    os.chdir(DIRM)
    for i in range(max(1, restartposition), int(nt) + 25):
        if os.path.isdir('mesh{0:04d}'.format(i)):
            os.system('rm -r ' + 'mesh{0:04d}'.format(i))
        if os.path.isfile('mesh2d/terminusdriven{0:04d}.pvtu'.format(i)):
            os.chdir(DIRM + "mesh2d")
예제 #2
0
def main():

    ##########
    # inputs #
    ##########

    args = get_arguments()

    RES = args.mesh
    partitions = str(args.n)
    regpar = str(args.regpar)
    frontbc = str(args.frontbc)
    glacier = args.glacier
    restartfile = args.restartfile
    restartposition = args.restartposition
    temperature = args.temperature
    itmax = args.itmax

    # Directories
    DIRS = os.path.join(os.getenv("CODE_HOME"),
                        "big3/modeling/solverfiles/ssa/")
    DIRM = os.path.join(os.getenv("MODEL_HOME"), glacier + "/3D/" + RES + "/")
    DIRR = os.path.join(DIRM + 'mesh2d/inversion_adjoint/')
    inputs = os.path.join(DIRM + "/inputs/")

    extent = np.loadtxt(inputs + "mesh_extent.dat")
    try:
        hole1 = np.loadtxt(inputs + "mesh_hole1.dat")
        hole2 = np.loadtxt(inputs + "mesh_hole2.dat")
        holes = [hole1, hole2]
    except:
        holes = []

    if not (os.path.exists(DIRR)):
        os.makedirs(DIRR)

    # Boundary numbers
    bbed = 4
    bsurf = 5
    runname = "adjoint_beta_ssa"

    if temperature == 'model':
        temperature_text = """
  mu = Variable Coordinate 1, Coordinate 2\r
    Real Procedure "USF_Init.so" "SSAViscosity""" ""
    else:
        try:
            print float(temperature)
            A = flowparameterlib.arrhenius(273.15 + float(temperature))
            E = 3
            temperature_text = """
  mu = Real $((""" + '2*' + str(E) + '*' + str(
                A[0]) + '*' + 'yearinsec)^(-1.0/3.0)*1.0e-6)'
        except:
            sys.exit("Unknown temperature of " + temperature)

    if frontbc == 'velocity' or frontbc == 'dirichlet':
        frontbc_text = """
  SSAVelocity 1= Equals vsurfini 1\r
  SSAVelocity 2= Equals vsurfini 2\r
  Adjoint 1 = Real 0.0\r
  Adjoint 2 = Real 0.0"""
    elif frontbc == 'pressure' or frontbc == 'neumann':
        frontbc_text = """
  Calving Front = Logical True"""

    ################################################
    # Compile fortran libraries for adjoint solver #
    ################################################

    # Check that fortran files are compiled
    DIRELMERLIB = os.path.join(os.getenv("CODE_HOME"),
                               "big3/modeling/elmerlib/")
    readfiles = os.listdir(DIRELMERLIB + "AdjointSSA/")
    outputfile = DIRELMERLIB + "AdjointSSASolvers.so"

    allstring = ''
    for f in readfiles:
        if f.endswith('.F90'):
            allstring = allstring + f + ' '

    os.chdir(DIRELMERLIB + "AdjointSSA/")
    call(["elmerf90", "-o", outputfile, allstring])
    del DIRELMERLIB

    #############################
    # Run inversion solver file #
    #############################

    if not (os.path.exists(DIRR + "summary.dat")):
        fid_info = open(DIRR + "summary.dat", "w")
        fid_info.write('Lambda Nsim Cost Norm RelPrec_G \n')

    if restartfile != 'none':
        solverfile = restartfile
        date = restartfile[-12:-4]

        # Move previous timesteps to output directory
        DIRR_lambda = DIRR + "lambda_" + regpar + "_" + date + "/"
        names = os.listdir(DIRM + "/mesh2d")
        if not os.path.exists(DIRR_lambda):
            os.makedirs(DIRR_lambda)
        for name in names:
            if name.endswith('vtu') and name.startswith('adjoint'):
                os.rename(DIRM + "/mesh2d/" + name, DIRR_lambda + name)
    else:
        # Get current date
        now = datetime.datetime.now()
        date = '{0}{1:02.0f}{2:02.0f}'.format((now.year), (now.month),
                                              (now.day))
        restartposition = 0

        solverfile = 'adjoint_beta_' + regpar + '_' + date + '.sif'
        os.chdir(DIRM)
        fid1 = open(DIRS + 'adjoint_beta_ssa.sif', 'r')
        fid2 = open(DIRM + solverfile, 'w')

        lines = fid1.read()
        lines = lines.replace('{Lambda}', '{0}'.format(regpar))
        lines = lines.replace('{ItMax}', '{0}'.format(int(itmax)))
        lines = lines.replace('{Temperature}', '{0}'.format(temperature_text))
        lines = lines.replace('{FrontBC}', '{0}'.format(frontbc_text))
        fid2.write(lines)
        fid1.close()
        fid2.close()
        del fid1, fid2

    returncode = elmerrunlib.run_elmer(DIRM + solverfile, n=partitions)

    #####################################
    # Write cost values to summary file #
    #####################################

    fidcost = open(DIRM + "cost.dat", "r")
    lines = fidcost.readlines()
    line = lines[-1]
    p = line.split()
    nsim = float(p[0])
    costsur = float(p[1])
    fidcost.close()

    fidcostreg = open(DIRM + "costreg.dat")
    lines = fidcostreg.readlines()
    line = lines[-1]
    p = line.split()
    nsim = float(p[0])
    costbed = float(p[1])  #/float(regpar)
    fidcostreg.close()

    costtot = costsur + float(regpar) * costbed

    fid_info = open(DIRR + "summary.dat", "a")
    fid_info.write('{} {} {} {} {}\n'.format(regpar, nsim + restartposition,
                                             costtot, costsur, costbed))
    fid_info.close()
    del fidcost, fidcostreg

    #######################################
    # Combine elmer results into one file #
    #######################################

    DIRR_lambda = DIRR + "lambda_" + regpar + "_" + date + "/"

    names = os.listdir(DIRM + "/mesh2d")
    if not os.path.exists(DIRR_lambda):
        os.makedirs(DIRR_lambda)
    for name in names:
        if name.endswith('pvtu') and name.startswith('adjoint'):
            os.rename(
                DIRM + "/mesh2d/" + name, DIRR_lambda + '{0}{1:04d}{2}'.format(
                    name[0:-9],
                    int(name[-9:-5]) + restartposition, '.pvtu'))
        elif name.endswith('vtu') and name.startswith('adjoint'):
            os.rename(
                DIRM + "/mesh2d/" + name, DIRR_lambda + '{0}{1:04d}{2}'.format(
                    name[0:-8],
                    int(name[-8:-4]) + restartposition, '.vtu'))
        elif name.startswith('adjoint') and 'result' in name:
            os.rename(DIRM + "/mesh2d/" + name, DIRR_lambda + name)

    bed = elmerreadlib.pvtu_file(DIRR_lambda + 'adjoint_beta_ssa0001.pvtu',
                                 ['ssavelocity', 'beta', 'vsurfini'])

    # Move outputs for optimization
    os.rename(DIRM + "M1QN3_adjoint_beta_ssa.out",
              DIRR_lambda + "M1QN3_adjoint_beta_ssa.out")
    os.rename(DIRM + "cost.dat", DIRR_lambda + "cost.dat")
    os.rename(DIRM + "costreg.dat", DIRR_lambda + "costreg.dat")
    os.rename(DIRM + "gradientnormadjoint_adjoint_beta_ssa.dat",
              DIRR_lambda + "gradientnormadjoint_adjoint_beta_ssa.dat")

    ################################
    # Output friction coefficients #
    ################################

    # Gridded linear beta square
    x, y, u = elmerreadlib.input_file(inputs + "udem.xy", dim=2)
    xx, yy = np.meshgrid(x, y)
    beta_linear = scipy.interpolate.griddata((bed['x'],bed['y']),bed['beta']**2,(xx,yy),\
        method='nearest')
    beta_linear_lin = scipy.interpolate.griddata((bed['x'],bed['y']),bed['beta']**2,(xx,yy),\
        method='linear')
    beta_weertman_lin = scipy.interpolate.griddata((bed['x'],bed['y']),\
        (bed['beta']**2)/(bed['ssavelocity']**(-2.0/3.0)), (xx,yy), method='linear')
    beta_weertman = scipy.interpolate.griddata((bed['x'],bed['y']),\
        (bed['beta']**2)*(bed['ssavelocity']**(-2.0/3.0)),(xx,yy),method='nearest')
    ind = np.where(~(np.isnan(beta_linear_lin)))
    beta_linear[ind] = beta_linear_lin[ind]
    ind = np.where(~(np.isnan(beta_weertman_lin)))
    beta_weertman[ind] = beta_weertman_lin[ind]
    del beta_linear_lin, beta_weertman_lin

    # Output original results
    fidr = open(inputs + "beta_linear_" + regpar + "_SSA_" + RES + ".dat", 'w')
    fidw = open(inputs + "beta_weertman_" + regpar + "_SSA_" + RES + ".dat",
                'w')
    fidt = open(inputs + "taub_" + regpar + "_SSA_" + RES + ".dat", 'w')
    fidr.write('{}\n'.format(len(bed['x'])))
    fidw.write('{}\n'.format(len(bed['x'])))
    fidt.write('{}\n'.format(len(bed['x'])))
    for i in range(0, len(bed['x'])):
        fidr.write('{0} {1} {2:.16f}\n'.format(bed['x'][i], bed['y'][i],
                                               bed['beta'][i]**2))
        fidw.write('{0} {1} {2:.16f}\n'.format(bed['x'][i],bed['y'][i],\
                (bed['beta'][i]**2)/(bed['ssavelocity'][i]**(-2.0/3.0))))
        fidt.write('{0} {1} {2:.16f}\n'.format(bed['x'][i], bed['y'][i],
                                               bed['taub'][i] * 1e3))
    fidr.close()
    fidw.close()
    fidt.close()
    del fidr, fidw, fidt

    fidl = open(inputs + "beta_linear.xy", 'w')
    fidw = open(inputs + "beta_weertman.xy", 'w')
    fidl.write('{}\n{}\n'.format(len(x), len(y)))
    fidw.write('{}\n{}\n'.format(len(x), len(y)))
    for i in range(0, len(x)):
        for j in range(0, len(y)):
            fidl.write('{0} {1} {2}\n'.format(x[i], y[j], beta_linear[j, i]))
            fidw.write('{0} {1} {2}\n'.format(x[i], y[j], beta_weertman[j, i]))
    fidl.close()
    fidw.close()
    del beta_linear, beta_weertman, u, x, y, fidl, fidw, xx, yy

    xgrid, ygrid, taubgrid = elmerreadlib.grid3d(bed, 'taub', holes, extent)
    xgrid, ygrid, vmodgrid = elmerreadlib.grid3d(bed, 'ssavelocity', holes,
                                                 extent)
    xgrid, ygrid, vmesgrid = elmerreadlib.grid3d(bed, 'vsurfini', holes,
                                                 extent)

    plt.figure(figsize=(6.5, 3))
    plt.subplot(121)
    plt.imshow(taubgrid * 1e3, origin='lower', clim=[0, 500])
    plt.xticks([])
    plt.yticks([])
    cb = plt.colorbar(ticks=np.arange(0, 600, 100))
    cb.ax.tick_params(labelsize=10)
    cb.set_label('Basal shear stress (kPa)')

    plt.subplot(122)
    plt.imshow(vmodgrid - vmesgrid,
               origin='lower',
               clim=[-200, 200],
               cmap='RdBu_r')
    plt.xticks([])
    plt.yticks([])
    cb = plt.colorbar(ticks=np.arange(-500, 600, 100))
    cb.ax.tick_params(labelsize=10)
    cb.set_label('Modeled-Measured (m/yr)')

    plt.tight_layout()

    plt.savefig(DIRR + 'lambda_' + regpar + '_' + date + '.pdf',
                format='PDF',
                dpi=400)
    plt.close()
예제 #3
0
def main():

    ##########
    # inputs #
    ##########

    args = get_arguments()

    RES = args.mesh
    partitions = str(args.n)
    regpar = str(args.regpar)
    method = args.method
    extrude = str(args.extrude)
    frontbc = str(args.frontbc)
    sidewallbc = str(args.sidewallbc)
    slipcoefficient = args.slipcoefficient
    glacier = args.glacier
    restartfile = args.restartfile
    restartposition = args.restartposition
    temperature = args.temperature
    itmax = args.itmax

    # Directories
    DIRS = os.path.join(os.getenv("CODE_HOME"),
                        "big3/modeling/solverfiles/3D/")
    DIRM = os.path.join(os.getenv("MODEL_HOME"), glacier + "/3D/" + RES + "/")
    DIRR = os.path.join(DIRM + 'mesh2d/inversion_' + method + '/')
    inputs = os.path.join(DIRM + "/inputs/")

    extent = np.loadtxt(inputs + "mesh_extent.dat")
    try:
        hole1 = np.loadtxt(inputs + "mesh_hole1.dat")
        hole2 = np.loadtxt(inputs + "mesh_hole2.dat")
        holes = [hole1, hole2]
    except:
        holes = []

    if not (os.path.exists(DIRR)):
        os.makedirs(DIRR)

    # Boundary numbers
    bbed = 4
    bsurf = 5
    runname = method + "_beta"

    # Grab boundary condition for front -- it will be different if we are using an actual
    # terminus position (neumann, pressure BC) or an outflow boundary (dirichlet, velocity BC)
    if method == 'adjoint':
        if (frontbc == 'neumann') or (frontbc == 'pressure'):
            frontbc_text = """
  Adjoint Force BC = Logical True
  Flow Force BC = Logical True
  External Pressure = Variable Coordinate 3 !we are in MPa units
  Real MATC "-1.0*waterpressure(tx)*1.0E-06" """
        elif (frontbc == 'dirichlet') or (frontbc == 'velocity'):
            frontbc_text = """
  Velocity 1 = Variable Coordinate 1
    Real procedure "USF_Init.so" "UIni"
  Velocity 2 = Variable Coordinate 1
    Real procedure "USF_Init.so" "VIni"
  ! Dirichlet BC => Dirichlet = 0 for Adjoint
  Adjoint 1 = Real 0.0
  Adjoint 2 = Real 0.0"""
        else:
            sys.exit("Unknown BC for front of glacier.")
    elif method == 'robin':
        if (frontbc == 'neumann') or (frontbc == 'pressure'):
            frontbc_text = """
  Flow Force BC = Logical True
  External Pressure = Variable Coordinate 3 !we are in MPa units
  Real MATC "-1.0*waterpressure(tx)*1.0E-06" """
        elif (frontbc == 'dirichlet') or (frontbc == 'velocity'):
            frontbc_text = """
  ! Dirichlet BCs
  Velocity 1 = Variable Coordinate 1, Coordinate 2
    Real procedure "USF_Init.so" "UIni"
  Velocity 2 = Variable Coordinate 1, Coordinate 2
    Real procedure "USF_Init.so" "VIni"
        
  ! Dirichlet BC => Same Dirichlet
  VeloD 1 = Variable Coordinate 1, Coordinate 2
    Real procedure "USF_Init.so" "UIni"
  VeloD 2 = Variable Coordinate 1, Coordinate 2
    Real procedure "USF_Init.so" "VIni" """

    # Set boundary condition for sidewalls (either measured velocity or slip coefficient).
    if method == 'adjoint':
        if sidewallbc == 'friction':
            sidewallbc_text = """
  Normal-Tangential Velocity = Logical True
  Normal-Tangential Adjoint = Logical True
  
  Adjoint Force BC = Logical True
  
  Velocity 1 = Real 0.0e0
  Adjoint 1 = Real 0.0e0
  
  Slip Coefficient 2 = Real """ + slipcoefficient + """
  Slip Coefficient 3 = Real """ + slipcoefficient
        elif sidewallbc == 'freeslip':
            sidewallbc_text = """
  Normal-Tangential Velocity = Logical True
  Normal-Tangential Adjoint = Logical True

  Adjoint Force BC = Logical True

  Velocity 1 = Real 0.0e0
  Adjoint 1 = Real 0.0e0"""
        elif sidewallbc == 'velocity':
            sidewallbc_text = """
  !! Dirichlet BC 
  Velocity 1 = Variable Coordinate 1
    Real procedure "USF_Init.so" "UWa"
  Velocity 2 = Variable Coordinate 1
    Real procedure "USF_Init.so" "VWa"
  
  ! Dirichlet BC => Dirichlet = 0 for Adjoint
  Adjoint 1 = Real 0.0
  Adjoint 2 = Real 0.0"""
        else:
            sys.exit("Unknown sidewall BC of " + sidewallbc)
    if method == 'robin':
        if sidewallbc == 'friction':
            sidewallbc_text = """
  Normal-Tangential Velocity = Logical True
  Normal-Tangential VeloD = Logical True

  Flow Force BC = Logical True

  Velocity 1 = Real 0.0
  VeloD 1 = Real 0.0e0
  Slip Coefficient 2 = Real """ + slipcoefficient + """
  Slip Coefficient 3 = Real """ + slipcoefficient
        elif sidewallbc == 'freeslip':
            sidewallbc_text = """
  Normal-Tangential Velocity = Logical True
  Normal-Tangential VeloD = Logical True

  Flow Force BC = Logical True

  Velocity 1 = Real 0.0
  VeloD 1 = Real 0.0e0"""
        elif sidewallbc == 'velocity':
            sidewallbc_text = """
  ! Dirichlet BCs
  Velocity 1 = Variable Coordinate 1
    Real procedure "USF_Init.so" "UWa"
  Velocity 2 = Variable Coordinate 1
    Real procedure "USF_Init.so" "VWa"

  ! Dirichlet BC => Same Dirichlet
  VeloD 1 = Variable Coordinate 1
    Real procedure "USF_Init.so" "UWa"
  VeloD 2 = Variable Coordinate 1
    Real procedure "USF_Init.so" "VWa" """
        else:
            sys.exit("Unknown sidewall BC of " + sidewallbc)

    if temperature == 'model':
        temperature_text = """
  Viscosity = Variable Coordinate 1, Coordinate 2
    Real Procedure "USF_Init.so" "ModelViscosity"
  Constant Temperature = Variable Coordinate 1, Coordinate 2
    Real Procedure "USF_Init.so" "ModelTemperature" """
    else:
        try:
            A = flowparameterlib.arrhenius(273.15 + float(temperature))
            E = 3
            temperature_text = """
  Viscosity = Real $((""" + '2*' + str(E) + '*' + str(
                A[0]) + '*' + 'yearinsec)^(-1.0/3.0)*1.0e-6)\r' + """
  Constant Temperature = Real """ + str(temperature)
        except:
            sys.exit("Unknown temperature of " + temperature)

    #############################
    # Run inversion solver file #
    #############################

    if not (os.path.exists(DIRR + "summary.dat")):
        fid_info = open(DIRR + "summary.dat", "w")
        fid_info.write('Lambda Nsim Cost Norm RelPrec_G \n')

    if restartfile != 'none':
        solverfile = restartfile
        date = restartfile[-12:-4]

        # Move previous timesteps to output directory
        DIRR_lambda = DIRR + "lambda_" + regpar + "_" + date + "/"
        names = os.listdir(DIRM + "/mesh2d")
        if not os.path.exists(DIRR_lambda):
            os.makedirs(DIRR_lambda)
        for name in names:
            if name.endswith('vtu') and name.startswith(method):
                os.rename(DIRM + "/mesh2d/" + name, DIRR_lambda + name)
        try:
            os.rename(
                DIRM + "M1QN3_" + method + "_beta.out",
                DIRR_lambda + "M1QN3_" + method + "_beta_beforerestart.out")
            os.rename(
                DIRM + "gradientnormadjoint_" + method + "_beta.dat",
                DIRR_lambda + "gradient_" + runname + "_beforerestart.dat")
            os.rename(DIRM + "cost_" + method + "_beta.dat",
                      DIRR_lambda + "cost_" + runname + "_beforerestart.dat")
        except:
            try:
                os.rename(
                    DIRR_lambda + "M1QN3_" + method + "_beta.out",
                    DIRR_lambda + "M1QN3_" + method +
                    "_beta_beforerestart.out")
                os.rename(
                    DIRR_lambda + "gradientnormadjoint_" + method +
                    "_beta.dat",
                    DIRR_lambda + "gradient_" + runname + "_beforerestart.dat")
                os.rename(
                    DIRR_lambda + "cost_" + method + "_beta.dat",
                    DIRR_lambda + "cost_" + runname + "_beforerestart.dat")
            except:
                pass

    else:
        # Get current date
        now = datetime.datetime.now()
        date = '{0}{1:02.0f}{2:02.0f}'.format((now.year), (now.month),
                                              (now.day))
        restartposition = 0

        solverfile = method + '_beta_' + regpar + '_' + date + '.sif'
        os.chdir(DIRM)
        fid1 = open(DIRS + method + '_beta_grounded.sif', 'r')
        fid2 = open(DIRM + solverfile, 'w')

        lines = fid1.read()
        lines = lines.replace('{Extrude}', '{0}'.format(extrude))
        lines = lines.replace('{Lambda}', '{0}'.format(regpar))
        lines = lines.replace('{FrontBC}', '{0}'.format(frontbc_text))
        lines = lines.replace('{SidewallBC}', '{0}'.format(sidewallbc_text))
        lines = lines.replace('{ItMax}', '{0}'.format(int(itmax)))
        lines = lines.replace('{Temperature}', '{0}'.format(temperature_text))
        fid2.write(lines + os.linesep)
        fid1.close()
        fid2.close()
        del fid1, fid2

    returncode = elmerrunlib.run_elmer(DIRM + solverfile, n=partitions)

    #####################################
    # Write cost values to summary file #
    #####################################

    fid = open(DIRM + "cost_" + method + "_beta.dat", "r")
    lines = fid.readlines()
    line = lines[-1]
    p = line.split()
    nsim = float(p[0])
    cost1 = float(p[1])
    cost2 = float(p[2])
    norm = float(p[3])
    fid.close()
    fid_info = open(DIRR + "summary.dat", "a")
    fid_info.write('{} {} {} {} {}\n'.format(regpar, nsim + restartposition,
                                             cost1, cost2, norm))
    fid_info.close()
    del fid

    #######################################
    # Move results to one directory #
    #######################################

    DIRR_lambda = DIRR + "lambda_" + regpar + "_" + date + "/"

    # Something wrong with saveline boundary, so moving to reading pvtu_files
    #bed = elmerreadlib.saveline_boundary(DIRM+"/mesh2d/",runname,bbed,['velocity 1','velocity 2','velocity 3','beta'])
    #surf = elmerreadlib.saveline_boundary(DIRM+"/mesh2d/",runname,bsurf,['vsurfini 1','vsurfini 2','velocity 1','velocity 2','velocity 3'])
    data = elmerreadlib.pvtu_file(
        DIRM + "/mesh2d/" + runname + "0001.pvtu",
        ['beta', 'velocity', 'vsurfini 1', 'vsurfini 2'])
    surf = elmerreadlib.values_in_layer(data, 'surf')
    bed = elmerreadlib.values_in_layer(data, 'bed')

    names = os.listdir(DIRM + "/mesh2d")
    if not os.path.exists(DIRR_lambda):
        os.makedirs(DIRR_lambda)
    for name in names:
        if name.endswith('pvtu') and name.startswith(method):
            os.rename(
                DIRM + "/mesh2d/" + name, DIRR_lambda + '{0}{1:04d}{2}'.format(
                    name[0:-9],
                    int(name[-9:-5]) + restartposition, '.pvtu'))
        elif name.endswith('vtu') and name.startswith(method):
            os.rename(
                DIRM + "/mesh2d/" + name, DIRR_lambda + '{0}{1:04d}{2}'.format(
                    name[0:-8],
                    int(name[-8:-4]) + restartposition, '.vtu'))
        elif name.startswith(method) and 'result' in name:
            os.rename(DIRM + "/mesh2d/" + name, DIRR_lambda + name)

    # Move saveline results
    files = os.listdir(DIRM + "/mesh2d/")
    for file in files:
        if file.startswith(runname) and ('.dat' in file):
            os.rename(DIRM + "/mesh2d/" + file, DIRR_lambda + file)

    # Move outputs for optimization
    os.rename(DIRM + "M1QN3_" + method + "_beta.out",
              DIRR_lambda + "M1QN3_" + method + "_beta.out")
    os.rename(DIRM + "gradientnormadjoint_" + method + "_beta.dat",
              DIRR_lambda + "gradient_" + runname + ".dat")
    os.rename(DIRM + "cost_" + method + "_beta.dat",
              DIRR_lambda + "cost_" + runname + ".dat")

    ################################
    # Output friction coefficients #
    ################################

    # Gridded linear beta square
    x, y, u = elmerreadlib.input_file(inputs + "udem.xy", dim=2)
    xx, yy = np.meshgrid(x, y)
    beta_linear = scipy.interpolate.griddata((bed['x'],bed['y']),\
      bed['beta']**2, (xx,yy), method='nearest')
    beta_linear_lin = scipy.interpolate.griddata((bed['x'],bed['y']),\
      bed['beta']**2, (xx,yy), method='linear')
    beta_weertman = scipy.interpolate.griddata((bed['x'],bed['y']),\
        (bed['beta']**2)/(bed['velocity']**(-2.0/3.0)), (xx,yy), method='nearest')
    beta_weertman_lin = scipy.interpolate.griddata((bed['x'],bed['y']),\
        (bed['beta']**2)/(bed['velocity']**(-2.0/3.0)), (xx,yy), method='linear')
    #if glacier == 'Helheim':
    #  # To get rid of edge effects near terminus, we take an average beta from farther upstream
    #  # and use that near the terminus
    #  ind_ave = np.where((xx > 302000) & (xx < 306000) & (yy < -2576000) & (yy > -2578000))
    #  ind = np.where((xx > 306000) & (yy < -2572000) & (yy > -2583000))
    #  beta_linear_lin[ind] = np.mean(beta_linear_lin[ind_ave])
    #  beta_weertman_lin[ind] = np.mean(beta_weertman_lin[ind_ave])
    ind = np.where(~(np.isnan(beta_linear_lin)))
    beta_linear[ind] = beta_linear_lin[ind]
    ind = np.where(~(np.isnan(beta_weertman_lin)))
    beta_weertman[ind] = beta_weertman_lin[ind]
    del beta_linear_lin, beta_weertman_lin

    # Output original results
    fidr = open(inputs + "beta_linear_" + regpar + "_FS_" + RES + ".dat", 'w')
    fidw = open(inputs + "beta_weertman_" + regpar + "_FS_" + RES + ".dat",
                'w')
    fidt = open(inputs + "taub_" + regpar + "_FS_" + RES + ".dat", 'w')
    fidr.write('{}\n'.format(len(bed['x'])))
    fidw.write('{}\n'.format(len(bed['x'])))
    fidt.write('{}\n'.format(len(bed['x'])))
    for i in range(0, len(bed['x'])):
        fidr.write('{0} {1} {2:.16f}\n'.format(bed['x'][i], bed['y'][i],
                                               bed['beta'][i]**2))
        fidw.write('{0} {1} {2:.16f}\n'.format(
            bed['x'][i], bed['y'][i],
            (bed['beta'][i]**2) / (bed['velocity'][i]**(-2.0 / 3.0))))
        fidt.write('{0} {1} {2:.16f}\n'.format(bed['x'][i], bed['y'][i],
                                               bed['taub'][i] * 1e3))
    fidr.close()
    fidw.close()
    fidt.close()
    del fidr, fidw, fidt

    # Output gridded results
    fidl = open(inputs + "beta_linear.xy", 'w')
    fidw = open(inputs + "beta_weertman.xy", 'w')
    fidl.write('{}\n{}\n'.format(len(x), len(y)))
    fidw.write('{}\n{}\n'.format(len(x), len(y)))
    for i in range(0, len(x)):
        for j in range(0, len(y)):
            fidl.write('{0} {1} {2:.6f}\n'.format(x[i], y[j], beta_linear[j,
                                                                          i]))
            fidw.write('{0} {1} {2:.6f}\n'.format(x[i], y[j],
                                                  beta_weertman[j, i]))
    fidl.close()
    fidw.close()
    del beta_linear, beta_weertman, u, x, y, fidl, fidw, xx, yy

    xgrid, ygrid, taubgrid = elmerreadlib.grid3d(bed, 'taub', holes, extent)
    xgrid, ygrid, vmodgrid = elmerreadlib.grid3d(surf, 'velocity', holes,
                                                 extent)
    xgrid, ygrid, vmesgrid1 = elmerreadlib.grid3d(surf, 'vsurfini 1', holes,
                                                  extent)
    xgrid, ygrid, vmesgrid2 = elmerreadlib.grid3d(surf, 'vsurfini 2', holes,
                                                  extent)

    plt.figure(figsize=(6.5, 3))
    plt.subplot(121)
    plt.imshow(taubgrid * 1e3, origin='lower', clim=[0, 500])
    plt.xticks([])
    plt.yticks([])
    cb = plt.colorbar(ticks=np.arange(0, 600, 100))
    cb.ax.tick_params(labelsize=10)
    cb.set_label('Basal shear stress (kPa)')

    plt.subplot(122)
    plt.imshow(vmodgrid - np.sqrt(vmesgrid1**2 + vmesgrid2**2),
               origin='lower',
               clim=[-200, 200],
               cmap='RdBu_r')
    plt.xticks([])
    plt.yticks([])
    cb = plt.colorbar(ticks=np.arange(-500, 600, 100))
    cb.ax.tick_params(labelsize=10)
    cb.set_label('Modeled-Measured (m/yr)')

    plt.tight_layout()

    plt.savefig(DIRR + 'lambda_' + regpar + '_' + date + '.pdf',
                format='PDF',
                dpi=400)
    plt.close()
예제 #4
0
def main():

    ##########
    # inputs #
    ##########

    args = get_arguments()

    RES = args.mesh
    partitions = str(args.n)
    extrude = str(args.extrude)
    frontbc = str(args.frontbc)
    glacier = args.glacier
    simulation = args.simulation
    iterations = args.iterations
    dt = args.timestepsize
    temperature = args.temperature
    restartfile = args.restartfile
    restartposition = args.restartposition

    # Directories
    DIRS = os.path.join(os.getenv("CODE_HOME"),
                        "big3/modeling/solverfiles/3D/")
    DIRM = os.path.join(os.getenv("MODEL_HOME"), glacier + "/3D/" + RES + "/")
    DIRR = os.path.join(DIRM + 'mesh2d/temperature/')
    DIRX = os.path.join(os.getenv("DATA_HOME"),
                        "ShapeFiles/Glaciers/3D/" + glacier)
    inputs = os.path.join(DIRM + "/inputs/")

    if not (os.path.exists(DIRR)):
        os.makedirs(DIRR)

    runname = "temperature"

    # Grab boundary condition for front -- it will be different if we are using an actual
    # terminus position (neumann, pressure BC) or an outflow boundary (dirichlet, velocity BC)
    if (frontbc == 'neumann') or (frontbc == 'pressure'):
        frontbc_text = """
  Flow Force BC = Logical True
  External Pressure = Variable Coordinate 3 !we are in MPa units
  Real MATC "-1.0*waterpressure(tx)*1.0E-06" """
    elif (frontbc == 'dirichlet') or (frontbc == 'velocity'):
        frontbc_text = """
  Velocity 1 = Variable Coordinate 1
    Real procedure "USF_Init.so" "UWa"
  Velocity 2 = Variable Coordinate 1
    Real procedure "USF_Init.so" "VWa" """
    else:
        sys.exit("Unknown BC for front of glacier.")

    # Set up text for simulation type (steady or transient) and the number of
    # timesteps or iterations we want the simulation to run for.
    if simulation == 'steady':
        simulation_text = """
  Simulation Type = Steady State
  Steady State Max Iterations = {0}
  Steady State Min Iterations = 1""".format(iterations)
    elif simulation == 'transient':
        simulation_text = """
  Simulation Type = Transient
  Timestepping Method = "BDF"
  BDF Order = 1
  Timestep Intervals = {0}     
  Timestep Sizes = {1}""".format(iterations, dt)

    # Set up text for temperature in viscosity parameter
    if temperature == 'model':
        temperature_text = """
  Constant Temperature = Variable Coordinate 1, Coordinate 2
    Real Procedure "USF_Init.so" "ModelTemperature" """
    else:
        try:
            float(temperature)
            temperature_text = """
  Constant Temperature = Real """ + str(temperature)
        except:
            sys.exit("Unknown temperature of " + temperature)

    # Add in restart file and restart position into simulation text if we are using it
    if restartfile != 'none':
        simulation_text = simulation_text + """
  Restart File = {0}
  Restart Position = {1}""".format(restartfile, restartposition)

    ###############################
    # Run temperature solver file #
    ###############################

    # Get current date
    now = datetime.datetime.now()
    date = '{0}{1:02.0f}{2:02.0f}'.format((now.year), (now.month), (now.day))

    solverfile = runname + '_' + date + '.sif'
    os.chdir(DIRM)
    fid1 = open(DIRS + runname + '.sif', 'r')
    fid2 = open(DIRM + solverfile, 'w')

    lines = fid1.read()
    lines = lines.replace('{Simulation Type}', '{0}'.format(simulation_text))
    lines = lines.replace('{Extrude}', '{0}'.format(extrude))
    lines = lines.replace('{FrontBC}', '{0}'.format(frontbc_text))
    lines = lines.replace('{Temperature}', '{0}'.format(temperature_text))
    fid2.write(lines)
    fid1.close()
    fid2.close()
    del fid1, fid2

    returncode = elmerrunlib.run_elmer(DIRM + solverfile, n=partitions)

    #####################
    # Move output files #
    #####################

    DIRR_output = DIRR + "temperature_" + date + "/"

    names = os.listdir(DIRM + "/mesh2d")
    if not os.path.exists(DIRR_output):
        os.makedirs(DIRR_output)
    for name in names:
        if name.endswith('pvtu') and name.startswith("temperature"):
            os.rename(DIRM + "/mesh2d/" + name, DIRR_output + name)
        elif name.endswith('vtu') and name.startswith("temperature"):
            os.rename(DIRM + "/mesh2d/" + name, DIRR_output + name)
        elif name.startswith("temperature") and 'result' in name:
            os.rename(DIRM + "/mesh2d/" + name, DIRR_output + name)
        elif name.startswith(
                "temperature") and not name.endswith('names') and ('.dat'
                                                                   in name):
            os.rename(DIRM + "/mesh2d/" + name, DIRR_output + name)
        elif name.startswith("temperature") and name.endswith('names'):
            os.rename(DIRM + "/mesh2d/" + name, DIRR_output + name)