void 
Stokhos::MPInverseModelEvaluator::evalModel(const InArgs& inArgs,
					    const OutArgs& outArgs) const
{
  // Create underlying inargs
  InArgs me_inargs = me->createInArgs();

  // Pass parameters
  for (int i=0; i<num_p; i++)
    me_inargs.set_p(i, inArgs.get_p(i));

  // Pass MP parameters
  for (int i=0; i<num_p_mp; i++) {
    mp_const_vector_t p_mp = inArgs.get_p_mp(mp_p_index_map[i]);
    if (p_mp != Teuchos::null) {
      me_inargs.set_p(i+num_p, p_mp->getBlockVector());
    }
  }

  // Create underlying outargs
  OutArgs me_outargs = me->createOutArgs();


  // MP Responses
  for (int i=0; i<num_g_mp; i++) {
    int ii = mp_g_index_map[i];

    // g
    mp_vector_t g_mp = outArgs.get_g_mp(ii);
    if (g_mp != Teuchos::null) {
      me_outargs.set_g(i, Teuchos::rcp_dynamic_cast<Epetra_Vector>(g_mp->getBlockVector()));
    }

    // dg/dp
    for (int j=0; j<num_p; j++) {
      if (!outArgs.supports(OUT_ARG_DgDp_mp, ii, j).none()) {
	MPDerivative dgdp_mp = outArgs.get_DgDp_mp(ii,j);
	Teuchos::RCP<Stokhos::ProductEpetraMultiVector> dgdp_mp_mv = 
	  dgdp_mp.getMultiVector();
	Teuchos::RCP<Epetra_Operator> dgdp_mp_op =
	  dgdp_mp.getLinearOp();
	if (dgdp_mp_mv != Teuchos::null) {
	  me_outargs.set_DgDp(
	    i, j, Derivative(dgdp_mp_mv->getBlockMultiVector(),
			     dgdp_mp.getMultiVectorOrientation()));
	}
	else if (dgdp_mp_op != Teuchos::null) {
	  me_outargs.set_DgDp(i, j, Derivative(dgdp_mp_op));
	}
      }
    }

  }

  // Compute the functions
  me->evalModel(me_inargs, me_outargs);

}
示例#2
0
AxisPhysicsModel::Derivative
AxisPhysicsModel::Evaluate(const State &aInitState, double aDeltaTime,
                           const Derivative &aDerivative)
{
  State state( aInitState.p + aDerivative.dp*aDeltaTime, aInitState.v + aDerivative.dv*aDeltaTime );

  return Derivative( state.v, Acceleration(state) );
}
double ActivationFunction::Derivative(double* variables, double* position) {
  if (variables == NULL || position == NULL)
    return 0.0;

  for (int x = 0; x < dims; x++)
    variables[x] -= position[x];

  return Derivative(variables);
}
示例#4
0
//---------------------------------------------------------------
//加载训练样本
int Dmplwr::Load(string path){
  mat DataM;
  string UpdatedPath;

  UpdatedPath = path+"demo.txt";
  DataM.load(UpdatedPath,raw_ascii);
  //DataM = DataM;
  model.demo = DataM.col(0);
 
 //----------------------------------------------------------------  
  model.demo_d =Derivative(model.demo,model.dt) ;
 
  model.demo_dd =Derivative(model.demo_d,model.dt) ; 
  model.demo = trans(model.demo);
  model.demo_d = trans(model.demo_d);
  model.demo_dd = trans(model.demo_dd);
  
  //cout<<"temp data"<<model.demo_dd<<endl;
  cout<<"Loading of the demonstartions is finished"<<endl;
  return 1;
}
示例#5
0
void integrate(State &state, float t, float dt)
{
	Derivative a = evaluate(state, t, 0.0f, Derivative());
	Derivative b = evaluate(state, t+dt*0.5f, dt*0.5f, a);
	Derivative c = evaluate(state, t+dt*0.5f, dt*0.5f, b);
	Derivative d = evaluate(state, t+dt, dt, c);
	
	const float dxdt = 1.0f/6.0f * (a.dx + 2.0f*(b.dx + c.dx) + d.dx);
	const float dvdt = 1.0f/6.0f * (a.dv + 2.0f*(b.dv + c.dv) + d.dv);
	
	state.x = state.x + dxdt * dt;
	state.v = state.v + dvdt * dt;
}
void Velocity_Update_X(double*** velocity_x, double*** velocity_x_tilda,
                       double*** rho, double*** pressure,
                       double dx, double dt,
                       int ldx, int ldy, int ldz)
{
  for (int k=0; k<ldz; k++){
    for (int j=0; j<ldy; j++){
      for (int i=0; i<ldx; i++){


        velocity_x[k][j][i] = velocity_x_tilda[k][j][i]
          -dt/rho[k][j][i]*
          ( (4./3.)*Derivative(pressure[k][j][i+1],pressure[k][j][i-1],
                               dx,2)
            -(1./3.)*Derivative(pressure[k][j][i+2],pressure[k][j][i-2],
                                dx,4));


      }
    }
  }
}
示例#7
0
文件: RK4.cpp 项目: Mischa-Alff/Wind
	void RK4_func(Entity &e, Engine::StandardDuration dt)
	{
		e.position += e.minimum_translation;
		acceleration = (e.force+e.impulse_force) / e.mass;
		Derivative a,b,c,d;
		a = evaluate(e, 0.f, 0.f, Derivative());
		b = evaluate(e, 0.f, dt.count()+0.5f, a);
		c = evaluate(e, 0.f, dt.count()+0.5f, b);
		d = evaluate(e, 0.f, dt.count(), c);

		Vector2f dpdt = 1.0f / 6.0f * (a.d_pos + 2.0f*(b.d_pos + c.d_pos) + d.d_pos);
		Vector2f dvdt = 1.0f / 6.0f * (a.d_vel + 2.0f*(b.d_vel + c.d_vel) + d.d_vel);

		e.position = e.position + dpdt * dt.count();
		e.velocity = e.velocity + dvdt * dt.count();
		e.minimum_translation = e.impulse_force = {0.f, 0.f};
	}
