Page MenuHomec4science

post_processing.py
No OneTemporary

File Metadata

Created
Sun, Jun 9, 23:40

post_processing.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 14 15:19:11 2019
Authors: Omid Ashtari and Armand Sieber
Description: Scripts intended to display minimization proplems results
"""
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def objective(X, *args): #objective function to be minimized
A = args[0]
B = args[1].T
S = np.dot(X.T, np.dot(A, X)) - np.dot(X.T, B)
return S
def plot_results(X_int, A, B, method):
# Pre-processing the data to be dispalyed
##########################################
# Set domain size and coordinates according to intermediate solution of minimization problem
x1 = np.max(np.abs(X_int[:, 0]))
x2 = np.max(np.abs(X_int[:, 1]))
X_domain = np.linspace(-1.5*x1, 1.5*x1, 100)
Y_domain = np.linspace(-1.5*x2, 1.5*x2, 100)
xx, yy = np.meshgrid(X_domain, Y_domain)
# Compute S for the computational domain (S_post) and intermediate minimization solution (S_int)
S_post = np.array([objective(np.array([x , y]), A, B)
for x, y in zip(np.ravel(xx), np.ravel(yy))]) # Value of S for 3D plot (whole X-Y domain)
S_post = S_post.reshape(xx.shape)
S_int = np.array([objective(np.array([x , y]), A, B)
for x, y in zip(X_int[:,0], X_int[:,1])]) # Value of S at intermediate minimization steps
# Dispalying the results
########################
fig = plt.figure(1) # 3D plot
ax = plt.axes(projection = '3d')
ax.plot3D(X_int[:,0], X_int[:,1], S_int, 'r--o', alpha = 1.0, zorder = 10)
surface = ax.plot_surface(xx, yy, S_post, cmap = 'coolwarm', edgecolor = 'none', alpha = 1.0, zorder = 0) # zorder 'best' solution found to plot S_int over S_post
# --> alternative using mayavi
ax.set_xlabel('$x$', fontsize = 14)
ax.set_ylabel('$y$', fontsize = 14)
ax.set_zlabel('$S$', fontsize = 14)
ax.view_init(elev = 60.0, azim = 130.0)
ax.set_title('Minimization method: %s' % method, fontsize = 14, pad = 32)
plt.savefig('3D_representation.pdf')
fig = plt.figure(2) # 2D contour plot
ax = fig.add_subplot(111)
ax.contour(xx, yy, S_post, colors = 'k', levels = 20)
contour = ax.contourf(xx, yy, S_post, cmap = 'coolwarm', levels = 20)
ax.plot(X_int[:,0], X_int[:,1], 'r--o')
ax.set_xlabel('$x$', fontsize = 14)
ax.set_ylabel('$y$', fontsize = 14)
ax.set_title('Minimization method: %s' % method, fontsize = 14)
fig.colorbar(contour, ax=ax)
plt.savefig('2D_projection.pdf')
plt.show()
return 0

Event Timeline