Exemple #1
0
def tensorized_basis_2D(order):

    total_integration_points = (order + 1) * (order + 1)
    eps, eta, rho = sym.symbols('epsilon eta rho')
    eps_gll = sym.symbols('epsilon_0:%d' % (order + 1))
    eta_gll = sym.symbols('eta_0:%d' % (order + 1))

    # Get N + 1 lagrange polynomials in each direction.
    generator_eps = generating_polynomial_lagrange(order, 'epsilon', eps_gll)
    generator_eta = generating_polynomial_lagrange(order, 'eta', eta_gll)

    # Get tensorized basis.
    basis = TensorProduct(generator_eta, generator_eps)
    basis = basis.subs([(v, c)
                        for v, c in zip(eps_gll, gll_coordinates(order))])
    basis = basis.subs([(v, c)
                        for v, c in zip(eta_gll, gll_coordinates(order))])
    sym.pprint(basis)

    # Get gradient of basis functions.
    basis_gradient_eps = sym.Matrix([sym.diff(i, eps) for i in basis])
    basis_gradient_eta = sym.Matrix([sym.diff(i, eta) for i in basis])

    # Get diagonal mass matrix
    mass_matrix = rho * basis * basis.T
    mass_matrix_diagonal = sym.Matrix(
        [mass_matrix[i, i] for i in range(total_integration_points)])

    # Write code
    routines = []
    autocode = CCodeGen()
    routines.append(
        autocode.routine('interpolate_order{}_square'.format(order),
                         basis,
                         argument_sequence=None))
    routines.append(
        autocode.routine(
            'interpolate_eps_derivative_order{}_square'.format(order),
            basis_gradient_eps,
            argument_sequence=None))
    routines.append(
        autocode.routine(
            'interpolate_eta_derivative_order{}_square'.format(order),
            basis_gradient_eta,
            argument_sequence=None))
    routines.append(
        autocode.routine('diagonal_mass_matrix_order{}_square'.format(order),
                         mass_matrix_diagonal,
                         argument_sequence=None))
    autocode.write(routines, 'order{}_square'.format(order), to_files=True)
def genP3code():
    autocode = CCodeGen(project="Salvus")
    r,s,t = symbols("r,s,t")
    rstn = p3ReferenceNodes()
    (rn,sn,tn) = rstn
    Np = len(rn)
    phi = p3ElementPolynomials(rstn,r,s,t)
    (dphi_dr,dphi_ds,dphi_dt) = p3ElementDerivatives(phi,r,s,t)
    dphi_dr_rstn = np.zeros((Np,Np))
    dphi_ds_rstn = np.zeros((Np,Np))
    dphi_dt_rstn = np.zeros((Np,Np))
    for i in range(0,Np):
        dphi_dr_i = lambdify((r,s,t),dphi_dr[i])
        dphi_ds_i = lambdify((r,s,t),dphi_ds[i])
        dphi_dt_i = lambdify((r,s,t),dphi_dt[i])        
        for (n,(ri,si,ti)) in enumerate(zip(rn,sn,tn)):
            dphi_dr_rstn[i,n] = dphi_dr_i(ri,si,ti)
            dphi_ds_rstn[i,n] = dphi_ds_i(ri,si,ti)
            dphi_dt_rstn[i,n] = dphi_dt_i(ri,si,ti)

    dphi_dr_rstn = Matrix(dphi_dr_rstn)
    dphi_ds_rstn = Matrix(dphi_ds_rstn)
    dphi_dt_rstn = Matrix(dphi_dt_rstn)
    
    rn = Matrix([ri for (ri,si,ti) in zip(rn,sn,tn)])
    sn = Matrix([si for (ri,si,ti) in zip(rn,sn,tn)])
    tn = Matrix([ti for (ri,si,ti) in zip(rn,sn,tn)])
    wn = Matrix(p3QuadratureWeights())
    rn_routine = autocode.routine("coordinates_p3_tetrahedra_rn",rn,
                                  argument_sequence=None)
    sn_routine = autocode.routine("coordinates_p3_tetrahedra_sn",sn,
                                  argument_sequence=None)
    tn_routine = autocode.routine("coordinates_p3_tetrahedra_tn",tn,
                                  argument_sequence=None)
    wn_routine  = autocode.routine("quadrature_weights_p3_tetrahedra",wn,
                                   argument_sequence=None)

    # NOTE: writes out in Row Major (so we write the
    # transpose). Salvus (via Eigen) assumes Column Major
    dphi_dr_rstn_routine  = autocode.routine("dphi_dr_rstn_p3_tetrahedra",
                                             dphi_dr_rstn.transpose(),
                                             argument_sequence=None)
    dphi_ds_rstn_routine  = autocode.routine("dphi_ds_rstn_p3_tetrahedra",
                                             dphi_ds_rstn.transpose(),
                                             argument_sequence=None)
    dphi_dt_rstn_routine  = autocode.routine("dphi_dt_rstn_p3_tetrahedra",
                                             dphi_dt_rstn.transpose(),
                                             argument_sequence=None)
    
    routines = [rn_routine,
                sn_routine,
                tn_routine,
                wn_routine,
                dphi_dr_rstn_routine,
                dphi_ds_rstn_routine,
                dphi_dt_rstn_routine]

    autocode.write(routines,"p3_tetrahedra",to_files=True)

    buildVisualizationP3(rn,sn,tn)
