PageRenderTime 23ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/hpc_kernel_samples/sparse_linear_algebra/casestudies/pagerank-petsc/petsc-2.3.2-p10/src/tops/server/cxx/TOPS_StructuredSolver_Impl.cxx

https://gitlab.com/pheinzlr/CodeVault
C++ | 704 lines | 451 code | 93 blank | 160 comment | 30 complexity | f29a05b1b06c94a1b0d398512719947a MD5 | raw file
  1. //
  2. // File: TOPS_StructuredSolver_Impl.cxx
  3. // Symbol: TOPS.StructuredSolver-v0.0.0
  4. // Symbol Type: class
  5. // Babel Version: 1.0.0
  6. // Description: Server-side implementation for TOPS.StructuredSolver
  7. //
  8. // WARNING: Automatically generated; only changes within splicers preserved
  9. //
  10. //
  11. #include "TOPS_StructuredSolver_Impl.hxx"
  12. //
  13. // Includes for all method dependencies.
  14. //
  15. #ifndef included_gov_cca_CCAException_hxx
  16. #include "gov_cca_CCAException.hxx"
  17. #endif
  18. #ifndef included_gov_cca_Services_hxx
  19. #include "gov_cca_Services.hxx"
  20. #endif
  21. #ifndef included_sidl_BaseInterface_hxx
  22. #include "sidl_BaseInterface.hxx"
  23. #endif
  24. #ifndef included_sidl_ClassInfo_hxx
  25. #include "sidl_ClassInfo.hxx"
  26. #endif
  27. #ifndef included_sidl_RuntimeException_hxx
  28. #include "sidl_RuntimeException.hxx"
  29. #endif
  30. #ifndef included_sidl_NotImplementedException_hxx
  31. #include "sidl_NotImplementedException.hxx"
  32. #endif
  33. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver._includes)
  34. #include <iostream>
  35. #include "TOPS_Structured_Matrix_Impl.hxx"
  36. #include "TOPS_ParameterHandling.hxx" // not from SIDL
  37. // This code is the same as DAVecGetArray() except instead of generating
  38. // raw C multidimensional arrays it gets a Babel array
  39. ::sidl::array<double> DAVecGetArrayBabel(DA da,Vec vec)
  40. {
  41. double *uu;
  42. VecGetArray(vec,&uu);
  43. PetscInt xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,dim,dof,N;
  44. DAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);
  45. DAGetGhostCorners(da,&gxs,&gys,&gzs,&gxm,&gym,&gzm);
  46. DAGetInfo(da,&dim,0,0,0,0,0,0,&dof,0,0,0);
  47. /* Handle case where user passes in global vector as opposed to local */
  48. VecGetLocalSize(vec,&N);
  49. if (N == xm*ym*zm*dof) {
  50. gxm = xm;
  51. gym = ym;
  52. gzm = zm;
  53. gxs = xs;
  54. gys = ys;
  55. gzs = zs;
  56. }
  57. sidl::array<double> ua;
  58. int lower[4],upper[4],stride[4];
  59. if (dof > 1) {
  60. dim++;
  61. lower[0] = 0; upper[0] = dof; stride[0] = 1;
  62. lower[1] = gxs; lower[2] = gys; lower[3] = gzs;
  63. upper[1] = gxm + gxs - 1; upper[2] = gym + gys - 1; upper[3] = gzm + gzs - 1;
  64. stride[1] = dof; stride[2] = gxm*dof; stride[3] = gxm*gym*dof;
  65. } else {
  66. lower[0] = gxs; lower[1] = gys; lower[2] = gzs;
  67. upper[0] = gxm +gxs - 1; upper[1] = gym + gys - 1 ; upper[2] = gzm + gzs - 1;
  68. stride[0] = 1; stride[1] = gxm; stride[2] = gxm*gym;
  69. }
  70. ua.borrow(uu,dim,*&lower,*&upper,*&stride);
  71. return ua;
  72. }
  73. #undef __FUNCT__
  74. #define __FUNCT__ "FormFunction"
  75. static PetscErrorCode FormFunction(SNES snes,Vec uu,Vec f,void *vdmmg)
  76. {
  77. PetscFunctionBegin;
  78. DMMG dmmg = (DMMG) vdmmg;
  79. TOPS::StructuredSolver *solver = (TOPS::StructuredSolver*) dmmg->user;
  80. TOPS::System::Compute::Residual system;
  81. system = ::babel_cast< TOPS::System::Compute::Residual >(
  82. solver->getServices().getPort("TOPS.System.Compute.Residual"));
  83. if (system._is_nil()) {
  84. std::cerr << "Error at " << __FILE__ << ":" << __LINE__
  85. << ": TOPS.System.Compute.Residual port is nil, "
  86. << "possibly not connected." << std::endl;
  87. PetscFunctionReturn(1);
  88. }
  89. DA da = (DA) dmmg->dm;
  90. Vec u;
  91. DAGetLocalVector(da,&u);
  92. DAGlobalToLocalBegin(da,uu,INSERT_VALUES,u);
  93. DAGlobalToLocalEnd(da,uu,INSERT_VALUES,u);
  94. int mx,my,mz;
  95. DAGetInfo(da,0,&mx,&my,&mz,0,0,0,0,0,0,0);
  96. solver->setLength(0,mx);
  97. solver->setLength(1,my);
  98. solver->setLength(2,mz);
  99. sidl::array<double> ua = DAVecGetArrayBabel(da,u);
  100. sidl::array<double> fa = DAVecGetArrayBabel(da,f);;
  101. system.computeResidual(ua,fa);
  102. VecRestoreArray(u,0);
  103. DARestoreLocalVector(da,&u);
  104. VecRestoreArray(f,0);
  105. solver->getServices().releasePort("TOPS.System.Compute.Residual");
  106. PetscFunctionReturn(0);
  107. }
  108. static PetscErrorCode FormInitialGuess(DMMG dmmg,Vec f)
  109. {
  110. PetscFunctionBegin;
  111. TOPS::StructuredSolver *solver = (TOPS::StructuredSolver*) dmmg->user;
  112. TOPS::System::Compute::InitialGuess system;
  113. system = ::babel_cast< TOPS::System::Compute::InitialGuess >(
  114. solver->getServices().getPort("TOPS.System.Compute.InitialGuess"));
  115. if (system._is_nil()) {
  116. std::cerr << "Error at " << __FILE__ << ":" << __LINE__
  117. << ": TOPS.System.Compute.InitialGuess port is nil, "
  118. << "possibly not connected." << std::endl;
  119. PetscFunctionReturn(1);
  120. }
  121. int mx,my,mz;
  122. DAGetInfo((DA)dmmg->dm,0,&mx,&my,&mz,0,0,0,0,0,0,0);
  123. solver->setLength(0,mx);
  124. solver->setLength(1,my);
  125. solver->setLength(2,mz);
  126. sidl::array<double> fa = DAVecGetArrayBabel((DA)dmmg->dm,f);
  127. system.computeInitialGuess(fa);
  128. VecRestoreArray(f,0);
  129. solver->getServices().releasePort("TOPS.System.Compute.InitialGuess");
  130. PetscFunctionReturn(0);
  131. }
  132. static PetscErrorCode FormMatrix(DMMG dmmg,Mat J,Mat B)
  133. {
  134. PetscFunctionBegin;
  135. TOPS::StructuredSolver *solver = (TOPS::StructuredSolver*) dmmg->user;
  136. TOPS::System::Compute::Matrix system;
  137. TOPS::Structured::Matrix matrix1 = TOPS::Structured::Matrix::_create();
  138. TOPS::Structured::Matrix matrix2 = TOPS::Structured::Matrix::_create();
  139. PetscInt xs,ys,zs,xm,ym,zm,gxs,gys,gzs,gxm,gym,gzm,dof,mx,my,mz;
  140. DAGetCorners((DA)dmmg->dm,&xs,&ys,&zs,&xm,&ym,&zm);
  141. DAGetGhostCorners((DA)dmmg->dm,&gxs,&gys,&gzs,&gxm,&gym,&gzm);
  142. DAGetInfo((DA)dmmg->dm,0,&mx,&my,&mz,0,0,0,&dof,0,0,0);
  143. #define GetImpl(A,b) (!(A)b) ? 0 : reinterpret_cast<A ## _impl*>(((A) b)._get_ior()->d_data)
  144. // currently no support for dof > 1
  145. TOPS::Structured::Matrix_impl *imatrix1 = GetImpl(TOPS::Structured::Matrix,matrix1);
  146. imatrix1->vlength[0] = xm; imatrix1->vlength[1] = ym; imatrix1->vlength[2] = zm;
  147. imatrix1->vlower[0] = xs; imatrix1->vlower[1] = ys; imatrix1->vlower[2] = zs;
  148. imatrix1->gghostlength[0] = gxm; imatrix1->gghostlength[1] = gym;
  149. imatrix1->gghostlength[2] = gzm;
  150. imatrix1->gghostlower[0] = gxs; imatrix1->gghostlower[1] = gys;
  151. imatrix1->gghostlower[2] = gzs;
  152. imatrix1->vdimen = dof;
  153. TOPS::Structured::Matrix_impl *imatrix2 = GetImpl(TOPS::Structured::Matrix,matrix2);
  154. imatrix2->vlength[0] = xm; imatrix2->vlength[1] = ym; imatrix2->vlength[2] = zm;
  155. imatrix2->vlower[0] = xs; imatrix2->vlower[1] = ys; imatrix2->vlower[2] = zs;
  156. imatrix2->gghostlength[0] = gxm; imatrix2->gghostlength[1] = gym;
  157. imatrix2->gghostlength[2] = gzm;
  158. imatrix2->gghostlower[0] = gxs; imatrix2->gghostlower[1] = gys;
  159. imatrix2->gghostlower[2] = gzs;
  160. imatrix2->vdimen = dof;
  161. imatrix1->mat = J;
  162. imatrix2->mat = B;
  163. DAGetInfo((DA)dmmg->dm,0,&mx,&my,&mz,0,0,0,0,0,0,0);
  164. solver->setLength(0,mx);
  165. solver->setLength(1,my);
  166. solver->setLength(2,mz);
  167. system = ::babel_cast< TOPS::System::Compute::Matrix >(
  168. solver->getServices().getPort("TOPS.System.Compute.Matrix"));
  169. if (system._is_nil()) {
  170. std::cerr << "Error at " << __FILE__ << ":" << __LINE__
  171. << ": TOPS.System.Compute.Matrix port is nil, "
  172. << "possibly not connected." << std::endl;
  173. PetscFunctionReturn(1);
  174. }
  175. // Use the port
  176. if (system._not_nil()) {
  177. system.computeMatrix(matrix1,matrix2);
  178. }
  179. solver->getServices().releasePort("TOPS.System.Compute.Matrix");
  180. MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
  181. MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
  182. if (J != B) {
  183. MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
  184. MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
  185. }
  186. PetscFunctionReturn(0);
  187. }
  188. #undef __FUNCT__
  189. #define __FUNCT__ "FormRightHandSide"
  190. static PetscErrorCode FormRightHandSide(DMMG dmmg,Vec f)
  191. {
  192. PetscFunctionBegin;
  193. int mx,my,mz;
  194. TOPS::StructuredSolver *solver = (TOPS::StructuredSolver*) dmmg->user;
  195. TOPS::System::Compute::RightHandSide system;
  196. system = ::babel_cast< TOPS::System::Compute::RightHandSide >(
  197. solver->getServices().getPort("TOPS.System.Compute.RightHandSide"));
  198. DAGetInfo((DA)dmmg->dm,0,&mx,&my,&mz,0,0,0,0,0,0,0);
  199. solver->setLength(0,mx);
  200. solver->setLength(1,my);
  201. solver->setLength(2,mz);
  202. sidl::array<double> fa = DAVecGetArrayBabel((DA)dmmg->dm,f);;
  203. if (system._not_nil()) {
  204. system.computeRightHandSide(fa);
  205. }
  206. solver->getServices().releasePort("TOPS.System.Compute.RightHandSide");
  207. VecRestoreArray(f,0);
  208. PetscFunctionReturn(0);
  209. }
  210. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver._includes)
  211. // speical constructor, used for data wrapping(required). Do not put code here unless you really know what you're doing!
  212. TOPS::StructuredSolver_impl::StructuredSolver_impl() :
  213. StubBase(reinterpret_cast<
  214. void*>(::TOPS::StructuredSolver::_wrapObj(reinterpret_cast< void*>(this))),
  215. false) , _wrapped(true){
  216. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver._ctor2)
  217. // Insert-Code-Here {TOPS.StructuredSolver._ctor2} (ctor2)
  218. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver._ctor2)
  219. }
  220. // user defined constructor
  221. void TOPS::StructuredSolver_impl::_ctor() {
  222. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver._ctor)
  223. #undef __FUNCT__
  224. #define __FUNCT__ "TOPS::StructuredSolver_impl::_ctor()"
  225. this->dmmg = PETSC_NULL;
  226. this->da = PETSC_NULL;
  227. this->m = PETSC_DECIDE;
  228. this->n = PETSC_DECIDE;
  229. this->p = PETSC_DECIDE;
  230. this->lengths[0] = 3;
  231. this->lengths[1] = 3;
  232. this->lengths[2] = 3;
  233. this->dim = 2;
  234. this->s = 1;
  235. this->wrap = DA_NONPERIODIC;
  236. this->bs = 1;
  237. this->stencil_type = DA_STENCIL_STAR;
  238. this->levels = 3;
  239. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver._ctor)
  240. }
  241. // user defined destructor
  242. void TOPS::StructuredSolver_impl::_dtor() {
  243. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver._dtor)
  244. #undef __FUNCT__
  245. #define __FUNCT__ "TOPS::StructuredSolver_impl::_dtor()"
  246. if (this->dmmg) {DMMGDestroy(this->dmmg);}
  247. if (this->startedpetsc) {
  248. PetscFinalize();
  249. }
  250. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver._dtor)
  251. }
  252. // static class initializer
  253. void TOPS::StructuredSolver_impl::_load() {
  254. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver._load)
  255. // Insert-Code-Here {TOPS.StructuredSolver._load} (class initialization)
  256. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver._load)
  257. }
  258. // user defined static methods: (none)
  259. // user defined non-static methods:
  260. /**
  261. * Method: getServices[]
  262. */
  263. ::gov::cca::Services
  264. TOPS::StructuredSolver_impl::getServices_impl ()
  265. {
  266. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.getServices)
  267. // Insert-Code-Here {TOPS.StructuredSolver.getServices} (getServices method)
  268. #undef __FUNCT__
  269. #define __FUNCT__ "TOPS::StructuredSolver_impl::getServices()"
  270. return this->myServices;
  271. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.getServices)
  272. }
  273. /**
  274. * Method: dimen[]
  275. */
  276. int32_t
  277. TOPS::StructuredSolver_impl::dimen_impl ()
  278. {
  279. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.dimen)
  280. return dim;
  281. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.dimen)
  282. }
  283. /**
  284. * Method: length[]
  285. */
  286. int32_t
  287. TOPS::StructuredSolver_impl::length_impl (
  288. /* in */int32_t a )
  289. {
  290. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.length)
  291. return this->lengths[a];
  292. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.length)
  293. }
  294. /**
  295. * Method: setDimen[]
  296. */
  297. void
  298. TOPS::StructuredSolver_impl::setDimen_impl (
  299. /* in */int32_t dim )
  300. {
  301. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.setDimen)
  302. this->dim = dim;
  303. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.setDimen)
  304. }
  305. /**
  306. * Method: setLength[]
  307. */
  308. void
  309. TOPS::StructuredSolver_impl::setLength_impl (
  310. /* in */int32_t a,
  311. /* in */int32_t l )
  312. {
  313. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.setLength)
  314. this->lengths[a] = l;
  315. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.setLength)
  316. }
  317. /**
  318. * Method: setStencilWidth[]
  319. */
  320. void
  321. TOPS::StructuredSolver_impl::setStencilWidth_impl (
  322. /* in */int32_t width )
  323. {
  324. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.setStencilWidth)
  325. // Insert-Code-Here {TOPS.StructuredSolver.setStencilWidth} (setStencilWidth method)
  326. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.setStencilWidth)
  327. }
  328. /**
  329. * Method: getStencilWidth[]
  330. */
  331. int32_t
  332. TOPS::StructuredSolver_impl::getStencilWidth_impl ()
  333. {
  334. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.getStencilWidth)
  335. // Insert-Code-Here {TOPS.StructuredSolver.getStencilWidth} (getStencilWidth method)
  336. return 0;
  337. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.getStencilWidth)
  338. }
  339. /**
  340. * Method: setLevels[]
  341. */
  342. void
  343. TOPS::StructuredSolver_impl::setLevels_impl (
  344. /* in */int32_t levels )
  345. {
  346. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.setLevels)
  347. this->levels = levels;
  348. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.setLevels)
  349. }
  350. /**
  351. * Method: Initialize[]
  352. */
  353. void
  354. TOPS::StructuredSolver_impl::Initialize_impl (
  355. /* in array<string> */::sidl::array< ::std::string> args )
  356. {
  357. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.Initialize)
  358. #undef __FUNCT__
  359. #define __FUNCT__ "TOPS::StructuredSolver_impl::Initialize"
  360. PetscTruth initialized;
  361. PetscInitialized(&initialized);
  362. if (initialized) {
  363. this->startedpetsc = 0;
  364. return;
  365. }
  366. this->startedpetsc = 1;
  367. int argc = args.upper(0) + 1;
  368. char **argv = new char* [argc];
  369. std::string arg;
  370. for(int i = 0; i < argc; i++) {
  371. arg = args[i];
  372. argv[i] = new char [arg.length()+1];
  373. arg.copy(argv[i], arg.length(), 0);
  374. argv[i][arg.length()] = 0;
  375. }
  376. PetscInitialize(&argc,&argv,0,0);
  377. // Process runtime parameters
  378. params = ::babel_cast< gov::cca::ports::ParameterPort >( myServices.getPort("tops_options") );
  379. std::string options = params.readConfigurationMap().getString("options","-help");
  380. processTOPSOptions(options);
  381. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.Initialize)
  382. }
  383. /**
  384. * Method: solve[]
  385. */
  386. void
  387. TOPS::StructuredSolver_impl::solve_impl ()
  388. {
  389. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.solve)
  390. #undef __FUNCT__
  391. #define __FUNCT__ "TOPS::StructuredSolver_impl::solve()"
  392. PetscErrorCode ierr;
  393. if (!this->dmmg) {
  394. TOPS::System::Initialize::Once once;
  395. once = ::babel_cast< TOPS::System::Initialize::Once >( myServices.getPort("TOPS.System.Initialize.Once"));
  396. if (once._not_nil()) {
  397. once.initializeOnce();
  398. }
  399. myServices.releasePort("TOPS.System.Initialize.Once");
  400. // create DMMG object
  401. DMMGCreate(PETSC_COMM_WORLD,this->levels,(void*)this,&this->dmmg);
  402. DACreate(PETSC_COMM_WORLD,this->dim,this->wrap,this->stencil_type,this->lengths[0],
  403. this->lengths[1],this->lengths[2],this->m,this->n,
  404. this->p,this->bs,this->s,PETSC_NULL,PETSC_NULL,PETSC_NULL,&this->da);
  405. DMMGSetDM(this->dmmg,(DM)this->da);
  406. TOPS::System::Compute::Residual residual;
  407. residual = ::babel_cast< TOPS::System::Compute::Residual >( myServices.getPort("TOPS.System.Compute.Residual"));
  408. if (residual._not_nil()) {
  409. ierr = DMMGSetSNES(this->dmmg, FormFunction, 0);
  410. } else {
  411. ierr = DMMGSetKSP(this->dmmg,FormRightHandSide,FormMatrix);
  412. }
  413. myServices.releasePort("TOPS.System.Compute.Residual");
  414. TOPS::System::Compute::InitialGuess guess;
  415. guess = ::babel_cast< TOPS::System::Compute::InitialGuess >( myServices.getPort("TOPS.System.Compute.InitialGuess") );
  416. if (guess._not_nil()) {
  417. ierr = DMMGSetInitialGuess(this->dmmg, FormInitialGuess);
  418. }
  419. }
  420. myServices.releasePort("TOPS.System.Compute.InitialGuess");
  421. TOPS::System::Initialize::EverySolve every;
  422. every = ::babel_cast< TOPS::System::Initialize::EverySolve >( myServices.getPort("TOPS.System.Initialize.EverySolve") );
  423. if (every._not_nil()) {
  424. every.initializeEverySolve();
  425. }
  426. myServices.releasePort("TOPS.System.Initialize.EverySolve");
  427. DMMGSolve(this->dmmg);
  428. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.solve)
  429. }
  430. /**
  431. * Method: setBlockSize[]
  432. */
  433. void
  434. TOPS::StructuredSolver_impl::setBlockSize_impl (
  435. /* in */int32_t bs )
  436. {
  437. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.setBlockSize)
  438. this->bs = bs;
  439. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.setBlockSize)
  440. }
  441. /**
  442. * Method: getSolution[]
  443. */
  444. ::sidl::array<double>
  445. TOPS::StructuredSolver_impl::getSolution_impl ()
  446. {
  447. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.getSolution)
  448. // Insert-Code-Here {TOPS.StructuredSolver.getSolution} (getSolution method)
  449. return 0;
  450. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.getSolution)
  451. }
  452. /**
  453. * Method: setSolution[]
  454. */
  455. void
  456. TOPS::StructuredSolver_impl::setSolution_impl (
  457. /* in array<double> */::sidl::array<double> location )
  458. {
  459. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.setSolution)
  460. // Insert-Code-Here {TOPS.StructuredSolver.setSolution} (setSolution method)
  461. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.setSolution)
  462. }
  463. /**
  464. * Starts up a component presence in the calling framework.
  465. * @param services the component instance's handle on the framework world.
  466. * Contracts concerning Svc and setServices:
  467. *
  468. * The component interaction with the CCA framework
  469. * and Ports begins on the call to setServices by the framework.
  470. *
  471. * This function is called exactly once for each instance created
  472. * by the framework.
  473. *
  474. * The argument Svc will never be nil/null.
  475. *
  476. * Those uses ports which are automatically connected by the framework
  477. * (so-called service-ports) may be obtained via getPort during
  478. * setServices.
  479. */
  480. void
  481. TOPS::StructuredSolver_impl::setServices_impl (
  482. /* in */::gov::cca::Services services )
  483. // throws:
  484. // ::gov::cca::CCAException
  485. // ::sidl::RuntimeException
  486. {
  487. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.setServices)
  488. #undef __FUNCT__
  489. #define __FUNCT__ "TOPS::StructuredSolver_impl::setServices"
  490. myServices = services;
  491. gov::cca::TypeMap tm = services.createTypeMap();
  492. if(tm._is_nil()) {
  493. fprintf(stderr, "Error:: %s:%d: gov::cca::TypeMap is nil\n",
  494. __FILE__, __LINE__);
  495. exit(1);
  496. }
  497. gov::cca::Port p = (*this); // Babel required casting
  498. if(p._is_nil()) {
  499. fprintf(stderr, "Error:: %s:%d: Error casting (*this) to gov::cca::Port \n",
  500. __FILE__, __LINE__);
  501. exit(1);
  502. }
  503. // Provides port
  504. services.addProvidesPort(p,
  505. "TOPS.Structured.Solver",
  506. "TOPS.Structured.Solver", tm);
  507. // Uses ports
  508. services.registerUsesPort("TOPS.System.Initialize.Once",
  509. "TOPS.System.Initialize.Once", tm);
  510. services.registerUsesPort("TOPS.System.Initialize.EverySolve",
  511. "TOPS.System.Initialize.EverySolve", tm);
  512. services.registerUsesPort("TOPS.System.Compute.InitialGuess",
  513. "TOPS.System.Compute.InitialGuess", tm);
  514. services.registerUsesPort("TOPS.System.Compute.Matrix",
  515. "TOPS.System.Compute.Matrix", tm);
  516. services.registerUsesPort("TOPS.System.Compute.RightHandSide",
  517. "TOPS.System.Compute.RightHandSide", tm);
  518. services.registerUsesPort("TOPS.System.Compute.Jacobian",
  519. "TOPS.System.Compute.Jacobian", tm);
  520. services.registerUsesPort("TOPS.System.Compute.Residual",
  521. "TOPS.System.Compute.Residual", tm);
  522. // Parameter port
  523. myServices.registerUsesPort(std::string("ParameterPortFactory"),
  524. std::string("gov.cca.ports.ParameterPortFactory"), tm);
  525. // Set up parameter port
  526. if (this->setupParameterPort() != 0) {
  527. std::cerr << "TOPS::StructuredSolver_impl::go: errors during setup of ParameterPort" << std::endl;
  528. }
  529. myServices.unregisterUsesPort(std::string("ParameterPortFactory"));
  530. return;
  531. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.setServices)
  532. }
  533. /**
  534. * Inform the listener that someone is about to fetch their
  535. * typemap. The return should be true if the listener
  536. * has changed the ParameterPort definitions.
  537. */
  538. bool
  539. TOPS::StructuredSolver_impl::updateParameterPort_impl (
  540. /* in */const ::std::string& portName )
  541. {
  542. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.updateParameterPort)
  543. // Insert-Code-Here {TOPS.StructuredSolver.updateParameterPort} (updateParameterPort method)
  544. std::cout << "TOPS::StructuredSolver_impl::updatedParameterPort called" << std::endl;
  545. // Get the runtime parameters
  546. params = ::babel_cast< gov::cca::ports::ParameterPort >( myServices.getPort("tops_options") );
  547. std::string options = params.readConfigurationMap().getString("options","-help");
  548. processTOPSOptions(options);
  549. SNES snes = DMMGGetSNES(dmmg);
  550. if (snes) SNESSetFromOptions(snes);
  551. return true;
  552. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.updateParameterPort)
  553. }
  554. /**
  555. * The component wishing to be told after a parameter is changed
  556. * implements this function.
  557. * @param portName the name of the port (typemap) on which the
  558. * value was set.
  559. * @param fieldName the name of the value in the typemap.
  560. */
  561. void
  562. TOPS::StructuredSolver_impl::updatedParameterValue_impl (
  563. /* in */const ::std::string& portName,
  564. /* in */const ::std::string& fieldName )
  565. {
  566. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver.updatedParameterValue)
  567. // Insert-Code-Here {TOPS.StructuredSolver.updatedParameterValue} (updatedParameterValue method)
  568. std::cout << "TOPS::StructuredSolver_impl::updatedParameterValue called" << std::endl;
  569. PetscTruth flg;
  570. PetscInt ierr;
  571. ierr = PetscInitialized(&flg);
  572. if (!flg) ierr = PetscInitializeNoArguments();
  573. params = ::babel_cast< gov::cca::ports::ParameterPort >( myServices.getPort("tops_options") );
  574. std::string options = params.readConfigurationMap().getString("options","-help");
  575. processTOPSOptions(options);
  576. SNES snes = DMMGGetSNES(dmmg);
  577. if (snes) SNESSetFromOptions(snes);
  578. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver.updatedParameterValue)
  579. }
  580. // DO-NOT-DELETE splicer.begin(TOPS.StructuredSolver._misc)
  581. // Insert-Code-Here {TOPS.StructuredSolver._misc} (miscellaneous code)
  582. int TOPS::StructuredSolver_impl::setupParameterPort() {
  583. #undef __FUNCT__
  584. #define __FUNCT__ "TOPS::StructuredSolver_impl::setupParameterPort"
  585. // First, get parameters
  586. ppf = ::babel_cast< gov::cca::ports::ParameterPortFactory >( myServices.getPort("ParameterPortFactory") );
  587. if (ppf._is_nil()) {
  588. std::cerr << "TOPS::StructuredSolver_impl::setupParameterPort: called without ParameterPortFactory connected." << std::endl;
  589. return -1;
  590. }
  591. gov::cca::TypeMap tm = myServices.createTypeMap();
  592. if (tm._is_nil()) {
  593. std::cerr << "TOPS::StructuredSolver_impl::setupParameterPort: myServices.createTypeMap failed." << std::endl;
  594. return -1;
  595. }
  596. ppf.initParameterData(tm, "tops_options");
  597. ppf.setBatchTitle(tm, "TOPS Options");
  598. ppf.addRequestString(tm, "options", "Space-separated list of TOPS options",
  599. "Enter runtime TOPS options", "-help");
  600. // We may want to respond to changes
  601. gov::cca::ports::ParameterSetListener paramSetListener = (*this);
  602. ppf.registerUpdatedListener(tm, paramSetListener);
  603. // We may want to change the parameters before sharing them
  604. gov::cca::ports::ParameterGetListener paramGetListener = (*this);
  605. ppf.registerUpdater(tm, paramGetListener);
  606. // publish the parameter port and release the parameter port factory
  607. ppf.addParameterPort(tm, myServices);
  608. myServices.releasePort("ParameterPortFactory");
  609. return 0;
  610. }
  611. // DO-NOT-DELETE splicer.end(TOPS.StructuredSolver._misc)