示例#8
0
void
AxisPhysicsModel::Integrate(double aDeltaTime)
{
  mPrevState = mNextState;

  // RK4 (Runge-Kutta method) Integration
  // http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
  Derivative a = Evaluate( mNextState, 0.0, Derivative() );
  Derivative b = Evaluate( mNextState, aDeltaTime * 0.5, a );
  Derivative c = Evaluate( mNextState, aDeltaTime * 0.5, b );
  Derivative d = Evaluate( mNextState, aDeltaTime, c );

  double dpdt = 1.0 / 6.0 * (a.dp + 2.0 * (b.dp + c.dp) + d.dp);
  double dvdt = 1.0 / 6.0 * (a.dv + 2.0 * (b.dv + c.dv) + d.dv);

  mNextState.p += dpdt * aDeltaTime;
  mNextState.v += dvdt * aDeltaTime;
}
示例#9
0
文件: physics.cpp 项目: Khady/p5
State Physics::rk4(real_t dt, const State &state)
{
    Derivative a,b,c,d;
    a = evaluate( state, 0.0f, Derivative() );
    b = evaluate( state, dt*0.5f, a );
    c = evaluate( state, dt*0.5f, b );
    d = evaluate( state, dt, c );

    Vector3 dxdt = ( a.dx + 2.0f*(b.dx + c.dx) + d.dx ) * (1.0f / 6.0f);

    Vector3 dvdt =
        ( a.dv + 2.0f*(b.dv + c.dv) + d.dv ) * (1.0f / 6.0f);

    State s;
    s.x = dxdt * dt;
    s.v = dvdt * dt;
    return s;
}
示例#10
0
文件: physics.cpp 项目: Chelsea21/p5
void Physics::rk4_integrate(State &state, Body *body_ptr, const real_t dt) const {
	Derivative d1 = take_derivative(state, Derivative(), body_ptr, 0);
	Derivative d2 = take_derivative(state, d1, body_ptr, dt * 0.5);
	Derivative d3 = take_derivative(state, d2, body_ptr, dt * 0.5);
	Derivative d4 = take_derivative(state, d3, body_ptr, dt);

	Vector3 dxdt = 1.f / 6 * (d1.d_position + 2 * (d2.d_position + d3.d_position) + d4.d_position);
	Vector3 dvdt = 1.f / 6 * (d1.d_velocity + 2 * (d2.d_velocity + d3.d_velocity) + d4.d_velocity);
	Vector3 daxdt = 1.f / 6 * (d1.d_angular_position + 2 * (d2.d_angular_position + d3.d_angular_position)
						+ d4.d_angular_position);
	Vector3 davdt = 1.f / 6 * (d1.d_angular_velocity + 2 * (d2.d_angular_velocity + d3.d_angular_velocity)
					+ d4.d_angular_velocity);

	state.position += dxdt * dt;
	state.velocity += dvdt * dt;
	// Since angular position is the rotation radian, we don't need to add it to old value.
	state.angular_position = daxdt * dt;
	state.angular_velocity += davdt * dt;
}
示例#11
0
//-----------------------------------------------------------------------------
//! @brief   TODO enter a description
//! @remark
//-----------------------------------------------------------------------------
State RKIntegrator::integrate( const State& initial, float t, float dt )
{
    Derivative a,b,c,d;
    State state = initial;

    a = evaluate( state, t, 0.0f, Derivative() );
    b = evaluate( state, t, dt*0.5f, a );
    c = evaluate( state, t, dt*0.5f, b );
    d = evaluate( state, t, dt, c );

    float oneSixth = 1.0f / 6.0f;
    float dxdt = oneSixth * ( a.dx + 2.0f*(b.dx + c.dx) + d.dx );
    float dvdt = oneSixth * ( a.dv + 2.0f*(b.dv + c.dv) + d.dv );

    state.m_position = state.m_position + dxdt * dt;
    state.m_velocity = state.m_velocity + dvdt * dt;

    return state;
}
PointMass PhysicalEntity::compute_next_state(float dt)
{
    Derivative a, b, c, d;

    a = evaluate(0.0f, Derivative());
    b = evaluate(dt * 0.5f, a);
    c = evaluate(dt * 0.5f, b);
    d = evaluate(dt, c);

    sf::Vector2f dpos_dt =
        1.0f / 6.0f *
        (a.delta_pos + 2.0f * (b.delta_pos + c.delta_pos) + d.delta_pos);

    sf::Vector2f dv_dt =
        1.0f / 6.0f *
        (a.delta_vee + 2.0f * (b.delta_vee + c.delta_vee) + d.delta_vee);

    return {current_state.position + dpos_dt * dt,
            current_state.velocity + dv_dt * dt, current_state.mass};
}
示例#13
0
//参数初始化--------------------------------------------------
Dmplwr::Dmplwr(int n, int s,double deltat){
  model.nbData = n; //Number of training sample of the model
  model.nbWeight =n*s; 
  model.dt = deltat;
  model.alpha_g = 15;
  model.beta_g = model.alpha_g/4;
  model.alpha_z = 5;
  model.move_time = (n-1)*model.dt;
  model.tau = 1/model.move_time;
  model.t = linspace(0,model.move_time,model.nbData);
  //计算核函数中心值及接收域宽度
  model.h = zeros(model.nbWeight,1);
  model.c = zeros(model.nbWeight,1);
  vec temp = linspace(0,1,model.nbWeight);

  model.c = exp(-model.alpha_z*temp);    
 model.h = Derivative(model.c,1)*0.65;
 model.h = 1/(model.h%model.h); 
   //cout<<"temp data"<<model.h<<endl;
  cout<<"Parameter initialization of the model is finished"<<endl;
};
示例#14
0
int BBCEfficiency::FitAcceptance(float left_low = -60., float left_high = 0., float right_low = 0., float right_high = 60.) {
  // these fit ranges should encompass any vertex cut range.
  TF1* left = new TF1("left","gaus",left_low,left_high);
  left->SetLineColor(kGreen);
  TF1* right = new TF1("right","gaus",right_low,right_high);
  right->SetLineColor(kBlue);
  Double_t par[6];
  Derivative(trigger_acceptance_, acceptance_first_derivative_);


  // Needs parameter settings, etc, but we've already successfully fit.
  // left_erf_  = new TF1("left_erf_","[0]+[1]*TMath::Erf((x-[2])/[3])",-60,0);
  // right_erf_ = new TF1("right_erf_","[0]+[1]*TMath::Erf((x-[2])/[3])",0,60);

  for(int i = 0; i < acceptance_first_derivative_->GetNbinsX(); i++) {
    abs_acceptance_first_derivative_->SetBinContent(i+1,fabs(acceptance_first_derivative_->GetBinContent(i+1)));
    abs_acceptance_first_derivative_->SetBinError(i+1,acceptance_first_derivative_->GetBinError(i+1));
  }

  abs_acceptance_first_derivative_->Fit(left,"R");
  abs_acceptance_first_derivative_->Fit(right,"R+");
  left->GetParameters(&par[0]);
  right->GetParameters(&par[3]);

  // store for access later in the class to member variables.
  left_gaus_ = left;
  right_gaus_ = right;
  
  z_vtx_cut_min_ = left_gaus_->GetParameter(1); 
  z_vtx_cut_max_ = right_gaus_->GetParameter(1); 

  std::cout << "Trigger vertex cut: " << z_vtx_cut_min_ << ", " << z_vtx_cut_max_ << std::endl;

  plot_registry_.push_back(left);
  plot_registry_.push_back(right);
  return 0;
}
void Intermediate_Velocity_Y_Press(double*** velocity_y_tilda,
				   double*** residual_y,
				   double*** residual_y_old,
				   double*** velocity_x, 
				   double*** velocity_y,
				   double*** velocity_z,
				   double*** flux_x, double***flux_y, 
				   double***flux_z,
				   double*** rho_new, double*** rho,
				   double*** temperature,
				   double*** pressure,
				   double Reynolds,double source,
				   double dx, double* dy,  double dz,
				   double dt, double time_total,
				   int ldx, int ldy, int ldz)
{

  Velocity_Residual_Y( residual_y,
                       velocity_x,  velocity_y, velocity_z,
                       flux_x, flux_y,  flux_z,
                       temperature, Reynolds,source,
                       dx, dy,  dz,
		       time_total,
                       ldx, ldy, ldz);


  /* After the calculation of the Velocity_Residual I will compute the Intermediate Velocity
     at the Grid*/


  for (int k=0; k<ldz; k++){
    for (int j=0; j<ldy; j++){
      for (int i=0; i<ldx; i++){



	double residual_sum=
	  1.5*residual_y[k][j][i] - 0.5*residual_y_old[k][j][i];


	// Introducing this term in order to fix the issue with the
	// pressure gradient in the tangential direction of the wall
	double interpolation_y[2];
	  for(int vj=0; vj<2; vj++)
	    {
	      interpolation_y[vj] =
		Interpolation_Y(pressure[k][j+vj][i],dy[j+vj],
				pressure[k][j+vj-1][i],dy[j+vj-1]);
	    }
	
	
	double pressure_gradient = Derivative(interpolation_y[1],
					      interpolation_y[0],
					      2.*dy[j],1);




        velocity_y_tilda[k][j][i] = 
	  (rho[k][j][i]*velocity_y[k][j][i] +
	   dt*(residual_sum - pressure_gradient))
	   /rho_new[k][j][i];



      }
    }
  }
}
示例#16
0
int main(int argc, char* argv[])
{

	UINT channel = 0;
	unsigned char ModuleAddress;

	int NumberOfUSB1s = 0;
	BOOL blnResult;

	//	unsigned long TimeStamp;
	//unsigned char ParallelInput;

	DWORD start, finish, duration;
	FILE *fp, *fn, *fw;
	int i, j, k, ii, jj;
	float tmp;
	const int m = 200;
	float e[3][m], u[3][m];
	float Kp[3] = { 3.0, 5.0, 0.0 }, Kd[3] = { 0.1, 0.1, 0.0 }; // Position control 
																//	float Kp[3] = {50.0, 3.0, 0.0}, Kd[3] ={1.00, 0.250, 0.0}; // Trajectory control 
	float theta[3][m], dtheta[3][m], de[3][m], thetad[3][m], dthetad[3][m], ddthetad[3][m];
	long lVal[3];
	double Kp_value[n], Kp_saturation = 50.0, KpVal[3][m];
	float c[3] = { 10.0f, 10.0f, 0.0f }, s[3][m], Phi_p[3] = { 10.0f, 6.0f, 0.0f }, Phi_d[3] = { 0.5f, 1.0f, 0.0f };
	float l = 1.0, h = 50.0, Enorm = 0.0f, Unorm = 0.0f;
	float H2IW[DOF][number_I][number_H];	// Hidden layer weights
	float O2HW[DOF][number_H + 1];	// Output layer weights 
	float H_Output[DOF][number_H + 1];
	float TInput[DOF][number_I];
	float NN_Output[DOF];

	if ((fw = fopen("TrainedWts.txt", "rt")) == NULL)
	{
		printf("Unable to open TrainedWeights file!\n"); exit(0);
	}

	for (k = 0; k < DOF; k++) {
		for (i = 0; i < number_I; i++)
		{
			for (j = 0; j < number_H; j++) fscanf(fw, "%f", &H2IW[k][i][j]);
		}
		for (i = 0; i < number_H + 1; i++)
		{
			fscanf(fw, "%f", &O2HW[k][i]);
		}
	}

	// ************ Position Control Reference Trajectories *********
/*
	for (i = 0; i < m; i++)
	{
		thetad[0][i] = 90.0f;
		thetad[1][i] = 90.0f;
		thetad[2][i] = 45.0f;

		for (j = 0; j < DOF; j++) {
			dthetad[j][i] = 0.0f;
			ddthetad[j][i] = 0.0f;
		}
	}
	*/

	//
	// ************ Trajectory Control Reference Trajectories: Cycloidal *********

	for (i=0; i < m; i++)
	{
	if (i < m/2.55)
	{
	thetad[0][i] = 45.0f *(TS*3*i - sin(TS*3*i))/(2 * PI);
	thetad[1][i] = 45.0f *(TS*3*i - sin(TS*3*i))/(2 * PI);
	thetad[2][i] = 45.0f *(TS*3*i - sin(TS*3*i))/(2 * PI);

	dthetad[0][i] = 40.0f *(3.0 - 3*cos(TS*3*i))/(2 * PI);
	dthetad[1][i] = 45.0f *(3.0 - 3*cos(TS*3*i))/(2 * PI);
	dthetad[2][i] = 45.0f *(3.0 - 3*cos(TS*3*i))/(2 * PI);

	ddthetad[0][i] = - 360.0f * sin(TS*3*i)/(2 * PI);
	ddthetad[1][i] = - 405.0f * sin(TS*3*i)/(2 * PI);
	ddthetad[2][i] = - 405.0f * sin(TS*3*i)/(2 * PI);
	} else
	{
	thetad[0][i] = 45.0f;
	thetad[1][i] = 45.0f;
	thetad[2][i] = 45.0f;

	dthetad[0][i] = dthetad[1][i] = dthetad[2][i] = 0.0f;
	ddthetad[0][i] = ddthetad[1][i] = ddthetad[2][i] = 0.0f;
	}

	}
	

	// ************ Trajectory Control Reference Trajectories: Sine *********

	// **********************************************************************


	NumberOfUSB1s = USB1Init();
	printf("Number of USB1s = %d\n", NumberOfUSB1s);

	blnResult = USB1ReturnModuleAddress(0, &ModuleAddress);
	if (blnResult == FALSE) printf("Cannot read Module Address!\n");

	InitKusb();

	for (i = 0; i < DOF; i++) {
		e[i][0] = 0.0;
		InitDAC();

		/*		printf("Initializing!\n");

		USB1GetIncPosition(ModuleAddress, i, &lVal[i]);
		if (i == 0 || i == 2) theta[i][0] = 360.0f*((float(lVal[i]%1600))/1600.0);
		else theta[i][0] = 360.0f*((float(lVal[i]%1250))/1250.0);
		while (theta[i][0] < 1.0f) DaOutput(2.0, channel+i);
		*/
		DaOutput(2.5, channel + i);
		USB1GetIncPosition(ModuleAddress, i, &lVal[i]);
		if (i == 0 || i == 2) theta[i][0] = 360.0f*((float(lVal[i] % 1600)) / 1600.0);
		else theta[i][0] = 360.0f*((float(lVal[i] % 1250)) / 1250.0);

	}
	printf("Initial angles: %10.3f\t%10.3f\n", 360.0f*((float(lVal[0] % 1600)) / 1600.0), 360.0f*((float(lVal[1] % 1250)) / 1250.0));
	for (i = 0; i < n; i++) Kp_value[i] = (i + 1) * (Kp_saturation) / n;

	printf("Press ANY key to start!\n"); getch();

	start = GetTickCount();


	for (i = 1; i <= m; i++)
	{
		l = l + 1.0;

		for (j = 0; j < DOF; j++)
		{
			USB1GetIncPosition(ModuleAddress, j, &lVal[j]);

			if (j == 0 || j == 2) theta[j][i] = 360.0f*((float(lVal[j] % 1600)) / 1600.0);
			else theta[j][i] = 360.0f*((float(lVal[j] % 1250)) / 1250.0);

			e[j][i] = thetad[j][i] - theta[j][i];
			dtheta[j][i] = Derivative(theta[j][i], theta[j][i - 1]);
			de[j][i] = dthetad[j][i] - dtheta[j][i];

			// ******************** Fuzzy Control ******************************

			Kp[j] = KpFuzzyRule(e[j][i] * 6, Kp_value) / 10.0;
			KpVal[j][i] = Kp[j];
			// *****************************************************************
			//
			/* ******************** Sliding Mode Control ***********************

			s[j][i] = c[j]*e[j][i] + de[j][i];
			if ((s[j][i]*e[j][i]) >= 0.0) Kp[j] = Phi_p[j];
			else Kp[j] = -Phi_p[j];
			if ((s[j][i]*de[j][i]) >= 0.0) Kd[j] = Phi_d[j];
			else Kd[j] = -Phi_d[j];

			*/
			u[j][i] = -(Kp[j] * e[j][i] + Kd[j] * de[j][i]) / 100;

			// ******** Calculate Feedforward Compensation from Neural Network ******

			// Calculate hidden layer neuron outputs

			TInput[j][0] = theta[j][i]; TInput[j][1] = dtheta[j][i]; TInput[j][2] = ddthetad[j][i];
			TInput[j][3] = 1.0;

			for (ii = 0; ii < number_H; ii++)
			{
				tmp = 0.0;
				for (k = 0; k <number_I; k++)
				{
					tmp = tmp + H2IW[j][k][ii] * TInput[ii][k];
				}
				H_Output[j][ii] = sigmoid(tmp);
			}
			H_Output[i][number_H] = 1.0f;

			// Calculate NN output and error

			for (j = 0; j < (number_H + 1); j++)
			{
				tmp = 0.0;
				tmp = tmp + O2HW[i][j] * H_Output[i][j];
			}
			NN_Output[i] = tmp;

			if (u[j][i] > 2.5) u[j][i] = 2.5; else if (u[j][i] < -2.5) u[j][i] = -2.5;

			InitDAC();
			DaOutput(u[j][i] + 2.5, channel + j);
		}
		Enorm = Enorm + e[0][i] * e[0][i] + e[1][i] * e[1][i]; Unorm = Unorm + u[0][i] * u[0][i] + u[1][i] * u[1][i];
	}

	finish = GetTickCount();
	duration = finish - start;

	Enorm = sqrt(Enorm); Unorm = sqrt(Unorm);
	printf("\nTS = %d msec\t Enorm = %10.3f\t Unorm = %10.3f\n", duration / m, Enorm, Unorm);
	printf("Initial angles: %10.3f\t%10.3f\n", 360.0f*((float(lVal[0] % 1600)) / 1600.0), 360.0f*((float(lVal[1] % 1250)) / 1250.0));

	for (j = 0; j < 3; j++) {

		InitDAC();
		DaOutput(2.5, channel + j);
	}

	if ((fp = fopen("P.txt", "wt")) == NULL)
	{
		printf("Cannot open the output file!\n");
	}
	if ((fn = fopen("NnetTrainingData.txt", "wt")) == NULL)
	{
		printf("Cannot open the output file!\n");
	}
	//	fprintf(fp, "\nTS = %d msec\t Enorm = %10.3f\t Unorm = %10.3f\n", duration/m, Enorm, Unorm);

	for (i = 1; i < m; i++)
	{

		fprintf(fp, "%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\n", i*TS, thetad[0][i], theta[0][i], e[0][i], u[0][i], KpVal[1][i], thetad[1][i], theta[1][i], e[1][i], u[1][i], KpVal[1][i]);
		fprintf(fn, "%10.6f\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%10.6f\t%10.6f\n", theta[0][i] / 57.3248, dtheta[0][i] / (40.0*57.3248), 0.0, 0.5*u[0][i], 1.0, theta[1][i] / 57.3248, dtheta[1][i] / (40.0*57.3248), 0.0, 0.5*u[1][i], 1.0);
	}
	rewind(fp);
	fclose(fp);

	InitDAC();
	DaOutput(2.5, channel + 1);

	olDaTerminate(board.hdrvr);

	std::cin.get();

	return((UINT)NULL);


}
double Viscous_Component_YY(double*** velocity_x, double*** velocity_y,
                            double*** velocity_z,
                            double*** temperature, double Reynolds,
                            double dx, double* dy, double dz,
                            int i, int j, int k)
{

  double derivative_yx[3][2],derivative_yz[3][2],derivative_yy[2],
    viscous_terms[2], viscosity[2], dy_total;



  //Calculation of the d/dy(dv/dy)
  //j-1/2
  dy_total= dy[j]+dy[j-1];
  derivative_yy[0] = 
    Derivative(velocity_y[k][j][i],velocity_y[k][j-1][i],
	       dy_total,1);

  //j+1/2
  dy_total= dy[j+1]+dy[j];
  derivative_yy[1] = 
    Derivative(velocity_y[k][j+1][i], velocity_y[k][j][i],
	       dy_total,1);


  //Calculation of the d/dy(du/dx)
  double sum[3];
  for (int vi=0; vi<3; vi++)
    {

      derivative_yx[vi][0]=4./3.*Derivative(velocity_x[k][j+vi-1][i+1],
                                            velocity_x[k][j+vi-1][i-1],
                                            dx,2);

      derivative_yx[vi][1]=-1./3.*Derivative(velocity_x[k][j+vi-1][i+2],
                                             velocity_x[k][j+vi-1][i-2],
                                             dx,4);
      sum[vi]=0.;
      for (int vj=0; vj<2; vj++)
        {
          sum[vi] += derivative_yx[vi][vj];
        }

    }

  double total_derivative_x[2];
  for (int vi=0; vi<2; vi++)
    {
      //interpolation to obtain the derivative at the desired point
      total_derivative_x[vi] = Interpolation_Y(sum[vi],dy[j-1+vi],
                                               sum[vi+1],dy[j+vi]);
    }



  // Calculation of the d/dy(dw/dz)

  for (int vi=0; vi<3; vi++)
    {
      derivative_yz[vi][0]=4./3.*Derivative(velocity_z[k+1][j+vi-1][i],
                                            velocity_z[k-1][j+vi-1][i],
                                            dz,2);

      derivative_yz[vi][1]=-1./3.*Derivative(velocity_z[k+2][j+vi-1][i],
                                             velocity_z[k-2][j+vi-1][i],
                                             dz,4);

      sum[vi]=0.;
      for (int vj=0; vj<2; vj++)
        {
          sum[vi] += derivative_yz[vi][vj];
        }
    }


  //interpolation to obtain the derivative at the desired point
  double total_derivative_z[2];
  for (int vi=0; vi<2; vi++)
    {

      total_derivative_z[vi] = Interpolation_Y(sum[vi],dy[j-1+vi],
                                               sum[vi+1],dy[j+vi]);
    }

  //Computing the viscosities.
  for (int vi=-1, vj=0; vi<1; vi++, vj++)
    {
      viscosity[vj]=
        Viscosity_Calculator(Interpolation(temperature[k][j+vi][i],
                                           temperature[k][j+vi+1][i]));
    }



  //computing the viscous tensors
  for (int vi=0; vi<2; vi++)
    {
      viscous_terms[vi] =viscosity[vi]*(4./3*derivative_yy[vi]
                                        -2./3.*(total_derivative_x[vi]+
                                                total_derivative_z[vi]) );
    }


  // cout<<"yy"<<endl;
  //     cout<<total_derivative_x[1]-total_derivative_x[0]<<endl;
  //     cout<<total_derivative_z[1]-total_derivative_z[0]<<endl;


  /* Summing the X-component of the Viscous Term of the Y-Momentum equation*/

  dy_total=2.*dy[j];
  double viscous_term =
    1./Reynolds*(Derivative(viscous_terms[1],
                            viscous_terms[0],
                            dy_total,1));

  return viscous_term;
}
示例#18
0
void
Albany::ModelEvaluator::evalModel(const InArgs& inArgs,
                                 const OutArgs& outArgs) const
{
  Teuchos::TimeMonitor Timer(*timer); //start timer
  //
  // Get the input arguments
  //
  Teuchos::RCP<const Epetra_Vector> x = inArgs.get_x();
  Teuchos::RCP<const Epetra_Vector> x_dot;
  Teuchos::RCP<const Epetra_Vector> x_dotdot;
  double alpha     = 0.0;
  double omega     = 0.0;
  double beta      = 1.0;
  double curr_time = 0.0;
  x_dot = inArgs.get_x_dot();
  x_dotdot = inArgs.get_x_dotdot();
  if (x_dot != Teuchos::null || x_dotdot != Teuchos::null) {
    alpha = inArgs.get_alpha();
    omega = inArgs.get_omega();
    beta = inArgs.get_beta();
    curr_time  = inArgs.get_t();
  }
  for (int i=0; i<num_param_vecs; i++) {
    Teuchos::RCP<const Epetra_Vector> p = inArgs.get_p(i);
    if (p != Teuchos::null) {
      for (unsigned int j=0; j<sacado_param_vec[i].size(); j++)
        sacado_param_vec[i][j].baseValue = (*p)[j];
    }
  }
  for (int i=0; i<num_dist_param_vecs; i++) {
    Teuchos::RCP<const Epetra_Vector> p = inArgs.get_p(i+num_param_vecs);
    if (p != Teuchos::null) {
      *(distParamLib->get(dist_param_names[i])->vector()) = *p;
    }
  }

  //
  // Get the output arguments
  //
  EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector> f_out = outArgs.get_f();
  Teuchos::RCP<Epetra_Operator> W_out = outArgs.get_W();

  // Cast W to a CrsMatrix, throw an exception if this fails
  Teuchos::RCP<Epetra_CrsMatrix> W_out_crs;

  if (W_out != Teuchos::null)
    W_out_crs = Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_out, true);

