コード例 #1
0
 def _get_example_poly1D(self):
     poly = PyPolynomial(1)
     powers = [2, 1]
     terms = [PyPolynomialTerm([powers[0]]), PyPolynomialTerm([powers[1]])]
     coeff = [2.0, -1.0]
     for c, t in zip(coeff, terms):
         poly.add_term(c, t)
     return poly, coeff, powers
コード例 #2
0
    def test_phase_field_poly(self):
        term1 = PyPolynomialTerm([2, 3])
        term2 = PyPolynomialTerm([1, 2])

        poly = PyPolynomial(2)
        poly.add_term(1.0, term1)
        poly.add_term(-2.0, term2)
        self.assertAlmostEqual(poly.evaluate([-1.0, 3.0]), 45.0)
        self.assertAlmostEqual(poly.deriv([-1.0, 3.0], 0), -72.0)
        self.assertAlmostEqual(poly.deriv([-1.0, 3.0], 1), 39.0)
コード例 #3
0
    def test_poly_term(self):
        power = [2]
        term = PyPolynomialTerm(power)
        self.assertAlmostEqual(term.evaluate([2.0]), 4.0)
        self.assertAlmostEqual(term.deriv([2.0], 0), 4.0)

        power = [2, 3]
        term = PyPolynomialTerm(power)
        self.assertAlmostEqual(term.evaluate([2.0, -4.0]), -256.0)
        self.assertAlmostEqual(term.deriv([2.0, -4.0], 0), -256.0)
        self.assertAlmostEqual(term.deriv([2.0, -4.0], 1), 3*4*16)
コード例 #4
0
 def _get_example_poly2D(self):
     poly = PyPolynomial(2)
     powers = [[2, 1], [1, 1], [1, 3]]
     coeff = [2.0, 1.0, 4.0]
     for c, p in zip(coeff, powers):
         poly.add_term(c, PyPolynomialTerm(p))
     return poly, coeff, powers
コード例 #5
0
def main():
    #prefix = "data/almgsi_chgl_random_seed_strain_noise2/chgl"
    prefix = "data/almgsi_chgl_3D_surface_1nm_64_strain_consistent/chgl"
    dx = 10.0  # Discretisation in angstrom
    dim = 3
    L = 64
    num_gl_fields = 3
    M = 0.1
    alpha = 5.0
    dt = 1.0
    gl_damping = M

    coeff, terms = get_polyterms(FNAME)

    poly = PyPolynomial(4)

    with open(FNAME, 'r') as infile:
        info = json.load(infile)

    kernel = PyGaussianKernel(info["kernel"]["std_dev"])
    regressor = PyKernelRegressor(info["kernel_regressor"]["xmin"],
                                  info["kernel_regressor"]["xmax"])
    regressor.set_kernel(kernel)
    regressor.set_coeff(info["kernel_regressor"]["coeff"])
    grad_coeff = info["gradient_coeff"]

    for item in info["terms"]:
        c = item["coeff"]
        powers = item["powers"]
        poly.add_term(c, PyPolynomialTerm(powers))
        print(c, powers)

    alpha = grad_coeff[0] / dx**2
    b1 = grad_coeff[1] / dx**2
    b2 = grad_coeff[2] / dx**2
    gradient_coeff = [[b2, b1, b2], [b1, b2, b2], [b2, b2, b1]]
    print(gradient_coeff)

    chgl = PyCHGLRealSpace(dim, L, prefix, num_gl_fields, M, alpha, dt,
                           gl_damping, gradient_coeff)

    landau = PyTwoPhaseLandau()
    landau.set_polynomial(poly)
    landau.set_kernel_regressor(regressor)
    landau.set_discontinuity(info["discontinuity_conc"],
                             info["discontinuity_jump"])

    chgl.set_free_energy(landau)
    #chgl.from_npy_array(precipitates_square(L))
    chgl.use_adaptive_stepping(1E-10, 1, 0.05)
    chgl.build3D()
    add_strain(chgl)
    chgl.from_file(prefix + "00000053000.grid")

    chgl.run(500000, 5000, start=53000)
    chgl.save_free_energy_map(prefix + "_free_energy_map.grid")
コード例 #6
0
    def test_quadratic_two_phase(self):
        chgl = self.get_chgl3D()
        chgl.build3D()

        # Initialize a two phase landau polynomial
        landau = PyQuadraticTwoPhasePoly()

        # Initlaise 1D polynomial (concentration)
        poly1 = PyPolynomial(1)
        poly1.add_term(1.0, PyPolynomialTerm([2]))
        landau.set_poly_phase1(poly1)

        # Initialize 4D polynomial (concentration, shape1, shape2, shape3)
        poly2 = PyPolynomial(4)
        poly2.add_term(2.0, PyPolynomialTerm([1, 2, 0, 0]))
        landau.set_poly_phase2(poly2)
        chgl.set_free_energy_quadratic(landau)

        chgl.run(5, 1000)
