Skip to content

Commit

Permalink
Some codacy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaem committed Mar 22, 2018
1 parent b45142f commit 6c6880f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 50 deletions.
1 change: 1 addition & 0 deletions .codacy.yml
Expand Up @@ -2,3 +2,4 @@
exclude_paths:
- docs/**/*
- docs/*.*
- sandbox/*.*
1 change: 1 addition & 0 deletions conf/conda/run_test.sh
@@ -1,2 +1,3 @@
#!bin/bash
pushd tests
py.test -v
91 changes: 45 additions & 46 deletions cpp/spectralDNS.cpp
@@ -1,6 +1,6 @@
#include "fftw3-mpi.h"
#include <math.h>
#include <iostream>
#include <iostream>
#include <iomanip>
#include <complex>
#include <vector>
Expand All @@ -15,33 +15,32 @@ typedef double precision;

int main( int argc, char *argv[] )
{
int rank, num_processes, M, N, Np, Nf;
int rank, num_processes, M, N, Nf;
double wtime, L, dx;
precision nu, dt, T;
double t0, t1, fastest_time, slowest_time, start_time;
MPI::Init ( argc, argv );
fftw_mpi_init();

num_processes = MPI::COMM_WORLD.Get_size();
rank = MPI::COMM_WORLD.Get_rank();
precision pi = 3.141592653589793238;
ptrdiff_t alloc_local, local_n0, local_0_start, local_n1, local_1_start, i, j, k;
vector<double> s_in(1), s_out(1), vs_in(2), vs_out(2);
vector<double> s_in(1), s_out(1), vs_in(2), vs_out(2);

nu = 0.000625;
T = 0.1;
dt = 0.01;
M = 7;
M = 7;
if ( argc > 1 ) {
M = atoi( argv[1] );
}

// N = pow(static_cast<int>(2), M); // Not accepted by Shaheen
N = 1;
for (int i=0; i<M;i++)
N *= 2;
L = 2*pi;
Np = N / num_processes;
dx = L / N;
std::cout << std::scientific << std::setprecision(16);
if (rank==0)
Expand All @@ -55,7 +54,7 @@ int main( int argc, char *argv[] )
alloc_local = fftw_mpi_local_size_3d_transposed(N, N, Nf, MPI::COMM_WORLD,
&local_n0, &local_0_start,
&local_n1, &local_1_start);

vector<precision> U(2*alloc_local);
vector<precision> V(2*alloc_local);
vector<precision> W(2*alloc_local);
Expand All @@ -64,7 +63,7 @@ int main( int argc, char *argv[] )
vector<precision> W_tmp(2*alloc_local);
vector<precision> CU(2*alloc_local);
vector<precision> CV(2*alloc_local);
vector<precision> CW(2*alloc_local);
vector<precision> CW(2*alloc_local);
vector<precision> P(2*alloc_local);
vector<int> dealias(2*alloc_local);
vector<precision> kk(2*alloc_local);
Expand All @@ -80,11 +79,11 @@ int main( int argc, char *argv[] )
vector<complex<precision> > W_hat1(alloc_local);
vector<complex<precision> > dU(alloc_local);
vector<complex<precision> > dV(alloc_local);
vector<complex<precision> > dW(alloc_local);
vector<complex<precision> > dW(alloc_local);
vector<complex<precision> > curlX(alloc_local);
vector<complex<precision> > curlY(alloc_local);
vector<complex<precision> > curlZ(alloc_local);

// Starting time
MPI::COMM_WORLD.Barrier();
t0 = MPI::Wtime();
Expand All @@ -103,11 +102,11 @@ int main( int argc, char *argv[] )

//fftw_plan plan_backward;
fftw_plan rfftn, irfftn;
rfftn = fftw_mpi_plan_dft_r2c_3d(N, N, N, U.data(), reinterpret_cast<fftw_complex*>(U_hat.data()),
rfftn = fftw_mpi_plan_dft_r2c_3d(N, N, N, U.data(), reinterpret_cast<fftw_complex*>(U_hat.data()),
MPI::COMM_WORLD, FFTW_MPI_TRANSPOSED_OUT);
irfftn = fftw_mpi_plan_dft_c2r_3d(N, N, N, reinterpret_cast<fftw_complex*>(U_hat.data()), U.data(),
MPI::COMM_WORLD, FFTW_MPI_TRANSPOSED_IN);
irfftn = fftw_mpi_plan_dft_c2r_3d(N, N, N, reinterpret_cast<fftw_complex*>(U_hat.data()), U.data(),
MPI::COMM_WORLD, FFTW_MPI_TRANSPOSED_IN);

for (int i=0; i<local_n0; i++)
for (int j=0; j<N; j++)
for (int k=0; k<N; k++)
Expand All @@ -117,30 +116,30 @@ int main( int argc, char *argv[] )
V[z] = -cos(dx*(i+local_0_start))*sin(dx*j)*cos(dx*k);
W[z] = 0.0;
}

fftw_mpi_execute_dft_r2c( rfftn, U.data(), reinterpret_cast<fftw_complex*>(U_hat.data()));
fftw_mpi_execute_dft_r2c( rfftn, V.data(), reinterpret_cast<fftw_complex*>(V_hat.data()));
fftw_mpi_execute_dft_r2c( rfftn, W.data(), reinterpret_cast<fftw_complex*>(W_hat.data()));
precision kmax = 2./3.*(N/2+1);

precision kmax = 2./3.*(N/2+1);
for (int i=0; i<local_n1; i++)
for (int j=0; j<N; j++)
for (int k=0; k<Nf; k++)
{
const int z = (i*N+j)*Nf+k;
dealias[z] = (abs(kx[i+local_1_start])<kmax)*(abs(kx[j])<kmax)*(abs(kx[k])<kmax) == true ? 1 : 0;
const int z = (i*N+j)*Nf+k;
dealias[z] = (abs(kx[i+local_1_start])<kmax)*(abs(kx[j])<kmax)*(abs(kx[k])<kmax) == true ? 1 : 0;
}

for (int i=0; i<local_n1; i++)
for (int j=0; j<N; j++)
for (int k=0; k<Nf; k++)
{
const int z = (i*N+j)*Nf+k;
int m = kx[i+local_1_start]*kx[i+local_1_start] + kx[j]*kx[j] + kx[k]*kx[k];
kk[z] = m > 0 ? m : 1;
}
complex<precision> one(0, 1);
}

complex<precision> one(0, 1);
double t=0.0;
int tstep = 0;
fastest_time = 1e8;
Expand All @@ -167,7 +166,7 @@ int main( int argc, char *argv[] )
// std::copy(U_hat.begin(), U_hat.end(), U_hat1.begin());
// std::copy(U_hat.begin(), U_hat.end(), U_hat1.begin());
// std::copy(U_hat.begin(), U_hat.end(), U_hat1.begin());

for (int rk=0; rk<4; rk++)
{
if (rk > 0)
Expand Down Expand Up @@ -201,7 +200,7 @@ int main( int argc, char *argv[] )
CV[k] /= tot;
CW[k] /= tot;
}

// Cross
for (int i=0; i<local_n0; i++)
for (int j=0; j<N; j++)
Expand All @@ -210,13 +209,13 @@ int main( int argc, char *argv[] )
const int z = (i*N+j)*2*Nf+k;
U_tmp[z] = V[z]*CW[z]-W[z]*CV[z];
V_tmp[z] = W[z]*CU[z]-U[z]*CW[z];
W_tmp[z] = U[z]*CV[z]-V[z]*CU[z];
W_tmp[z] = U[z]*CV[z]-V[z]*CU[z];
}

fftw_mpi_execute_dft_r2c( rfftn, U_tmp.data(), reinterpret_cast<fftw_complex*>(dU.data()));
fftw_mpi_execute_dft_r2c( rfftn, V_tmp.data(), reinterpret_cast<fftw_complex*>(dV.data()));
fftw_mpi_execute_dft_r2c( rfftn, W_tmp.data(), reinterpret_cast<fftw_complex*>(dW.data()));

for (int i=0; i<local_n1; i++)
for (int j=0; j<N; j++)
for (int k=0; k<Nf; k++)
Expand All @@ -226,7 +225,7 @@ int main( int argc, char *argv[] )
dV[z] *= (dealias[z]*dt);
dW[z] *= (dealias[z]*dt);
}
//
//
for (int i=0; i<local_n1; i++)
for (int j=0; j<N; j++)
for (int k=0; k<Nf; k++)
Expand All @@ -237,7 +236,7 @@ int main( int argc, char *argv[] )
dV[z] -= (P_hat[z]*kx[j] + nu*dt*kk[z]*V_hat[z]);
dW[z] -= (P_hat[z]*kz[k] + nu*dt*kk[z]*W_hat[z]);
}

if (rk < 3)
{
for (int i=0; i<local_n1; i++)
Expand All @@ -249,7 +248,7 @@ int main( int argc, char *argv[] )
V_hat[z] = V_hat0[z] + b[rk]*dV[z];
W_hat[z] = W_hat0[z] + b[rk]*dW[z];
}

}
for (int i=0; i<local_n1; i++)
for (int j=0; j<N; j++)
Expand All @@ -259,8 +258,8 @@ int main( int argc, char *argv[] )
U_hat1[z] += a[rk]*dU[z];
V_hat1[z] += a[rk]*dV[z];
W_hat1[z] += a[rk]*dW[z];
}
}
}
}
for (int i=0; i<local_n1; i++)
for (int j=0; j<N; j++)
for (int k=0; k<Nf; k++)
Expand All @@ -269,8 +268,8 @@ int main( int argc, char *argv[] )
U_hat[z] = U_hat1[z];
V_hat[z] = V_hat1[z];
W_hat[z] = W_hat1[z];
}
}

if (tstep % 2 == 0)
{
s_in[0] = 0.0;
Expand All @@ -283,17 +282,17 @@ int main( int argc, char *argv[] )
}
s_in[0] *= (0.5*dx*dx*dx/L/L/L);

MPI::COMM_WORLD.Reduce(s_in.data(), s_out.data(), 1, MPI::DOUBLE, MPI::SUM, 0);
MPI::COMM_WORLD.Reduce(s_in.data(), s_out.data(), 1, MPI::DOUBLE, MPI::SUM, 0);
if (rank==0)
std::cout << " k = " << s_out[0] << std::endl;
}
}

t1 = MPI::Wtime();
if (tstep > 1)
{
fastest_time = min(t1-t0, fastest_time);
slowest_time = max(t1-t0, slowest_time);
}
}
t0 = t1;
}

Expand All @@ -307,25 +306,25 @@ int main( int argc, char *argv[] )
}
s_in[0] *= (0.5*dx*dx*dx/L/L/L);

MPI::COMM_WORLD.Reduce(s_in.data(), s_out.data(), 1, MPI::DOUBLE, MPI::SUM, 0);
MPI::COMM_WORLD.Reduce(s_in.data(), s_out.data(), 1, MPI::DOUBLE, MPI::SUM, 0);
if (rank==0)
std::cout << " k = " << s_out[0] << std::endl;

MPI::COMM_WORLD.Barrier();
t1 = MPI::Wtime();
if (rank == 0)
if (rank == 0)
std::cout << "Time = " << t1 - start_time << std::endl;

fftw_destroy_plan(rfftn);
fftw_destroy_plan(irfftn);
vs_in[0] = fastest_time;
vs_in[1] = slowest_time;
MPI::COMM_WORLD.Reduce(vs_in.data(), vs_out.data(), 2, MPI::DOUBLE, MPI::MIN, 0);
if (rank==0)
std::cout << "Fastest = " << vs_out[0] << ", " << vs_out[1] << std::endl;
std::cout << "Fastest = " << vs_out[0] << ", " << vs_out[1] << std::endl;
MPI::COMM_WORLD.Reduce(vs_in.data(), vs_out.data(), 2, MPI::DOUBLE, MPI::MAX, 0);
if (rank==0)
std::cout << "Slowest = " << vs_out[0] << ", " << vs_out[1] << std::endl;
std::cout << "Slowest = " << vs_out[0] << ", " << vs_out[1] << std::endl;

MPI::Finalize ( );
}
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -12,7 +12,7 @@
directive_defaults = get_directive_defaults()
#directive_defaults['linetrace'] = True
#directive_defaults['binding'] = True
except:
except ImportError:
pass

#define_macros=[('CYTHON_TRACE', '1')]
Expand Down
2 changes: 1 addition & 1 deletion spectralDNS/config.py
Expand Up @@ -378,4 +378,4 @@ def update(new, mesh="triplyperiodic"):
if 'planner_effort' in new:
fft_plans.update(new['planner_effort'])
new['planner_effort'] = fft_plans
exec(mesh + ".set_defaults(**new)")
globals()[mesh].set_defaults(**new)
6 changes: 4 additions & 2 deletions spectralDNS/optimization/__init__.py
Expand Up @@ -21,9 +21,11 @@ def optimizer(func):
NS solver.
"""

try: # Look for optimized version of function
mod = importlib.import_module("_".join((config.params.optimization,
config.params.precision)))
mod = globals()["_".join((config.params.optimization,
config.params.precision))]


# Check for generic implementation first, then solver specific
name = func.__name__
Expand Down

0 comments on commit 6c6880f

Please sign in to comment.