int test_var = 0;
if(test_var != 0){
std::cout << "The current solution length is: " << x->MyLength() << std::endl;
x->Print(std::cout);

}

  // Get preconditioner operator, if requested
  Teuchos::RCP<Epetra_Operator> WPrec_out;
  if (outArgs.supports(OUT_ARG_WPrec)) WPrec_out = outArgs.get_WPrec();

  //
  // Compute the functions
  //
  bool f_already_computed = false;

  // W matrix
  if (W_out != Teuchos::null) {
    app->computeGlobalJacobian(alpha, beta, omega, curr_time, x_dot.get(), x_dotdot.get(),*x,
                               sacado_param_vec, f_out.get(), *W_out_crs);
    f_already_computed=true;
if(test_var != 0){
//std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
//f_out->Print(std::cout);
std::cout << "The current Jacobian length is: " << W_out_crs->NumGlobalRows() << std::endl;
W_out_crs->Print(std::cout);
}
  }

  if (WPrec_out != Teuchos::null) {
    app->computeGlobalJacobian(alpha, beta, omega, curr_time, x_dot.get(), x_dotdot.get(), *x,
                               sacado_param_vec, f_out.get(), *Extra_W_crs);
    f_already_computed=true;
if(test_var != 0){
//std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
//f_out->Print(std::cout);
std::cout << "The current preconditioner length is: " << Extra_W_crs->NumGlobalRows() << std::endl;
Extra_W_crs->Print(std::cout);
}

    app->computeGlobalPreconditioner(Extra_W_crs, WPrec_out);
  }

  // scalar df/dp
  for (int i=0; i<num_param_vecs; i++) {
    Teuchos::RCP<Epetra_MultiVector> dfdp_out =
      outArgs.get_DfDp(i).getMultiVector();
    if (dfdp_out != Teuchos::null) {
      Teuchos::Array<int> p_indexes =
        outArgs.get_DfDp(i).getDerivativeMultiVector().getParamIndexes();
      Teuchos::RCP<ParamVec> p_vec;
      if (p_indexes.size() == 0)
        p_vec = Teuchos::rcp(&sacado_param_vec[i],false);
      else {
        p_vec = Teuchos::rcp(new ParamVec);
        for (int j=0; j<p_indexes.size(); j++)
          p_vec->addParam(sacado_param_vec[i][p_indexes[j]].family,
                          sacado_param_vec[i][p_indexes[j]].baseValue);
      }

      app->computeGlobalTangent(0.0, 0.0, 0.0, curr_time, false, x_dot.get(), x_dotdot.get(), *x,
                                sacado_param_vec, p_vec.get(),
                                NULL, NULL, NULL, NULL, f_out.get(), NULL,
                                dfdp_out.get());

      f_already_computed=true;
if(test_var != 0){
std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
f_out->Print(std::cout);
}
    }
  }

  // distributed df/dp
  for (int i=0; i<num_dist_param_vecs; i++) {
    Teuchos::RCP<Epetra_Operator> dfdp_out =
      outArgs.get_DfDp(i+num_param_vecs).getLinearOp();
    if (dfdp_out != Teuchos::null) {
      Teuchos::RCP<DistributedParameterDerivativeOp> dfdp_op =
        Teuchos::rcp_dynamic_cast<DistributedParameterDerivativeOp>(dfdp_out);
      dfdp_op->set(curr_time, x_dot, x_dotdot, x,
                   Teuchos::rcp(&sacado_param_vec,false));
    }
  }

  // f
  if (app->is_adjoint) {
    Derivative f_deriv(f_out, DERIV_TRANS_MV_BY_ROW);
    int response_index = 0; // need to add capability for sending this in
    app->evaluateResponseDerivative(response_index, curr_time, x_dot.get(), x_dotdot.get(), *x,
                                    sacado_param_vec, NULL,
                                    NULL, f_deriv, Derivative(), Derivative(), Derivative());
  }
  else {
    if (f_out != Teuchos::null && !f_already_computed) {
      app->computeGlobalResidual(curr_time, x_dot.get(), x_dotdot.get(), *x,
                                 sacado_param_vec, *f_out);
if(test_var != 0){
std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
f_out->Print(std::cout);
}
    }
  }

  // Response functions
  for (int i=0; i<outArgs.Ng(); i++) {
    Teuchos::RCP<Epetra_Vector> g_out = outArgs.get_g(i);
    bool g_computed = false;

    Derivative dgdx_out = outArgs.get_DgDx(i);
    Derivative dgdxdot_out = outArgs.get_DgDx_dot(i);
    Derivative dgdxdotdot_out = outArgs.get_DgDx_dotdot(i);

    // dg/dx, dg/dxdot
    if (!dgdx_out.isEmpty() || !dgdxdot_out.isEmpty() || !dgdxdotdot_out.isEmpty() ) {
      app->evaluateResponseDerivative(i, curr_time, x_dot.get(), x_dotdot.get(), *x,
                                      sacado_param_vec, NULL,
                                      g_out.get(), dgdx_out,
                                      dgdxdot_out, dgdxdotdot_out, Derivative());
      g_computed = true;
    }

    // dg/dp
    for (int j=0; j<num_param_vecs; j++) {
      Teuchos::RCP<Epetra_MultiVector> dgdp_out =
        outArgs.get_DgDp(i,j).getMultiVector();
      if (dgdp_out != Teuchos::null) {
        Teuchos::Array<int> p_indexes =
          outArgs.get_DgDp(i,j).getDerivativeMultiVector().getParamIndexes();
        Teuchos::RCP<ParamVec> p_vec;
        if (p_indexes.size() == 0)
          p_vec = Teuchos::rcp(&sacado_param_vec[j],false);
        else {
          p_vec = Teuchos::rcp(new ParamVec);
          for (int k=0; k<p_indexes.size(); k++)
            p_vec->addParam(sacado_param_vec[j][p_indexes[k]].family,
                            sacado_param_vec[j][p_indexes[k]].baseValue);
        }
        app->evaluateResponseTangent(i, alpha, beta, omega, curr_time, false,
                                     x_dot.get(), x_dotdot.get(), *x,
                                     sacado_param_vec, p_vec.get(),
                                     NULL, NULL, NULL, NULL, g_out.get(), NULL,
                                     dgdp_out.get());
        g_computed = true;
      }
    }

    // Need to handle dg/dp for distributed p

    if (g_out != Teuchos::null && !g_computed)
      app->evaluateResponse(i, curr_time, x_dot.get(), x_dotdot.get(), *x, sacado_param_vec,
                            *g_out);
  }

  //
  // Stochastic Galerkin
  //
