Ejemplo n.º 1
0
bool solveNonadaptive(Mesh &mesh, NonlinSystem &nls,
		Solution &Cp, Solution &Ci, Solution &phip, Solution &phii) {
	Solution Csln, phisln;
	for (int n = 1; n <= NSTEP; n++) {

		int it = 1;
		double res_l2_norm;

		do {

			it++;
			nls.assemble();
			nls.solve(Tuple<Solution*>(&Csln, &phisln));
			res_l2_norm = nls.get_residual_l2_norm();

			Ci.copy(&Csln);
			phii.copy(&phisln);

		} while (res_l2_norm > NEWTON_TOL);
		phip.copy(&phii);
		Cp.copy(&Ci);
	}
	scalar *sol_vector;
	int n_dof;
	nls.get_solution_vector(sol_vector, n_dof);
	printf("n_dof: %d\n", n_dof);
	double sum = 0;
	for (int i = 0; i < n_dof; i++) {
		 sum += sol_vector[i];
	}
	printf("coefficient sum = %g\n", sum);

	// Actual test. The value of 'sum' depend on the
	// current shapeset. If you change the shapeset,
	// you need to correct this number.
	printf("ret: %g\n", fabs(sum - 50505));
	return !(fabs(sum - 50505) > 1);

}
Ejemplo n.º 2
0
bool solveAdaptive(Mesh &Cmesh, Mesh &phimesh, Mesh &basemesh, NonlinSystem &nls, H1Space &C, H1Space &phi,
                   Solution &Cp, Solution &Ci, Solution &phip, Solution &phii) {

  // create a selector which will select optimal candidate
  H1ProjBasedSelector selector(CAND_LIST, 1.0, H2DRS_DEFAULT_ORDER);

  Solution Csln_coarse, phisln_coarse, Csln_fine, phisln_fine;
  int at_index = 1; //for saving screenshot
  for (int n = 1; n <= NSTEP; n++) {
    if (n % UNREF_FREQ == 0) {
      Cmesh.copy(&basemesh);
      if (MULTIMESH) {
        phimesh.copy(&basemesh);
      }
      C.set_uniform_order(P_INIT);
      phi.set_uniform_order(P_INIT);
      int ndofs;
      ndofs = C.assign_dofs();
      phi.assign_dofs(ndofs);
    }

    int at = 0;
    bool done = false;
    double err;
    do {
      at++;
      at_index++;
      int it = 1;
      double res_l2_norm;
      if (n > 1 || at > 1) {
        nls.project_global(Tuple<MeshFunction*>(&Csln_fine, &phisln_fine), 
                           Tuple<Solution*>(&Ci, &phii));
      } else {
        /* No need to set anything, already set. */
        nls.project_global(Tuple<MeshFunction*>(&Ci, &phii), 
                           Tuple<Solution*>(&Ci, &phii));
      }
      //Loop for coarse mesh solution
      do {
        it++;

        nls.assemble();
        nls.solve(Tuple<Solution*>(&Csln_coarse, &phisln_coarse));
        res_l2_norm = nls.get_residual_l2_norm();

        Ci.copy(&Csln_coarse);
        phii.copy(&phisln_coarse);
      } while (res_l2_norm > NEWTON_TOL_COARSE);

      it = 1;
      // Loop for fine mesh solution
      RefSystem rs(&nls);
      if (n > 1 || at > 1) {
        rs.project_global(Tuple<MeshFunction*>(&Csln_fine, &phisln_fine), 
                          Tuple<Solution*>(&Ci, &phii));
      } else {
        rs.project_global(Tuple<MeshFunction*>(&Ci, &phii), 
                          Tuple<Solution*>(&Ci, &phii));
      }

      do {
        it++;

        rs.assemble();
        rs.solve(Tuple<Solution*>(&Csln_fine, &phisln_fine));
        res_l2_norm = rs.get_residual_l2_norm();

        Ci.copy(&Csln_fine);
        phii.copy(&phisln_fine);
      } while (res_l2_norm > NEWTON_TOL_REF);

      // Calculate element errors and total estimate
      H1Adapt hp(&nls);
      hp.set_solutions(Tuple<Solution*>(&Csln_coarse, &phisln_coarse), Tuple<Solution*>(&Csln_fine, &phisln_fine));
      info("Calculating element errors");
      err = hp.calc_error() * 100;
      info("Error: %g%%", err);

      if (err < ERR_STOP) {
        done = true;
      } else {
        hp.adapt(&selector, THRESHOLD, STRATEGY);
      }

      int ndofs;
      ndofs = C.assign_dofs();
      ndofs += phi.assign_dofs(ndofs);
      info("NDOFS after adapting: %d", ndofs);
      if (ndofs >= NDOF_STOP) {
        info("NDOFs reached to the max %d", NDOF_STOP);
        done = true;
      }
    } while (!done);
    phip.copy(&phii);
    Cp.copy(&Ci);
  }
  int nd = C.get_num_dofs() + phi.get_num_dofs();
  info("NDOFs at the end of timestep: %d", nd);
	bool success = false;
  if (nd < 283)
    success = true;

	scalar *sol_vector;
  int n_dof;
	nls.get_solution_vector(sol_vector, n_dof);
	printf("n_dof: %d\n", n_dof);
	double sum = 0;
	for (int i = 0; i < n_dof; i++) {
		 sum += sol_vector[i];
	}
	printf("coefficient sum = %g\n", sum);

  return success;
}