def init(): global geometry_component, line_2D, lines_3D, line_comp, cbar, top_text, ax1, ax2, cmap, show_geometry, show_components, solution_components, scaling_factors # determine in which direction the 1D fibre extends the most min_x, max_x = py_reader.get_min_max(data, "geometry", "x") min_y, max_y = py_reader.get_min_max(data, "geometry", "y") min_z, max_z = py_reader.get_min_max(data, "geometry", "z") min_s, max_s = py_reader.get_min_max(data, "solution", "0") print( "value range: [{}, {}]".format(min_s, max_s)) if np.isinf(min_s) or np.isnan(min_s): min_s = 0 if np.isinf(max_s) or np.isnan(max_s): max_s = 0 solution_components = py_reader.get_component_names(data[0], "solution") n_components = len(solution_components) span_x = max_x - min_x span_y = max_y - min_y span_z = max_z - min_z # if the geometry is a line, do not show geometry plot if span_y == 0 and span_z == 0: show_geometry = False if (not show_geometry) and n_components > 1: if solution_components[1] != "1": show_components = True if span_x >= span_y and span_x >= span_z: geometry_component = "x" elif span_y >= span_x and span_y >= span_z: geometry_component = "y" else: geometry_component = "z" if plot_over_time: # x-axis is time min_x = data[0]['currentTime'] max_x = data[-1]['currentTime'] else: # x-axis is geometry min_x, max_x = py_reader.get_min_max(data, "geometry", geometry_component) if show_geometry: print( "geometry bounding box: x:[{},{}], y:[{},{}], z:[{},{}]".format(min_x,max_x,min_y,max_y,min_z,max_z)) # prepare plot if show_geometry: gs = gridspec.GridSpec(2,1,height_ratios=[4,1]) ax2 = plt.subplot(gs[0], projection='3d') ax1 = plt.subplot(gs[1]) elif show_components: gs = gridspec.GridSpec(2,1,height_ratios=[3,4]) ax1 = plt.subplot(gs[0]) # main component ax3 = plt.subplot(gs[1]) # all other components else: ax1 = plt.gca() # prepare main plot if data[0]["basisFunction"] == "Hermite" and data[0]["onlyNodalValues"] == False: # for Hermite line_2D, = ax1.plot([], [], '-', color="b", lw=2, label=solution_components[0]) else: line_2D, = ax1.plot([], [], '+-', color="b", lw=2, label=solution_components[0]) margin = abs(max_s - min_s) * 0.1 ax1.set_xlim(min_x, max_x) ax1.set_ylim(min_s - margin, max_s + margin) top_text = ax1.text(0.5,0.95,"",size=20,horizontalalignment='center',transform=ax1.transAxes) xlabel = geometry_component if plot_over_time: ax1.set_xlabel('t') else: ax1.set_xlabel(xlabel.upper()) ax1.set_ylabel('Solution') if solution_components[0] != "0": ax1.legend() # prepare geometry plot if show_geometry: ax2.set_title("geometry") ax2.set_xlim(min_x, max_x) ax2.set_ylim(min_y, max_y) ax2.set_zlim(min_z, max_z) lines_3D = [] # plot line segments with corresponding color xdata = py_reader.get_values(data[0], "geometry", "x") for i in range(len(xdata)): p, = ax2.plot(xdata[i:i+2], xdata[i:i+2], xdata[i:i+2], lw=3) lines_3D.append(p) ax2.set_xlabel('X') ax2.set_ylabel('Y') ax2.set_zlabel('Z') # manually create colorbar [https://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots] plt.sca(ax2) norm = mpl.colors.Normalize(vmin=min_s, vmax=max_s) cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet) cmap.set_array([]) cbar = fig.colorbar(cmap) # prepare other components plot if show_components: # determine scaling factors and axis limits for plot 2 scaling_factors = [] line_comp = [] min_value = 0 max_value = 1 for (j,component_name) in enumerate(solution_components): min_comp, max_comp = py_reader.get_min_max(data, "solution", component_name) values_comp = py_reader.get_values(data[0], "solution", component_name) v = max(abs(max_comp), abs(min_comp)) if abs(v) < 1e-5: v = 1e-5 scaling_factor = abs(1./v) scaling_factors.append(scaling_factor) #print "{}, scaling_factor: {}, min_comp: {}, max_comp: {}".format(j, scaling_factor, min_comp, max_comp) if j > 0: min_value = min(min_value, min_comp*scaling_factor) max_value = max(max_value, max_comp*scaling_factor) line_plot, = ax3.plot([], [], '+-', lw=1, label=component_name) line_comp.append(line_plot) #print " min_value: {} -> {}, max_value: {} -> {}".format(min_comp*scaling_factor, min_value, max_comp*scaling_factor, max_value) ax3.set_xlim(min_x, max_x) if plot_over_time: ax3.set_xlabel('t') margin = abs(max_value - min_value) * 0.1 ax3.set_ylim(min_value - margin, max_value + margin) ax3.set_ylabel('Other components') if len(solution_components) > 5: ncol = len(solution_components)/10 ax3.legend(prop={'size': 6, }, ncol=ncol) else: ax3.legend() return top_text,
if show_plot: plt.show() #################### # 2D if dimension == 2: field_variable_names = py_reader.get_field_variable_names(data[0]) # classical 2D scalar field variables, like in Laplace eq. if "solution" in field_variable_names: debug = False min_value, max_value = py_reader.get_min_max(data, "solution", "0") min_x, max_x = py_reader.get_min_max(data, "geometry", "x") min_y, max_y = py_reader.get_min_max(data, "geometry", "y") print( "value range: [{}, {}]".format(min_value, max_value)) # prepare plot fig = plt.figure() margin = abs(max_value - min_value) * 0.1 ax = fig.add_subplot(111, projection='3d', xlim=(min_x, max_x), ylim=(min_y, max_y), zlim=(min_value-margin, max_value+margin)) # surface = ax.plot_surface([], [], [], cmap=cm.coolwarm, linewidth=1,rstride=1,cstride=1) # needed? error with python3 text = plt.figtext(0.15,0.85,"timestep",size=20) ax.set_xlabel('X') ax.set_ylabel('Y')
def init(): global geometry_component, line_2D, lines_2D, lines_3D, line_comp, cbar, top_text, ax1, ax2, cmap, show_geometry, show_components, show_specified_fields, \ specified_fields, solution_components, solution_name, solution_component, scaling_factors, min_x, max_x, min_y, max_y, min_z, max_z field_variable_names = py_reader.get_field_variable_names(data[0]) # determine solution variable and component # if field names have been specified, only plot those if show_specified_fields: # get the specified fields as (field_variable_name, component_name) tuples specified_fields = [] for specified_field_name in specified_field_names: for field_variable_name in field_variable_names: component_names = py_reader.get_component_names( data[0], field_variable_name) if field_variable_name == specified_field_name: if len(component_names) == 1: component_name = component_names[0] specified_fields.append( (field_variable_name, component_name)) else: for component_name in component_names: specified_fields.append( (field_variable_name, component_name)) elif specified_field_name in component_names: specified_fields.append( (field_variable_name, specified_field_name)) if len(specified_fields) == 0: print( "\033[0;31mError! Given names {} do not match any existing field variable names or component names.\033[0m\n" .format(specified_field_names)) quit() n_components = 0 # determine min/max values min_s = None max_s = None for (field_variable_name, component_name) in specified_fields: min_s_, max_s_ = py_reader.get_min_max(data, field_variable_name, component_name) print("\"{}.{}\" value range: [{}, {}]".format( field_variable_name, component_name, min_s_, max_s_)) min_s = (min_s_ if min_s is None else min(min_s_, min_s)) max_s = (max_s_ if max_s is None else max(max_s_, max_s)) # if no field names have been specified, search for the solution variable and component else: solution_name = "solution" if "solution" not in field_variable_names: for field_variable_name in field_variable_names: if field_variable_name != "geometry": component_names = py_reader.get_component_names( data[0], field_variable_name) if len(component_names) == 1: solution_name = field_variable_name break component_names = py_reader.get_component_names( data[0], solution_name) solution_component = component_names[0] min_s, max_s = py_reader.get_min_max(data, solution_name, solution_component) print("\"{}\" value range: [{}, {}]".format( solution_name, min_s, max_s)) solution_components = py_reader.get_component_names( data[0], solution_name) n_components = len(solution_components) if min_s is None or np.isinf(min_s) or np.isnan(min_s): min_s = 0 if max_s is None or np.isinf(max_s) or np.isnan(max_s): max_s = 0 span_x = max_x - min_x span_y = max_y - min_y span_z = max_z - min_z # if the geometry is a line, do not show geometry plot if span_y == 0 and span_z == 0: show_geometry = False if (not show_geometry) and n_components > 1: if solution_components[1] != "1": if not show_specified_fields: show_components = True if span_x >= span_y and span_x >= span_z: geometry_component = "x" elif span_y >= span_x and span_y >= span_z: geometry_component = "y" else: geometry_component = "z" if plot_over_time: # x-axis is time min_x = data[0]['currentTime'] max_x = data[-1]['currentTime'] else: # x-axis is geometry min_x, max_x = py_reader.get_min_max(data, "geometry", geometry_component) if show_geometry: print("geometry bounding box: x:[{},{}], y:[{},{}], z:[{},{}]". format(min_x, max_x, min_y, max_y, min_z, max_z)) # prepare plot if show_geometry: gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) ax2 = plt.subplot(gs[0], projection='3d') ax1 = plt.subplot(gs[1]) elif show_components: gs = gridspec.GridSpec(2, 1, height_ratios=[3, 4]) ax1 = plt.subplot(gs[0]) # main component ax3 = plt.subplot(gs[1]) # all other components else: ax1 = plt.gca() # prepare plot of specified fields if show_specified_fields: lines_2D = [] for (field_variable_name, component_name) in specified_fields: if component_name == "0": label = field_variable_name else: label = component_name line, = ax1.plot([], [], '+-', lw=2, label=label) lines_2D.append(line) ax1.set_xlim(min_x, max_x) ax1.set_xlabel('t') plt.subplots_adjust(right=0.66, top=0.94, bottom=0.18) ax1.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., frameon=False) else: # prepare main plot if data[0]["basisFunction"] == "Hermite" and data[0][ "onlyNodalValues"] == False: # for Hermite line_2D, = ax1.plot([], [], '-', color="b", lw=2, label=solution_components[0]) else: line_2D, = ax1.plot([], [], '+-', color="b", lw=2, label=solution_components[0]) last_length = 0 xlabel = geometry_component if plot_over_time: ax1.set_xlabel('t') else: ax1.set_xlabel(xlabel.upper()) ax1.set_ylabel(solution_name) if solution_components[0] != "0": plt.subplots_adjust(right=0.66, top=0.94, bottom=0.18) ax1.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., frameon=False) plt.tight_layout() ax1.set_xlim(min_x, max_x) margin = abs(max_s - min_s) * 0.1 ax1.set_ylim(min_s - margin, max_s + margin) top_text = ax1.text(0.5, 0.94, "", horizontalalignment='center', transform=ax1.transAxes, family='monospace') plt.grid(which='major') # prepare geometry plot if show_geometry: ax2.set_title("geometry") ax2.set_xlim(min_x, max_x) ax2.set_ylim(min_y, max_y) ax2.set_zlim(min_z, max_z) lines_3D = [] # plot line segments with corresponding color xdata = py_reader.get_values(data[0], "geometry", "x") for i in range(len(xdata)): p, = ax2.plot(xdata[i:i + 2], xdata[i:i + 2], xdata[i:i + 2], lw=3) lines_3D.append(p) ax2.set_xlabel('X') ax2.set_ylabel('Y') ax2.set_zlabel('Z') # manually create colorbar [https://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots] plt.sca(ax2) norm = mpl.colors.Normalize(vmin=min_s, vmax=max_s) cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet) cmap.set_array([]) cbar = fig.colorbar(cmap) # prepare other components plot if show_components: # determine scaling factors and axis limits for plot 2 scaling_factors = [] line_comp = [] min_value = 0 max_value = 1 for (j, component_name) in enumerate(solution_components): min_comp, max_comp = py_reader.get_min_max( data, solution_name, component_name) values_comp = py_reader.get_values(data[0], solution_name, component_name) v = max(abs(max_comp), abs(min_comp)) if abs(v) < 1e-5: v = 1e-5 scaling_factor = abs(1. / v) scaling_factors.append(scaling_factor) #print "{}, scaling_factor: {}, min_comp: {}, max_comp: {}".format(j, scaling_factor, min_comp, max_comp) if j > 0: min_value = min(min_value, min_comp * scaling_factor) max_value = max(max_value, max_comp * scaling_factor) line_plot, = ax3.plot([], [], '+-', lw=1, label=component_name) line_comp.append(line_plot) #print " min_value: {} -> {}, max_value: {} -> {}".format(min_comp*scaling_factor, min_value, max_comp*scaling_factor, max_value) ax3.set_xlim(min_x, max_x) if plot_over_time: ax3.set_xlabel('t') margin = abs(max_value - min_value) * 0.1 ax3.set_ylim(min_value - margin, max_value + margin) ax3.set_ylabel('Other components') if len(solution_components) > 5: ncol = max(1, len(solution_components) / 10) plt.subplots_adjust(right=0.66, top=0.94, bottom=0.18) ax3.legend(prop={ 'size': 6, }, ncol=ncol, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., frameon=False) else: plt.subplots_adjust(right=0.66, top=0.94, bottom=0.18) ax3.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., frameon=False) return top_text,
if dimension == 1: fig = plt.figure(figsize=(10, 8)) show_geometry = True # if the fibre geometry should be displayed in a 3D plot in a separate axis (ax2) on top of the solution plot show_components = False # if all the components of the solution should be displayed show_specified_fields = False # if only the specified fields should be plotted in 2D in a single axis plot_over_time = data[0]['nElements'] == [ 0 ] # if the plot should have time as x-axis instead of geometry if len(specified_field_names) > 0: show_specified_fields = True plot_over_time = True min_x, max_x = py_reader.get_min_max(data, "geometry", "x") min_y, max_y = py_reader.get_min_max(data, "geometry", "y") min_z, max_z = py_reader.get_min_max(data, "geometry", "z") if abs(min_x - max_x) < 1e-5 and abs(min_y - max_y) < 1e-5 and abs( min_z - max_z) < 1e-5: plot_over_time = True if plot_over_time: print("Plot over time.") last_length = 0 def init(): global geometry_component, line_2D, lines_2D, lines_3D, line_comp, cbar, top_text, ax1, ax2, cmap, show_geometry, show_components, show_specified_fields, \ specified_fields, solution_components, solution_name, solution_component, scaling_factors, min_x, max_x, min_y, max_y, min_z, max_z
#print(os.getcwd()) # load data data1 = py_reader.load_data(["build_release/out/febio_0000001.py"]) data2 = py_reader.load_data(["build_release/out/opendihu_0000001.py"]) # if files do not exist if data1 == [] or data2 == []: quit() component_name = "0" total_error = 0 values1 = py_reader.get_values(data1[0], "geometry", component_name) values2 = py_reader.get_values(data2[0], "geometry", component_name) min_u1, max_u1 = py_reader.get_min_max(data1, "u", "2") min_u2, max_u2 = py_reader.get_min_max(data2, "u", "z") print("maximum z-displacement febio: {}".format(max_u1)) print("maximum z-displacement opendihu: {}".format(max_u2)) # values2 contains entries for quadratic elements # extract the corner values n_elements = data2[0]['nElements'] nx = n_elements[0] ny = n_elements[1] nz = n_elements[2] mx = nx * 2 + 1 my = ny * 2 + 1 mz = nz * 2 + 1
print("{} files".format(len(solution_files))) data = py_reader.load_data(solution_files) if len(data) == 0: print("no data found.") sys.exit(0) dimension = data[0]['dimension'] #################### # 1D if dimension == 1: min_value, max_value = py_reader.get_min_max(data, "solution", "0") min_x, max_x = py_reader.get_min_max(data, "geometry", "x") # check if solution diverged diverged_tolerance = 1e100 if min_value < -diverged_tolerance or max_value > diverged_tolerance: print("Test failed: solution diverged. Value range: [{}, {}]".format( min_value, max_value)) sys.exit(0) # compare to reference solution error_absolute_timestep = [] error_relative_timestep = [] for dataset in data: timestep_no = dataset['timeStepNo']