#ifdef ALBANY_SG_MP
  InArgs::sg_const_vector_t x_sg = inArgs.get_x_sg();
  if (x_sg != Teuchos::null) {
    app->init_sg(inArgs.get_sg_basis(),
                 inArgs.get_sg_quadrature(),
                 inArgs.get_sg_expansion(),
                 x_sg->productComm());
    InArgs::sg_const_vector_t x_dot_sg  = inArgs.get_x_dot_sg();
    InArgs::sg_const_vector_t x_dotdot_sg  = inArgs.get_x_dotdot_sg();
    if (x_dot_sg != Teuchos::null || x_dotdot_sg != Teuchos::null) {
      alpha = inArgs.get_alpha();
      omega = inArgs.get_omega();
      beta = inArgs.get_beta();
      curr_time  = inArgs.get_t();
    }

    InArgs::sg_const_vector_t epetra_p_sg = inArgs.get_p_sg(0);
    Teuchos::Array<int> p_sg_index;
    for (int i=0; i<num_param_vecs; i++) {
      InArgs::sg_const_vector_t p_sg = inArgs.get_p_sg(i);
      if (p_sg != Teuchos::null) {
        p_sg_index.push_back(i);
        for (int j=0; j<p_sg_vals[i].size(); j++) {
          int num_sg_blocks = p_sg->size();
          p_sg_vals[i][j].reset(app->getStochasticExpansion(), num_sg_blocks);
          p_sg_vals[i][j].copyForWrite();
          for (int l=0; l<num_sg_blocks; l++) {
            p_sg_vals[i][j].fastAccessCoeff(l) = (*p_sg)[l][j];
          }
        }
      }
    }

    OutArgs::sg_vector_t f_sg = outArgs.get_f_sg();
    OutArgs::sg_operator_t W_sg = outArgs.get_W_sg();
    bool f_sg_computed = false;

    // W_sg
    if (W_sg != Teuchos::null) {
      Stokhos::VectorOrthogPoly<Epetra_CrsMatrix> W_sg_crs(W_sg->basis(),
                                                           W_sg->map());
      for (int i=0; i<W_sg->size(); i++)
        W_sg_crs.setCoeffPtr(
          i,
          Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_sg->getCoeffPtr(i)));
      app->computeGlobalSGJacobian(alpha, beta, omega, curr_time,
                                   x_dot_sg.get(),  x_dotdot_sg.get(), *x_sg,
                                   sacado_param_vec, p_sg_index, p_sg_vals,
                                   f_sg.get(), W_sg_crs);
      f_sg_computed = true;
    }

    // df/dp_sg
    for (int i=0; i<num_param_vecs; i++) {
      Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > dfdp_sg
        = outArgs.get_DfDp_sg(i).getMultiVector();
      if (dfdp_sg != Teuchos::null) {
        Teuchos::Array<int> p_indexes =
          outArgs.get_DfDp_sg(i).getDerivativeMultiVector().getParamIndexes();
        Teuchos::RCP<ParamVec> p_vec;
        if (p_indexes.size() == 0)
          p_vec = Teuchos::rcp(&sacado_param_vec[i],false);
        else {
          p_vec = Teuchos::rcp(new ParamVec);
          for (int j=0; j<p_indexes.size(); j++)
            p_vec->addParam(sacado_param_vec[i][p_indexes[j]].family,
                            sacado_param_vec[i][p_indexes[j]].baseValue);
        }

        app->computeGlobalSGTangent(0.0, 0.0, 0.0, curr_time, false,
                                    x_dot_sg.get(), x_dotdot_sg.get(),*x_sg,
                                    sacado_param_vec, p_sg_index, p_sg_vals,
                                    p_vec.get(), NULL, NULL, NULL, NULL,
                                    f_sg.get(), NULL, dfdp_sg.get());

        f_sg_computed = true;
      }
    }

    if (f_sg != Teuchos::null && !f_sg_computed)
      app->computeGlobalSGResidual(curr_time, x_dot_sg.get(), x_dotdot_sg.get(),*x_sg,
                                   sacado_param_vec, p_sg_index, p_sg_vals,
                                   *f_sg);

    // Response functions
    for (int i=0; i<outArgs.Ng(); i++) {
      OutArgs::sg_vector_t g_sg = outArgs.get_g_sg(i);
      bool g_sg_computed = false;

      SGDerivative dgdx_sg = outArgs.get_DgDx_sg(i);
      SGDerivative dgdxdot_sg = outArgs.get_DgDx_dot_sg(i);
      SGDerivative dgdxdotdot_sg = outArgs.get_DgDx_dotdot_sg(i);

      // dg/dx, dg/dxdot
      if (!dgdx_sg.isEmpty() || !dgdxdot_sg.isEmpty() || !dgdxdotdot_sg.isEmpty()) {
        app->evaluateSGResponseDerivative(
            i, curr_time, x_dot_sg.get(), x_dotdot_sg.get(), *x_sg,
            sacado_param_vec, p_sg_index, p_sg_vals,
            NULL, g_sg.get(), dgdx_sg,
            dgdxdot_sg, dgdxdotdot_sg, SGDerivative());
        g_sg_computed = true;
      }

      // dg/dp
      for (int j=0; j<num_param_vecs; j++) {
        Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > dgdp_sg =
          outArgs.get_DgDp_sg(i,j).getMultiVector();
        if (dgdp_sg != Teuchos::null) {
          Teuchos::Array<int> p_indexes =
            outArgs.get_DgDp_sg(i,j).getDerivativeMultiVector().getParamIndexes();
          Teuchos::RCP<ParamVec> p_vec;
          if (p_indexes.size() == 0)
            p_vec = Teuchos::rcp(&sacado_param_vec[j],false);
          else {
            p_vec = Teuchos::rcp(new ParamVec);
            for (int k=0; k<p_indexes.size(); k++)
              p_vec->addParam(sacado_param_vec[j][p_indexes[k]].family,
                              sacado_param_vec[j][p_indexes[k]].baseValue);
          }
          app->evaluateSGResponseTangent(i, alpha, beta, omega, curr_time, false,
                                         x_dot_sg.get(), x_dotdot_sg.get(), *x_sg,
                                         sacado_param_vec, p_sg_index,
                                         p_sg_vals, p_vec.get(),
                                         NULL, NULL, NULL, NULL, g_sg.get(),
                                         NULL, dgdp_sg.get());
          g_sg_computed = true;

        }
      }

      if (g_sg != Teuchos::null && !g_sg_computed)
        app->evaluateSGResponse(i, curr_time, x_dot_sg.get(), x_dotdot_sg.get(), *x_sg,
                                sacado_param_vec, p_sg_index, p_sg_vals,
                                *g_sg);
    }
  }

  //
  // Multi-point evaluation
  //
  mp_const_vector_t x_mp = inArgs.get_x_mp();
  if (x_mp != Teuchos::null) {
    mp_const_vector_t x_dot_mp  = inArgs.get_x_dot_mp();
    mp_const_vector_t x_dotdot_mp  = inArgs.get_x_dotdot_mp();
    if (x_dot_mp != Teuchos::null || x_dotdot_mp != Teuchos::null) {
      alpha = inArgs.get_alpha();
      omega = inArgs.get_omega();
      beta = inArgs.get_beta();
      curr_time  = inArgs.get_t();
    }

    Teuchos::Array<int> p_mp_index;
    for (int i=0; i<num_param_vecs; i++) {
      mp_const_vector_t p_mp = inArgs.get_p_mp(i);
      if (p_mp != Teuchos::null) {
        p_mp_index.push_back(i);
        for (int j=0; j<p_mp_vals[i].size(); j++) {
          int num_mp_blocks = p_mp->size();
          p_mp_vals[i][j].reset(num_mp_blocks);
          p_mp_vals[i][j].copyForWrite();
          for (int l=0; l<num_mp_blocks; l++) {
            p_mp_vals[i][j].fastAccessCoeff(l) = (*p_mp)[l][j];
          }
        }
      }
    }

    mp_vector_t f_mp = outArgs.get_f_mp();
    mp_operator_t W_mp = outArgs.get_W_mp();
    bool f_mp_computed = false;

    // W_mp
    if (W_mp != Teuchos::null) {
      Stokhos::ProductContainer<Epetra_CrsMatrix> W_mp_crs(W_mp->map());
      for (int i=0; i<W_mp->size(); i++)
        W_mp_crs.setCoeffPtr(
          i,
          Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_mp->getCoeffPtr(i)));
      app->computeGlobalMPJacobian(alpha, beta, omega, curr_time,
                                   x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                   sacado_param_vec, p_mp_index, p_mp_vals,
                                   f_mp.get(), W_mp_crs);
      f_mp_computed = true;
    }

    // df/dp_mp
    for (int i=0; i<num_param_vecs; i++) {
      Teuchos::RCP< Stokhos::ProductEpetraMultiVector > dfdp_mp
        = outArgs.get_DfDp_mp(i).getMultiVector();
      if (dfdp_mp != Teuchos::null) {
        Teuchos::Array<int> p_indexes =
          outArgs.get_DfDp_mp(i).getDerivativeMultiVector().getParamIndexes();
        Teuchos::RCP<ParamVec> p_vec;
        if (p_indexes.size() == 0)
          p_vec = Teuchos::rcp(&sacado_param_vec[i],false);
        else {
          p_vec = Teuchos::rcp(new ParamVec);
          for (int j=0; j<p_indexes.size(); j++)
            p_vec->addParam(sacado_param_vec[i][p_indexes[j]].family,
                            sacado_param_vec[i][p_indexes[j]].baseValue);
        }

        app->computeGlobalMPTangent(0.0, 0.0, 0.0, curr_time, false,
                                    x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                    sacado_param_vec, p_mp_index, p_mp_vals,
                                    p_vec.get(), NULL, NULL, NULL, NULL,
                                    f_mp.get(), NULL, dfdp_mp.get());

        f_mp_computed = true;
      }
    }

    if (f_mp != Teuchos::null && !f_mp_computed)
      app->computeGlobalMPResidual(curr_time, x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                   sacado_param_vec, p_mp_index, p_mp_vals,
                                   *f_mp);

    // Response functions
    for (int i=0; i<outArgs.Ng(); i++) {
      mp_vector_t g_mp = outArgs.get_g_mp(i);
      bool g_mp_computed = false;

      MPDerivative dgdx_mp = outArgs.get_DgDx_mp(i);
      MPDerivative dgdxdot_mp = outArgs.get_DgDx_dot_mp(i);
      MPDerivative dgdxdotdot_mp = outArgs.get_DgDx_dotdot_mp(i);

      // dg/dx, dg/dxdot
      if (!dgdx_mp.isEmpty() || !dgdxdot_mp.isEmpty() || !dgdxdotdot_mp.isEmpty() ) {
        app->evaluateMPResponseDerivative(
            i, curr_time, x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
            sacado_param_vec, p_mp_index, p_mp_vals,
            NULL, g_mp.get(), dgdx_mp,
            dgdxdot_mp, dgdxdotdot_mp, MPDerivative());
        g_mp_computed = true;
      }

      // dg/dp
      for (int j=0; j<num_param_vecs; j++) {
        Teuchos::RCP< Stokhos::ProductEpetraMultiVector > dgdp_mp =
          outArgs.get_DgDp_mp(i,j).getMultiVector();
        if (dgdp_mp != Teuchos::null) {
          Teuchos::Array<int> p_indexes =
            outArgs.get_DgDp_mp(i,j).getDerivativeMultiVector().getParamIndexes();
          Teuchos::RCP<ParamVec> p_vec;
          if (p_indexes.size() == 0)
            p_vec = Teuchos::rcp(&sacado_param_vec[j],false);
          else {
            p_vec = Teuchos::rcp(new ParamVec);
            for (int k=0; k<p_indexes.size(); k++)
              p_vec->addParam(sacado_param_vec[j][p_indexes[k]].family,
                              sacado_param_vec[j][p_indexes[k]].baseValue);
          }
          app->evaluateMPResponseTangent(i, alpha, beta, omega, curr_time, false,
                                         x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                         sacado_param_vec, p_mp_index,
                                         p_mp_vals, p_vec.get(),
                                         NULL, NULL, NULL, NULL, g_mp.get(),
                                         NULL, dgdp_mp.get());
          g_mp_computed = true;
        }
      }

      if (g_mp != Teuchos::null && !g_mp_computed)
        app->evaluateMPResponse(i, curr_time, x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                sacado_param_vec, p_mp_index, p_mp_vals,
                                *g_mp);
    }
  }