def tensorized_basis_2D(order):

    total_integration_points = (order + 1) * (order + 1)
    eps, eta, rho = sym.symbols("epsilon eta rho")
    eps_gll = sym.symbols("epsilon_0:%d" % (order + 1))
    eta_gll = sym.symbols("eta_0:%d" % (order + 1))

    # Get N + 1 lagrange polynomials in each direction.
    generator_eps = generating_polynomial_lagrange(order, "epsilon", eps_gll)
    generator_eta = generating_polynomial_lagrange(order, "eta", eta_gll)

    # Get tensorized basis.
    basis = TensorProduct(generator_eta, generator_eps)
    basis = basis.subs([(v, c) for v, c in zip(eps_gll, gll_coordinates(order))])
    basis = basis.subs([(v, c) for v, c in zip(eta_gll, gll_coordinates(order))])
    sym.pprint(basis)

    # Get gradient of basis functions.
    basis_gradient_eps = sym.Matrix([sym.diff(i, eps) for i in basis])
    basis_gradient_eta = sym.Matrix([sym.diff(i, eta) for i in basis])

    # Get diagonal mass matrix
    mass_matrix = rho * basis * basis.T
    mass_matrix_diagonal = sym.Matrix([mass_matrix[i, i] for i in range(total_integration_points)])

    # Write code
    routines = []
    autocode = CCodeGen()
    routines.append(autocode.routine("interpolate_order{}_square".format(order), basis, argument_sequence=None))
    routines.append(
        autocode.routine(
            "interpolate_eps_derivative_order{}_square".format(order), basis_gradient_eps, argument_sequence=None
        )
    )
    routines.append(
        autocode.routine(
            "interpolate_eta_derivative_order{}_square".format(order), basis_gradient_eta, argument_sequence=None
        )
    )
    routines.append(
        autocode.routine(
            "diagonal_mass_matrix_order{}_square".format(order), mass_matrix_diagonal, argument_sequence=None
        )
    )
    autocode.write(routines, "order{}_square".format(order), to_files=True)