コード例 #7
0
    def test_1D(self):
        poly = PyPolynomial(1)
        terms = [
            PyPolynomialTerm([2]),
            PyPolynomialTerm([1]),
            PyPolynomialTerm([0])
        ]
        coeff = [2.0, 1.0, -1.0]

        for c, t in zip(coeff, terms):
            poly.add_term(c, t)

        quad = PyQuadraticTwoPhasePoly()
        quad.set_poly_phase1(poly)
        quad.set_poly_phase2(poly)

        x = 2.5
        expected = coeff[0] * x**2 + coeff[1] * x + coeff[2]

        # Divide result by two, since we add the same polynomial
        # two places
        self.assertEqual(expected, 0.5 * quad.evaluate_vec([x]))
コード例 #8
0
ファイル: tools.py プロジェクト: davidkleiven/APAL
def get_polyterms(fname):
    """Parse JSON file and return list of PyPolyterms.

    :param str fname: JSON file with the parameters
    """
    from apal_cxx import PyPolynomialTerm

    with open(fname, 'r') as infile:
        data = json.load(infile)

    poly_terms = []
    coefficients = []
    for entry in data["terms"]:
        poly_terms.append(PyPolynomialTerm(entry["powers"]))
        coefficients.append(entry["coeff"])
    return coefficients, poly_terms
コード例 #9
0
def main(prefix, start, startfile, initfunc, dx=30.0, steps=0, update_freq=0):
    #prefix = "data/almgsi_chgl_random_seed_strain_noise2/chgl"
    #prefix = "data/almgsi_chgl_3D_surface_3nm_64_strain_meV/chgl"
    #prefix = "data/almgsi_chgl_3D_MLdx1_1nm_64_strain_meV/chgl"
    #prefix = "/work/sophus/almgsi_chgl_3D_MLdx1_3nm_64_strain_meV/chgl"
    dim = 3
    L = 128
    num_gl_fields = 3
    M = 0.1 / dx**2
    alpha = 5.0
    dt = 0.003
    gl_damping = M / dx**2

    coeff, terms = get_polyterms(FNAME)

    poly = PyPolynomial(4)
    poly1 = PyPolynomial(1)

    with open(FNAME, 'r') as infile:
        info = json.load(infile)

    # kernel = PyGaussianKernel(info["kernel"]["std_dev"])
    # regressor = PyKernelRegressor(
    #     info["kernel_regressor"]["xmin"],
    #     info["kernel_regressor"]["xmax"])
    # regressor.set_kernel(kernel)
    # regressor.set_coeff(info["kernel_regressor"]["coeff"])
    grad_coeff = info["gradient_coeff"]

    for item in info["terms"]:
        c = item["coeff"]
        powers = item["powers"]
        poly.add_term(c, PyPolynomialTerm(powers))
        print(c, powers)

    N = len(info["conc_phase1"])
    for i, c in enumerate(info["conc_phase1"]):
        poly1.add_term(c, PyPolynomialTerm([N - i - 1]))

    alpha = grad_coeff[0] / dx**2
    b1 = grad_coeff[1] / dx**2
    b2 = grad_coeff[2] / dx**2
    #gradient_coeff = [[b2, b1, b2],
    #                  [b1, b2, b2],
    #                  [b2, b2, b1]]
    gradient_coeff = [[b1, b2, b1], [b2, b1, b1], [b1, b1, b2]]

    print(gradient_coeff)
    chgl = PyCHGLRealSpace(dim, L, prefix, num_gl_fields, M, alpha, dt,
                           gl_damping, gradient_coeff)

    # landau = PyTwoPhaseLandau()
    # landau.set_polynomial(poly)
    # landau.set_kernel_regressor(regressor)
    # landau.set_discontinuity(info["discontinuity_conc"], info["discontinuity_jump"])
    landau = PyQuadraticTwoPhasePoly()
    landau.set_poly_phase1(poly1)
    landau.set_poly_phase2(poly)

    chgl.set_free_energy_quadratic(landau)
    chgl.use_adaptive_stepping(1E-10, 1, 0.1)
    #chgl.set_field_update_rate(10)
    #chgl.set_strain_update_rate(100)
    chgl.build3D()
    add_strain(chgl)
    chgl.conserve_volume(1)

    if startfile is not None:
        chgl.from_file(prefix + startfile)
    else:
        if initfunc == "precipitate_square":
            chgl.from_npy_array(precipitates_square(L))
        elif initfunc == "matsuda":
            chgl.from_npy_array(create_matsuda(L))
        elif initfunc == 'prec_square_bck':
            chgl.from_npy_array(precipitate_square_bck(L))
        else:
            raise ValueError("Unknown init function!")

    chgl.run(steps, update_freq, start=start)
    chgl.save_free_energy_map(prefix + "_free_energy_map.grid")
