Skip to content

Commit

Permalink
add saving and loading PETSc matrix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Elarif committed Aug 29, 2022
1 parent 96cceb6 commit 9af3a49
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import feelpp
from mpi4py import MPI
from petsc4py import PETSc


class FeelppSolution(object):
Expand Down Expand Up @@ -96,17 +97,18 @@ def CompressSolution(self, CorrelationOperator, reducedOrderBasis):
of size (numberOfModes, numberOfDOFs)
"""

numberOfModes = reducedOrderBasis.shape[0]
numberOfModes = reducedOrderBasis.size[0]

globalScalarProduct = PETSc.Vec().create()
globalScalarProduct.setSizes(numberOfModes)
globalScalarProduct.setFromOptions()

for time, solution in self.solution.items():

matVecProduct =solution.to_petsc().vec().copy()
CorrelationOperator.mat().mult(solution.to_petsc().vec(), matVecProduct)
CorrelationOperator.mult(solution.to_petsc().vec(), matVecProduct)

matVecProduct = np.array(matVecProduct)
localScalarProduct = np.dot(reducedOrderBasis, matVecProduct)
globalScalarProduct = np.zeros(numberOfModes)
MPI.COMM_WORLD.Allreduce([localScalarProduct, MPI.DOUBLE], [globalScalarProduct, MPI.DOUBLE])
reducedOrderBasis.mult(matVecProduct, globalScalarProduct)

self.compressedSol[time] = globalScalarProduct

Expand Down Expand Up @@ -144,7 +146,7 @@ def GetSnapshot(self, time):
return self.snapshots[time]


def GetreducedCoeff(self):
def GetCompressedSolution(self):
"""
Returns the compressed snapshot at time time
Expand All @@ -155,8 +157,7 @@ def GetreducedCoeff(self):
Returns
-------
np.ndarray
compressed snapshot
PETSc vector of compressed solution
"""
return self.compressedSol

Expand Down Expand Up @@ -209,42 +210,6 @@ def GetNumberOfNodes(self):
return self.numberOfNodes


def GetSnapshots(self):
"""
Returns the complete snapshots dictionary
Returns
-------
dict
the snapshots dictionary of the solution
"""
return self.solution


def GetCompressedSnapshots(self):
"""
Returns the complete compressedSnapshots dictionary
Returns
-------
dict
the compressed representation of the solution
"""
return self.compressedSnapshots


def GetCompressedSnapshotsList(self):
"""
Returns the compressed snapshots in the form of a list
Returns
-------
list
list containing the snapshots of the solution
"""
return list(self.compressedSnapshots.values())


def GetNumberOfSnapshots(self):
"""
Returns the number of snapshots
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
SavePetscArrayBin(file, l2ScalarProducMatrix.mat())
file = "stiffnessMatrix.dat"
SavePetscArrayBin(file, h1ScalarProducMatrix.mat())
file="reducedBasisU"
file="reducedBasisU.dat"
SavePetscArrayBin(file, reducedOrderBasisU)
# SIO.SaveState(file, reducedOrderBasisU)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## 01/2021

from dataclasses import Field
# from fileinput import filename
from hashlib import new
import os
import os.path as osp
Expand All @@ -15,6 +16,7 @@
from pathlib import Path
import array
import warnings
import timeit


import feelpp
Expand All @@ -24,7 +26,7 @@
import SnapshotReducedBasis as SRB
from Mordicus.Modules.Cemosis.Containers.SolutionStructure.FeelppSol import FeelppSolution as FppSol
from NIRBinitCase import *
from Mordicus.Core.IO import StateIO as SIO
from Mordicus.Modules.Cemosis.IO.StateIO import *


print("-----------------------------------")
Expand Down Expand Up @@ -73,6 +75,8 @@
geo_path = f"{modelsFolder}/square/square.geo"
model_path = f"{modelsFolder}/square/square.json"

msh_path = f"{dataFolder}/square.msh"

# fineness of two grids
H = 0.1 # CoarseMeshSize
h = H**2 # fine mesh size
Expand Down Expand Up @@ -102,21 +106,19 @@
Load offline datas for online part
-------------------------------------------------------
"""
filename= dataFolder + "reducedBasisU.dat"
reducedOrderBasisU = LoadPetscArrayBin(filename)
nev = reducedOrderBasisU.size[0]
filename= dataFolder + "massMatrix.dat"
MassMatrix = LoadPetscArrayBin(filename)
filename= dataFolder + "stiffnessMatrix.dat"
StiffnessMatrix = LoadPetscArrayBin(filename)

reducedOrderBasisU = SIO.LoadState(dataFolder+"reducedBasisU")
nev = reducedOrderBasisU.shape[0]

# to be done later (read the matrix from file)
Vh = feelpp.functionSpace(mesh=FineMesh)
MassMatrix=FppOp.mass(test=Vh,trial=Vh,range=feelpp.elements(FineMesh))
StiffnessMatrix=FppOp.stiffness(test=Vh,trial=Vh,range=feelpp.elements(FineMesh))
MassMatrix.mat().assemble()
StiffnessMatrix.mat().assemble()
MassMatrix.assemble()
StiffnessMatrix.assemble()

l2ScalarProducMatrix = MassMatrix
h1ScalarProducMatrix = StiffnessMatrix


"""
-----------------------------------------------------------------
Get coarse solution and project it on fine mesh
Expand All @@ -137,25 +139,49 @@
newSol = FppSol("UH",dimension, numberOfNodes)
newSol.AddSolution(interpSol, 0)
newSol.CompressSolution(l2ScalarProducMatrix, reducedOrderBasisU)
newCompressedSol = newSol.GetreducedCoeff()
reconstructedSolution = np.dot(newCompressedSol[0], reducedOrderBasisU)
newCompressedSol = newSol.GetCompressedSolution()