Exemple #4
0
def genP3code():
    autocode = CCodeGen(project="Salvus")
    r,s = symbols("r,s")
    rsn = p3ReferenceNodes()
    Np = len(rsn)
    phi = p3ElementPolynomials(rsn,r,s)
    (dphi_dr,dphi_ds) = p3ElementDerivatives(phi,r,s)
    dphi_dr_rsn = np.zeros((Np,Np))
    dphi_ds_rsn = np.zeros((Np,Np))
    for i in range(0,Np):
        dphi_dr_i = lambdify((r,s),dphi_dr[i])
        dphi_ds_i = lambdify((r,s),dphi_ds[i])
        for (n,(rn,sn)) in enumerate(rsn):
            dphi_dr_rsn[i,n] = dphi_dr_i(rn,sn)
            dphi_ds_rsn[i,n] = dphi_ds_i(rn,sn)

    dphi_dr_rsn = Matrix(dphi_dr_rsn)
    dphi_ds_rsn = Matrix(dphi_ds_rsn)
    print(dphi_dr_rsn[0,:])
    rn = Matrix([ri for (ri,si) in rsn])
    sn = Matrix([si for (ri,si) in rsn])
    wn = Matrix(p3QuadratureWeights())
    rn_routine = autocode.routine("coordinates_p3_triangle_rn",rn,
                                  argument_sequence=None)
    sn_routine = autocode.routine("coordinates_p3_triangle_sn",sn,
                                  argument_sequence=None)
    wn_routine  = autocode.routine("quadrature_weights_p3_triangle",wn,
                                   argument_sequence=None)

    # NOTE: by default, it writes out in Row Major (so we write the
    # transpose). Salvus (via Eigen) assumes Column Major
    dphi_dr_rsn_routine  = autocode.routine("dphi_dr_rsn_p3_triangle",
                                            dphi_dr_rsn.transpose(),
                                            argument_sequence=None)
    dphi_ds_rsn_routine  = autocode.routine("dphi_ds_rsn_p3_triangle",
                                            dphi_ds_rsn.transpose(),
                                            argument_sequence=None)

    routines = [rn_routine,
                sn_routine,
                wn_routine,
                dphi_dr_rsn_routine,
                dphi_ds_rsn_routine]

    autocode.write(routines,"p3_triangle",to_files=True)
Exemple #5
0
def _fixed_autowrap(energy_expr, prefix, save_filename=None):

    codegen = CCodeGen()

    routines = []
    _logger.debug('Writing code for energy_expr')
    routines.append(codegen.routine('autofunc', energy_expr))
    [(c_name, bad_c_code), (h_name, h_code)] = codegen.write(
        routines,
        prefix,
    )
    c_code = '#include <complex.h>\n'
    c_code += bad_c_code
    c_code = c_code.replace('conjugate', 'conj')

    write_directory = Path(tempfile.mkdtemp()).absolute()

    c_path = write_directory / Path(prefix + '.c')
    h_path = write_directory / Path(prefix + '.h')
    o_path = write_directory / Path(prefix + '.o')
    so_path = write_directory / Path(prefix + '.so')

    with open(c_path, 'w') as fh:
        fh.write(c_code)
        fh.write('\n')
    with open(h_path, 'w') as fh:
        fh.write(h_code)
        fh.write('\n')

    start = time.time()
    _logger.debug('Start compiling code.')
    os.system(f'gcc -c -lm {c_path} -o {o_path}')
    os.system(f'gcc -shared {o_path} -o {so_path}')
    _logger.debug(f'Compiling code took {time.time()-start} seconds.')

    if save_filename is not None:
        if Path(save_filename).exists():
            Path(save_filename).unlink()
        shutil.copy(so_path, save_filename)

    cost_func = _load_func(so_path)

    shutil.rmtree(write_directory)

    return cost_func
