def main(argv=None): """ """ parser = OptionParser( usage="usage: %prog [options]", version="%prog ", description=__doc__ ) parser.add_option( "-m", action="store", dest="mesh", type="string", default="", help="Specifies the mesh to work on" ) parser.add_option( "-t", action="store", dest="template", type="string", default="template", help="""Specifies the template case, that will be cloned. (Default = template)""" ) parser.add_option( "-s", "--subpath", action="store", dest="subpath", type="string", default="", help="""Optional subpath. This should be used to sort custom parameter variations into the existing file structure. %mesh/(%subpath, optional)/%beta/%v """ ) parser.add_option( "-b", action="store", dest="beta", type="float", default=0.0, help="Drift angle beta (Default = 0.0)" ) parser.add_option( "-n", action="store", dest="steps", type="int", default=10, help="""Number of velocities between 0 and vMax. v=0 m/s will be neglected. (Default = 10)""" ) parser.add_option( "-u", action="store", dest="u", type="string", default="0.0", help="Service speed in m/s (Default = 0.0m/s)" ) parser.add_option( "-r", action="store", dest="dh", type="float", default=100.0, help="""Characteristic length of the flow. For ships this should be the shiplength. (Default = 100.0m)""" ) group = OptionGroup(parser,"Flag Options") group.add_option( "--without-turbulence", action="store_true", dest="withoutTurbulence", help="""Do not look for the turbulence model and set the particular values in the boundary conditions. The values are calculated based on the equations presented in the Fluent (R) handbook.""" ) parser.add_option_group(group) (options, args) = parser.parse_args() try: u = float(options.u) v = linspace(0, u, options.steps + 1)[1:] except ValueError: exec "v = array(%s)" %options.u # Assemble the current working directory workingDir = path.join(getcwd(),options.mesh) # Put together the name of the target angle folder driftAngleName = "beta%.2f" %options.beta # Assemble the absolute path of the angle folder if not options.subpath: driftAngleDirectory = path.join(workingDir,driftAngleName) else: driftAngleDirectory = path.join(workingDir,options.subpath,driftAngleName) # Check if the directory exists, that should store the cases for the current # drift angle. if not path.exists(driftAngleDirectory): makedirs(driftAngleDirectory) else: raise IOError("Directory %s does already exist" %driftAngleDirectory) template = Case.case( path.join(getcwd(),options.mesh,options.template), archive=None, paraviewLink=False ) template.addToClone("runCluster") template.addToClone("customRegexp") print "\nDrift angle beta = %.2f\n" %options.beta i = 1 for vI in v: print "Cloning case for v = %.3f" %vI # Clone the template case and open velocity boundary condition case = template.cloneCase(path.join(driftAngleDirectory,"v%02d" %i)) uFile = ParsedParameterFile(path.join(case.name,"0","U")) # Rotate the velocity vector around z axis, according to the specified # drift angle. U = Vector( vI*math.cos(math.radians(options.beta)), vI*math.sin(math.radians(options.beta)), 0 ) # Update the boundary condition(s) with the respective values, that have # been calculated previously. uFile["internalField"].setUniform(U) for b in ["XMIN","XMAX","YMIN","YMAX","ZMIN", "ZMAX"]: patchType = uFile["boundaryField"][b]['type'] setPatch = False for inlet in ['value', 'inletValue', 'tangentialVelocity']: try: uFile["boundaryField"][b][inlet].setUniform(U) setPatch = True except KeyError: pass if setPatch: print "\tSetting patch: %s type %s" %(b, patchType) # Write changes to the boundary conditions uFile.writeFile() if not options.withoutTurbulence: print "\tFixing turbulence Dh =", options.dh case.writeTurbulence(options.dh, Utilities.mag(U)) # Update counter i += 1 print "\nDone!"
def main(): parser = OptionParser( usage="usage: %prog [options] area ref.Length (case)", version="%prog 1.0") parser.add_option( "-U", "--velocity", action="store", dest="U", type="string", default="False", help="Velocity vector as a list \"[x,y,z]\". If nothing is\ stated, a inlet patch needs to be specified. From that\ patch, the velocity is read automatically.") parser.add_option("-i", "--inlet", action="store", dest="inletPatch", type="string", default="XMAX", help="Name of the inlet patch. (default=XMAX)") parser.add_option( "-s", "--start", action="store", dest="startAtTime", type="float", default=0.0, help="Starttime of the plotting interval, spanning from that\ time to the end. (default=0.0)") parser.add_option( "-d", "--deviation-start", action="store", dest="deviationInterval", type="float", default=0.5, help="Specifies over which part of the plotted interval, the\ relative deviation between CF and ITTC\'57 as well as\ the average of CF should be calculated. (default=0.5)") parser.add_option("-f", action="store", type="choice", choices=['1', '2', '3'], dest="direction", default=1, help="Main flow direction (1=x,2=y,3=z). (default=1)") (options, args) = parser.parse_args() if len(args) == 2: caseDir = getcwd() elif len(args) == 3: caseDir = args[2] else: parser.error("wrong number of arguments") area = float(args[0]) L = float(args[1]) U = eval(options.U) startAtElement = 0 deviationStartElement = 0 case = Case.case(caseDir, archive=None, paraviewLink=False, inletPatch=options.inletPatch, U=U) uInf = Utilities.mag(case.inletVelocity) Re = FlowProperties.Re(L=L, u=uInf) data = case.forces t = data[0] RF = abs(data[options.direction + 3]) RT = RF + abs(data[options.direction]) # The list element of the time has to be figured out, beyond which the # plotting should start. if options.startAtTime > 0.0: for i in range(0, len(data[0])): if data[0][i] > options.startAtTime: startAtElement = i break # Determine the start element for the deviation calculation deviationStartElement = startAtElement + \ int(len(t[startAtElement:])*(1-options.deviationInterval)) # Gather all data CF = Resistance.forceCoeff(RF, area, u=uInf) CT = Resistance.forceCoeff(RT, area, u=uInf) ittc57 = ones(len(CT)) * SkinFriction.ittc57(Re=Re) deviationList = CF * 100.0 / ittc57 - 100 # Calculate the relative error between CF and ITTC57 for the respective # intervall deviation = sum(deviationList[deviationStartElement:]) / \ len(deviationList[deviationStartElement:]) # Calculate mean value of CF for the respective interval CFmean = mean(CF[deviationStartElement:]) fig = plt.figure() plt.title("case: %s\nCFmean = %.2e, rel.Error = %.2f%%, Re = %.2e" % (case.shortCasePath, CFmean, deviation, Re)) plt.grid(True) ax1 = fig.add_subplot(111) plt.grid(True) ax2 = ax1.twinx() CFPlot = ax1.plot(t[startAtElement:], CF[startAtElement:], '-', label='C_F', color='green') ittc57Plot = ax1.plot(t[startAtElement:], ittc57[startAtElement:], '-', label='ITTC57', color='red') CTPlot = ax2.plot(t[startAtElement:], CT[startAtElement:], '-', label='C_T') ax1.fill_between(t[deviationStartElement:], CF[deviationStartElement:], ittc57[deviationStartElement:], color="green", alpha=0.5) ax1.set_xlabel("simulation time [s]") ax1.set_ylabel("CF [-]") ax2.set_ylabel("CT [-]") #ax1.set_ylim([ # 0, # 1.2*max(CF[startAtElement].max(),ittc57[0]) # ]) lines = CFPlot + ittc57Plot + CTPlot labels = [l.get_label() for l in lines] plt.legend( lines, labels, loc=9, #bbox_to_anchor=(0., 1.02, 1., .102), ncol=3, mode="expand") plt.show()
def main(): parser = OptionParser( usage="usage: %prog [options] area ref.Length", version="%prog 1.0", description=__doc__ ) parser.add_option( "-c", "--case", action="append", dest="cases", type="string", help="Specifies multiple cases and has to be called multiple\ times to do so." ) parser.add_option( "-p", "--pattern", action="store", dest="pattern", type="string", help="Specifies the cases in the current working directory\ via regular expressions." ) parser.add_option( "-i", "--inlet", action="store", dest="inletPatch", type="string", default="XMAX", help="Name of the inlet patch. (default=XMAX)" ) parser.add_option( "-s", "--start", action="store", dest="startAtTime", type="float", default=0.0, help="Starttime of the plotting interval, spanning from that\ time to the end. (default=0.0)" ) parser.add_option( "-e", "--end", action="store", dest="endAtTime", type="float", default=1000000.0, help="Endtime of the plotting interval" ) parser.add_option( "-f", action="store", type="choice", choices=['1','-1','2','-2','3','-3'], dest="direction", default=-1, help="Main flow direction (1=x,2=y,3=z). (default=1)" ) parser.add_option( "-d","--data", action="store", type="choice", choices=['CF','CT','RF','RT'], dest="dataToPlot", default='CF', help="Sets the data to be plotted. (default=CF)" ) group = OptionGroup(parser,"Flag Options") group.add_option( "--without-ittc57", action="store_true", dest="withoutIttc", help="Do not print ITTC\'57 correlation line" ) group.add_option( "--without-average", action="store_true", dest="withoutAverage", help="Do not calculate the average of the data" ) parser.add_option_group(group) group = OptionGroup(parser,"Statistics Options") group.add_option( "--with-absolute", action="store_true", dest="withAbsolute", help="If specified, all statistics intervalls have to be\ defined in terms of absolute times, rather than\ relatives" ) group.add_option( "--average-start", action="store", dest="averageStart", type="float", default=0.5, help="Start of the averaging interval. (default=0.5)" ) group.add_option( "--average-end", action="store", dest="averageEnd", type="float", default=1.0, help="End of the averaging interval. (default=1.0)" ) group.add_option( "--ittc-start", action="store", dest="ittcStart", type="float", default=0.5, help="Start of the averaging interval. (default=0.5)" ) group.add_option( "--ittc-end", action="store", dest="ittcEnd", type="float", default=1.0, help="End of the averaging interval. (default=1.0)" ) parser.add_option_group(group) (options, args) = parser.parse_args() if len(args) != 2: parser.error("wrong number of arguments") # Get the reference area and reference lengths from the command line # parameters. U needs to be evaluated, as either a string containg "False" # is passed or a string with a 3D list in it. area = float(args[0]) L = float(args[1]) ############################ # Variable initialisations # ############################ startAtElement = 0 cases = [] longestDataSet = -1 i = 0 CTaverage = [] caseFolders = [] if options.pattern: casePattern = compile(r"%s" %(options.pattern.replace( "_F_", Settings.floatPattern ))) for fI in listdir(getcwd()): if findall(casePattern,fI): caseFolders.append(fI) else: caseFolders = options.cases # Gather all cases for cI in caseFolders: cases.append( Case.case( path.join(getcwd(),cI), archive=None, paraviewLink=False, inletPatch=options.inletPatch, L=L, A=area, direction=options.direction ) ) # Calculation of force coefficients has to be called explicitely, as the # area and a reference length have to be specified. Furtheron it is not # essential that forces are present. cases[-1].calculateCoeffs() # Figure out which data set is the longest if len(cases[i].t) >= len(cases[longestDataSet].t): longestDataSet = i i += 1 # Store the length of all data sets in an extra list dataLengths = [len(cI.t) for cI in cases] # Initialise the plot fig = plt.figure(figsize=(11, 6)) plt.title("Comparison of resistances") plt.grid(True) ax1 = fig.add_subplot(111) ax1.set_xlabel("simulation time [s]") ax1.set_ylabel("CF [-]") # Shink current axis by 20% box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.7, box.height]) plt.grid(True) allPlots = [] # Create a list with all Reynoldsnumbers in it. This is needed for the # decision, whether only one or multiple ITTC lines have to be plotted Re = [] ReNumbers = len(list(set([cI.Re for cI in cases]))) for cI in cases: startAtElement = DataFile.element( cI.t, options.startAtTime, True ) endAtElement = DataFile.element( cI.t, options.endAtTime, True ) # The average of CF needs to be calculated anyway, as it is needed for # the ittc deviation calculation. aveStart = DataFile.element( cI.t, options.averageStart, options.withAbsolute ) aveEnd = DataFile.element( cI.t, options.averageEnd, options.withAbsolute ) ave = average(cI.resistances[options.dataToPlot][aveStart:aveEnd]) if not options.withoutAverage: label = "%s (%.2e)" %(cI.shortCaseName,ave) else: label = cI.shortCaseName allPlots.append( ax1.plot( cI.t[startAtElement:endAtElement], cI.resistances[options.dataToPlot][startAtElement:endAtElement], '-', label=label ) ) # If the ITTC line is enabled, calculate the numpy array for each # dataset and fill the area between the respective CF curve and the ITTC # line with the same color. Then the relative deviation between CF and # ITTC is annotated. if not options.withoutIttc and options.dataToPlot == 'CF': ittcStart = DataFile.element( cI.t[startAtElement:endAtElement], options.ittcStart, options.withAbsolute ) ittcEnd = DataFile.element( #cI.t, cI.t[startAtElement:endAtElement], options.ittcEnd, options.withAbsolute ) ittc57 = ones(len(cI.t))*SkinFriction.ittc57(Re=cI.Re) ax1.fill_between( cI.t[ittcStart:ittcEnd], cI.resistances[options.dataToPlot][ittcStart:ittcEnd], ittc57[ittcStart:ittcEnd], color=allPlots[-1][0].get_color(), alpha=0.5 ) if not cI.Re in Re and ReNumbers > 1: label='ITTC\'57 (%.2e)' %(ittc57[0]) allPlots.append( ax1.plot( cases[longestDataSet].t[startAtElement:endAtElement], ittc57[startAtElement:endAtElement], '--', color=allPlots[-1][0].get_color(), label=label ) ) # Compute the deviation of the current CF from the ITTC line and # create a label inside the plot, that shows the relative deviation # as a numerical value. deviationList = cI.resistances[options.dataToPlot]*100.0/ittc57 - 100 centerX = cI.t[int(ittcStart + (ittcEnd-ittcStart)/2)] centerY = min(ave,ittc57[0]) + (max(ave,ittc57[0])-min(ave,ittc57[0]))/2 plt.text( centerX, centerY, '%.2f%%' %average(deviationList[ittcStart:ittcEnd]) ) # Finally append the current Reynoldsnumber to the global list of # Reynoldsnumbers. Re.append(cI.Re) if not options.withoutIttc and options.dataToPlot == 'CF': ittc57 = ones(dataLengths[longestDataSet])*\ SkinFriction.ittc57(Re=cases[-1].Re) startAtElement = DataFile.element( cases[longestDataSet].t, options.startAtTime, True ) endAtElement = DataFile.element( cases[longestDataSet].t, options.endAtTime, True ) if not options.withoutIttc and ReNumbers == 1: label='ITTC\'57 (%.2e)' %(ittc57[0]) allPlots.append( ax1.plot( cases[longestDataSet].t[startAtElement:endAtElement], ittc57[startAtElement:endAtElement], '--', color='black', label=label ) ) # Assemble all plots for a proper legend lines = [pI[0] for pI in allPlots] labels = [l.get_label() for l in lines] ax1.legend( lines, labels, bbox_to_anchor=(1.02, 1), loc=2, #mode="expand", #borderaxespad=0., ncol=1 ) plt.show()
def main(): parser = OptionParser( usage="usage: %prog [options] area ref.Length", version="%prog 1.0", description=__doc__ ) parser.add_option( "-s", "--data-set", action="append", dest="dataSets", type="string", help="Specifies a set of cases in the current working directory,\ that are considered to be of one data set. The selection is\ done via regular expressions. Multiple data sets can be\ specified." ) parser.add_option( "-i", "--inlet", action="store", dest="inletPatch", type="string", default="XMAX", help="Name of the inlet patch. (default=XMAX)" ) parser.add_option( "-f", action="store", type="choice", choices=['1','-1','2','-2','3','-3'], dest="direction", default=-1, help="Main flow direction (1=x,2=y,3=z). (default=1)" ) parser.add_option( "-x","--x-data", action="store", type="choice", choices=['v','Fr','Re'], dest="xDataToPlot", default='Fr', help="Which data to show on the x-axis. (default=Fr)" ) parser.add_option( "-y","--y-data", action="store", type="choice", choices=['CF','CT','RF','RT'], dest="yDataToPlot", default='CF', help="Which data to show on the y-axis. (default=CF)" ) group = OptionGroup(parser,"Flag Options") group.add_option( "--without-ittc57", action="store_true", dest="withoutIttc", help="Do not print ITTC\'57 correlation line" ) group.add_option( "--without-errorbars", action="store_true", dest="withoutErrorbars", help="Do not print the error bars for the averaging" ) parser.add_option_group(group) group = OptionGroup(parser,"Statistics Options") group.add_option( "--with-absolute", action="store_true", dest="withAbsolute", help="If specified, all statistics intervalls have to be\ defined in terms of absolute times, rather than\ relatives" ) group.add_option( "--average-start", action="store", dest="averageStart", type="float", default=0.5, help="Start of the averaging interval. (default=0.5)" ) group.add_option( "--average-end", action="store", dest="averageEnd", type="float", default=1.0, help="End of the averaging interval. (default=1.0)" ) parser.add_option_group(group) (options, args) = parser.parse_args() if len(args) != 2: parser.error("wrong number of arguments") # Get the reference area and reference lengths from the command line # parameters. U needs to be evaluated, as either a string containg "False" # is passed or a string with a 3D list in it. area = float(args[0]) L = float(args[1]) ############################ # Variable initialisations # ############################ startAtElement = 0 dataSets = [] dataSetNames = [] thisSet = [] for sI in options.dataSets: setPattern = compile(r"%s" %(sI.replace("_F_",Settings.floatPattern))) for fI in listdir(getcwd()): if findall(setPattern,fI): thisSet.append(fI) dataSetNames.append(thisSet) thisSet = [] # Gather all cases for sI in dataSetNames: casesOfThisSet = [] for cI in sI: casesOfThisSet.append( Case.case( path.join(getcwd(),cI), archive=None, paraviewLink=False, inletPatch=options.inletPatch, L=L, A=area, direction=options.direction ) ) # Calculation of force coefficients has to be called explicitely, as the # area and a reference length have to be specified. Furtheron it is not # essential that forces are present. casesOfThisSet[-1].calculateCoeffs() dataSets.append(casesOfThisSet) casesOfThisSet = [] if options.xDataToPlot == 'v': unitX = 'm/s' else: unitX = '-' if options.yDataToPlot in ('RT','RF'): unitY = 'N' else: unitY = '-' # Initialise the plot fig = plt.figure(figsize=(11, 6)) plt.title("Comparison of resistances over velocity") plt.grid(True) ax1 = fig.add_subplot(111) ax1.set_xlabel("%s [%s]" %(options.xDataToPlot,unitX)) ax1.set_ylabel("%s [%s]" %(options.yDataToPlot,unitY)) # Shink current axis by 20% box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.7, box.height]) plt.grid(True) allPlots = [] dataToPlot = [] thisSetToPlot = [] Re = [1e15,0] Fr = [1e15,0] uInf = [1e15,0] for sI in dataSets: for cI in sI: aveStart = DataFile.element( cI.t, options.averageStart, options.withAbsolute ) aveEnd = DataFile.element( cI.t, options.averageEnd, options.withAbsolute ) ave = average(cI.resistances[options.yDataToPlot][aveStart:aveEnd]) # Update velocities Re[0] = min(Re[0],cI.Re) Re[1] = max(Re[1],cI.Re) Fr[0] = min(Fr[0],cI.Fr) Fr[1] = max(Fr[1],cI.Fr) uInf[0] = min(uInf[0],cI.uInf) uInf[1] = max(uInf[1],cI.uInf) tmpList = [ cI.Fr, cI.Re, cI.uInf, ave, std(cI.resistances[options.yDataToPlot][aveStart:aveEnd]) ] thisSetToPlot.append(tmpList) dataToPlot.append(thisSetToPlot) thisSetToPlot = [] dataToPlot = array(dataToPlot) i = 0 for dI in dataToPlot: # The data needs to be sorted # http://stackoverflow.com/questions/2706605/sorting-a-2d-numpy-array-by-multiple-axes ind=lexsort((dI[:,1],dI[:,0])) sorted = dI[ind] if options.xDataToPlot == 'v': xData = sorted[:,2] elif options.xDataToPlot == 'Fr': xData = sorted[:,0] else: xData = sorted[:,1] if not options.withoutErrorbars: allPlots.append( ax1.errorbar( xData, sorted[:,3], sorted[:,4], fmt='-,', ) ) else: allPlots.append( ax1.plot( xData, sorted[:,3], '-o', label=options.dataSets[i] ) ) i += 1 if not options.withoutIttc: ittcRe = linspace(Re[0],Re[1],100) if options.xDataToPlot == 'Fr': xData = linspace(Fr[0],Fr[1],100) elif options.xDataToPlot == 'v': xData = linspace(uInf[0],uInf[1],100) else: xData = ittcRe ittc = SkinFriction.ittc57(Re=ittcRe) allPlots.append( ax1.plot( xData, ittc, '-', color='black', label='ITTC\'57' ) ) # Assemble all plots for a proper legend lines = [pI[0] for pI in allPlots] labels = [l.get_label() for l in lines] ax1.legend( lines, labels, bbox_to_anchor=(1.02, 1), loc=2, #mode="expand", #borderaxespad=0., ncol=1 ) plt.show()
def main(): parser = OptionParser(usage="usage: %prog [options] area ref.Length (case)", version="%prog 1.0") parser.add_option( "-U", "--velocity", action="store", dest="U", type="string", default="False", help="Velocity vector as a list \"[x,y,z]\". If nothing is\ stated, a inlet patch needs to be specified. From that\ patch, the velocity is read automatically." ) parser.add_option( "-i", "--inlet", action="store", dest="inletPatch", type="string", default="XMAX", help="Name of the inlet patch. (default=XMAX)" ) parser.add_option( "-s", "--start", action="store", dest="startAtTime", type="float", default=0.0, help="Starttime of the plotting interval, spanning from that\ time to the end. (default=0.0)" ) parser.add_option( "-d", "--deviation-start", action="store", dest="deviationInterval", type="float", default=0.5, help="Specifies over which part of the plotted interval, the\ relative deviation between CF and ITTC\'57 as well as\ the average of CF should be calculated. (default=0.5)" ) parser.add_option( "-f", action="store", type="choice", choices=['1','2','3'], dest="direction", default=1, help="Main flow direction (1=x,2=y,3=z). (default=1)" ) (options, args) = parser.parse_args() if len(args) == 2: caseDir = getcwd() elif len(args) == 3: caseDir = args[2] else: parser.error("wrong number of arguments") area = float(args[0]) L = float(args[1]) U = eval(options.U) startAtElement = 0 deviationStartElement = 0 case = Case.case( caseDir, archive=None, paraviewLink=False, inletPatch=options.inletPatch, U=U ) uInf = Utilities.mag(case.inletVelocity) Re = FlowProperties.Re(L=L,u=uInf) data = case.forces t = data[0] RF = abs(data[options.direction+3]) RT = RF + abs(data[options.direction]) # The list element of the time has to be figured out, beyond which the # plotting should start. if options.startAtTime > 0.0: for i in range(0,len(data[0])): if data[0][i] > options.startAtTime: startAtElement = i break # Determine the start element for the deviation calculation deviationStartElement = startAtElement + \ int(len(t[startAtElement:])*(1-options.deviationInterval)) # Gather all data CF = Resistance.forceCoeff(RF,area,u=uInf) CT = Resistance.forceCoeff(RT,area,u=uInf) ittc57 = ones(len(CT))* SkinFriction.ittc57(Re=Re) deviationList = CF*100.0/ittc57 - 100 # Calculate the relative error between CF and ITTC57 for the respective # intervall deviation = sum(deviationList[deviationStartElement:]) / \ len(deviationList[deviationStartElement:]) # Calculate mean value of CF for the respective interval CFmean = mean(CF[deviationStartElement:]) fig = plt.figure() plt.title("case: %s\nCFmean = %.2e, rel.Error = %.2f%%, Re = %.2e" %(case.shortCasePath,CFmean,deviation,Re)) plt.grid(True) ax1 = fig.add_subplot(111) plt.grid(True) ax2 = ax1.twinx() CFPlot = ax1.plot(t[startAtElement:],CF[startAtElement:],'-',label='C_F',color='green') ittc57Plot = ax1.plot(t[startAtElement:], ittc57[startAtElement:],'-',label='ITTC57',color='red') CTPlot = ax2.plot(t[startAtElement:],CT[startAtElement:],'-',label='C_T') ax1.fill_between(t[deviationStartElement:], CF[deviationStartElement:], ittc57[deviationStartElement:], color="green",alpha=0.5) ax1.set_xlabel("simulation time [s]") ax1.set_ylabel("CF [-]") ax2.set_ylabel("CT [-]") #ax1.set_ylim([ # 0, # 1.2*max(CF[startAtElement].max(),ittc57[0]) # ]) lines = CFPlot+ittc57Plot+CTPlot labels = [l.get_label() for l in lines] plt.legend( lines, labels, loc=9, #bbox_to_anchor=(0., 1.02, 1., .102), ncol=3, mode="expand" ) plt.show()
def main(): parser = OptionParser(usage="usage: %prog [options] area ref.Length", version="%prog 1.0", description=__doc__) parser.add_option( "-s", "--data-set", action="append", dest="dataSets", type="string", help="Specifies a set of cases in the current working directory,\ that are considered to be of one data set. The selection is\ done via regular expressions. Multiple data sets can be\ specified.") parser.add_option("-i", "--inlet", action="store", dest="inletPatch", type="string", default="XMAX", help="Name of the inlet patch. (default=XMAX)") parser.add_option("-f", action="store", type="choice", choices=['1', '-1', '2', '-2', '3', '-3'], dest="direction", default=-1, help="Main flow direction (1=x,2=y,3=z). (default=1)") parser.add_option("-x", "--x-data", action="store", type="choice", choices=['v', 'Fr', 'Re'], dest="xDataToPlot", default='Fr', help="Which data to show on the x-axis. (default=Fr)") parser.add_option("-y", "--y-data", action="store", type="choice", choices=['CF', 'CT', 'RF', 'RT'], dest="yDataToPlot", default='CF', help="Which data to show on the y-axis. (default=CF)") group = OptionGroup(parser, "Flag Options") group.add_option("--without-ittc57", action="store_true", dest="withoutIttc", help="Do not print ITTC\'57 correlation line") group.add_option("--without-errorbars", action="store_true", dest="withoutErrorbars", help="Do not print the error bars for the averaging") parser.add_option_group(group) group = OptionGroup(parser, "Statistics Options") group.add_option("--with-absolute", action="store_true", dest="withAbsolute", help="If specified, all statistics intervalls have to be\ defined in terms of absolute times, rather than\ relatives") group.add_option("--average-start", action="store", dest="averageStart", type="float", default=0.5, help="Start of the averaging interval. (default=0.5)") group.add_option("--average-end", action="store", dest="averageEnd", type="float", default=1.0, help="End of the averaging interval. (default=1.0)") parser.add_option_group(group) (options, args) = parser.parse_args() if len(args) != 2: parser.error("wrong number of arguments") # Get the reference area and reference lengths from the command line # parameters. U needs to be evaluated, as either a string containg "False" # is passed or a string with a 3D list in it. area = float(args[0]) L = float(args[1]) ############################ # Variable initialisations # ############################ startAtElement = 0 dataSets = [] dataSetNames = [] thisSet = [] for sI in options.dataSets: setPattern = compile(r"%s" % (sI.replace("_F_", Settings.floatPattern))) for fI in listdir(getcwd()): if findall(setPattern, fI): thisSet.append(fI) dataSetNames.append(thisSet) thisSet = [] # Gather all cases for sI in dataSetNames: casesOfThisSet = [] for cI in sI: casesOfThisSet.append( Case.case(path.join(getcwd(), cI), archive=None, paraviewLink=False, inletPatch=options.inletPatch, L=L, A=area, direction=options.direction)) # Calculation of force coefficients has to be called explicitely, as the # area and a reference length have to be specified. Furtheron it is not # essential that forces are present. casesOfThisSet[-1].calculateCoeffs() dataSets.append(casesOfThisSet) casesOfThisSet = [] if options.xDataToPlot == 'v': unitX = 'm/s' else: unitX = '-' if options.yDataToPlot in ('RT', 'RF'): unitY = 'N' else: unitY = '-' # Initialise the plot fig = plt.figure(figsize=(11, 6)) plt.title("Comparison of resistances over velocity") plt.grid(True) ax1 = fig.add_subplot(111) ax1.set_xlabel("%s [%s]" % (options.xDataToPlot, unitX)) ax1.set_ylabel("%s [%s]" % (options.yDataToPlot, unitY)) # Shink current axis by 20% box = ax1.get_position() ax1.set_position([box.x0, box.y0, box.width * 0.7, box.height]) plt.grid(True) allPlots = [] dataToPlot = [] thisSetToPlot = [] Re = [1e15, 0] Fr = [1e15, 0] uInf = [1e15, 0] for sI in dataSets: for cI in sI: aveStart = DataFile.element(cI.t, options.averageStart, options.withAbsolute) aveEnd = DataFile.element(cI.t, options.averageEnd, options.withAbsolute) ave = average(cI.resistances[options.yDataToPlot][aveStart:aveEnd]) # Update velocities Re[0] = min(Re[0], cI.Re) Re[1] = max(Re[1], cI.Re) Fr[0] = min(Fr[0], cI.Fr) Fr[1] = max(Fr[1], cI.Fr) uInf[0] = min(uInf[0], cI.uInf) uInf[1] = max(uInf[1], cI.uInf) tmpList = [ cI.Fr, cI.Re, cI.uInf, ave, std(cI.resistances[options.yDataToPlot][aveStart:aveEnd]) ] thisSetToPlot.append(tmpList) dataToPlot.append(thisSetToPlot) thisSetToPlot = [] dataToPlot = array(dataToPlot) i = 0 for dI in dataToPlot: # The data needs to be sorted # http://stackoverflow.com/questions/2706605/sorting-a-2d-numpy-array-by-multiple-axes ind = lexsort((dI[:, 1], dI[:, 0])) sorted = dI[ind] if options.xDataToPlot == 'v': xData = sorted[:, 2] elif options.xDataToPlot == 'Fr': xData = sorted[:, 0] else: xData = sorted[:, 1] if not options.withoutErrorbars: allPlots.append( ax1.errorbar( xData, sorted[:, 3], sorted[:, 4], fmt='-,', )) else: allPlots.append( ax1.plot(xData, sorted[:, 3], '-o', label=options.dataSets[i])) i += 1 if not options.withoutIttc: ittcRe = linspace(Re[0], Re[1], 100) if options.xDataToPlot == 'Fr': xData = linspace(Fr[0], Fr[1], 100) elif options.xDataToPlot == 'v': xData = linspace(uInf[0], uInf[1], 100) else: xData = ittcRe ittc = SkinFriction.ittc57(Re=ittcRe) allPlots.append( ax1.plot(xData, ittc, '-', color='black', label='ITTC\'57')) # Assemble all plots for a proper legend lines = [pI[0] for pI in allPlots] labels = [l.get_label() for l in lines] ax1.legend( lines, labels, bbox_to_anchor=(1.02, 1), loc=2, #mode="expand", #borderaxespad=0., ncol=1) plt.show()
def main(argv=None): """ """ parser = OptionParser(usage="usage: %prog [options]", version="%prog ", description=__doc__) parser.add_option("-m", action="store", dest="mesh", type="string", default="", help="Specifies the mesh to work on") parser.add_option("-t", action="store", dest="template", type="string", default="template", help="""Specifies the template case, that will be cloned. (Default = template)""") parser.add_option( "-s", "--subpath", action="store", dest="subpath", type="string", default="", help="""Optional subpath. This should be used to sort custom parameter variations into the existing file structure. %mesh/(%subpath, optional)/%beta/%v """) parser.add_option("-b", action="store", dest="beta", type="float", default=0.0, help="Drift angle beta (Default = 0.0)") parser.add_option("-n", action="store", dest="steps", type="int", default=10, help="""Number of velocities between 0 and vMax. v=0 m/s will be neglected. (Default = 10)""") parser.add_option("-u", action="store", dest="u", type="string", default="0.0", help="Service speed in m/s (Default = 0.0m/s)") parser.add_option("-r", action="store", dest="dh", type="float", default=100.0, help="""Characteristic length of the flow. For ships this should be the shiplength. (Default = 100.0m)""") group = OptionGroup(parser, "Flag Options") group.add_option("--without-turbulence", action="store_true", dest="withoutTurbulence", help="""Do not look for the turbulence model and set the particular values in the boundary conditions. The values are calculated based on the equations presented in the Fluent (R) handbook.""") parser.add_option_group(group) (options, args) = parser.parse_args() try: u = float(options.u) v = linspace(0, u, options.steps + 1)[1:] except ValueError: exec "v = array(%s)" % options.u # Assemble the current working directory workingDir = path.join(getcwd(), options.mesh) # Put together the name of the target angle folder driftAngleName = "beta%.2f" % options.beta # Assemble the absolute path of the angle folder if not options.subpath: driftAngleDirectory = path.join(workingDir, driftAngleName) else: driftAngleDirectory = path.join(workingDir, options.subpath, driftAngleName) # Check if the directory exists, that should store the cases for the current # drift angle. if not path.exists(driftAngleDirectory): makedirs(driftAngleDirectory) else: raise IOError("Directory %s does already exist" % driftAngleDirectory) template = Case.case(path.join(getcwd(), options.mesh, options.template), archive=None, paraviewLink=False) template.addToClone("runCluster") template.addToClone("customRegexp") print "\nDrift angle beta = %.2f\n" % options.beta i = 1 for vI in v: print "Cloning case for v = %.3f" % vI # Clone the template case and open velocity boundary condition case = template.cloneCase(path.join(driftAngleDirectory, "v%02d" % i)) uFile = ParsedParameterFile(path.join(case.name, "0", "U")) # Rotate the velocity vector around z axis, according to the specified # drift angle. U = Vector(vI * math.cos(math.radians(options.beta)), vI * math.sin(math.radians(options.beta)), 0) # Update the boundary condition(s) with the respective values, that have # been calculated previously. uFile["internalField"].setUniform(U) for b in ["XMIN", "XMAX", "YMIN", "YMAX", "ZMIN", "ZMAX"]: patchType = uFile["boundaryField"][b]['type'] setPatch = False for inlet in ['value', 'inletValue', 'tangentialVelocity']: try: uFile["boundaryField"][b][inlet].setUniform(U) setPatch = True except KeyError: pass if setPatch: print "\tSetting patch: %s type %s" % (b, patchType) # Write changes to the boundary conditions uFile.writeFile() if not options.withoutTurbulence: print "\tFixing turbulence Dh =", options.dh case.writeTurbulence(options.dh, Utilities.mag(U)) # Update counter i += 1 print "\nDone!"