Skip to content

Commit 54332df

Browse files
authored
Migration (#1)
* beginning migration of hippyflow * updating collectives, migrating away from the open PR brach of hippylib for collectives, moved those files here so now I am using hippylib master, adding generalized EVP for the active subspace * working on updating active subspace and kle * updating active subspace and driver * updating * updating readme and data generators * adding ice and confusion models * updating slightly for ccgo machines * missing hdf5s * adding confusion neural network training driver * updating ice problem setup, parallel runs are no go for larger 3d meshes I guess * updating * updating helmholtz is crashing my computer * updated some things want to check in * this is the setup I used that gets 3% margin of victory * updating gitignore * updating gitignore * updating the repo * removing ice_flow for now * adding notebook for plotting eigenvectors * updating the repo * updating * updating * udapting * readme rendering * readme rendering * readme rendering * updating install markdown * adding before removing * updating with licences * updating header for copyright * updating copyright * adding version * wrong version in version * updating hippylib files * adding some doc strings * adding more docstrings * updating * adding helmholtz neural network training * updating for the night * updatings * updating markdown files * install readme * install readme * install readme * install readme
1 parent 3e7e956 commit 54332df

38 files changed

Lines changed: 5692 additions & 1 deletion

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55

6+
# My custom ignores
7+
*logging*
8+
*.DS_Store*
9+
610
# C extensions
711
*.so
812

COPYRIGHT

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file is part of the hIPPYflow library.
2+
#
3+
# hIPPYflow is free software; you can redistribute it and/or modify it under the
4+
# terms of the GNU General Public License (as published by the Free
5+
# Software Foundation) version 2.0 dated June 1991.
6+
7+
COPYRIGHT
8+
9+
The following copyright applies to each file in the hIPPYlib
10+
distribution, unless otherwise stated in the file:
11+
12+
Copyright (c) 2020-, The University of Texas at Austin
13+
& Washington University in St. Louis.
14+
15+
LICENSE
16+
17+
hIPPYflow is a free software; you can redistribute it and/or modify it
18+
under the terms of the GNU General Public License (as published by the
19+
Free Software Foundation) version 2.0 dated June 1991.
20+
21+
hIPPYflow is distributed in the hope that it will be useful, but
22+
WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
23+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms
24+
and conditions of the GNU Lesser General Public License for more
25+
details.
26+
27+
You should have received a copy of the GNU Lesser General Public
28+
License along with this library (file LICENSE); if not, write to the
29+
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30+
Boston, MA 02110-1301 USA.
31+
32+
33+
CONTACT INFORMATION
34+
35+
tom.olearyroseberry@utexas.edu

INSTALL.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Dimension reduced surrogate construction for parametric PDE maps
2+
3+
___ ___ ___
4+
/__/\ ___ / /\ / /\ ___
5+
\ \:\ / /\ / /::\ / /::\ /__/|
6+
\__\:\ / /:/ / /:/\:\ / /:/\:\ | |:|
7+
___ / /::\ /__/::\ / /:/~/:/ / /:/~/:/ | |:|
8+
/__/\ /:/\:\ \__\/\:\__ /__/:/ /:/ /__/:/ /:/ __|__|:|
9+
\ \:\/:/__\/ \ \:\/\ \ \:\/:/ \ \:\/:/ /__/::::\
10+
\ \::/ \__\::/ \ \::/ \ \::/ ~\~~\:\
11+
\ \:\ /__/:/ \ \:\ \ \:\ \ \:\
12+
\ \:\ \__\/ \ \:\ \ \:\ \__\/
13+
\__\/ \__\/ \__\/
14+
15+
16+
___ ___ ___
17+
/ /\ / /\ /__/\
18+
/ /:/_ / /::\ _\_ \:\
19+
/ /:/ /\ ___ ___ / /:/\:\ /__/\ \:\
20+
/ /:/ /:/ /__/\ / /\ / /:/ \:\ _\_ \:\ \:\
21+
/__/:/ /:/ \ \:\ / /:/ /__/:/ \__\:\ /__/\ \:\ \:\
22+
\ \:\/:/ \ \:\ /:/ \ \:\ / /:/ \ \:\ \:\/:/
23+
\ \::/ \ \:\/:/ \ \:\ /:/ \ \:\ \::/
24+
\ \:\ \ \::/ \ \:\/:/ \ \:\/:/
25+
\ \:\ \__\/ \ \::/ \ \::/
26+
\__\/ \__\/ \__\/
27+
28+
29+
30+
* PDE data generation is handled by `FEniCS` and `hIPPYlib`. For this [`hIPPYlib`](https://github.com/hippylib/hippylib) must be installed.
31+
32+
With conda
33+
34+
* `conda create -n hippyflow -c uvilla -c conda-forge fenics==2019.1.0 tensorflow matplotlib scipy jupyter mpi4py keras`
35+
36+
* `pip install pympler` pympler is used to check memory usage during subspace creation
37+
38+
Assumes that the environmental variables `HIPPYLIB_PATH` and `HIPPYFLOW_PATH` have been set.
39+
40+
* `export HIPPYLIB_PATH=path/to/hippylib`
41+
* `export HIPPYFLOW_PATH=path/to/hippyflow`
42+
43+
44+
45+
Neural network training is handled by `keras` / second order methods in hessianlearn
46+
47+
When using second order optimizers, the code assumes that the environmental variable `HESSIANLEARN_PATH` has been set to the path to [`hessianlearn`](https://github.com/tomoleary/hessianlearn).
48+
49+
* `export HESSIANLEARN_PATH=path/to/hessianlearn`
50+
51+
In some cases (such as on Unix clusters) the dependencies for `hIPPYlib` and `tensorflow` clash. In this case different conda environments may be needed for the data generation phase and the neural network training phase. These two phases can be easily de-coupled in this case.
52+
53+
On Mac, there has been no issue creating a singular conda environment to handle all dependencies.

LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
# hippyflow
1+
Dimension reduced surrogate construction for parametric PDE maps
2+
3+
___ ___ ___
4+
/__/\ ___ / /\ / /\ ___
5+
\ \:\ / /\ / /::\ / /::\ /__/|
6+
\__\:\ / /:/ / /:/\:\ / /:/\:\ | |:|
7+
___ / /::\ /__/::\ / /:/~/:/ / /:/~/:/ | |:|
8+
/__/\ /:/\:\ \__\/\:\__ /__/:/ /:/ /__/:/ /:/ __|__|:|
9+
\ \:\/:/__\/ \ \:\/\ \ \:\/:/ \ \:\/:/ /__/::::\
10+
\ \::/ \__\::/ \ \::/ \ \::/ ~\~~\:\
11+
\ \:\ /__/:/ \ \:\ \ \:\ \ \:\
12+
\ \:\ \__\/ \ \:\ \ \:\ \__\/
13+
\__\/ \__\/ \__\/
14+
15+
16+
___ ___ ___
17+
/ /\ / /\ /__/\
18+
/ /:/_ / /::\ _\_ \:\
19+
/ /:/ /\ ___ ___ / /:/\:\ /__/\ \:\
20+
/ /:/ /:/ /__/\ / /\ / /:/ \:\ _\_ \:\ \:\
21+
/__/:/ /:/ \ \:\ / /:/ /__/:/ \__\:\ /__/\ \:\ \:\
22+
\ \:\/:/ \ \:\ /:/ \ \:\ / /:/ \ \:\ \:\/:/
23+
\ \::/ \ \:\/:/ \ \:\ /:/ \ \:\ \::/
24+
\ \:\ \ \::/ \ \:\/:/ \ \:\/:/
25+
\ \:\ \__\/ \ \::/ \ \::/
26+
\__\/ \__\/ \__\/
27+
28+
29+
30+
* This code is used to construct dimension reduced neural network surrogeates for parametric mappings governed by PDEs
31+
32+
* [`hIPPYlib`](https://github.com/hippylib/hippylib) is used for the data generation and construction of model based projectors
33+
34+
* `Tensorflow` and `keras` are used for the construction of neural network surrogates
35+
36+
* [`hessianlearn`](https://github.com/tomoleary/hessianlearn) is used for second order optimization of keras neural network models.
37+
38+
39+
## Model Based Projectors:
40+
41+
* `hIPPYflow` implements software infrastructure for input and output dimension reduction strategies for parametric mappings governed by PDEs. Given a parametric PDE Variational Problem implemented in `hIPPYlib` (using `FEniCS` for finite element representation), and a PDE observable, this code automates the construction of dominant subspaces of the input and output for these mappings. `hIPPYflow` implements both active subspace (AS) and Karhunen Loeve expansion (KLE) for input dimension reduction. `hIPPYflow` implements proper orthogonal decomposition (POD) for output dimension reduction.
42+
43+
* These constructs also implement the generation of training data to be used in surrogate construction, as well as projection error tests that exemplify how good the different model projectors are at capturing key information, and help to detect the "intrinsic dimensionality" of the mappings from inputs to outputs.
44+
45+
## Dimension Reduced Neural Network Strategies
46+
47+
* Given information about dominant subspaces of the input and output spaces for the parametric mappings, `hIPPYflow` implements dimension reduced neural network surrogates. These surrogates allow for parsimonious representations of input-output mappings that can achieve good accuracy for very few training data. Few data is a key feature of many high dimensional PDE based inference problems.
48+
49+
* Neural network models are implemented in `keras`. Training can be handled directly by `keras`, or using second order optimizers implemented in `hessianlearn`.

applications/confusion/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/*
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Copyright (c) 2020, The University of Texas at Austin
2+
# & Washington University in St. Louis.
3+
#
4+
# All Rights reserved.
5+
# See file COPYRIGHT for details.
6+
#
7+
# This file is part of the hIPPYflow package. For more information see
8+
# https://github.com/hippylib/hippyflow/
9+
#
10+
# hIPPYflow is free software; you can redistribute it and/or modify it under the
11+
# terms of the GNU General Public License (as published by the Free
12+
# Software Foundation) version 2.0 dated June 1991.
13+
14+
15+
import dolfin as dl
16+
import numpy as np
17+
import ufl
18+
19+
path_to_hippylib = '../../hippylib/'
20+
import sys, os
21+
sys.path.append( os.environ.get('HIPPYLIB_PATH',path_to_hippylib))
22+
from hippylib import *
23+
24+
sys.path.append( os.environ.get('HIPPYFLOW_PATH'))
25+
from hippyflow import LinearStateObservable
26+
27+
def confusion_linear_observable(mesh,sqrt_n_obs = 10,output_folder ='confusion_setup/',\
28+
verbose = False,seed = 0):
29+
class confusion_varf:
30+
def __init__(self,Vh,save_fields = False,output_folder = 'confusion_setup/'):
31+
'''
32+
33+
'''
34+
self.Vh = Vh
35+
# Gaussian blob for the right hand side
36+
self.f = dl.interpolate( dl.Expression('max(0.5,exp(-25*(pow(x[0]-0.7,2) + pow(x[1]-0.7,2))))',degree=5), Vh[STATE])
37+
# Compute velocity field
38+
self.v = self.computeVelocityField(Vh[STATE].mesh())
39+
if save_fields:
40+
if not os.path.isdir(output_folder):
41+
os.mkdir(output_folder)
42+
f_pvd = dl.File(output_folder+'f_blob.pvd')
43+
f_pvd << self.f
44+
v_pvd = dl.File(output_folder+'v_sol.pvd')
45+
v_pvd << self.v
46+
# Constant coefficients for the PDE
47+
self.c = dl.Constant(1.0)
48+
self.k = dl.Constant(0.01)
49+
50+
51+
def computeVelocityField(self,mesh):
52+
def v_boundary(x,on_boundary):
53+
return on_boundary
54+
55+
def q_boundary(x,on_boundary):
56+
return x[0] < dl.DOLFIN_EPS and x[1] < dl.DOLFIN_EPS
57+
58+
Xh = dl.VectorFunctionSpace(mesh,'Lagrange', 2)
59+
Wh = dl.FunctionSpace(mesh, 'Lagrange', 1)
60+
mixed_element = ufl.MixedElement([Xh.ufl_element(), Wh.ufl_element()])
61+
XW = dl.FunctionSpace(mesh, mixed_element)
62+
63+
Re = dl.Constant(1e2)
64+
65+
g = dl.Expression(('0.0','(x[0] < 1e-14) - (x[0] > 1 - 1e-14)'), degree=1)
66+
bc1 = dl.DirichletBC(XW.sub(0), g, v_boundary)
67+
bc2 = dl.DirichletBC(XW.sub(1), dl.Constant(0), q_boundary, 'pointwise')
68+
bcs = [bc1, bc2]
69+
70+
vq = dl.Function(XW)
71+
(v,q) = ufl.split(vq)
72+
(v_test, q_test) = dl.TestFunctions (XW)
73+
74+
def strain(v):
75+
return ufl.sym(ufl.grad(v))
76+
77+
F = ( (2./Re)*ufl.inner(strain(v),strain(v_test))+ ufl.inner (ufl.nabla_grad(v)*v, v_test)
78+
- (q * ufl.div(v_test)) + ( ufl.div(v) * q_test) ) * ufl.dx
79+
80+
dl.solve(F == 0, vq, bcs, solver_parameters={"newton_solver":
81+
{"relative_tolerance":1e-4, "maximum_iterations":150}})
82+
return vq.split()[0]
83+
84+
85+
def __call__(self,u,m,p):
86+
'''
87+
Return the variational form of the PDE
88+
Inputs
89+
-u: state variable
90+
-m: model parameter
91+
-p: adjoint variable
92+
Outputs:
93+
Variational form of the PDE
94+
'''
95+
h = dl.CellDiameter(Vh[STATE].mesh())
96+
v_norm = dl.sqrt( dl.dot(self.v,self.v) + 1e-6)
97+
98+
return (h/v_norm)*dl.dot( self.v, dl.nabla_grad(u)) * dl.dot( self.v, dl.nabla_grad(p)) * dl.dx\
99+
+ self.k*dl.inner(dl.nabla_grad(u), dl.nabla_grad(p))*dl.dx \
100+
+ dl.inner(dl.nabla_grad(u), self.v*p)*dl.dx \
101+
+ self.c*dl.exp(m)*u*u*u*p*dl.dx \
102+
- self.f*p*dl.dx
103+
104+
105+
106+
def u_boundary(x, on_boundary):
107+
return on_boundary
108+
109+
########################################################################
110+
111+
# Construct the linear observable
112+
113+
# Define the function spaces
114+
Vh1 = dl.FunctionSpace(mesh, 'Lagrange', 1)
115+
Vh = [Vh1, Vh1, Vh1]
116+
if verbose:
117+
print( "Number of dofs: STATE={0}, PARAMETER={1}, ADJOINT={2}".format(Vh[STATE].dim(), Vh[PARAMETER].dim(), Vh[ADJOINT].dim()) )
118+
119+
# Define points for the observable
120+
# Turn this into a meshgrid?
121+
x_targets = np.linspace(0.6,0.8,sqrt_n_obs)
122+
y_targets = np.linspace(0.6,0.8,sqrt_n_obs)
123+
targets = []
124+
for xi in x_targets:
125+
for yi in y_targets:
126+
targets.append((xi,yi))
127+
targets = np.array(targets)
128+
if verbose:
129+
print( "Number of observation points: {0}".format(targets.shape[0]) )
130+
131+
# Define Dirichlet boundary conditions
132+
u_bdr = dl.Constant(0.0)
133+
u_bdr0 = dl.Constant(0.0)
134+
bc = dl.DirichletBC(Vh[STATE], u_bdr, u_boundary)
135+
bc0 = dl.DirichletBC(Vh[STATE], u_bdr0, u_boundary)
136+
137+
138+
m0 = dl.interpolate(dl.Constant(0.0), Vh[PARAMETER]).vector()
139+
param_dimension = m0.get_local().shape[0]
140+
m0.set_local(np.random.randn(param_dimension))
141+
142+
varf_handler = confusion_varf(Vh, output_folder = output_folder)
143+
144+
pde = PDEVariationalProblem(Vh, varf_handler, bc, bc0, is_fwd_linear=False)
145+
146+
B = assemblePointwiseObservation(Vh[STATE], targets)
147+
148+
observable = LinearStateObservable(pde,B)
149+
150+
return observable

0 commit comments

Comments
 (0)