## Get new reconstructed solution
reconstructedSolution = interpSol.to_petsc().vec().copy()
reducedOrderBasisU.multTranspose(newCompressedSol[0], reconstructedSolution)

##################################################
# Save Online data for vizualisation
##################################################

print("-----------------------------------")
print(" STEP II. 4: Saving datas on Disk ")
print("-----------------------------------")

# export = feelpp.exporter(mesh=FineMesh, name="feelpp_nirb")
# export.addScalar("un", 1.)
# export.addP1c("u", reconstructedSolution)
# export.addP1c("U_interp", interpSol)
# e.addP0d("pid", feelpp.pid(P0h))
# export.save()
##################################################
# ONLINE ERRORS
##################################################
if ComputingError==1:

print("-----------------------------------")
print(" STEP II. 3: Compute online errors ")
print("-----------------------------------")

fineSol = np.array(interpSol.to_petsc().vec())
reducedSol = reconstructedSolution
diff = np.abs(fineSol - reducedSol)
print("-----------------------------------")
print(" STEP II. 3: Compute online errors ")
print("-----------------------------------")

FineSol = SolveFpp(tbFine, mu)

diffSolve = FineSol.to_petsc().vec() - reconstructedSolution
diffInterp = FineSol.to_petsc().vec() - interpSol.to_petsc().vec()

print("---------- NIRB Solve Error -----------------")
print ('l2-norm =', diffSolve.norm())
print ('Infinity-norm =', diffSolve.norm(PETSc.NormType.NORM_INFINITY))

print("---------- NIRB Interp Error -----------------")
print ('l2-norm =', diffInterp.norm())
print ('Infinity-norm =', diffInterp.norm(PETSc.NormType.NORM_INFINITY))

print('Inf norm = ', np.max(diff)/np.max(fineSol))
print('l2 discret norm = ',np.sqrt(diff.dot(diff))/np.sqrt(fineSol.dot(fineSol)) )
print("-----------------------------------------------")

print("NIRB ONLINE DONE! ")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,19 @@ def ComputeReducedOrderBasisWithPOD(snapshotList, snapshotCorrelationOperator, t
eigenMatrix.setFromOptions()
eigenMatrix.setUp()

eigenpairs = {}
# eigenpairs = {}

for i in range(nbePODModes):
eigenval[i] = float(E.getEigenvalue(i).real)
E.getEigenvector(i, eigenvect)
eigenpairs[eigenval[i]] = eigenvect/np.sqrt(eigenval[i]) # normalized eigenvect
# eigenpairs[eigenval[i]] = eigenvect/np.sqrt(eigenval[i]) # normalized eigenvect
eigenMatrix[i,:] = eigenvect[:]/np.sqrt(eigenval[i])

eigenMatrix.assemble()



## Set reduced basis
for s in snapshotList:
snapshots.append(s.to_petsc().vec()[:])


reducedOrderBasis = PETSc.Mat().createDense(size=(nbePODModes,numberOfDofs))
reducedOrderBasis.setFromOptions()
reducedOrderBasis.setUp()
Expand All @@ -122,13 +118,8 @@ def ComputeReducedOrderBasisWithPOD(snapshotList, snapshotCorrelationOperator, t

tempMat = reducedOrderBasis.copy()

# for i in range(nbePODModes):
# for j in range(numberOfDofs):
# reducedOrderBasis[i,j] =

for i in range(nbePODModes):
tempMat[i,:] = snapshotList[i].to_petsc().vec()[:]
# reducedOrderBasis[i,:] = snapshotList[i].to_petsc().vec()[:]

tempMat.assemble()

Expand Down
11 changes: 10 additions & 1 deletion src/poc-1/src/Mordicus/Modules/Cemosis/IO/StateIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
from mpi4py import MPI


def SavePetscArrayBin(filename, PetscAray):

def LoadPetscArrayBin(filename):
""" Load a PETSc array from filename writing in binary format """
outputfile = os.path.join(filename)
viewer = PETSc.Viewer().createBinary(outputfile, 'r')
PetscAray = PETSc.Mat().load(viewer)
return PetscAray

def SavePetscArrayBin(filename, PetscAray):
""" Save a PETSc array on filename in binary format """
outputfile = os.path.join(filename)

viewer = PETSc.Viewer().createBinary(outputfile, 'w')
viewer(PetscAray)


def SavePetscArrayASCII(filename, PetscAray):
""" Save a PETSc array on filename in ASCII format """

outputfile = os.path.join(filename)

Expand Down

0 comments on commit 9af3a49

Please sign in to comment.