Exemple #6
0
def _make_energy_func_chunked(energy_expr_list, prefix, save_filename=None):

    codegen = CCodeGen()

    routines = []
    for i, expr in enumerate(energy_expr_list):
        _logger.debug(f'Writing code for expr {i} of {len(energy_expr_list)}')
        routines.append(codegen.routine('expr' + str(i), expr))
    [(c_name, bad_c_code), (h_name, h_code)] = codegen.write(
        routines,
        prefix,
    )
    c_code = '#include <complex.h>\n'
    c_code += bad_c_code
    c_code += """
double autofunc(double *xs){

    double autofunc_result;
    autofunc_result = %s;
    return autofunc_result;

}
""" % ('+'.join(
        ['expr' + str(i) + '(xs)' for i in range(len(energy_expr_list))]))
    c_code = c_code.replace('conjugate', 'conj')

    h_code = h_code.replace('\n#endif', """double autofunc(double *xs);

#endif
""")

    write_directory = Path(tempfile.mkdtemp()).absolute()

    c_path = write_directory / Path(prefix + '.c')
    h_path = write_directory / Path(prefix + '.h')
    o_path = write_directory / Path(prefix + '.o')
    so_path = write_directory / Path(prefix + '.so')

    with open(c_path, 'w') as fh:
        fh.write(c_code)
        fh.write('\n')
    with open(h_path, 'w') as fh:
        fh.write(h_code)
        fh.write('\n')

    start = time.time()
    _logger.debug('Start compiling code.')
    os.system(f'gcc -c -lm -fPIC {c_path} -o {o_path}')
    os.system(f'gcc -shared -fPIC {o_path} -o {so_path}')
    _logger.debug(f'Compiling code took {time.time()-start} seconds.')

    if save_filename is not None:
        if Path(save_filename).exists():
            Path(save_filename).unlink()
        shutil.copy(so_path, save_filename)

    cost_func = _load_func(so_path)

    shutil.rmtree(write_directory)

    return cost_func
for d, i in zip(rght_e, boun_matrix_nor):
    boun_shell_rght[d,d] = i
for d, i in zip(rght_n, boun_matrix_tan):
    boun_shell_rght[d,d] = i
for d, i in zip(tops_e, boun_matrix_tan):
    boun_shell_tops[d,d] = i
for d, i in zip(tops_n, boun_matrix_nor):
    boun_shell_tops[d,d] = i

boun_shell_left = sym.Matrix([boun_shell_left[i,i] for i in range(dof_tot)])
boun_shell_rght = sym.Matrix([boun_shell_rght[i,i] for i in range(dof_tot)])
boun_shell_tops = sym.Matrix([boun_shell_tops[i,i] for i in range(dof_tot)])
boun_shell_bttm = sym.Matrix([boun_shell_bttm[i,i] for i in range(dof_tot)])

print 'Printing boundary shells...'
routines.append(sem_c.routine(
    'boun_shell_left', boun_shell_left, argument_sequence=None))
routines.append(sem_c.routine(
    'boun_shell_rght', boun_shell_rght, argument_sequence=None))
routines.append(sem_c.routine(
    'boun_shell_bttm', boun_shell_bttm, argument_sequence=None))
routines.append(sem_c.routine(
    'boun_shell_tops', boun_shell_tops, argument_sequence=None))
    
# // INTEGRATE MASS MATRIX //
print 'Integrating mass matrix...'
mass_matrix = rho * N.T * N
mass_matrix = mass_matrix.subs(replace_e)
mass_matrix = mass_matrix.subs(replace_n)
mass_matrix_int = sym.Matrix.zeros(dof_tot, dof_tot)
for c_e, w_x in zip(coords, weights):
    for c_n, w_y in zip(coords, weights):
