def test_field_io_mpi(case): mesh = Mesh.create(case) Field.setMesh(mesh) time = 1.0 config.hdf5 = False with IOField.handle(time): U = IOField.read('U') U.partialComplete() config.hdf5 = True with IOField.handle(time, case=case): Uh = IOField.read('U') Uh.partialComplete()
def diffAllTimes(): case1, case2, field = sys.argv[1:] mesh1 = Mesh.create(case1) mesh2 = Mesh.create(case2) times1 = mesh1.getTimes() times2 = mesh2.getTimes() for time1, time2 in zip(times1, times2): Field.setMesh(mesh1) with IOField.handle(time1): phi1 = IOField.read(field) Field.setMesh(mesh2) with IOField.handle(time2): phi2 = IOField.read(field) diff = np.abs(phi1.field-phi2.field) norm = np.sqrt(parallel.sum(diff**2*mesh1.volumes)) pprint(parallel.min(diff)) pprint('norm:', norm)
#!/usr/bin/python2 import sys, os import numpy as np from adFVM import config from adFVM.field import Field, IOField from adFVM.mesh import Mesh case1, case2 = sys.argv[1:] mesh = Mesh.create(case1) times = mesh.getTimes() Field.setMesh(mesh) fields = ['p', 'T', 'U'] for name in fields: phimax = -1 for time in times: mesh.case = case1 phi1 = IOField.read(name, mesh, time) phi1.partialComplete() if phimax < 0: phimax = phi1.field.max() mesh.case = case2 phi2 = IOField.read(name, mesh, time) phi2.partialComplete() phi1.name += '_diff' phi1.field = (phi2.field-phi1.field)/phimax phi1.write(time)
def run(self, checkpointData, primalData): mesh = self.mesh result, firstCheckpoint = checkpointData (timeSteps,) = primalData sensTimeSeries = [] energyTimeSeries = [] startTime = timeSteps[nSteps - firstCheckpoint*writeInterval][0] if (firstCheckpoint > 0) or self.forceReadFields: fields = self.readFields(startTime) for phi in fields: phi.field *= mesh.volumes else: fields = self.fields pprint('STARTING ADJOINT') pprint('Number of steps:', nSteps) pprint('Write interval:', writeInterval) pprint() perturbations = [] for index in range(0, len(perturb)): perturbation = perturb[index](None, mesh, 0) if isinstance(perturbation, tuple): perturbation = list(perturbation) if not isinstance(perturbation, list):# or (len(parameters) == 1 and len(perturbation) > 1): perturbation = [perturbation] # complex parameter perturbation not supported perturbations.append(perturbation) totalCheckpoints = nSteps//writeInterval nCheckpoints = min(firstCheckpoint + runCheckpoints, totalCheckpoints) checkpoint = firstCheckpoint while checkpoint < nCheckpoints: pprint('PRIMAL FORWARD RUN {0}/{1}: {2} Steps\n'.format(checkpoint, totalCheckpoints, writeInterval)) primalIndex = nSteps - (checkpoint + 1)*writeInterval t = timeSteps[primalIndex, 0] dts = timeSteps[primalIndex:primalIndex+writeInterval+1, 1] solutions = primal.run(startTime=t, dt=dts, nSteps=writeInterval, mode='forward', reportInterval=reportInterval) pprint('ADJOINT BACKWARD RUN {0}/{1}: {2} Steps\n'.format(checkpoint, totalCheckpoints, writeInterval)) pprint('Time marching for', ' '.join(self.names)) if checkpoint == 0: t, _ = timeSteps[-1] if primal.dynamicMesh: lastMesh, lastSolution = solutions[-1] mesh.boundary = lastMesh.boundarydata[m:].reshape(-1,1) else: lastSolution = solutions[-1] fieldsCopy = [phi.copy() for phi in fields] for phi in fields: phi.field /= mesh.volumes self.writeFields(fields, t, skipProcessor=True) fields = fieldsCopy primal.updateSource(source(solutions[-1], mesh, 0)) pprint('Time step', writeInterval) for phi in fields: phi.info() #energyTimeSeries.append(getAdjointEnergy(primal, *fields)) inputs = [phi.field for phi in fields + solutions[-1]] + mesh.getTensor() + mesh.getScalar() energyTimeSeries.append(self.computeEnergy(*inputs)) pprint() for step in range(0, writeInterval): report = ((step + 1) % reportInterval) == 0 sample = ((step + 1) % sampleInterval) == 0 viscous = ((step + 1) % viscousInterval) == 0 adjointIndex = writeInterval-1 - step t, dt = timeSteps[primalIndex + adjointIndex] if primal.dynamicMesh: previousMesh, previousSolution = solutions[adjointIndex] # new mesh boundary mesh.boundary = previousMesh.boundary else: previousSolution = solutions[adjointIndex] pprint('Time step', adjointIndex) if report: pprint('Time marching for', ' '.join(self.names)) start = time.time() #for index in range(0, n): # fields[index].field *= mesh.volumes dtca = np.zeros((1, 1)).astype(config.precision) obja = np.ones((1, 1)).astype(config.precision) if self.homogeneousAdjoint: obja *= 0. inputs = [phi.field for phi in previousSolution] + \ [np.array([[dt]], config.precision)] + \ mesh.getTensor() + mesh.getScalar() + \ [x[1] for x in primal.sourceTerms] + \ primal.getBoundaryTensor(1) + \ [x[1] for x in primal.extraArgs] + \ [phi.field for phi in fields] + \ [dtca, obja] + \ [np.array([[self.scaling]], config.precision)] options = {'return_static': sample, 'zero_static': sample, 'return_reusable': report, 'replace_reusable': False, } start10 = time.time() if self.viscosityType and not matop_python and viscous: outputs = self.viscousMap(*inputs, **options) else: outputs = self.map(*inputs, **options) pprint(time.time()-start10) #print(sum([(1e-3*phi).sum() for phi in gradient])) #inp1 = inputs[:3] + inputs[-3:-1] #inp2 = [phi + 1e-3 for phi in inputs[:3]] + inputs[-3:-1] #x1 = primal.map(*inp1)[-2] #x2 = primal.map(*inp2)[-2] #print(x1, x2, x1-x2) #import pdb;pdb.set_trace() # gradients n = len(fields) gradient = outputs[:n] for index in range(0, n): fields[index].field = gradient[index] #fields[index].field = gradient[index]/mesh.volumes if matop_python: stackedFields = np.concatenate([phi.field/mesh.volumes for phi in fields], axis=1) stackedFields = np.ascontiguousarray(stackedFields) inputs = previousSolution + [self.scaling] kwargs = {'visc': self.viscosityType, 'scale': self.viscosityScaler, 'report':report} weight = interp.centralOld(getAdjointViscosity(*inputs, **kwargs), mesh) stackedPhi = Field('a', stackedFields, (5,)) stackedPhi.old = stackedFields newStackedFields = (matop_petsc.ddt(stackedPhi, dt) - matop_petsc.laplacian(stackedPhi, weight, correction=False)).solve() #newStackedFields = stackedFields/(1 + weight*dt) newFields = [newStackedFields[:,[0]], newStackedFields[:,[1,2,3]], newStackedFields[:,[4]] ] fields = self.getFields(newFields, IOField) for phi in fields: phi.field = np.ascontiguousarray(phi.field)*mesh.volumes #print([type(x) for x in outputs]) if sample: paramGradient = list(outputs[n:n + self.nParams]) # compute sensitivity using adjoint solution sensitivities = [] for index, perturbation in enumerate(perturbations): # make efficient cpu implementation #sensitivity = 0. #for derivative, delphi in zip(paramGradient, perturbation): # sensitivity += np.sum(derivative * delphi) sensitivity = cmesh.computeSensitivity(paramGradient, perturbation) sensitivities.append(sensitivity) sensitivities = parallel.sum(sensitivities, allreduce=False) if (nSteps - (primalIndex + adjointIndex)) > avgStart: for index in range(0, len(perturb)): result[index] += sensitivities[index] sensitivities = [sens/sampleInterval for sens in sensitivities] for i in range(0, sampleInterval): sensTimeSeries.append(sensitivities) if report: for phi in fields: phi.info() #energyTimeSeries.append(getAdjointEnergy(primal, *fields)) inputs = [phi.field for phi in fields + previousSolution] + mesh.getTensor() + mesh.getScalar() energyTimeSeries.append(self.computeEnergy(*inputs)) #print(self.computeEnergy(*inputs), getSymmetrizedAdjointEnergy(primal, *inputs[:6])) end = time.time() pprint('Time for adjoint iteration: {0}'.format(end-start)) pprint('Time since beginning:', end-config.runtime) pprint('Simulation Time and step: {0}, {1}'.format(*timeSteps[primalIndex + adjointIndex + 1])) pprint() #parallel.mpi.Barrier() self.writeStatusFile([checkpoint + 1, result]) #energyTimeSeries = mpi.gather(timeSeries, root=0) if parallel.rank == 0: with open(self.sensTimeSeriesFile, 'ab') as f: np.savetxt(f, sensTimeSeries) with open(self.energyTimeSeriesFile, 'ab') as f: np.savetxt(f, energyTimeSeries) sensTimeSeries = [] energyTimeSeries = [] checkpoint += 1 #exit(1) #print(fields[0].field.max()) fieldsCopy = [phi.copy() for phi in fields] for phi in fields: phi.field /= mesh.volumes self.writeFields(fields, t, skipProcessor=True) fields = fieldsCopy # write M_2norm if write_M_2norm: with IOField.handle(t): IOField('M_2norm', outputs[-1], (1,)).write() #for phi in fields: # phi.field /= mesh.volumes #self.writeFields(fields, t, skipProcessor=True) #for phi in fields: # phi.field *= mesh.volumes #print(fields[0].field.max()) #pprint(checkpoint, totalCheckpoints) if checkpoint >= totalCheckpoints: writeResult('adjoint', result, str(self.scaling), self.sensTimeSeriesFile) #for index in range(0, nPerturb): # writeResult('adjoint', result[index], '{} {}'.format(index, self.scaling)) self.removeStatusFile() return
import numpy as np import pytest @pytest.mark.skip def relative_error(U, Ur): if isinstance(Ur, np.ndarray): assert U.shape == Ur.shape return np.max(np.abs(U - Ur)) / np.max(np.abs(Ur)) def test_second_order(): case = '../cases/convection' mesh = Mesh.create(case) Field.setMesh(mesh) thres = 1e-5 X, Y = mesh.cellCentres[:, [0]], mesh.cellCentres[:, [1]] Uc = Field('U', 2 * X + X**3 * Y**2, (1, )) gradUc = gradOld(centralOld(Uc, mesh), ghost=True) gradUc.field = gradUc.field.reshape((mesh.nCells, 1, 3)) Xf, Yf = mesh.faceCentres[:, [0]], mesh.faceCentres[:, [1]] Ur = 2 * Xf + Xf**3 * Yf**2 U = Variable((mesh.symMesh.nCells, 1)) gradU = Variable((mesh.symMesh.nCells, 1, 3)) def interpolate(U, gradU, *meshArgs): mesh = Mesh.container(meshArgs) return secondOrder(U, gradU, mesh, 0)
import glob import os import sys import numpy as np import subprocess import pytest from adFVM import config from adFVM.field import Field, CellField, IOField from adFVM.mesh import Mesh from deep_eq import deep_eq def test_field(): case = '../cases/convection/' mesh = Mesh.create(case) Field.setMesh(mesh) n = mesh.nFaces Ur = np.random.rand(n, 1) Vr = np.random.rand(n, 1) + 2 U = Field('U', Ur, (1,)) V = Field('V', Vr, (1,)) Wr = (Ur + Vr/Ur)*Vr**0.5 W = (U + V/U)*V**0.5 assert np.allclose(W.field, Wr) @pytest.mark.skip def test_field_io(case, hdf5):
centres1 = mesh1.cellCentres[:mesh1.nInternalCells] centres2 = mesh2.cellCentres[:mesh2.nInternalCells] internalIndices = mapNearest(centres1, centres2) patchIndices = {} for patchID in mesh1.boundary: startFace, endFace, indices1 = getPatchInfo(mesh1, patchID) centres1 = mesh1.faceCentres[startFace:endFace] startFace, endFace, indices2 = getPatchInfo(mesh2, patchID) centres2 = mesh2.faceCentres[startFace:endFace] patchIndices[patchID] = mapNearest(centres1, centres2) #for field in mesh1.getFields(time1): for field in ['U', 'T', 'p', 'rho', 'rhoU', 'rhoE']: print 'interpolating', field Field.setMesh(mesh1) with IOField.handle(time1): phi1 = IOField.read(field) phi1.partialComplete() dims = phi1.dimensions phi2 = np.zeros((mesh2.nCells, ) + dims) phi2 = IOField(field, phi2, dims) phi2.field[:mesh2.nInternalCells] = phi1.field[internalIndices] for patchID in phi1.boundary: startFace, endFace, indices1 = getPatchInfo(mesh1, patchID) startFace, endFace, indices2 = getPatchInfo(mesh2, patchID) phi2.field[indices2] = phi1.field[indices1][patchIndices[patchID]] phi2.boundary = copy.deepcopy(phi1.boundary)