<reponame>PGE383-HPC-Students/pge383-assignment18-harrykhuc
#!/usr/bin/env python
from assignment8 import StressStrainConverter
import numpy as np
import scipy.integrate

from PyTrilinos import Epetra

class EpetraParallelToughness(StressStrainConverter):

    def __init__(self, filename, comm):
        super().__init__(filename)

        self.comm = comm
        self.rank = comm.MyPID()
        self.size = comm.NumProc()

        if self.rank == 0:
            self.convert_to_true_stress_and_strain()
        else:
            self.true_stress = np.array([], dtype=np.double)
            self.true_strain = np.array([], dtype=np.double)

        ######################
        #### Add Code Here ###
        ######################
        #Creating Unbalanced Vector
        global_length = self.true_stress.shape[0]
        if self.rank == 0:
            vector_length = global_length
        else:
            vector_length = 0
        
        stdstressMap = Epetra.Map(global_length, vector_length, 0, self.comm)
        balancedstressMap = Epetra.Map(global_length, 0, self.comm)
        stdstrainMap = Epetra.Map(global_length, vector_length, 0, self.comm)
        balancedstrainMap = Epetra.Map(global_length, 0, self.comm)
        
        xstress = Epetra.Vector(stdstressMap, self.true_stress)
        ystress = Epetra.Vector(balancedstressMap)
        xstrain = Epetra.Vector(stdstrainMap, self.true_strain)
        ystrain = Epetra.Vector(balancedstrainMap)
        
        self.comm.Barrier
        
        importer_stress = Epetra.Import(balancedstressMap, stdstressMap)
        ystress.Import(xstress, importer_stress, Epetra.Insert)
        
        importer_strain = Epetra.Import(balancedstrainMap, stdstrainMap)
        ystrain.Import(xstrain, importer_strain, Epetra.Insert)
        
    def compute_toughness(self):
        
        my_toughness = scipy.integrate.trapz(self.true_stress, self.true_strain)

        return self.comm.SumAll(my_toughness)


if __name__ == "__main__":

    from PyTrilinos import Epetra

    comm = Epetra.PyComm()

    T = EpetraParallelToughness('data.dat', comm)

    if comm.MyPID() == 0:
        print(T.compute_toughness())

#!/usr/bin/env python
from assignment8 import StressStrainConverter
import numpy as np
import scipy.integrate

from PyTrilinos import Epetra

class EpetraParallelToughness(StressStrainConverter):

    def __init__(self, filename, comm):
        super().__init__(filename)

        self.comm = comm
        self.rank = comm.MyPID()
        self.size = comm.NumProc()

        if self.rank == 0:
            self.convert_to_true_stress_and_strain()
        else:
            self.true_stress = np.array([], dtype=np.double)
            self.true_strain = np.array([], dtype=np.double)

        ######################
        #### Add Code Here ###
        ######################
        #Creating Unbalanced Vector
        global_length = self.true_stress.shape[0]
        if self.rank == 0:
            vector_length = global_length
        else:
            vector_length = 0
        
        stdstressMap = Epetra.Map(global_length, vector_length, 0, self.comm)
        balancedstressMap = Epetra.Map(global_length, 0, self.comm)
        stdstrainMap = Epetra.Map(global_length, vector_length, 0, self.comm)
        balancedstrainMap = Epetra.Map(global_length, 0, self.comm)
        
        xstress = Epetra.Vector(stdstressMap, self.true_stress)
        ystress = Epetra.Vector(balancedstressMap)
        xstrain = Epetra.Vector(stdstrainMap, self.true_strain)
        ystrain = Epetra.Vector(balancedstrainMap)
        
        self.comm.Barrier
        
        importer_stress = Epetra.Import(balancedstressMap, stdstressMap)
        ystress.Import(xstress, importer_stress, Epetra.Insert)
        
        importer_strain = Epetra.Import(balancedstrainMap, stdstrainMap)
        ystrain.Import(xstrain, importer_strain, Epetra.Insert)
        
    def compute_toughness(self):
        
        my_toughness = scipy.integrate.trapz(self.true_stress, self.true_strain)

        return self.comm.SumAll(my_toughness)


if __name__ == "__main__":

    from PyTrilinos import Epetra

    comm = Epetra.PyComm()

    T = EpetraParallelToughness('data.dat', comm)

    if comm.MyPID() == 0:
        print(T.compute_toughness())
#!/usr/bin/env python ###################### #### Add Code Here ### ###################### #Creating Unbalanced Vector