#endif //ALBANY_SG_MP
}
示例#19
0
JNIEXPORT jdouble JNICALL Java_com_starlab_component_processor_jni_JNIderivative_nativeDerivative
  (JNIEnv *env, jobject obj, jint id, jdouble sample)
{
	return Derivative(id, sample);
}
double Viscous_Component_XY_2ND_Order(double*** velocity_x,
                                      double*** velocity_y,
				      double*** temperature, 
                                      double Reynolds,
                                      double dx, double* dy,
                                      int i, int j, int k)
{

  double derivative_y[2], dy_total=0.;

  //Calculation of the du/dy component
  for (int vi=0; vi<2; vi++)
    {
      dy_total=dy[j+vi]+dy[j+vi-1];
      derivative_y[vi] = Derivative(velocity_x[k][j+vi][i],
				    velocity_x[k][j+vi-1][i],
				    dy_total, 1);
    }


  // Calculation of the dv/dx component
  // interpolating the velocities at i\pm 1/2 (j-1,j,j+1)
  double interpolation_x[3][2];
  for(int vi=0; vi<2; vi++){
    for(int vj=0; vj<3; vj++){
  
      interpolation_x[vj][vi] = 
	Interpolation(velocity_y[k][j+vj-1][i+vi], 
		      velocity_y[k][j+vj-1][i+vi-1]); 
					
    }
  }

  //computing the derivative dv/dx at j-1,j,j+1
  double derivative_x[3];
  for(int vi=0; vi<3; vi++){
      
    derivative_x[vi]= Derivative(interpolation_x[vi][1],
				 interpolation_x[vi][0],
				 dx, 1);
  }

  //interpolation in the x direction
  double final_derivative_x[2];
  for(int vi=0, vj=1; vi<2; vi++, vj++){
    
    final_derivative_x[vi]=  Interpolation(derivative_x[vj],
					   derivative_x[vj-1]);
  }
  


  //Computing the viscosities.
  double viscosity[2];
  for (int vi=0; vi<2; vi++)
    {
      viscosity[vi]=
        Viscosity_Calculator(Interpolation(temperature[k][j+vi][i],
                                           temperature[k][j+vi-1][i]));
    }

  //Summing the X-component of the Viscous Term of the X-Momentum
  //equation
  double viscous_terms[2];
  for (int vi=0; vi<2; vi++)
    {
      viscous_terms[vi] =viscosity[vi]*(derivative_y[vi]                   
					+final_derivative_x[vi]);
    }

  dy_total=2.*dy[j];
  double result =
    1./Reynolds*(1.*Derivative(viscous_terms[1],
			       viscous_terms[0],
			       dy_total,1));
  
  return result;
}
double Energy_Convection(double*** temperature,
                         double*** velocity_x, double*** velocity_y,
                         double*** velocity_z,
                         double dx, double* dy, double dz,
                         int i, int j, int k)
{

  double convective_terms[3];
  double interpolated[4], derivative[2],total_interpolated[2];


  //X-Direction
  for (int vi=0; vi<2; vi++)
    {
      interpolated[vi]=Interpolation(velocity_x[k][j][i+vi],
                                     velocity_x[k][j][i+vi-1]);

      interpolated[vi+2]=Interpolation(velocity_x[k][j][i+2*vi],
                                       velocity_x[k][j][i+2*vi-2]);
    }

  total_interpolated[0]=interpolated[0]+interpolated[1];
  total_interpolated[1]=interpolated[2]+interpolated[3];


  derivative[0]=4./3.*Derivative(temperature[k][j][i+1],
                                 temperature[k][j][i-1],
                                 dx, 2);

  derivative[1]=-1./3.*Derivative(temperature[k][j][i+2],
                                  temperature[k][j][i-2],
                                  dx, 4);

  convective_terms[0]=0.;
  for(int vi=0; vi<2; vi++)
    convective_terms[0]+=derivative[vi]*total_interpolated[vi];



  //Y-direction. This quantity I computed it by just implementing vdT/dy. At the
  // paper it says that the v has also to be interpolated so this source can
  // cause errors

  double dy_total=dy[j+1]+2.*dy[j]+dy[j-1];
  derivative[0]=Derivative(velocity_y[k][j+1][i],
                           velocity_y[k][j-1][i],
                           dy_total,1);

  convective_terms[1] =velocity_y[k][j][i]*derivative[0];


  //Z-Component
  for (int vi=0; vi<2; vi++)
    {
      interpolated[vi]=Interpolation( velocity_z[k+vi][j][i],
                                      velocity_z[k+vi-1][j][i]);

      interpolated[vi+2]=Interpolation(velocity_z[k+2*vi][j][i],
                                       velocity_x[k+2*vi-2][j][i]);

    }

  total_interpolated[0]=interpolated[0]+interpolated[1];
  total_interpolated[1]=interpolated[2]+interpolated[3];


  derivative[0]=4./3.*Derivative(temperature[k+1][j][i],
                                 temperature[k-1][j][i],
                                 dz, 2);

  derivative[1]=-1./3.*Derivative(temperature[k+2][j][i],
                                  temperature[k-2][j][i],
                                  dz, 4);

  convective_terms[2]=0.;
  for(int vi=0; vi<2; vi++)
    convective_terms[2]+=derivative[vi]*total_interpolated[vi];


  double convection=0.;
  for (int vi=0; vi<3; vi++)
    convection+=convective_terms[vi];

  return convection;
}
LensDistortionParameters::Derivative LensDistortionParameters::transformp(const LensDistortionParameters::Point& source) const
{
    /* Not implemented because not needed */
    return Derivative(0);
}
double Viscous_Component_YZ(double*** velocity_y, double*** velocity_z,
                            double*** temperature, double Reynolds,
                            double* dy, double dz,
                            int i, int j, int k)
{

  double derivative_zy[4][2], derivative_zz[4][2],
    viscous_terms[4], viscosity[4], dy_total;

  //Calculation of the d/dz(dv/dz) component
  double total_derivative_z[4];
  for (int vi=0; vi<4; vi++)
    {
      derivative_zz[vi][0]= 9./8.*Derivative(velocity_y[k+vi-1][j][i],
                                             velocity_y[k+vi-2][j][i],
                                             dz,1);

      derivative_zz[vi][1]= -1./8.*Derivative(velocity_y[k+vi][j][i],
                                              velocity_y[k+vi-3][j][i],
                                              dz,3);

      //computing the total derivative
      total_derivative_z[vi]=0.;
      for (int vj=0; vj<2; vj++)
        total_derivative_z[vi]+=derivative_zz[vi][vj];

    }

  //Calculation of the d/dz(dw/dy) component

  //k-3/2
  //k-3
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_zy[0][0] = Derivative(velocity_z[k-3][j+1][i],
                                   velocity_z[k-3][j-1][i],
                                   dy_total,1);

  //k
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_zy[0][1] = Derivative(velocity_z[k][j+1][i],
                                   velocity_z[k][j-1][i],
                                   dy_total,1);

  //k-1/2

  //k-1
  //k
  dy_total=dy[j-1] +2.*dy[j] +dy[j+1];
  derivative_zy[1][0] = Derivative(velocity_z[k-1][j+1][i],
                                   velocity_z[k-1][j-1][i],
                                   dy_total,1);

  //k
  derivative_zy[1][1] = derivative_zy[0][1];


  //k+1/2

  //k
  derivative_zy[2][0] = derivative_zy[0][1];


  //k+1
  dy_total=dy[j-1] +2.*dy[j] +dy[j+1];
  derivative_zy[2][1] = Derivative(velocity_z[k+1][j+1][i],
                                   velocity_z[k+1][j-1][i],
                                   dy_total,1);

  // k+3/2
  //k
  derivative_zy[3][0] = derivative_zy[0][1];
  //k+3
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_zy[3][1] = Derivative(velocity_z[k+3][j+1][i],
                                   velocity_z[k+3][j-1][i],
                                   dy_total,1);

  //computing the total derivative in the y-direction
  double total_derivative_y[4];
  for(int vi=0; vi<4; vi++)
    {
      total_derivative_y[vi]=Interpolation(derivative_zy[vi][0],
                                           derivative_zy[vi][1]);
    }


  //Computing the viscosities.
  for (int vi=-1, vj=0; vj<4; vi++, vj++)
    {
      viscosity[vj]=
        Viscosity_Calculator(Interpolation(temperature[k+vi][j][i],
                                           temperature[k+vi+1][j][i]));
    }



  //computing the viscous tensors
  for (int vi=0; vi<4; vi++)
    {
      viscous_terms[vi] =viscosity[vi]*(total_derivative_y[vi]+
                                        total_derivative_z[vi]);
    }



  // cout<<"yz"<<endl;
  // cout<<-1./24.*(total_derivative_y[3]-total_derivative_y[0])
  //   +9./8*(total_derivative_y[2]-total_derivative_y[1])<<endl;


  double viscous_component =
    1./Reynolds*(9./8.*Derivative(viscous_terms[2],
                                  viscous_terms[1],
                                  dz,1)-
                 1./8.*Derivative(viscous_terms[3],
                                  viscous_terms[0],
                                  dz,3));


  return viscous_component;

}
void
Albany::ModelEvaluator::evalModel(const InArgs& inArgs,
                                 const OutArgs& outArgs) const
{
  Teuchos::TimeMonitor Timer(*timer); //start timer
  //
  // Get the input arguments
  //
  Teuchos::RCP<const Epetra_Vector> x = inArgs.get_x();
  Teuchos::RCP<const Epetra_Vector> x_dot;
  Teuchos::RCP<const Epetra_Vector> x_dotdot;

  //create comm and node objects for Epetra -> Tpetra conversions
  Teuchos::RCP<const Teuchos::Comm<int> > commT = app->getComm();
  Teuchos::RCP<Epetra_Comm> comm = Albany::createEpetraCommFromTeuchosComm(commT);
  //Create Tpetra copy of x, call it xT
  Teuchos::RCP<const Tpetra_Vector> xT;
  if (x != Teuchos::null)
    xT  = Petra::EpetraVector_To_TpetraVectorConst(*x, commT);

  double alpha     = 0.0;
  double omega     = 0.0;
  double beta      = 1.0;
  double curr_time = 0.0;

  if(num_time_deriv > 0)
    x_dot = inArgs.get_x_dot();
  if(num_time_deriv > 1)
    x_dotdot = inArgs.get_x_dotdot();

  //Declare and create Tpetra copy of x_dot, call it x_dotT
  Teuchos::RCP<const Tpetra_Vector> x_dotT;
  if (Teuchos::nonnull(x_dot))
    x_dotT = Petra::EpetraVector_To_TpetraVectorConst(*x_dot, commT);

  //Declare and create Tpetra copy of x_dotdot, call it x_dotdotT
  Teuchos::RCP<const Tpetra_Vector> x_dotdotT;
  if (Teuchos::nonnull(x_dotdot))
    x_dotdotT = Petra::EpetraVector_To_TpetraVectorConst(*x_dotdot, commT);

  if (Teuchos::nonnull(x_dot)){
    alpha = inArgs.get_alpha();
    beta = inArgs.get_beta();
    curr_time  = inArgs.get_t();
  }
  if (Teuchos::nonnull(x_dotdot)) {
    omega = inArgs.get_omega();
  }

  for (int i=0; i<num_param_vecs; i++) {
    Teuchos::RCP<const Epetra_Vector> p = inArgs.get_p(i);
    if (p != Teuchos::null) {
      for (unsigned int j=0; j<sacado_param_vec[i].size(); j++) {
        sacado_param_vec[i][j].baseValue = (*p)[j];
      }
    }
  }
  for (int i=0; i<num_dist_param_vecs; i++) {
    Teuchos::RCP<const Epetra_Vector> p = inArgs.get_p(i+num_param_vecs);
    //create Tpetra copy of p
    Teuchos::RCP<const Tpetra_Vector> pT;
    if (p != Teuchos::null) {
      pT = Petra::EpetraVector_To_TpetraVectorConst(*p, commT);
      //*(distParamLib->get(dist_param_names[i])->vector()) = *p;
      *(distParamLib->get(dist_param_names[i])->vector()) = *pT;
    }
  }

  //
  // Get the output arguments
  //
  EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector> f_out = outArgs.get_f();
  Teuchos::RCP<Epetra_Operator> W_out = outArgs.get_W();

  // Cast W to a CrsMatrix, throw an exception if this fails
  Teuchos::RCP<Epetra_CrsMatrix> W_out_crs;
#ifdef WRITE_MASS_MATRIX_TO_MM_FILE
  //IK, 7/15/14: adding object to hold mass matrix to be written to matrix market file
  Teuchos::RCP<Epetra_CrsMatrix> Mass;
  //IK, 7/15/14: needed for writing mass matrix out to matrix market file
  EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector> ftmp = outArgs.get_f();
#endif

  if (W_out != Teuchos::null) {
    W_out_crs = Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_out, true);
#ifdef WRITE_MASS_MATRIX_TO_MM_FILE
    //IK, 7/15/14: adding object to hold mass matrix to be written to matrix market file
    Mass = Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_out, true);
