### Install Compilers Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/installation.txt Install the necessary C, C++, and Fortran compilers using apt-get. Verify installation by checking versions and paths. ```bash sudo apt-get update sudo apt-get install gcc gfortran g++ gcc --version which gcc gfortran g++ ``` -------------------------------- ### Install OpenMPI Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/installation.txt Download, compile, and install OpenMPI version 3.1.3. Ensure to configure with the desired prefix and disable dynamic loading. After installation, update the library linking cache. ```bash wget https://download.open-mpi.org/release/open-mpi/v3.1/openmpi-3.1.3.tar.gz tar zxf openmpi-3.1.3.tar.gz cd openmpi-3.1.3 ./configure prefix=/usr/local/ --disable-dlopen make sudo make install sudo ldconfig ``` -------------------------------- ### Prepare and Run MEAMfit Test Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/installation.txt Navigate to the test directory, update test scripts, and copy the compiled shared object file to the test directory. Then, run the MCMC tester script using python3. ```bash cd /home/andyduff/Documents/Work/progs/meamfit_python/meamfit/SampleCalculation/Input/MEAM/Test ./update.sh ``` ```bash cp meamfit_py15.cpython-38-x86_64-linux-gnu.so ../../../SampleCalculation/Input/MEAM/Test/ ``` ```python import meamfit_py15 ``` ```python python3 MCMC_tester.py ``` -------------------------------- ### Configure Environment Paths for OpenMPI Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/installation.txt Set the PATH and LD_LIBRARY_PATH environment variables to include the OpenMPI bin and lib directories. Replace placeholders with your actual OpenMPI path. ```bash export PATH=$HOME///bin:$PATH export LD_LIBRARY_PATH=$HOME///lib:$LD_LIBRARY_PATH ``` -------------------------------- ### Compare MEAMfit Parameters with Energy Landscape Minimums Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Compares the parameters obtained from MEAMfit with the minimums found in the 1D energy landscape plots. It reads MEAMfit parameters and plots vertical lines at these parameter values on the corresponding energy landscape plots. ```python #Check the minimum from grid search is the same as the one from MEAMfit: meamfit_params = meamfit_py15.read_params_py() theta = [0,0,0,1] theta[0] = meamfit_params[1][0][1] theta[1] = meamfit_params[3][0][1] theta[2] = meamfit_params[5][0][1] plt.plot(E_para_df["parameter"],E_para_df["E_p1"],label="parameter1") plt.vlines(theta[0], 0, 10, colors='blue', linestyles='solid', label='MEAMfit p1') plt.legend() plt.show() plt.plot(E_para_df["parameter"],E_para_df["E_p2"],label="parameter2") plt.vlines(theta[1], 0, 10, colors='blue', linestyles='solid', label='MEAMfit p2') plt.legend() plt.show() plt.plot(E_para_df["parameter"],E_para_df["E_p3"],label="parameter3") plt.vlines(theta[2], 0, 10, colors='blue', linestyles='solid', label='MEAMfit p3') plt.legend() plt.show() ``` -------------------------------- ### Compile MEAMfit for gfortran Parallel Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/installation.txt Clean previous builds, configure MEAMfit for the GNU compiler, and recompile. Remove stale module files if necessary before recompiling. ```bash make clean ./tools/mkconfig config/gnu ``` ```bash rm src/*.mod make ``` -------------------------------- ### Grid Search for Minimum Energy Parameters Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Performs a grid search over a 3D parameter space to find the parameters that minimize energy, using `meamfit_py15.meamfit`. It initializes MPI, creates a meshgrid of parameters, calculates energy for each combination, and identifies the minimum energy parameters. ```python #Grid of energy vs parameter, and brut force finding the minimum energy parameters meamfit_py15.start_mpi() p1, p2, p3 = np.meshgrid(np.arange(0, 5.1, 0.25),np.arange(0, 5.1, 0.25),np.arange(0, 5.1, 0.25)) grid_energy = np.array([[i,j,k, meamfit_py15.meamfit(i, j, k)] for [i,j,k] in np.array([p1.flatten(), p2.flatten(), p3.flatten()]).T]) print(grid_energy.T[3].argmin()) grid_energy[grid_energy.T[3].argmin()] ``` -------------------------------- ### Histogram Comparison of Accepted Parameters and Priors (b1, b2, b3) Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Compares the histograms of accepted MCMC parameters ('accepted_b1', 'accepted_b2', 'accepted_b3') with their respective prior distributions (prior1, prior2, prior3). Vertical lines indicate the first accepted value for each parameter. ```python plt.hist(df["accepted_b1"].values,alpha=0.45,label="b1") plt.hist(df["accepted_b2"].values,alpha=0.45,label="b2") plt.hist(df["accepted_b3"].values,alpha=0.45,label="b3") plt.vlines(df["accepted_b1"].iloc[0], 0, 1000, colors='blue', linestyles='solid', label='MEAMfit b1') plt.vlines(df["accepted_b2"].iloc[0], 0, 1000, colors='orange', linestyles='solid', label='MEAMfit b2') plt.vlines(df["accepted_b3"].iloc[0], 0, 1000, colors='green', linestyles='solid', label='MEAMfit b3') plt.hist(prior1,color="black",alpha=0.2) plt.hist(prior2,color="black",alpha=0.2) plt.hist(prior3,color="black",alpha=0.2) plt.legend() plt.xlabel("Parameter") plt.ylabel("Counts") plt.show() ``` -------------------------------- ### MCMC Sampler Implementation Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb This snippet implements the core Metropolis-Hastings MCMC algorithm. It iteratively samples from a posterior distribution by proposing new parameter values and accepting or rejecting them based on their probability relative to the current state. It records accepted and rejected parameters, posterior probabilities, and likelihoods at each step. ```python import numpy as np import scipy.stats import math import re import pandas as pd import subprocess import time import os import arviz as az import meamfit_py as meamfit_py15 import matplotlib.pyplot as plt # Dataframe for recording values at each step vals = pd.DataFrame(columns=['accepted_b1', 'rejected_b1', 'accepted_b2', 'rejected_b2', 'accepted_b3', 'rejected_b3', 'accepted_b4', 'rejected_b4', 'curr_post', 'prop_post', 'prob', 'likelihood', 'change_accepted']) iterations = 10000 meamfit_params = meamfit_py15.read_params_py() theta = [0,0,0,1] theta[0] = meamfit_params[1][0][1] theta[1] = meamfit_params[3][0][1] theta[2] = meamfit_params[5][0][1] def likelihood(theta): likelihoods = np.exp((-meamfit_py15.meamfit(theta[0], theta[1], theta[2]))/theta[3]) return likelihoods #Priors meam_prior_var = 5 b1_dist = scipy.stats.halfnorm(0,meam_prior_var) b2_dist = scipy.stats.halfnorm(0,meam_prior_var) b3_dist = scipy.stats.halfnorm(0,meam_prior_var) b4_dist = scipy.stats.gamma(2,0.5) # Functions needed for prob calc at each step def prior(theta): b1_prior = b1_dist.pdf(theta[0]) b2_prior = b2_dist.pdf(theta[1]) b3_prior = b3_dist.pdf(theta[2]) b4_prior = b4_dist.pdf(theta[3]) return b1_prior * b2_prior * b3_prior * b4_prior def posterior(theta): return likelihood(theta) * prior(theta) # Current posterior curr_post = posterior(theta) # MCMC Loop for i in range(1, iterations): if i % 100 == 0: print("---------------- \n {}".format(i)) # Proposed step theta_proposal = [np.random.normal(theta[0], 0.25), np.random.normal(theta[1], 0.25), np.random.normal(theta[2], 0.25), np.random.normal(theta[3], 0.25)] # Proposed posterior prop_post = posterior(theta_proposal) # If prob > 1 the proposed theta is accepted, if not, value between 0 and 1 is the prob of acceptance prob = (prop_post / curr_post) if np.random.uniform(0,1) < prob and prob > 0: vals = vals.append({'accepted_b1' :theta_proposal[0], 'rejected_b1' :theta[0], 'accepted_b2' :theta_proposal[1], 'rejected_b2' :theta[1], 'accepted_b3' :theta_proposal[2], 'rejected_b3' :theta[2], 'accepted_b4' :theta_proposal[3], 'rejected_b4' :theta[3], 'curr_post' :curr_post, 'prop_post' :prop_post, 'prob' :prob, 'likelihood' :likelihood(theta_proposal), 'change_accepted': True}, ignore_index=True) theta = theta_proposal curr_post = prop_post else: vals = vals.append({'accepted_b1' :theta[0], 'rejected_b1' :theta_proposal[0], 'accepted_b2' :theta[1], 'rejected_b2' :theta_proposal[1], 'accepted_b3' :theta[2], 'rejected_b3' :theta_proposal[2], 'accepted_b4' :theta[3], 'rejected_b4' :theta_proposal[3], 'curr_post' :curr_post, 'prop_post' :prop_post, 'prob' :prob, 'likelihood' :likelihood(theta), 'change_accepted': False}, ignore_index=True) ``` -------------------------------- ### Plotting MCMC Convergence Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Visualizes MCMC convergence by plotting accepted and rejected coefficient values against the number of iterations. Requires matplotlib and seaborn. ```python def plot_converg(accept, reject ,string, predicted): ax = plt.subplot(1,1,1) plt.grid(which='both', color='black', linestyle='-', linewidth=0.5, alpha=0.5) plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.4) ax.plot(range(1,len(accept)+1), accept, color = 'black', marker=None, alpha=0.8) plt.title("coefficient {} (starting {})".format(string, predicted)) plt.scatter(range(1,len(accept)+1), reject, marker='x', alpha=0.18, color='red') plt.xlabel('no. iterations') plt.ylabel(string) ax.annotate(string, xy=(range(1,len(accept)+1),accept), xytext=(5,0), xycoords=(ax.get_xaxis_transform(), ax.get_yaxis_transform()), textcoords='offset points', va='center') plt.legend(["accepted", "rejected"], loc ="lower right") plt.show() plt.draw() import seaborn as sns burn_in =0 import matplotlib.pyplot as plt ``` -------------------------------- ### Clone MEAMfit Repository Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/installation.txt Clone the MEAMfit repository from GitLab. Use the single-branch option to directly checkout the specified branch. ```bash git clone git@gitlab.com:AndyDuff123/meamfit.git git switch gfortran_modifications ``` ```bash git clone --single-branch --branch gfortran_modifications git@gitlab.com:AndyDuff123/meamfit.git ``` -------------------------------- ### Generating Prior Distributions Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Generates random variates for prior distributions using scipy.stats.halfnorm and numpy.random.gamma. These priors are used for comparison with the MCMC results. ```python prior1 = scipy.stats.halfnorm(0,meam_prior_var).rvs(size=df.shape[0]) prior2 = scipy.stats.halfnorm(0,meam_prior_var).rvs(size=df.shape[0]) prior3 = scipy.stats.halfnorm(0,meam_prior_var).rvs(size=df.shape[0]) prior4 = np.random.gamma(2,0.5,size=df.shape[0]) ``` -------------------------------- ### Histogram Comparison of Accepted b4 and Prior Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Compares the histogram of accepted 'b4' MCMC parameter with its prior distribution (prior4). This visualization helps assess how the posterior distribution of 'b4' relates to its prior. ```python plt.hist(df["accepted_b4"].values,alpha=0.45,label="sigma posterior") plt.hist(prior4,color="black",alpha=0.2, label="sigma prior") plt.legend() plt.xlabel("Parameter") plt.ylabel("Counts") plt.show() ``` -------------------------------- ### Histogram Comparison of Accepted b3 and Prior Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Compares the histogram of accepted 'b3' MCMC parameter with its prior distribution (prior3). A vertical line indicates the first accepted 'b3' value. ```python plt.hist(df["accepted_b3"].values,alpha=0.45,label="b3") plt.vlines(df["accepted_b3"].iloc[0], 0, 1000, colors='blue', linestyles='solid', label='MEAMfit b3') plt.hist(prior3,color="black",alpha=0.2) plt.legend() plt.xlabel("Parameter") plt.ylabel("Counts") plt.show() ``` -------------------------------- ### Plot MCMC Posterior Distribution Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Visualizes the posterior distributions of MCMC parameters. Requires `matplotlib.pyplot` to be imported as `plt`. ```python az.plot_posterior(dict_acc) plt.show() ``` -------------------------------- ### Histogram Comparison of Accepted b2 and Prior Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Compares the histogram of accepted 'b2' MCMC parameter with its prior distribution (prior2). A vertical line indicates the first accepted 'b2' value. ```python plt.hist(df["accepted_b2"].values,alpha=0.45,label="b2") plt.vlines(df["accepted_b2"].iloc[0], 0, 1000, colors='blue', linestyles='solid', label='MEAMfit b2') plt.hist(prior2,color="black",alpha=0.2) plt.legend() plt.xlabel("Parameter") plt.ylabel("Counts") plt.show() ``` -------------------------------- ### Histogram Comparison of Accepted b1 and Prior Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Compares the histogram of accepted 'b1' MCMC parameter with its prior distribution (prior1). A vertical line indicates the first accepted 'b1' value. ```python plt.hist(df["accepted_b1"].values,alpha=0.45,label="b1") plt.vlines(df["accepted_b1"].iloc[0], 0, 1000, colors='blue', linestyles='solid', label='MEAMfit b1') plt.hist(prior1,color="black",alpha=0.2) plt.legend() plt.xlabel("Parameter") plt.ylabel("Counts") plt.show() ``` -------------------------------- ### Prepare Data for ArviZ Plotting Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Converts selected columns from a pandas DataFrame into a dictionary format suitable for ArviZ plotting functions. ```python df_acc = df[["accepted_b1","accepted_b2","accepted_b3"]] dict_acc = dict(df_acc) ``` -------------------------------- ### Plot MCMC Pairwise Relationships Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Creates a matrix of pairwise relationships between MCMC parameters, including scatter plots and marginal distributions. ```python az.plot_pair( dict_acc ); ``` -------------------------------- ### 1D Energy Landscape Plot Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Generates 1D plots of the energy landscape for each parameter around the identified minimums. It calculates energy values for a range of each parameter while keeping others fixed at their minimum values, then plots these against the parameter values. Requires `pandas` and `matplotlib.pyplot`. ```python #1D plot of energy landscape about minimums E_para_df = pd.DataFrame( { "parameter": np.array([k for k in np.linspace(0,5,25)]), "E_p1": np.array([meamfit_py15.meamfit(2, 2, k) for k in np.linspace(0,5,25)]), "E_p2": np.array([meamfit_py15.meamfit(2, j, 3) for j in np.linspace(0,5,25)]), "E_p3": np.array([meamfit_py15.meamfit(i, 2, 3) for i in np.linspace(0,5,25)]), } ) plt.plot(E_para_df["parameter"],E_para_df["E_p1"],label="parameter1") plt.plot(E_para_df["parameter"],E_para_df["E_p2"],label="parameter2") plt.plot(E_para_df["parameter"],E_para_df["E_p3"],label="parameter3") plt.xlabel("Parameter") plt.ylabel("Energy") plt.legend() plt.show() ``` -------------------------------- ### Plot MCMC Trace Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Generates trace plots for MCMC chains, showing the sampled values over iterations. Requires `matplotlib.pyplot` to be imported as `plt`. ```python az.plot_trace(dict_acc) plt.show() ``` -------------------------------- ### Display DataFrame Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Displays the contents of the 'vals' DataFrame. This is typically used to inspect the loaded or processed data. ```python vals ``` -------------------------------- ### End MPI and Save/Load Data Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Ends the MPI process and saves the current data to a CSV file, then reloads it. This is useful for checkpointing or preparing data for further analysis. ```python meamfit_py15.end_mpi() vals.to_csv('run14.csv') vals=pd.read_csv('run14.csv') ``` -------------------------------- ### Plotting Accepted MCMC Chains Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Plots the 'accepted_b1', 'accepted_b2', and 'accepted_b3' values from the filtered DataFrame. This is useful for visualizing the trends in accepted parameters during the MCMC process. ```python df = vals[vals["change_accepted"] == True] # df = vals plt.plot(df["accepted_b1"].values) plt.plot(df["accepted_b2"].values) plt.plot(df["accepted_b3"].values) plt.show() ``` -------------------------------- ### Plot MCMC Forest Plot Source: https://gitlab.com/andyduff123/meamfit/-/blob/master/pythonFunctionality/MCMC_test3.ipynb Generates a forest plot from a dictionary of MCMC chains, useful for visualizing credible intervals. ```python az.plot_forest(dict_acc) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.