Skip to content

Commit

Permalink
Merge pull request #29 from FPAL-Stanford-University/parallel
Browse files Browse the repository at this point in the history
Added explicit differentiator
  • Loading branch information
mlwong committed Aug 23, 2017
2 parents 00af8f2 + 23b8795 commit 0a61736
Show file tree
Hide file tree
Showing 13 changed files with 2,693 additions and 214 deletions.
1 change: 1 addition & 0 deletions floatpy/derivatives/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from explicit_differentiator import ExplicitDifferentiator
from compact_differentiator import CompactDifferentiator
128 changes: 85 additions & 43 deletions floatpy/derivatives/compact_differentiator.py

Large diffs are not rendered by default.

789 changes: 789 additions & 0 deletions floatpy/derivatives/explicit/first.py

Large diffs are not rendered by default.

851 changes: 851 additions & 0 deletions floatpy/derivatives/explicit/second.py

Large diffs are not rendered by default.

531 changes: 531 additions & 0 deletions floatpy/derivatives/explicit_differentiator.py

Large diffs are not rendered by default.

74 changes: 22 additions & 52 deletions floatpy/examples/ExampleFirstDerivatives.ipynb

Large diffs are not rendered by default.

76 changes: 23 additions & 53 deletions floatpy/examples/ExampleSecondDerivatives.ipynb

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions floatpy/tests/mpitest_differentiator_compact_2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def testSecondDerivativeY(self):

def testGradient(self):
"""
Test the gradient function
Test the gradient function.
"""

dfdx, dfdy = self.der.gradient(self.f)
Expand All @@ -127,7 +127,7 @@ def testGradient(self):

def testDivergence(self):
"""
Test the divergence function
Test the divergence function.
"""