#endif
  }


int test_var = 0;
if(test_var != 0){
std::cout << "The current solution length is: " << x->MyLength() << std::endl;
x->Print(std::cout);

}

  // Get preconditioner operator, if requested
  Teuchos::RCP<Epetra_Operator> WPrec_out;
  if (outArgs.supports(OUT_ARG_WPrec)) WPrec_out = outArgs.get_WPrec();

  //
  // Compute the functions
  //
  bool f_already_computed = false;

  // W matrix
  if (W_out != Teuchos::null) {
    app->computeGlobalJacobian(alpha, beta, omega, curr_time, x_dot.get(), x_dotdot.get(),*x,
                               sacado_param_vec, f_out.get(), *W_out_crs);
#ifdef WRITE_MASS_MATRIX_TO_MM_FILE
    //IK, 7/15/14: write mass matrix to matrix market file
    //Warning: to read this in to MATLAB correctly, code must be run in serial.
    //Otherwise Mass will have a distributed Map which would also need to be read in to MATLAB for proper
    //reading in of Mass.
    app->computeGlobalJacobian(1.0, 0.0, 0.0, curr_time, x_dot.get(), x_dotdot.get(), *x,
                               sacado_param_vec, ftmp.get(), *Mass);
    EpetraExt::RowMatrixToMatrixMarketFile("mass.mm", *Mass);
    EpetraExt::BlockMapToMatrixMarketFile("rowmap.mm", Mass->RowMap());
    EpetraExt::BlockMapToMatrixMarketFile("colmap.mm", Mass->ColMap());
    Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
#endif
    f_already_computed=true;
if(test_var != 0){
//std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
//f_out->Print(std::cout);
std::cout << "The current Jacobian length is: " << W_out_crs->NumGlobalRows() << std::endl;
W_out_crs->Print(std::cout);
}
  }

  if (WPrec_out != Teuchos::null) {
    app->computeGlobalJacobian(alpha, beta, omega, curr_time, x_dot.get(), x_dotdot.get(), *x,
                               sacado_param_vec, f_out.get(), *Extra_W_crs);
    f_already_computed=true;
if(test_var != 0){
//std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
//f_out->Print(std::cout);
std::cout << "The current preconditioner length is: " << Extra_W_crs->NumGlobalRows() << std::endl;
Extra_W_crs->Print(std::cout);
}

    app->computeGlobalPreconditioner(Extra_W_crs, WPrec_out);
  }

  // scalar df/dp
  for (int i=0; i<num_param_vecs; i++) {
    Teuchos::RCP<Epetra_MultiVector> dfdp_out =
      outArgs.get_DfDp(i).getMultiVector();
    if (dfdp_out != Teuchos::null) {
      Teuchos::Array<int> p_indexes =
        outArgs.get_DfDp(i).getDerivativeMultiVector().getParamIndexes();
      Teuchos::RCP<ParamVec> p_vec;
      if (p_indexes.size() == 0)
        p_vec = Teuchos::rcp(&sacado_param_vec[i],false);
      else {
        p_vec = Teuchos::rcp(new ParamVec);
        for (int j=0; j<p_indexes.size(); j++)
          p_vec->addParam(sacado_param_vec[i][p_indexes[j]].family,
                          sacado_param_vec[i][p_indexes[j]].baseValue);
      }

      app->computeGlobalTangent(0.0, 0.0, 0.0, curr_time, false, x_dot.get(), x_dotdot.get(), *x,
                                sacado_param_vec, p_vec.get(),
                                NULL, NULL, NULL, NULL, f_out.get(), NULL,
                                dfdp_out.get());

      f_already_computed=true;
if(test_var != 0){
std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
f_out->Print(std::cout);
}
    }
  }

  // distributed df/dp
  for (int i=0; i<num_dist_param_vecs; i++) {
    Teuchos::RCP<Epetra_Operator> dfdp_out =
      outArgs.get_DfDp(i+num_param_vecs).getLinearOp();
    if (dfdp_out != Teuchos::null) {
      Teuchos::RCP<DistributedParameterDerivativeOp> dfdp_op =
        Teuchos::rcp_dynamic_cast<DistributedParameterDerivativeOp>(dfdp_out);
      dfdp_op->set(curr_time, x_dotT, x_dotdotT, xT,
                   Teuchos::rcp(&sacado_param_vec,false));
    }
  }

  // f
  if (app->is_adjoint) {
    Derivative f_deriv(f_out, DERIV_TRANS_MV_BY_ROW);
    int response_index = 0; // need to add capability for sending this in
    app->evaluateResponseDerivative(response_index, curr_time, x_dot.get(), x_dotdot.get(), *x,
                                    sacado_param_vec, NULL,
                                    NULL, f_deriv, Derivative(), Derivative(), Derivative());
  }
  else {
    if (f_out != Teuchos::null && !f_already_computed) {
      app->computeGlobalResidual(curr_time, x_dot.get(), x_dotdot.get(), *x,
                                  sacado_param_vec, *f_out);
if(test_var != 0){
std::cout << "The current rhs length is: " << f_out->MyLength() << std::endl;
f_out->Print(std::cout);
}
    }
  }


  // Response functions
  for (int i=0; i<outArgs.Ng(); i++) {
    //Set curr_time to final time at which response occurs.
    if(num_time_deriv > 0)
      curr_time  = inArgs.get_t();
    Teuchos::RCP<Epetra_Vector> g_out = outArgs.get_g(i);
    //Declare Tpetra_Vector copy of g_out
    Teuchos::RCP<Tpetra_Vector> g_outT;
    bool g_computed = false;

    Derivative dgdx_out = outArgs.get_DgDx(i);
    Derivative dgdxdot_out = outArgs.get_DgDx_dot(i);
    Derivative dgdxdotdot_out = outArgs.get_DgDx_dotdot(i);

    // dg/dx, dg/dxdot
    if (!dgdx_out.isEmpty() || !dgdxdot_out.isEmpty() || !dgdxdotdot_out.isEmpty() ) {
      app->evaluateResponseDerivative(i, curr_time, x_dot.get(), x_dotdot.get(), *x,
                                      sacado_param_vec, NULL,
                                      g_out.get(), dgdx_out,
                                      dgdxdot_out, dgdxdotdot_out, Derivative());
      g_computed = true;
    }

    // dg/dp
    for (int j=0; j<num_param_vecs; j++) {
      Teuchos::RCP<Epetra_MultiVector> dgdp_out =
        outArgs.get_DgDp(i,j).getMultiVector();
      //Declare Tpetra copy of dgdp_out
      Teuchos::RCP<Tpetra_MultiVector> dgdp_outT;
      if (dgdp_out != Teuchos::null) {
        Teuchos::Array<int> p_indexes =
          outArgs.get_DgDp(i,j).getDerivativeMultiVector().getParamIndexes();
        Teuchos::RCP<ParamVec> p_vec;
        if (p_indexes.size() == 0)
          p_vec = Teuchos::rcp(&sacado_param_vec[j],false);
        else {
          p_vec = Teuchos::rcp(new ParamVec);
          for (int k=0; k<p_indexes.size(); k++)
            p_vec->addParam(sacado_param_vec[j][p_indexes[k]].family,
                            sacado_param_vec[j][p_indexes[k]].baseValue);
        }
        //create Tpetra copy of g_out, call it g_outT
        if (g_out != Teuchos::null)
           g_outT = Petra::EpetraVector_To_TpetraVectorNonConst(*g_out, commT);
        //create Tpetra copy of dgdp_out, call it dgdp_outT
        if (dgdp_out != Teuchos::null)
           dgdp_outT = Petra::EpetraMultiVector_To_TpetraMultiVector(*dgdp_out, commT);
	app->evaluateResponseTangentT(i, alpha, beta, omega, curr_time, false,
				     x_dotT.get(), x_dotdotT.get(), *xT,
				     sacado_param_vec, p_vec.get(),
				     NULL, NULL, NULL, NULL, g_outT.get(), NULL,
				     dgdp_outT.get());
        //convert g_outT to Epetra_Vector g_out
        if (g_out != Teuchos::null)
          Petra::TpetraVector_To_EpetraVector(g_outT, *g_out, comm);
        //convert dgdp_outT to Epetra_MultiVector dgdp_out
        if (dgdp_out != Teuchos::null)
          Petra::TpetraMultiVector_To_EpetraMultiVector(dgdp_outT, *dgdp_out, comm);
        g_computed = true;
      }
    }

    // Need to handle dg/dp for distributed p
    for(int j=0; j<num_dist_param_vecs; j++) {
      Derivative dgdp_out = outArgs.get_DgDp(i,j+num_param_vecs);
      if (!dgdp_out.isEmpty()) {
        dgdp_out.getMultiVector()->PutScalar(0.);
        app->evaluateResponseDistParamDeriv(i, curr_time, x_dot.get(), x_dotdot.get(), *x, sacado_param_vec, dist_param_names[j], dgdp_out.getMultiVector().get());
      }
    }

    if (g_out != Teuchos::null && !g_computed) {
      //create Tpetra copy of g_out, call it g_outT
      g_outT = Petra::EpetraVector_To_TpetraVectorNonConst(*g_out, commT);
      app->evaluateResponseT(i, curr_time, x_dotT.get(), x_dotdotT.get(), *xT, sacado_param_vec,
			    *g_outT);
      //convert g_outT to Epetra_Vector g_out
      Petra::TpetraVector_To_EpetraVector(g_outT, *g_out, comm);
    }
  }

  //
  // Stochastic Galerkin
  //
