#!/bin/bash

# This script takes the block of atoms stored in the LAMMPS data file data.mo-101_111.R15.d111,
# remembers which atom is active (tagged #1) and which inactive (tagged #0) and assigns these #1
# and #0 flags to the atoms in the file data.relaxed that is obtained by relaxing the block with the
# dislocation in LAMMPS and storing it by write_data. This tagged file is then stored as
# data.relaxed01.

funrel='data.mo-101_111.R15.d111';   # this is the file generated by makextal (unrelaxed dislo.)
frel='data.relaxed';    # this file is obtained from LAMMPS by write_data
fout='data.relaxed01';  # this is the same as data.relaxed but contains #0 and #1 from funrel

awk '$2 ~ "atoms" { nat=$1; } 
	{ print ""; }
	/Atoms/ {
		getline; 
		for (i=1; i<=nat; i++) {
			getline;
			tag[$1] = $6;
		}
	}
	END {
		print "";
		for (i=1; i<=nat; i++) {
			printf("%s\n", tag[i]);
		}
	}' $funrel > tmp

awk '$2 ~ "atoms" { nat=$1; }
    { print $0; }
	/Atoms/ {
		getline;
		for (i=1; i<=nat; i++) {
			getline;
			line[$1] = $1 " " $2 " " $3 " " $4 " " $5;
		}
		exit;
	}
	END {
		print "";
		for (i=1; i<=nat; i++) {
			printf("%s\n", line[i]);
		}
	}' $frel > tmp2

paste tmp2 tmp > $fout
rm tmp tmp2

echo 'The data file for LAMMPS is stored in' $fout;