コード例 #10
0
def main(prefix,
         start,
         startfile,
         initfunc,
         dx=30.0,
         dt=0.3,
         steps=0,
         update_freq=0,
         prec_x0=20,
         prec_x1=50,
         a=1,
         b=1):
    #prefix = "data/almgsi_chgl_random_seed_strain_noise2/chgl"
    #prefix = "data/almgsi_chgl_3D_surface_3nm_64_strain_meV/chgl"
    #prefix = "data/almgsi_chgl_3D_MLdx1_1nm_64_strain_meV/chgl"
    #prefix = "/work/sophus/almgsi_chgl_3D_MLdx1_3nm_64_strain_meV/chgl"
    dim = 2
    L = 1024
    num_gl_fields = 2
    M = 0.1  #/dx**2
    alpha = 5.0
    gl_damping = M  #/dx**2

    coeff, terms = get_polyterms(FNAME)

    poly = PyPolynomial(3)
    poly1 = PyPolynomial(1)

    with open(FNAME, 'r') as infile:
        info = json.load(infile)

    # kernel = PyGaussianKernel(info["kernel"]["std_dev"])
    # regressor = PyKernelRegressor(
    #     info["kernel_regressor"]["xmin"],
    #     info["kernel_regressor"]["xmax"])
    # regressor.set_kernel(kernel)
    # regressor.set_coeff(info["kernel_regressor"]["coeff"])
    grad_coeff = info["gradient_coeff"]
    conc_scale = 1.0
    for item in info["terms"]:
        c = item["coeff"] * conc_scale
        powers = item["powers"]
        if powers[-1] > 0:
            continue
        poly.add_term(c, PyPolynomialTerm(powers[:-1]))
        print(c, powers)

    N = len(info["conc_phase1"])
    for i, c in enumerate(info["conc_phase1"]):
        poly1.add_term(c * conc_scale, PyPolynomialTerm([N - i - 1]))

    alpha = grad_coeff[0] / dx**2
    b1 = grad_coeff[1] / dx**2
    b2 = grad_coeff[2] / dx**2
    #gradient_coeff = [[b2, b1],
    #                  [b1, b2]]
    gradient_coeff = [[b1, b2], [b2, b1]]

    print(gradient_coeff)
    chgl = PyCHGL(dim, L, prefix, num_gl_fields, M, alpha, dt, gl_damping,
                  gradient_coeff)

    # landau = PyTwoPhaseLandau()
    # landau.set_polynomial(poly)
    # landau.set_kernel_regressor(regressor)
    # landau.set_discontinuity(info["discontinuity_conc"], info["discontinuity_jump"])
    landau = PyQuadraticTwoPhasePoly()
    landau.set_poly_phase1(poly1)
    landau.set_poly_phase2(poly)

    chgl.set_free_energy_quadratic(landau)
    chgl.use_adaptive_stepping(1E-10, 1, 0.05)
    omega_cut = 0.2 * np.pi
    roll_off = 0.3
    #chgl.set_raised_cosine_filter(omega_cut, roll_off)
    chgl.set_vandeven_filter(3)
    #chgl.set_gaussian_filter(omega_cut)
    #chgl.set_vandeven_filter(2)
    #chgl.use_HeLiuTang_stabilizer(500)
    #chgl.set_field_update_rate(100)
    #chgl.set_strain_update_rate(1000)
    #chgl.build2D()
    add_strain(chgl)
    chgl.set_conc_type_allen_cahn()
    chgl.conserve_volume(0)
    chgl.conserve_volume(1)
    chgl.conserve_volume(2)

    if startfile is not None:
        chgl.from_file(prefix + startfile)
    else:
        if initfunc == "precipitate_square":
            chgl.from_npy_array(
                precipitates_square(L, start=prec_x0, end=prec_x1))
        elif initfunc == "matsuda":
            chgl.from_npy_array(create_matsuda(L))
        elif initfunc == 'prec_square_bck':
            chgl.from_npy_array(precipitate_square_bck(L))
        elif initfunc == 'random':
            chgl.from_npy_array(random_orientation(L))
        elif initfunc == 'ellipse':
            chgl.from_npy_array(ellipse(L, a, b))
        else:
            raise ValueError("Unknown init function!")

    chgl.run(steps, update_freq, start=start)
    chgl.save_free_energy_map(prefix + "_free_energy_map.grid")