#ifdef ALBANY_SG
  InArgs::sg_const_vector_t x_sg = inArgs.get_x_sg();
  if (x_sg != Teuchos::null) {
    app->init_sg(inArgs.get_sg_basis(),
                 inArgs.get_sg_quadrature(),
                 inArgs.get_sg_expansion(),
                 x_sg->productComm());
    InArgs::sg_const_vector_t x_dot_sg  = Teuchos::null;
    InArgs::sg_const_vector_t x_dot_sg  = Teuchos::null;
    if(num_time_deriv > 0)
      x_dotdot_sg  = inArgs.get_x_dotdot_sg();
    if(num_time_deriv > 1)
      x_dotdot_sg  = inArgs.get_x_dotdot_sg();
    if (x_dot_sg != Teuchos::null || x_dotdot_sg != Teuchos::null) {
      alpha = inArgs.get_alpha();
      beta = inArgs.get_beta();
      curr_time  = inArgs.get_t();
    }
    if (x_dotdot_sg != Teuchos::null) {
      omega = inArgs.get_omega();
    }

    InArgs::sg_const_vector_t epetra_p_sg = inArgs.get_p_sg(0);
    Teuchos::Array<int> p_sg_index;
    for (int i=0; i<num_param_vecs; i++) {
      InArgs::sg_const_vector_t p_sg = inArgs.get_p_sg(i);
      if (p_sg != Teuchos::null) {
        p_sg_index.push_back(i);
        for (int j=0; j<p_sg_vals[i].size(); j++) {
          int num_sg_blocks = p_sg->size();
          p_sg_vals[i][j].reset(app->getStochasticExpansion(), num_sg_blocks);
          p_sg_vals[i][j].copyForWrite();
          for (int l=0; l<num_sg_blocks; l++) {
            p_sg_vals[i][j].fastAccessCoeff(l) = (*p_sg)[l][j];
          }
        }
      }
    }

    OutArgs::sg_vector_t f_sg = outArgs.get_f_sg();
    OutArgs::sg_operator_t W_sg = outArgs.get_W_sg();
    bool f_sg_computed = false;

    // W_sg
    if (W_sg != Teuchos::null) {
      Stokhos::VectorOrthogPoly<Epetra_CrsMatrix> W_sg_crs(W_sg->basis(),
                                                           W_sg->map());
      for (int i=0; i<W_sg->size(); i++)
        W_sg_crs.setCoeffPtr(
          i,
          Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_sg->getCoeffPtr(i)));
      app->computeGlobalSGJacobian(alpha, beta, omega, curr_time,
                                   x_dot_sg.get(),  x_dotdot_sg.get(), *x_sg,
                                   sacado_param_vec, p_sg_index, p_sg_vals,
                                   f_sg.get(), W_sg_crs);
      f_sg_computed = true;
    }

    // df/dp_sg
    for (int i=0; i<num_param_vecs; i++) {
      Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > dfdp_sg
        = outArgs.get_DfDp_sg(i).getMultiVector();
      if (dfdp_sg != Teuchos::null) {
        Teuchos::Array<int> p_indexes =
          outArgs.get_DfDp_sg(i).getDerivativeMultiVector().getParamIndexes();
        Teuchos::RCP<ParamVec> p_vec;
        if (p_indexes.size() == 0)
          p_vec = Teuchos::rcp(&sacado_param_vec[i],false);
        else {
          p_vec = Teuchos::rcp(new ParamVec);
          for (int j=0; j<p_indexes.size(); j++)
            p_vec->addParam(sacado_param_vec[i][p_indexes[j]].family,
                            sacado_param_vec[i][p_indexes[j]].baseValue);
        }

        app->computeGlobalSGTangent(0.0, 0.0, 0.0, curr_time, false,
                                    x_dot_sg.get(), x_dotdot_sg.get(),*x_sg,
                                    sacado_param_vec, p_sg_index, p_sg_vals,
                                    p_vec.get(), NULL, NULL, NULL, NULL,
                                    f_sg.get(), NULL, dfdp_sg.get());

        f_sg_computed = true;
      }
    }

    if (f_sg != Teuchos::null && !f_sg_computed)
      app->computeGlobalSGResidual(curr_time, x_dot_sg.get(), x_dotdot_sg.get(),*x_sg,
                                   sacado_param_vec, p_sg_index, p_sg_vals,
                                   *f_sg);

    // Response functions
    for (int i=0; i<outArgs.Ng(); i++) {
      OutArgs::sg_vector_t g_sg = outArgs.get_g_sg(i);
      bool g_sg_computed = false;

      SGDerivative dgdx_sg = outArgs.get_DgDx_sg(i);
      SGDerivative dgdxdot_sg = outArgs.get_DgDx_dot_sg(i);
      SGDerivative dgdxdotdot_sg = outArgs.get_DgDx_dotdot_sg(i);

      // dg/dx, dg/dxdot
      if (!dgdx_sg.isEmpty() || !dgdxdot_sg.isEmpty() || !dgdxdotdot_sg.isEmpty()) {
        app->evaluateSGResponseDerivative(
            i, curr_time, x_dot_sg.get(), x_dotdot_sg.get(), *x_sg,
            sacado_param_vec, p_sg_index, p_sg_vals,
            NULL, g_sg.get(), dgdx_sg,
            dgdxdot_sg, dgdxdotdot_sg, SGDerivative());
        g_sg_computed = true;
      }

      // dg/dp
      for (int j=0; j<num_param_vecs; j++) {
        Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > dgdp_sg =
          outArgs.get_DgDp_sg(i,j).getMultiVector();
        if (dgdp_sg != Teuchos::null) {
          Teuchos::Array<int> p_indexes =
            outArgs.get_DgDp_sg(i,j).getDerivativeMultiVector().getParamIndexes();
          Teuchos::RCP<ParamVec> p_vec;
          if (p_indexes.size() == 0)
            p_vec = Teuchos::rcp(&sacado_param_vec[j],false);
          else {
            p_vec = Teuchos::rcp(new ParamVec);
            for (int k=0; k<p_indexes.size(); k++)
              p_vec->addParam(sacado_param_vec[j][p_indexes[k]].family,
                              sacado_param_vec[j][p_indexes[k]].baseValue);
          }
          app->evaluateSGResponseTangent(i, alpha, beta, omega, curr_time, false,
                                         x_dot_sg.get(), x_dotdot_sg.get(), *x_sg,
                                         sacado_param_vec, p_sg_index,
                                         p_sg_vals, p_vec.get(),
                                         NULL, NULL, NULL, NULL, g_sg.get(),
                                         NULL, dgdp_sg.get());
          g_sg_computed = true;

        }
      }

      if (g_sg != Teuchos::null && !g_sg_computed)
        app->evaluateSGResponse(i, curr_time, x_dot_sg.get(), x_dotdot_sg.get(), *x_sg,
                                sacado_param_vec, p_sg_index, p_sg_vals,
                                *g_sg);
    }
  }
#endif
#ifdef ALBANY_ENSEMBLE

  //
  // Multi-point evaluation
  //
  mp_const_vector_t x_mp = inArgs.get_x_mp();
  if (x_mp != Teuchos::null) {
    mp_const_vector_t x_dot_mp  = Teuchos::null;
    mp_const_vector_t x_dotdot_mp  = Teuchos::null;
    if(num_time_deriv > 0)
      x_dot_mp  = inArgs.get_x_dot_mp();
    if(num_time_deriv > 1)
      x_dotdot_mp  = inArgs.get_x_dotdot_mp();
    if (x_dot_mp != Teuchos::null || x_dotdot_mp != Teuchos::null) {
      alpha = inArgs.get_alpha();
      //omega = inArgs.get_omega();
      beta = inArgs.get_beta();
      curr_time  = inArgs.get_t();
    }
    if (x_dotdot_mp != Teuchos::null) {
      omega = inArgs.get_omega();
    }

    Teuchos::Array<int> p_mp_index;
    for (int i=0; i<num_param_vecs; i++) {
      mp_const_vector_t p_mp = inArgs.get_p_mp(i);
      if (p_mp != Teuchos::null) {
        p_mp_index.push_back(i);
        for (int j=0; j<p_mp_vals[i].size(); j++) {
          int num_mp_blocks = p_mp->size();
          p_mp_vals[i][j].reset(num_mp_blocks);
          p_mp_vals[i][j].copyForWrite();
          for (int l=0; l<num_mp_blocks; l++) {
            p_mp_vals[i][j].fastAccessCoeff(l) = (*p_mp)[l][j];
          }
        }
      }
    }

    mp_vector_t f_mp = outArgs.get_f_mp();
    mp_operator_t W_mp = outArgs.get_W_mp();
    bool f_mp_computed = false;

    // W_mp
    if (W_mp != Teuchos::null) {
      Stokhos::ProductContainer<Epetra_CrsMatrix> W_mp_crs(W_mp->map());
      for (int i=0; i<W_mp->size(); i++)
        W_mp_crs.setCoeffPtr(
          i,
          Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_mp->getCoeffPtr(i)));
      app->computeGlobalMPJacobian(alpha, beta, omega, curr_time,
                                   x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                   sacado_param_vec, p_mp_index, p_mp_vals,
                                   f_mp.get(), W_mp_crs);
      f_mp_computed = true;
    }

    // df/dp_mp
    for (int i=0; i<num_param_vecs; i++) {
      Teuchos::RCP< Stokhos::ProductEpetraMultiVector > dfdp_mp
        = outArgs.get_DfDp_mp(i).getMultiVector();
      if (dfdp_mp != Teuchos::null) {
        Teuchos::Array<int> p_indexes =
          outArgs.get_DfDp_mp(i).getDerivativeMultiVector().getParamIndexes();
        Teuchos::RCP<ParamVec> p_vec;
        if (p_indexes.size() == 0)
          p_vec = Teuchos::rcp(&sacado_param_vec[i],false);
        else {
          p_vec = Teuchos::rcp(new ParamVec);
          for (int j=0; j<p_indexes.size(); j++)
            p_vec->addParam(sacado_param_vec[i][p_indexes[j]].family,
                            sacado_param_vec[i][p_indexes[j]].baseValue);
        }

        app->computeGlobalMPTangent(0.0, 0.0, 0.0, curr_time, false,
                                    x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                    sacado_param_vec, p_mp_index, p_mp_vals,
                                    p_vec.get(), NULL, NULL, NULL, NULL,
                                    f_mp.get(), NULL, dfdp_mp.get());

        f_mp_computed = true;
      }
    }

    if (f_mp != Teuchos::null && !f_mp_computed)
      app->computeGlobalMPResidual(curr_time, x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                   sacado_param_vec, p_mp_index, p_mp_vals,
                                   *f_mp);

    // Response functions
    for (int i=0; i<outArgs.Ng(); i++) {
      mp_vector_t g_mp = outArgs.get_g_mp(i);
      bool g_mp_computed = false;

      MPDerivative dgdx_mp = outArgs.get_DgDx_mp(i);
      MPDerivative dgdxdot_mp = outArgs.get_DgDx_dot_mp(i);
      MPDerivative dgdxdotdot_mp = outArgs.get_DgDx_dotdot_mp(i);

      // dg/dx, dg/dxdot
      if (!dgdx_mp.isEmpty() || !dgdxdot_mp.isEmpty() || !dgdxdotdot_mp.isEmpty() ) {
        app->evaluateMPResponseDerivative(
            i, curr_time, x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
            sacado_param_vec, p_mp_index, p_mp_vals,
            NULL, g_mp.get(), dgdx_mp,
            dgdxdot_mp, dgdxdotdot_mp, MPDerivative());
        g_mp_computed = true;
      }

      // dg/dp
      for (int j=0; j<num_param_vecs; j++) {
        Teuchos::RCP< Stokhos::ProductEpetraMultiVector > dgdp_mp =
          outArgs.get_DgDp_mp(i,j).getMultiVector();
        if (dgdp_mp != Teuchos::null) {
          Teuchos::Array<int> p_indexes =
            outArgs.get_DgDp_mp(i,j).getDerivativeMultiVector().getParamIndexes();
          Teuchos::RCP<ParamVec> p_vec;
          if (p_indexes.size() == 0)
            p_vec = Teuchos::rcp(&sacado_param_vec[j],false);
          else {
            p_vec = Teuchos::rcp(new ParamVec);
            for (int k=0; k<p_indexes.size(); k++)
              p_vec->addParam(sacado_param_vec[j][p_indexes[k]].family,
                              sacado_param_vec[j][p_indexes[k]].baseValue);
          }
          app->evaluateMPResponseTangent(i, alpha, beta, omega, curr_time, false,
                                         x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                         sacado_param_vec, p_mp_index,
                                         p_mp_vals, p_vec.get(),
                                         NULL, NULL, NULL, NULL, g_mp.get(),
                                         NULL, dgdp_mp.get());
          g_mp_computed = true;
        }
      }

      if (g_mp != Teuchos::null && !g_mp_computed)
        app->evaluateMPResponse(i, curr_time, x_dot_mp.get(), x_dotdot_mp.get(), *x_mp,
                                sacado_param_vec, p_mp_index, p_mp_vals,
                                *g_mp);
    }
  }
