Exemplo n.º 1
0
int test_auto3()
{
	auto d = 5.0; // 5.0 is a double literal, so d will be type double
	auto i = 1 + 2; // 1 + 2 evaluates to an integer, so i will be type int

	auto sum = add_3(5, 6); // add_3() returns an int, so sum will be type int

	return 0;
}
Exemplo n.º 2
0
static void
compute_grad_point(struct efp *efp, size_t frag_idx, size_t pt_idx)
{
	const struct frag *fr_i = efp->frags + frag_idx;
	const struct polarizable_pt *pt_i = fr_i->polarizable_pts + pt_idx;
	size_t idx_i = fr_i->polarizable_offset + pt_idx;

	vec_t dipole_i = {
		0.5 * (efp->indip[idx_i].x + efp->indipconj[idx_i].x),
		0.5 * (efp->indip[idx_i].y + efp->indipconj[idx_i].y),
		0.5 * (efp->indip[idx_i].z + efp->indipconj[idx_i].z)
	};

	for (size_t j = 0; j < efp->n_frag; j++) {
		if (j == frag_idx || efp_skip_frag_pair(efp, frag_idx, j))
			continue;

		struct frag *fr_j = efp->frags + j;
		struct swf swf = efp_make_swf(efp, fr_i, fr_j);

		/* energy without switching applied */
		double energy = 0.0;

		/* induced dipole - nuclei */
		for (size_t k = 0; k < fr_j->n_atoms; k++) {
			struct efp_atom *at_j = fr_j->atoms + k;

			vec_t dr = {
				at_j->x - pt_i->x - swf.cell.x,
				at_j->y - pt_i->y - swf.cell.y,
				at_j->z - pt_i->z - swf.cell.z
			};

			double p1 = 1.0, p2 = 0.0;

			if (efp->opts.pol_damp == EFP_POL_DAMP_TT) {
				double r = vec_len(&dr);

				p1 = efp_get_pol_damp_tt(r, fr_i->pol_damp,
						fr_j->pol_damp);
				p2 = efp_get_pol_damp_tt_grad(r, fr_i->pol_damp,
						fr_j->pol_damp);
			}

			vec_t force, add_i, add_j;

			double e = -efp_charge_dipole_energy(at_j->znuc, &dipole_i, &dr);

			efp_charge_dipole_grad(at_j->znuc, &dipole_i, &dr,
					       &force, &add_j, &add_i);
			vec_negate(&force);

			vec_scale(&force, p1);
			vec_scale(&add_i, p1);
			vec_scale(&add_j, p1);

			force.x += p2 * e * dr.x;
			force.y += p2 * e * dr.y;
			force.z += p2 * e * dr.z;

			vec_scale(&force, swf.swf);
			vec_scale(&add_i, swf.swf);
			vec_scale(&add_j, swf.swf);

			efp_add_force(efp->grad + frag_idx, CVEC(fr_i->x),
					CVEC(pt_i->x), &force, &add_i);
			efp_sub_force(efp->grad + j, CVEC(fr_j->x),
					CVEC(at_j->x), &force, &add_j);
			efp_add_stress(&swf.dr, &force, &efp->stress);

			energy += p1 * e;
		}

		/* induced dipole - multipoles */
		for (size_t k = 0; k < fr_j->n_multipole_pts; k++) {
			struct multipole_pt *pt_j = fr_j->multipole_pts + k;

			vec_t dr = {
				pt_j->x - pt_i->x - swf.cell.x,
				pt_j->y - pt_i->y - swf.cell.y,
				pt_j->z - pt_i->z - swf.cell.z
			};

			double p1 = 1.0, p2 = 0.0;

			if (efp->opts.pol_damp == EFP_POL_DAMP_TT) {
				double r = vec_len(&dr);

				p1 = efp_get_pol_damp_tt(r, fr_i->pol_damp,
						fr_j->pol_damp);
				p2 = efp_get_pol_damp_tt_grad(r, fr_i->pol_damp,
						fr_j->pol_damp);
			}

			double e = 0.0;

			vec_t force_, add_i_, add_j_;
			vec_t force = vec_zero, add_i = vec_zero, add_j = vec_zero;

			/* induced dipole - charge */
			e -= efp_charge_dipole_energy(pt_j->monopole, &dipole_i, &dr);

			efp_charge_dipole_grad(pt_j->monopole, &dipole_i, &dr,
					       &force_, &add_j_, &add_i_);
			vec_negate(&force_);
			add_3(&force, &force_, &add_i, &add_i_, &add_j, &add_j_);

			/* induced dipole - dipole */
			e += efp_dipole_dipole_energy(&dipole_i, &pt_j->dipole, &dr);

			efp_dipole_dipole_grad(&dipole_i, &pt_j->dipole, &dr,
					       &force_, &add_i_, &add_j_);
			vec_negate(&add_j_);
			add_3(&force, &force_, &add_i, &add_i_, &add_j, &add_j_);

			/* induced dipole - quadrupole */
			e += efp_dipole_quadrupole_energy(&dipole_i, pt_j->quadrupole, &dr);

			efp_dipole_quadrupole_grad(&dipole_i, pt_j->quadrupole, &dr,
						   &force_, &add_i_, &add_j_);
			add_3(&force, &force_, &add_i, &add_i_, &add_j, &add_j_);

			/* induced dipole - octupole interactions are ignored */

			vec_scale(&force, p1);
			vec_scale(&add_i, p1);
			vec_scale(&add_j, p1);

			force.x += p2 * e * dr.x;
			force.y += p2 * e * dr.y;
			force.z += p2 * e * dr.z;

			vec_scale(&force, swf.swf);
			vec_scale(&add_i, swf.swf);
			vec_scale(&add_j, swf.swf);

			efp_add_force(efp->grad + frag_idx, CVEC(fr_i->x),
					CVEC(pt_i->x), &force, &add_i);
			efp_sub_force(efp->grad + j, CVEC(fr_j->x),
					CVEC(pt_j->x), &force, &add_j);
			efp_add_stress(&swf.dr, &force, &efp->stress);

			energy += p1 * e;
		}

		/* induced dipole - induced dipoles */
		for (size_t jj = 0; jj < fr_j->n_polarizable_pts; jj++) {
			struct polarizable_pt *pt_j = fr_j->polarizable_pts + jj;
			size_t idx_j = fr_j->polarizable_offset + jj;

			vec_t dr = {
				pt_j->x - pt_i->x - swf.cell.x,
				pt_j->y - pt_i->y - swf.cell.y,
				pt_j->z - pt_i->z - swf.cell.z
			};

			vec_t half_dipole_i = {
				0.5 * efp->indip[idx_i].x,
				0.5 * efp->indip[idx_i].y,
				0.5 * efp->indip[idx_i].z
			};

			double p1 = 1.0, p2 = 0.0;

			if (efp->opts.pol_damp == EFP_POL_DAMP_TT) {
				double r = vec_len(&dr);

				p1 = efp_get_pol_damp_tt(r, fr_i->pol_damp,
						fr_j->pol_damp);
				p2 = efp_get_pol_damp_tt_grad(r, fr_i->pol_damp,
						fr_j->pol_damp);
			}

			vec_t force, add_i, add_j;

			double e = efp_dipole_dipole_energy(&half_dipole_i,
						&efp->indipconj[idx_j], &dr);

			efp_dipole_dipole_grad(&half_dipole_i, &efp->indipconj[idx_j],
						&dr, &force, &add_i, &add_j);
			vec_negate(&add_j);

			vec_scale(&force, p1);
			vec_scale(&add_i, p1);
			vec_scale(&add_j, p1);

			force.x += p2 * e * dr.x;
			force.y += p2 * e * dr.y;
			force.z += p2 * e * dr.z;

			vec_scale(&force, swf.swf);
			vec_scale(&add_i, swf.swf);
			vec_scale(&add_j, swf.swf);

			efp_add_force(efp->grad + frag_idx, CVEC(fr_i->x),
					CVEC(pt_i->x), &force, &add_i);
			efp_sub_force(efp->grad + j, CVEC(fr_j->x),
					CVEC(pt_j->x), &force, &add_j);
			efp_add_stress(&swf.dr, &force, &efp->stress);

			energy += p1 * e;
		}

		vec_t force = {
			swf.dswf.x * energy,
			swf.dswf.y * energy,
			swf.dswf.z * energy
		};

		six_atomic_add_xyz(efp->grad + frag_idx, &force);
		six_atomic_sub_xyz(efp->grad + j, &force);
		efp_add_stress(&swf.dr, &force, &efp->stress);
	}

	/* induced dipole - ab initio nuclei */
	if (efp->opts.terms & EFP_TERM_AI_POL) {
		for (size_t j = 0; j < efp->n_ptc; j++) {
			vec_t dr = vec_sub(efp->ptc_xyz + j, CVEC(pt_i->x));
			vec_t force, add_i, add_j;

			efp_charge_dipole_grad(efp->ptc[j], &dipole_i, &dr,
					       &force, &add_j, &add_i);
			vec_negate(&add_i);

			vec_atomic_add(efp->ptc_grad + j, &force);
			efp_sub_force(efp->grad + frag_idx, CVEC(fr_i->x),
					CVEC(pt_i->x), &force, &add_i);
		}
	}
}
Exemplo n.º 3
0
static int _ggi_clip2d_3(ggi_visual *vis,
			     int *_x0, int *_y0,
			     int *_x1, int *_y1,
			     int *clip_first, int *clip_last)
{
	int first,last, code;
	int x0,y0,x1,y1;
	int x,y;
	unsigned dx[3], dy[3], tmp[3];
	unsigned int absdx, absdy;
	int xmajor;
	int slope;
	int i;

	*clip_first = first = 0;
	*clip_last = last = 0;
	outcode(first,*_x0,*_y0);
	outcode(last,*_x1,*_y1);

	if ((first | last) == 0) {
		return 1; /* Trivially accepted! */
	}
	if ((first & last) != 0) {
		return 0; /* Trivially rejected! */
	}

	x0=*_x0; y0=*_y0;
	x1=*_x1; y1=*_y1;
	
	assign_int_3(dx, x1);
	assign_int_3(tmp, x0);
	sub_3(dx, tmp);

	assign_int_3(dy, y1);
	assign_int_3(tmp, y0);
	sub_3(dy, tmp);

	absdx = x0 < x1 ? x1 - x0 : x0 - x1;
	absdy = y0 < y1 ? y1 - y0 : y0 - y1;
	xmajor = absdx > absdy;
	slope = ((x1>=x0) && (y1>=y0)) || ((x1<x0) && (y1<y0));

	for (i = 0; i < 4; i++) {
		code = first;
		if (first==0)
			code = last;

		if (code&OC_LEFT) {
			x = LIBGGI_GC(vis)->cliptl.x;
			if (xmajor) {
				/* y = *_y0 + FloorDiv(dy*(x - *_x0)*2 + dx,
							2*dx); */
				unsigned _x[3], res[3];
				assign_int_3(_x, x);
				assign_int_3(tmp, *_x0);
				sub_3(_x, tmp);
				lshift_3(_x, 1);
				mul_3(_x, dy);
				add_3(_x, dx);
				assign_3(tmp, dx);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _x, tmp);
				assign_int_3(tmp, *_y0);
				add_3(res, tmp);
				y = res[0];
			} else if (slope) {
				/* y = *_y0 + CeilDiv(dy*((x - *_x0)*2 - 1),
							2*dx); */
				unsigned _x[3], res[3];
				assign_int_3(_x, x);
				assign_int_3(tmp, *_x0);
				sub_3(_x, tmp);
				lshift_3(_x, 1);
				dec_3(_x);
				mul_3(_x, dy);
				assign_3(tmp, dx);
				lshift_3(tmp, 1);
				CeilDiv_3(res, _x, tmp);
				assign_int_3(tmp, *_y0);
				add_3(res, tmp);
				y = res[0];
			} else {
				/* y = *_y0 + FloorDiv(dy*((x - *_x0)*2 - 1),
							2*dx); */
				unsigned _x[3], res[3];
				assign_int_3(_x, x);
				assign_int_3(tmp, *_x0);
				sub_3(_x, tmp);
				lshift_3(_x, 1);
				dec_3(_x);
				mul_3(_x, dy);
				assign_3(tmp, dx);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _x, tmp);
				assign_int_3(tmp, *_y0);
				add_3(res, tmp);
				y = res[0];
			}
		} else if (code&OC_RIGHT) {
			x = LIBGGI_GC(vis)->clipbr.x - 1;
			if (xmajor) {
				/* y = *_y0 + FloorDiv(dy*(x - *_x0)*2 + dx,
							2*dx); */
				unsigned _x[3], res[3];
				assign_int_3(_x, x);
				assign_int_3(tmp, *_x0);
				sub_3(_x, tmp);
				lshift_3(_x, 1);
				mul_3(_x, dy);
				add_3(_x, dx);
				assign_3(tmp, dx);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _x, tmp);
				assign_int_3(tmp, *_y0);
				add_3(res, tmp);
				y = res[0];
			} else if (slope) {
				/* y = *_y0 + CeilDiv(dy*((x - *_x0)*2 + 1),
							2*dx)-1; */
				unsigned _x[3], res[3];
				assign_int_3(_x, x);
				assign_int_3(tmp, *_x0);
				sub_3(_x, tmp);
				lshift_3(_x, 1);
				inc_3(_x);
				mul_3(_x, dy);
				assign_3(tmp, dx);
				lshift_3(tmp, 1);
				CeilDiv_3(res, _x, tmp);
				dec_3(res);
				assign_int_3(tmp, *_y0);
				add_3(res, tmp);
				y = res[0];
			} else {
				/* y = *_y0 + FloorDiv(dy*((x - *_x0)*2 + 1),
							2*dx)+1; */
				unsigned _x[3], res[3];
				assign_int_3(_x, x);
				assign_int_3(tmp, *_x0);
				sub_3(_x, tmp);
				lshift_3(_x, 1);
				inc_3(_x);
				mul_3(_x, dy);
				assign_3(tmp, dx);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _x, tmp);
				inc_3(res);
				assign_int_3(tmp, *_y0);
				add_3(res, tmp);
				y = res[0];
			}
		} else if (code&OC_TOP) {
			y = LIBGGI_GC(vis)->cliptl.y;
			if (!xmajor) {
				/* x = *_x0 + FloorDiv(dx*(y - *_y0)*2 + dy,
							2*dy); */
				unsigned _y[3], res[3];
				assign_int_3(_y, y);
				assign_int_3(tmp, *_y0);
				sub_3(_y, tmp);
				lshift_3(_y, 1);
				mul_3(_y, dx);
				add_3(_y, dy);
				assign_3(tmp, dy);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _y, tmp);
				assign_int_3(tmp, *_x0);
				add_3(res, tmp);
				x = res[0];
			} else if (slope) {
				/* x = *_x0 + CeilDiv(dx*((y - *_y0)*2 - 1),
							2*dy); */
				unsigned _y[3], res[3];
				assign_int_3(_y, y);
				assign_int_3(tmp, *_y0);
				sub_3(_y, tmp);
				lshift_3(_y, 1);
				dec_3(_y);
				mul_3(_y, dx);
				assign_3(tmp, dy);
				lshift_3(tmp, 1);
				CeilDiv_3(res, _y, tmp);
				assign_int_3(tmp, *_x0);
				add_3(res, tmp);
				x = res[0];
			} else {
				/* x = *_x0 + FloorDiv(dx*((y - *_y0)*2 - 1),
							2*dy); */
				unsigned _y[3], res[3];
				assign_int_3(_y, y);
				assign_int_3(tmp, *_y0);
				sub_3(_y, tmp);
				lshift_3(_y, 1);
				dec_3(_y);
				mul_3(_y, dx);
				assign_3(tmp, dy);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _y, tmp);
				assign_int_3(tmp, *_x0);
				add_3(res, tmp);
				x = res[0];
			}
		} else { /* OC_BOTTOM */
			LIB_ASSERT((code & OC_BOTTOM), "unknown outcode\n");
			y = LIBGGI_GC(vis)->clipbr.y - 1;
			if (!xmajor) {
				/* x = *_x0 + FloorDiv(dx*(y - *_y0)*2 + dy,
							2*dy); */
				unsigned _y[3], res[3];
				assign_int_3(_y, y);
				assign_int_3(tmp, *_y0);
				sub_3(_y, tmp);
				lshift_3(_y, 1);
				mul_3(_y, dx);
				add_3(_y, dy);
				assign_3(tmp, dy);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _y, tmp);
				assign_int_3(tmp, *_x0);
				add_3(res, tmp);
				x = res[0];
			} else if (slope) {
				/* x = *_x0 + CeilDiv(dx*((y - *_y0)*2 + 1),
							2*dy)-1; */
				unsigned _y[3], res[3];
				assign_int_3(_y, y);
				assign_int_3(tmp, *_y0);
				sub_3(_y, tmp);
				lshift_3(_y, 1);
				inc_3(_y);
				mul_3(_y, dx);
				assign_3(tmp, dy);
				lshift_3(tmp, 1);
				CeilDiv_3(res, _y, tmp);
				dec_3(res);
				assign_int_3(tmp, *_x0);
				add_3(res, tmp);
				x = res[0];
			} else {
				/* x = *_x0 + FloorDiv(dx*((y - *_y0)*2 + 1),
							2*dy)+1; */
				unsigned _y[3], res[3];
				assign_int_3(_y, y);
				assign_int_3(tmp, *_y0);
				sub_3(_y, tmp);
				lshift_3(_y, 1);
				inc_3(_y);
				mul_3(_y, dx);
				assign_3(tmp, dy);
				lshift_3(tmp, 1);
				FloorDiv_3(res, _y, tmp);
				inc_3(res);
				assign_int_3(tmp, *_x0);
				add_3(res, tmp);
				x = res[0];
			}
		}

		if (first!=0) {
			x0 = x;
			y0 = y;
			outcode(first,x0,y0);
			*clip_first = 1;
		} else {
			x1 = x;
			y1 = y;
			last = code;
			outcode(last,x1,y1);
			*clip_last = 1;
		}

		if ((first & last) != 0) {
			return 0; /* Trivially rejected! */
		}
		if ((first | last) == 0) {
			*_x0=x0; *_y0=y0;
			*_x1=x1; *_y1=y1;
			return 1; /* Trivially accepted! */
		}
	}
	return 0; /* Aieee! Failed to clip, clip whole line... */
}