def genP3code():
    autocode = CCodeGen(project="Salvus")
    r, s, t = symbols("r,s,t")
    rstn = p3ReferenceNodes()
    (rn, sn, tn) = rstn
    Np = len(rn)
    phi = p3ElementPolynomials(rstn, r, s, t)
    (dphi_dr, dphi_ds, dphi_dt) = p3ElementDerivatives(phi, r, s, t)
    dphi_dr_rstn = np.zeros((Np, Np))
    dphi_ds_rstn = np.zeros((Np, Np))
    dphi_dt_rstn = np.zeros((Np, Np))
    for i in range(0, Np):
        dphi_dr_i = lambdify((r, s, t), dphi_dr[i])
        dphi_ds_i = lambdify((r, s, t), dphi_ds[i])
        dphi_dt_i = lambdify((r, s, t), dphi_dt[i])
        for (n, (ri, si, ti)) in enumerate(zip(rn, sn, tn)):
            dphi_dr_rstn[i, n] = dphi_dr_i(ri, si, ti)
            dphi_ds_rstn[i, n] = dphi_ds_i(ri, si, ti)
            dphi_dt_rstn[i, n] = dphi_dt_i(ri, si, ti)

    dphi_dr_rstn = Matrix(dphi_dr_rstn)
    dphi_ds_rstn = Matrix(dphi_ds_rstn)
    dphi_dt_rstn = Matrix(dphi_dt_rstn)

    rn = Matrix([ri for (ri, si, ti) in zip(rn, sn, tn)])
    sn = Matrix([si for (ri, si, ti) in zip(rn, sn, tn)])
    tn = Matrix([ti for (ri, si, ti) in zip(rn, sn, tn)])
    wn = Matrix(p3QuadratureWeights())
    rn_routine = autocode.routine("coordinates_p3_tetrahedra_rn",
                                  rn,
                                  argument_sequence=None)
    sn_routine = autocode.routine("coordinates_p3_tetrahedra_sn",
                                  sn,
                                  argument_sequence=None)
    tn_routine = autocode.routine("coordinates_p3_tetrahedra_tn",
                                  tn,
                                  argument_sequence=None)
    wn_routine = autocode.routine("quadrature_weights_p3_tetrahedra",
                                  wn,
                                  argument_sequence=None)

    # NOTE: writes out in Row Major (so we write the
    # transpose). Salvus (via Eigen) assumes Column Major
    dphi_dr_rstn_routine = autocode.routine("dphi_dr_rstn_p3_tetrahedra",
                                            dphi_dr_rstn.transpose(),
                                            argument_sequence=None)
    dphi_ds_rstn_routine = autocode.routine("dphi_ds_rstn_p3_tetrahedra",
                                            dphi_ds_rstn.transpose(),
                                            argument_sequence=None)
    dphi_dt_rstn_routine = autocode.routine("dphi_dt_rstn_p3_tetrahedra",
                                            dphi_dt_rstn.transpose(),
                                            argument_sequence=None)

    routines = [
        rn_routine, sn_routine, tn_routine, wn_routine, dphi_dr_rstn_routine,
        dphi_ds_rstn_routine, dphi_dt_rstn_routine
    ]

    autocode.write(routines, "p3_tetrahedra", to_files=True)

    buildVisualizationP3(rn, sn, tn)
	# Get gradient of basis functions
	gn = sym.Matrix(([sym.diff(i, eta) for i in n])).T
	sym.pprint(gn)

	# Mass matrix
	mm = (rho * n.T * n)
	mm = sym.Matrix([mm[i,i] for i in range(nPtsPelm)])

	# Get mapping
	m = sym.Matrix(getMap(nPtsPvtx, nPtsPedg, nPtsPfac))

	# Write code
	routines = []
	sem_c = CCodeGen()
	routines.append(sem_c.routine(
		'interpolateOrder%dDim%d' % (args.order, args.dimension), n,
		argument_sequence=None))
	routines.append(sem_c.routine(
      'massMatrixOrder%dDim%d' % (args.order, args.dimension), mm, 
      argument_sequence=None))
	routines.append(sem_c.routine(
      'gradientOperatorOrder%dDim%d' % (args.order, args.dimension), gn, 
      argument_sequence=None))
	routines.append(sem_c.routine(
      'mappingOrder%dDim%d' % (args.order, args.dimension), m, 
      argument_sequence=None))	
	sem_c.write(
		routines, 'auto/order%dDim%d' % (args.order, args.dimension), 
		to_files=True)