#endif
}
void
Intermediate_Velocity_Z_Corrector(double***velocity_z_tilda,
				  double*** residual_z,
				  double*** residual_z_old,
				  double*** velocity_z_old,
				  double*** velocity_x,
				  double*** velocity_y,
				  double*** velocity_z,
				  double*** flux_x, double***flux_y,
				  double***flux_z,
				  double*** rho_new, double*** rho,
				  double*** temperature,
				  double*** pressure,
				  double Reynolds,double source,
				  double dx, double* dy,  double dz,
				  double dt, double time_total,
				  int ldx, int ldy, int ldz)
{
  //Allocating memory for the three vectors that will be used by the
  //tridiagonal solver
  double *lower_diagonal, *central_diagonal, *upper_diagonal, *rhs,
  *result, *auxiliary;

  lower_diagonal = new double [ldy+1];
  central_diagonal = new double [ldy+1];
  upper_diagonal = new double [ldy+1];
  rhs = new double [ldy+1];
  result = new double [ldy+1];
  auxiliary = new double [ldy+1];


  for (int k=0; k<ldz; k++){
    for (int i=0; i<ldx; i++){


      for (int j=0; j<ldy; j++){


        double explicit_term=0.;
        Velocity_Residual_Z_Implicit(residual_z,
                                     velocity_x, velocity_y, velocity_z,
                                     flux_x, flux_y,  flux_z,
                                     temperature, Reynolds,
                                     source,
                                     dx, dy, dz,
                                     time_total,
                                     i, j, k,
                                     &explicit_term);

        double residual_sum=
          (residual_z[k][j][i] + residual_z_old[k][j][i])/2.;



        // Introducing this term in order to fix the issue with the
        // pressure gradient in the tangential direction of the wall
        double pressure_gradient =
          ((4./3.)*Derivative(pressure[k+1][j][i],pressure[k-1][j][i],
                              dz,2)
           -(1./3.)*Derivative(pressure[k+2][j][i],pressure[k-2][j][i],
                               dz,4));


	////////////Define the implicit methodology that I will use
	double implicit_scheme=0.5;
	explicit_term *=(1.-implicit_scheme); 

	// computing the inputs of the vectors that the tridiagonal
	//solver will get
	double coefficients[3];
	Coefficient_Calculator(temperature, Reynolds,
			       coefficients,
			       dy, dt, 
			       implicit_scheme,
			       i, j, k);



        // Filling the vectors. Be cautious in the introduction of
        //the boundary conditions
        if (j==0)
          {

            lower_diagonal[j+1] = 0.;
            central_diagonal[j+1] = coefficients[1]+ rho_new[k][j][i]
	      -coefficients[0];
            
	    upper_diagonal[j+1] = coefficients[2];

            rhs[j+1] = ((rho[k][j][i]*velocity_z_old[k][j][i]+
			 dt*(residual_sum+explicit_term
			     -2.*pressure_gradient)));

              
          }
        else if (j==ldy-1)
          {

            lower_diagonal[j+1] = coefficients[0];
            
	    central_diagonal[j+1] = coefficients[1]+ rho_new[k][j][i]
	      -coefficients[2];
            
	    upper_diagonal[j+1] = 0.;

            rhs[j+1] = ((rho[k][j][i]*velocity_z_old[k][j][i]+
			 dt*(residual_sum+explicit_term
			     -2.*pressure_gradient)));


          }
        else
          {
            lower_diagonal[j+1] = coefficients[0];
            central_diagonal[j+1] = coefficients[1]+ rho_new[k][j][i];
            upper_diagonal[j+1] = coefficients[2];

            rhs[j+1] = ((rho[k][j][i]*velocity_z_old[k][j][i]+
			 dt*(residual_sum +explicit_term
			     -pressure_gradient)));


          }
      }

      // Solving the tridiagonal system
      int flag=0;
      tridag(lower_diagonal,central_diagonal,upper_diagonal,
	     rhs, result, ldy, auxiliary, flag, i, k);


      for (int vj = 0; vj < ldy; vj++)
	{
	  velocity_z_tilda[k][vj][i] = result[vj+1];
	}


    }
  }


  //deallocating the memory for the  vectors that will be used by the
  //tridiagonal solver

  delete[] lower_diagonal;
  delete[] central_diagonal;
  delete[] upper_diagonal;
  delete[] rhs ;
  delete[] result;
  delete [] auxiliary;





}
void Intermediate_Velocity_Z_Projection_Corrector
(double*** velocity_z_tilda,
 double*** residual_z,
 double*** residual_z_old,
 double*** velocity_z_old,
 double*** velocity_x,
 double*** velocity_y,
 double*** velocity_z,
 double*** flux_x, double***flux_y,
 double***flux_z,
 double*** rho_new, double*** rho,
 double*** temperature,
 double*** pressure,
 double Reynolds, double source_term,
 double dx, double* dy,  double dz,
 double dt, double time_total,
 int ldx, int ldy, int ldz)

{

  // At first I will calculate the Velocity Residual
  Velocity_Residual_Z( residual_z,
                       velocity_x, velocity_y, velocity_z,
                       flux_x, flux_y,  flux_z,
                       temperature, Reynolds, source_term,
                       dx, dy, dz,
                       time_total,
                       ldx, ldy, ldz);

  // After the calculation of the Velocity_Residual I will compute the
  // Intermediate Velocity


  for (int k=0; k<ldz; k++){
    for (int j=0; j<ldy; j++){
      for (int i=0; i<ldx; i++){

        double residual_sum=
          (residual_z[k][j][i] + residual_z_old[k][j][i])/2.;


        // Introducing this term in order to fix the issue with the
        // pressure gradient in the tangential direction of the wall

        double pressure_gradient =
          ((4./3.)*Derivative(pressure[k+1][j][i],pressure[k-1][j][i],
                              dz,2)
           -(1./3.)*Derivative(pressure[k+2][j][i],pressure[k-2][j][i],
                               dz,4));



        velocity_z_tilda[k][j][i] =

          (rho[k][j][i]*velocity_z_old[k][j][i]
           +dt* (residual_sum - pressure_gradient))
          /rho_new[k][j][i];

      }
    }
  }

}
示例#27
0
double Viscous_Component_YX(double*** velocity_x, double*** velocity_y,
                            double*** temperature,
                            double*** eddy_viscosity,
                            double Reynolds,
                            double dx, double* dy,
                            int i, int j, int k)
{

  double  derivative_xx[4][2], derivative_xy[4][2];
  double dy_total,  viscous_terms[4];

  // Calculation of the (dv/dx) component
  double total_derivative_x[4];
  for (int vi=0; vi<4; vi++)
    {

      derivative_xx[vi][0] = 9./(8.)*Derivative(velocity_y[k][j][i+vi-1],
                                                velocity_y[k][j][i+vi-2],
                                                dx,1);

      derivative_xx[vi][1] = -1./(8.)*Derivative(velocity_y[k][j][i+vi],
                                                 velocity_y[k][j][i+vi-3],
                                                 dx,3);

      //initalizing the vector
      total_derivative_x[vi]=0.;
      for (int vj=0; vj<2; vj++)
        total_derivative_x[vi]+=derivative_xx[vi][vj];

    }

  // Calculation of the (du/dy) component
  //i-3/2
  //i-3
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_xy[0][0] = Derivative(velocity_x[k][j+1][i-3],
                                   velocity_x[k][j-1][i-3],
                                   dy_total,1);

  //i
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_xy[0][1] = Derivative(velocity_x[k][j+1][i],
                                   velocity_x[k][j-1][i],
                                   dy_total,1);

  //i-1/2
  //i-1
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_xy[1][0] = Derivative(velocity_x[k][j+1][i-1],
                                   velocity_x[k][j-1][i-1],
                                   dy_total,1);

  //i/
  derivative_xy[1][1] = derivative_xy[0][1];

  //i+1/2
  //i
  derivative_xy[2][0] = derivative_xy[0][1];
  //i+1
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_xy[2][1] = Derivative(velocity_x[k][j+1][i+1],
                                   velocity_x[k][j-1][i+1],
                                   dy_total,1);

  //i+3/2
  //i
  derivative_xy[3][0] = derivative_xy[0][1];
  //i+3
  dy_total=dy[j-1]+2.*dy[j]+dy[j+1];
  derivative_xy[3][1] = Derivative(velocity_x[k][j+1][i+3],
                                   velocity_x[k][j-1][i+3],
                                   dy_total,1);


  //summing the derivatives in the y direction
  double total_derivative_y[4];
  for(int vi=0; vi<4; vi++)
    {
      total_derivative_y[vi]=Interpolation(derivative_xy[vi][0],
                                           derivative_xy[vi][1]);
    }

  //Computing the viscosities.
  double viscosity[4];
  for (int vi=-2, vj=0; vi<2; vi++, vj++)
    {
      viscosity[vj]=
        Viscosity_Calculator(Interpolation(temperature[k][j][i+vi+1],
                                           temperature[k][j][i+vi]))
        +Interpolation(eddy_viscosity[k][j][i+vi+1],
                       eddy_viscosity[k][j][i+vi]);
    }


  //computing the viscous tensors
  for (int vi=0; vi<4; vi++)
    {
      viscous_terms[vi] =viscosity[vi]*(total_derivative_x[vi]
                                        +total_derivative_y[vi]);
    }


  // cout<<"yx"<<endl;
  // cout<<-1./24.*(total_derivative_y[3]-total_derivative_y[0])
  //   +9./8.*(total_derivative_y[2]-total_derivative_y[1])<<endl;



  /* Summing the X-component of the Viscous Term of the Y-Momentum equation*/
  double viscous_component =
    1./Reynolds*(9./8.*Derivative(viscous_terms[2],
                                  viscous_terms[1],
                                  dx,1)-
                 1./8.*Derivative(viscous_terms[3],
                                  viscous_terms[0],
                                  dx,3));

  return viscous_component;


}