Made the DSL syntax less confusing: Input and output arrays are now ScalarField and VectorFields instead of scalars and vectors. C++ initializers are now also possible, removing the need to declare Fields as int or int3 which was very confusing, like "what, you assing an int value to a real, what the &^%@?"

This commit is contained in:
jpekkila
2019-08-08 21:07:36 +03:00
parent 5397495496
commit b53cabbc44
6 changed files with 54 additions and 45 deletions

View File

@@ -1,11 +1,11 @@
Preprocessed Scalar Preprocessed Scalar
value(in Scalar vertex) value(in ScalarField vertex)
{ {
return vertex[vertexIdx]; return vertex[vertexIdx];
} }
Preprocessed Vector Preprocessed Vector
gradient(in Scalar vertex) gradient(in ScalarField vertex)
{ {
return (Vector){derx(vertexIdx, vertex), return (Vector){derx(vertexIdx, vertex),
dery(vertexIdx, vertex), dery(vertexIdx, vertex),
@@ -15,7 +15,7 @@ gradient(in Scalar vertex)
#if LUPWD #if LUPWD
Preprocessed Scalar Preprocessed Scalar
der6x_upwd(in Scalar vertex) der6x_upwd(in ScalarField vertex)
{ {
Scalar inv_ds = DCONST_REAL(AC_inv_dsx); Scalar inv_ds = DCONST_REAL(AC_inv_dsx);
@@ -30,7 +30,7 @@ der6x_upwd(in Scalar vertex)
} }
Preprocessed Scalar Preprocessed Scalar
der6y_upwd(in Scalar vertex) der6y_upwd(in ScalarField vertex)
{ {
Scalar inv_ds = DCONST_REAL(AC_inv_dsy); Scalar inv_ds = DCONST_REAL(AC_inv_dsy);
@@ -45,7 +45,7 @@ der6y_upwd(in Scalar vertex)
} }
Preprocessed Scalar Preprocessed Scalar
der6z_upwd(in Scalar vertex) der6z_upwd(in ScalarField vertex)
{ {
Scalar inv_ds = DCONST_REAL(AC_inv_dsz); Scalar inv_ds = DCONST_REAL(AC_inv_dsz);
@@ -62,7 +62,7 @@ der6z_upwd(in Scalar vertex)
#endif #endif
Preprocessed Matrix Preprocessed Matrix
hessian(in Scalar vertex) hessian(in ScalarField vertex)
{ {
Matrix hessian; Matrix hessian;

View File

@@ -25,14 +25,14 @@ uniform int nz;
Vector Vector
value(in Vector uu) value(in VectorField uu)
{ {
return (Vector){value(uu.x), value(uu.y), value(uu.z)}; return (Vector){value(uu.x), value(uu.y), value(uu.z)};
} }
#if LUPWD #if LUPWD
Scalar Scalar
upwd_der6(in Vector uu, in Scalar lnrho) upwd_der6(in VectorField uu, in ScalarField lnrho)
{ {
Scalar uux = fabs(value(uu).x); Scalar uux = fabs(value(uu).x);
Scalar uuy = fabs(value(uu).y); Scalar uuy = fabs(value(uu).y);
@@ -42,13 +42,13 @@ upwd_der6(in Vector uu, in Scalar lnrho)
#endif #endif
Matrix Matrix
gradients(in Vector uu) gradients(in VectorField uu)
{ {
return (Matrix){gradient(uu.x), gradient(uu.y), gradient(uu.z)}; return (Matrix){gradient(uu.x), gradient(uu.y), gradient(uu.z)};
} }
Scalar Scalar
continuity(in Vector uu, in Scalar lnrho) { continuity(in VectorField uu, in ScalarField lnrho) {
return -dot(value(uu), gradient(lnrho)) return -dot(value(uu), gradient(lnrho))
#if LUPWD #if LUPWD
//This is a corrective hyperdiffusion term for upwinding. //This is a corrective hyperdiffusion term for upwinding.
@@ -59,7 +59,7 @@ continuity(in Vector uu, in Scalar lnrho) {
#if LENTROPY #if LENTROPY
Vector Vector
momentum(in Vector uu, in Scalar lnrho, in Scalar ss, in Vector aa) { momentum(in VectorField uu, in ScalarField lnrho, in ScalarField ss, in VectorField aa) {
const Matrix S = stress_tensor(uu); const Matrix S = stress_tensor(uu);
const Scalar cs2 = cs2_sound * exp(gamma * value(ss) / cp_sound + (gamma - 1) * (value(lnrho) - lnrho0)); const Scalar cs2 = cs2_sound * exp(gamma * value(ss) / cp_sound + (gamma - 1) * (value(lnrho) - lnrho0));
const Vector j = (Scalar(1.) / mu0) * (gradient_of_divergence(aa) - laplace_vec(aa)); // Current density const Vector j = (Scalar(1.) / mu0) * (gradient_of_divergence(aa) - laplace_vec(aa)); // Current density
@@ -82,7 +82,7 @@ momentum(in Vector uu, in Scalar lnrho, in Scalar ss, in Vector aa) {
} }
#elif LTEMPERATURE #elif LTEMPERATURE
Vector Vector
momentum(in Vector uu, in Scalar lnrho, in Scalar tt) { momentum(in VectorField uu, in ScalarField lnrho, in ScalarField tt) {
Vector mom; Vector mom;
const Matrix S = stress_tensor(uu); const Matrix S = stress_tensor(uu);
@@ -103,7 +103,7 @@ momentum(in Vector uu, in Scalar lnrho, in Scalar tt) {
} }
#else #else
Vector Vector
momentum(in Vector uu, in Scalar lnrho) { momentum(in VectorField uu, in ScalarField lnrho) {
Vector mom; Vector mom;
const Matrix S = stress_tensor(uu); const Matrix S = stress_tensor(uu);
@@ -126,7 +126,7 @@ momentum(in Vector uu, in Scalar lnrho) {
Vector Vector
induction(in Vector uu, in Vector aa) { induction(in VectorField uu, in VectorField aa) {
// Note: We do (-nabla^2 A + nabla(nabla dot A)) instead of (nabla x (nabla // Note: We do (-nabla^2 A + nabla(nabla dot A)) instead of (nabla x (nabla
// x A)) in order to avoid taking the first derivative twice (did the math, // x A)) in order to avoid taking the first derivative twice (did the math,
// yes this actually works. See pg.28 in arXiv:astro-ph/0109497) // yes this actually works. See pg.28 in arXiv:astro-ph/0109497)
@@ -144,7 +144,7 @@ induction(in Vector uu, in Vector aa) {
#if LENTROPY #if LENTROPY
Scalar Scalar
lnT( in Scalar ss, in Scalar lnrho) { lnT( in ScalarField ss, in ScalarField lnrho) {
const Scalar lnT = lnT0 + gamma * value(ss) / cp_sound + const Scalar lnT = lnT0 + gamma * value(ss) / cp_sound +
(gamma - Scalar(1.)) * (value(lnrho) - lnrho0); (gamma - Scalar(1.)) * (value(lnrho) - lnrho0);
return lnT; return lnT;
@@ -152,7 +152,7 @@ lnT( in Scalar ss, in Scalar lnrho) {
// Nabla dot (K nabla T) / (rho T) // Nabla dot (K nabla T) / (rho T)
Scalar Scalar
heat_conduction( in Scalar ss, in Scalar lnrho) { heat_conduction( in ScalarField ss, in ScalarField lnrho) {
const Scalar inv_cp_sound = AcReal(1.) / cp_sound; const Scalar inv_cp_sound = AcReal(1.) / cp_sound;
const Vector grad_ln_chi = - gradient(lnrho); const Vector grad_ln_chi = - gradient(lnrho);
@@ -174,7 +174,7 @@ heating(const int i, const int j, const int k) {
} }
Scalar Scalar
entropy(in Scalar ss, in Vector uu, in Scalar lnrho, in Vector aa) { entropy(in ScalarField ss, in VectorField uu, in ScalarField lnrho, in VectorField aa) {
const Matrix S = stress_tensor(uu); const Matrix S = stress_tensor(uu);
const Scalar inv_pT = Scalar(1.) / (exp(value(lnrho)) * exp(lnT(ss, lnrho))); const Scalar inv_pT = Scalar(1.) / (exp(value(lnrho)) * exp(lnT(ss, lnrho)));
const Vector j = (Scalar(1.) / mu0) * (gradient_of_divergence(aa) - laplace_vec(aa)); // Current density const Vector j = (Scalar(1.) / mu0) * (gradient_of_divergence(aa) - laplace_vec(aa)); // Current density
@@ -191,7 +191,7 @@ entropy(in Scalar ss, in Vector uu, in Scalar lnrho, in Vector aa) {
#if LTEMPERATURE #if LTEMPERATURE
Scalar Scalar
heat_transfer(in Vector uu, in Scalar lnrho, in Scalar tt) heat_transfer(in VectorField uu, in ScalarField lnrho, in ScalarField tt)
{ {
const Matrix S = stress_tensor(uu); const Matrix S = stress_tensor(uu);
const Scalar heat_diffusivity_k = 0.0008; //8e-4; const Scalar heat_diffusivity_k = 0.0008; //8e-4;
@@ -290,26 +290,26 @@ forcing(int3 globalVertexIdx, Scalar dt)
// Declare input and output arrays using locations specified in the // Declare input and output arrays using locations specified in the
// array enum in astaroth.h // array enum in astaroth.h
in Scalar lnrho = VTXBUF_LNRHO; in ScalarField lnrho(VTXBUF_LNRHO);
out Scalar out_lnrho = VTXBUF_LNRHO; out ScalarField out_lnrho(VTXBUF_LNRHO);
in Vector uu = (int3) {VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ}; in VectorField uu(VTXBUF_UUX, VTXBUF_UUY, VTXBUF_UUZ);
out Vector out_uu = (int3) {VTXBUF_UUX,VTXBUF_UUY,VTXBUF_UUZ}; out VectorField out_uu(VTXBUF_UUX,VTXBUF_UUY,VTXBUF_UUZ);
#if LMAGNETIC #if LMAGNETIC
in Vector aa = (int3) {VTXBUF_AX,VTXBUF_AY,VTXBUF_AZ}; in VectorField aa(VTXBUF_AX,VTXBUF_AY,VTXBUF_AZ);
out Vector out_aa = (int3) {VTXBUF_AX,VTXBUF_AY,VTXBUF_AZ}; out VectorField out_aa(VTXBUF_AX,VTXBUF_AY,VTXBUF_AZ);
#endif #endif
#if LENTROPY #if LENTROPY
in Scalar ss = VTXBUF_ENTROPY; in ScalarField ss(VTXBUF_ENTROPY);
out Scalar out_ss = VTXBUF_ENTROPY; out ScalarField out_ss(VTXBUF_ENTROPY);
#endif #endif
#if LTEMPERATURE #if LTEMPERATURE
in Scalar tt = VTXBUF_TEMPERATURE; in ScalarField tt(VTXBUF_TEMPERATURE);
out Scalar out_tt = VTXBUF_TEMPERATURE; out ScalarField out_tt(VTXBUF_TEMPERATURE);
#endif #endif
Kernel void Kernel void

View File

@@ -15,6 +15,8 @@ L [a-zA-Z_]
"void" { return VOID; } /* Rest of the types inherited from C */ "void" { return VOID; } /* Rest of the types inherited from C */
"int" { return INT; } "int" { return INT; }
"int3" { return INT3; } "int3" { return INT3; }
"ScalarField" { return SCALAR; }
"VectorField" { return VECTOR; }
"Kernel" { return KERNEL; } /* Function specifiers */ "Kernel" { return KERNEL; } /* Function specifiers */
"Preprocessed" { return PREPROCESSED; } "Preprocessed" { return PREPROCESSED; }

View File

@@ -101,6 +101,7 @@ exec_statement: declaration
; ;
assignment: declaration '=' expression { $$ = astnode_create(NODE_UNKNOWN, $1, $3); $$->infix = '='; } assignment: declaration '=' expression { $$ = astnode_create(NODE_UNKNOWN, $1, $3); $$->infix = '='; }
| declaration '(' expression_list ')' { $$ = astnode_create(NODE_UNKNOWN, $1, $3); $$->infix = '('; $$->postfix = ')'; } // C++ style initializer
| expression '=' expression { $$ = astnode_create(NODE_UNKNOWN, $1, $3); $$->infix = '='; } | expression '=' expression { $$ = astnode_create(NODE_UNKNOWN, $1, $3); $$->infix = '='; }
; ;

View File

@@ -229,8 +229,11 @@ translate_latest_symbol(void)
// IN / OUT // IN / OUT
else if (symbol->type != SYMBOLTYPE_FUNCTION_PARAMETER && else if (symbol->type != SYMBOLTYPE_FUNCTION_PARAMETER &&
(symbol->type_qualifier == IN || symbol->type_qualifier == OUT)) { (symbol->type_qualifier == IN || symbol->type_qualifier == OUT)) {
const char* inout_type_qualifier = "static __device__ const auto";
printf("%s %s%s", inout_type_qualifier, inout_name_prefix, symbol_table[handle].identifier); printf("static __device__ const %s %s%s", symbol->type_specifier == SCALAR ? "int" : "int3",
inout_name_prefix, symbol_table[handle].identifier);
if (symbol->type_specifier == VECTOR)
printf(" = make_int3");
} }
// OTHER // OTHER
else { else {

View File

@@ -46,6 +46,9 @@ IDX(const int3 idx)
return DEVICE_VTXBUF_IDX(idx.x, idx.y, idx.z); return DEVICE_VTXBUF_IDX(idx.x, idx.y, idx.z);
} }
#define make_int3(a, b, c) \
(int3) { (int)a, (int)b, (int)c }
static __forceinline__ AcMatrix static __forceinline__ AcMatrix
create_rotz(const AcReal radians) create_rotz(const AcReal radians)
{ {