df = numpy.concatenate((self.dfdx_exact[..., numpy.newaxis], \
Expand All @@ -144,9 +144,28 @@ def testDivergence(self):
self.assertLess(error[0], 5.0e-14, "Incorrect divergence!")


def testCurl(self):
"""
Test the curl function.
"""

df = numpy.concatenate((self.dfdx_exact[..., numpy.newaxis], \
self.dfdy_exact[..., numpy.newaxis]), axis=2)

curl = self.der.curl(df)

myerror = numpy.zeros(1)
myerror[0] = numpy.absolute(curl).max()

error = numpy.zeros(1)
self.comm.Allreduce(myerror, error, op=MPI.MAX)

self.assertLess(error[0], 5.0e-14, "Incorrect curl!")


def testLaplacian(self):
"""
Test the laplacian function
Test the laplacian function.
"""

laplacian = self.der.laplacian(self.f)
Expand Down
42 changes: 32 additions & 10 deletions floatpy/tests/mpitest_differentiator_compact_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from floatpy.parallel import _t3dmod
from floatpy.derivatives import CompactDifferentiator

class TestDerivativesCompact(unittest.TestCase):
class TestDifferentiatorCompact(unittest.TestCase):

def setUp(self):
self.nx, self.ny, self.nz = 64, 64, 64
Expand Down Expand Up @@ -63,7 +63,7 @@ def testFirstDerivativeX(self):
error = numpy.array([ myerror[0] ])
self.comm.Allreduce(myerror, error, op=MPI.MAX)

self.assertLess(error[0], 5.0e-14, "Incorrect periodic first derivative in X!")
self.assertLess(error[0], 5.0e-14, "Incorrect periodic first derivative in the X direction!")


def testFirstDerivativeY(self):
Expand All @@ -78,7 +78,7 @@ def testFirstDerivativeY(self):
error = numpy.array([ myerror[0] ])
self.comm.Allreduce(myerror, error, op=MPI.MAX)

self.assertLess(error[0], 5.0e-14, "Incorrect periodic first derivative in Y!")
self.assertLess(error[0], 5.0e-14, "Incorrect periodic first derivative in the Y direction!")


def testFirstDerivativeZ(self):
Expand All @@ -93,7 +93,7 @@ def testFirstDerivativeZ(self):
error = numpy.array([ myerror[0] ])
self.comm.Allreduce(myerror, error, op=MPI.MAX)

self.assertLess(error[0], 5.0e-14, "Incorrect periodic first derivative in Z!")
self.assertLess(error[0], 5.0e-14, "Incorrect periodic first derivative in the Z direction!")


def testSecondDerivativeX(self):
Expand All @@ -108,7 +108,7 @@ def testSecondDerivativeX(self):
error = numpy.array([ myerror[0] ])
self.comm.Allreduce(myerror, error, op=MPI.MAX)

self.assertLess(error[0], 5.0e-12, "Incorrect periodic second derivative in X!")
self.assertLess(error[0], 5.0e-12, "Incorrect periodic second derivative in the X direction!")


def testSecondDerivativeY(self):
Expand All @@ -123,7 +123,7 @@ def testSecondDerivativeY(self):
error = numpy.array([ myerror[0] ])
self.comm.Allreduce(myerror, error, op=MPI.MAX)

self.assertLess(error[0], 5.0e-12, "Incorrect periodic second derivative in X!")
self.assertLess(error[0], 5.0e-12, "Incorrect periodic second derivative in the Y direction!")


def testSecondDerivativeZ(self):
Expand All @@ -138,12 +138,12 @@ def testSecondDerivativeZ(self):
error = numpy.array([ myerror[0] ])
self.comm.Allreduce(myerror, error, op=MPI.MAX)

self.assertLess(error[0], 5.0e-12, "Incorrect periodic second derivative in X!")
self.assertLess(error[0], 5.0e-12, "Incorrect periodic second derivative in the Z direction!")


def testGradient(self):
"""
Test the gradient function
Test the gradient function.
"""

dfdx, dfdy, dfdz = self.der.gradient(self.f)
Expand All @@ -162,7 +162,7 @@ def testGradient(self):

def testDivergence(self):
"""
Test the divergence function
Test the divergence function.
"""

df = numpy.concatenate((self.dfdx_exact[..., numpy.newaxis], \
Expand All @@ -179,9 +179,31 @@ def testDivergence(self):
self.assertLess(error[0], 5.0e-14, "Incorrect divergence!")


def testCurl(self):
"""
Test the curl function.
"""

df = numpy.concatenate((self.dfdx_exact[..., numpy.newaxis], \
self.dfdy_exact[..., numpy.newaxis], self.dfdz_exact[..., numpy.newaxis]), axis=3)

curl = self.der.curl(df)

myerror = numpy.zeros(3)
myerror[0] = numpy.absolute(curl[:,:,:,0]).max()
myerror[1] = numpy.absolute(curl[:,:,:,1]).max()
myerror[2] = numpy.absolute(curl[:,:,:,2]).max()

error = numpy.zeros(3)
self.comm.Allreduce(myerror, error, op=MPI.MAX)

for i in range(3):
self.assertLess(error[i], 5.0e-14, "Incorrect curl!")


def testLaplacian(self):
"""
Test the laplacian function
Test the laplacian function.
"""

laplacian = self.der.laplacian(self.f)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy
import unittest

import floatpy.derivatives.explicit.first_order_differentiator
import floatpy.derivatives.explicit.first as first_der

class TestDerivativesFirst(unittest.TestCase):

Expand All @@ -17,13 +17,9 @@ def testFirstDirection(self):
y = numpy.sin(x)
y_prime_exact = numpy.cos(x)

dydx_2 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('second_order', direction=0, data_order='C')
dydx_4 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('fourth_order', direction=0, data_order='C')
dydx_6 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('sixth_order', direction=0, data_order='C')

y_prime_2 = dydx_2.diff(y, dx, 0, True)
y_prime_4 = dydx_4.diff(y, dx, 0, True)
y_prime_6 = dydx_6.diff(y, dx, 0, True)
y_prime_2 = first_der.differentiateSecondOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')
y_prime_4 = first_der.differentiateFourthOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')
y_prime_6 = first_der.differentiateSixthOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')

error_2 = numpy.linalg.norm(y_prime_exact[0, :] - y_prime_2[:])
error_4 = numpy.linalg.norm(y_prime_exact[0, :] - y_prime_4[:])
Expand All @@ -46,13 +42,9 @@ def testSecondDirection(self):
y = numpy.sin(x)
y_prime_exact = numpy.cos(x)

dydx_2 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('second_order', direction=1, data_order='C')
dydx_4 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('fourth_order', direction=1, data_order='C')
dydx_6 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('sixth_order', direction=1, data_order='C')

y_prime_2 = dydx_2.diff(y, dx, 0, True)
y_prime_4 = dydx_4.diff(y, dx, 0, True)
y_prime_6 = dydx_6.diff(y, dx, 0, True)
y_prime_2 = first_der.differentiateSecondOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')
y_prime_4 = first_der.differentiateFourthOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')
y_prime_6 = first_der.differentiateSixthOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')

error_2 = numpy.linalg.norm(y_prime_exact[0, 0, :] - y_prime_2[0, :])
error_4 = numpy.linalg.norm(y_prime_exact[0, 0, :] - y_prime_4[0, :])
Expand All @@ -75,13 +67,9 @@ def testThirdDirection(self):
y = numpy.sin(x)
y_prime_exact = numpy.cos(x)

dydx_2 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('second_order', direction=2, data_order='C')
dydx_4 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('fourth_order', direction=2, data_order='C')
dydx_6 = floatpy.derivatives.explicit.first_order_differentiator.FirstOrderDifferentiator('sixth_order', direction=2, data_order='C')

y_prime_2 = dydx_2.diff(y, dx, 0, True)
y_prime_4 = dydx_4.diff(y, dx, 0, True)
y_prime_6 = dydx_6.diff(y, dx, 0, True)
y_prime_2 = first_der.differentiateSecondOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')
y_prime_4 = first_der.differentiateFourthOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')
y_prime_6 = first_der.differentiateSixthOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')

error_2 = numpy.linalg.norm(y_prime_exact[0, 0, 0, :] - y_prime_2[0, 0, :])
error_4 = numpy.linalg.norm(y_prime_exact[0, 0, 0, :] - y_prime_4[0, 0, :])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy
import unittest

import floatpy.derivatives.explicit.second_order_differentiator
import floatpy.derivatives.explicit.second as second_der

class TestDerivativesSecond(unittest.TestCase):

Expand All @@ -17,16 +17,9 @@ def testFirstDirection(self):
y = numpy.sin(x)
y_prime_exact = -numpy.sin(x)

d2ydx2_2 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('second_order', direction=0, \
data_order='C')
d2ydx2_4 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('fourth_order', direction=0, \
data_order='C')
d2ydx2_6 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('sixth_order', direction=0, \
data_order='C')

y_prime_2 = d2ydx2_2.diff(y, dx, 0, True)
y_prime_4 = d2ydx2_4.diff(y, dx, 0, True)
y_prime_6 = d2ydx2_6.diff(y, dx, 0, True)
y_prime_2 = second_der.differentiateSecondOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')
y_prime_4 = second_der.differentiateFourthOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')
y_prime_6 = second_der.differentiateSixthOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')

error_2 = numpy.linalg.norm(y_prime_exact[0, :] - y_prime_2[:])
error_4 = numpy.linalg.norm(y_prime_exact[0, :] - y_prime_4[:])
Expand All @@ -49,16 +42,9 @@ def testSecondDirection(self):
y = numpy.sin(x)
y_prime_exact = -numpy.sin(x)

d2ydx2_2 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('second_order', direction=1, \
data_order='C')
d2ydx2_4 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('fourth_order', direction=1, \
data_order='C')
d2ydx2_6 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('sixth_order', direction=1, \
data_order='C')

y_prime_2 = d2ydx2_2.diff(y, dx, 0, True)
y_prime_4 = d2ydx2_4.diff(y, dx, 0, True)
y_prime_6 = d2ydx2_6.diff(y, dx, 0, True)
y_prime_2 = second_der.differentiateSecondOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')
y_prime_4 = second_der.differentiateFourthOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')
y_prime_6 = second_der.differentiateSixthOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')

error_2 = numpy.linalg.norm(y_prime_exact[0, 0, :] - y_prime_2[0, :])
error_4 = numpy.linalg.norm(y_prime_exact[0, 0, :] - y_prime_4[0, :])
Expand All @@ -81,16 +67,9 @@ def testThirdDirection(self):
y = numpy.sin(x)
y_prime_exact = -numpy.sin(x)

d2ydx2_2 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('second_order', direction=2, \
data_order='C')
d2ydx2_4 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('fourth_order', direction=2, \
data_order='C')
d2ydx2_6 = floatpy.derivatives.explicit.second_order_differentiator.SecondOrderDifferentiator('sixth_order', direction=2, \
data_order='C')

y_prime_2 = d2ydx2_2.diff(y, dx, 0, True)
y_prime_4 = d2ydx2_4.diff(y, dx, 0, True)
y_prime_6 = d2ydx2_6.diff(y, dx, 0, True)
y_prime_2 = second_der.differentiateSecondOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')
y_prime_4 = second_der.differentiateFourthOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')
y_prime_6 = second_der.differentiateSixthOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')

error_2 = numpy.linalg.norm(y_prime_exact[0, 0, 0, :] - y_prime_2[0, 0, :])
error_4 = numpy.linalg.norm(y_prime_exact[0, 0, 0, :] - y_prime_4[0, 0, :])
Expand Down
Loading

0 comments on commit 0a61736

Please sign in to comment.