ElmerFoamFSI  2.0
ElmerFoamFSI is fluid-solid interaction simulation application built up from OpenFOAM CFD and Elmer CSM coupled through the IMPACT multiphysics software integration infrastructure.
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros Groups Pages
ElmerFoamDriverPar.C
Go to the documentation of this file.
1 
9 #include "Driver.H"
10 #include "Parameters.H"
11 #include "FsiCouplingPar.H"
12 
13 //typedef SolverUtils::TransferObject transferagent;
14 //typedef openfoamagent fluidagent;
15 //typedef elmeragent solidagent;
16 //typedef IRAD::Profiler::ProfilerObj ProfilerType;
17 //typedef std::string StackType;
18 //typedef IRAD::Global::GlobalObj<StackType,int,ProfilerType> GlobalType;
19 
20 COM_EXTERN_MODULE(ElmerCSCParallel);
21 COM_EXTERN_MODULE(OpenFoamFSIPar);
22 
23 namespace ElmerFoamFSI {
24 
25  int ParallelDriverProgram::DriverRun()
26  {
27 
28  // FunctionEntry("NAME"): Updates the user-defined stack and
29  // the program profiles with timing information. The placement
30  // of FunctionEntry and FunctionExit calls is at the developer's
31  // discretion.
32  FunctionEntry("Run");
33 
34 
35  int myid = _communicator.Rank();
36  int nproc = _communicator.Size();
37  bool use_file = !output_name.empty();
38 
39  // Put out a quick blurb about number of procs just
40  // to give the user confidence we are actually running
41  // in parallel. Note that when using the "StdOut" method
42  // that it automatically does output only on rank 0. If
43  // the developer wants asyncrhonous output, then they
44  // have to revert to using standard streams.
45  std::ostringstream RepStr;
46  if(verblevel > 1)
47  RepStr << "Running on " << nproc << " processors." << std::endl;
48  StdOut(RepStr.str());
49  _communicator.Barrier();
50  if(verblevel > 1)
51  StdOut("All procesors ready.\n");
52 
53  // reading user parameters from input file
54  std::ostringstream outString;
55  IRAD::Util::Parameters userParameters;
56  std::ifstream paramFileIn;
57  paramFileIn.open(input_name.c_str());
58  paramFileIn >> userParameters;
59  outString.clear();
60  outString.str("");
61  outString << "User Parameters: " << std::endl
62  << userParameters << std::endl;
63  StdOut(outString.str(), 1, true);
64  _communicator.Barrier();
65 
66  std::string fluidSolverName(userParameters.GetValue("FluidSolver"));
67  std::string solidSolverName(userParameters.GetValue("SolidSolver"));
68  std::string transferServiceName(userParameters.GetValue("TransferService"));
69  std::string runMode(userParameters.GetValue("RunMode"));
70 
71  // processing other extra parameters passed by user
72  // check request for probing
73  // ProbSolverName =
74  // FluidsComponentInterface : for probing fluid solver
75  // StructuresComponentInterface : for probing structures solver
76 
77  int probProcId(-1), probNdeId(0);
78  std::string probSolverName("");
79  if (userParameters.IsSet("ProbSolverName")) {
80  probSolverName = userParameters.GetValue("ProbSolverName");
81  probProcId = userParameters.GetValue<int>("ProbProcId");
82  probNdeId = userParameters.GetValue<int>("ProbNdeId");
83  if (probProcId>=nproc || probProcId<0) {
84  outString << "The requested process ID for probing is not valid.\n";
85  ErrOut(outString.str());
86  _communicator.SetExit(1);
87  } else {
88  outString.clear();
89  outString.str("");
90  outString << "Probing "
91  << probSolverName
92  << " solution at rank "
93  << probProcId
94  << " registered node "
95  << probNdeId
96  << std::endl;
97  StdOut(outString.str(), 1, true);
98  }
99  }
100  // Check to see if an error condition was set
101  // in the file open block above. If so, then
102  // return with an error code.
103  if(_communicator.Check()){
104  // don't forget to tell the profiler/stacker the
105  // function is exiting.
106  FunctionExit("Run");
107  return(1);
108  }
109 
110  // Open the specified output file for writing on rank 0
111  if(use_file && !myid){
112  Ouf.open(output_name.c_str(),std::ios::app);
113  if(!Ouf){
114  // If the output file failed to open, notify
115  // to error stream and return non-zero
116  std::ostringstream Ostr;
117  Ostr << "Error: Unable to open output file, " << output_name << ".";
118  ErrOut(Ostr.str());
119  // In parallel, we don't return right away since
120  // this part of the code is only done on proc 0.
121  // Instead, the error value is set in the communicator
122  // which will indicate to all processors that there
123  // has been some error.
124  _communicator.SetExit(1);
125  }
126  }
127  // Check to see if an error condition was set
128  // in the file open block above. If so, then
129  // return with an error code.
130  if(_communicator.Check()){
131  // don't forget to tell the profiler/stacker the
132  // function is exiting.
133  FunctionExit("Run");
134  return(1);
135  }
136 
137  double time_final = 0;
138  time_final = userParameters.GetValue<double>("FinalTime");
139  if(time_final <= 0){
140  // setting to default
141  time_final = 1.0e-3;
142  }
143 
144  double timestep = 0;
145  timestep = userParameters.GetValue<double>("TimeStep");
146  if(timestep <= 1e-5){
147  // setting to default
148  timestep = 1.0e-5;
149  }
150 
151  if(fluidSolverName.empty()){
152  outString << "Failed to specify FluidSolver parameter.\n";
153  ErrOut(outString.str());
154  _communicator.SetExit(1);
155  }
156  if(solidSolverName.empty()){
157  outString << "Failed to specify SolidSolver parameter.\n";
158  ErrOut(outString.str());
159  _communicator.SetExit(1);
160  }
161 
162  // Check to see if an error condition was set
163  // in the file open block above. If so, then
164  // return with an error code.
165  if(_communicator.Check()){
166  // don't forget to tell the profiler/stacker the
167  // function is exiting.
168  FunctionExit("Run");
169  return(1);
170  }
171 
172  FunctionEntry("LoadModules");
173  COM_load_module(fluidSolverName.c_str(),"FluidsComponentInterface");
174  COM_load_module(solidSolverName.c_str(),"StructuresComponentInterface");
175  COM_load_module("SurfUtil","SurfUtil");
176  COM_load_module("Simpal","Simpal");
177  COM_load_module("SimOUT","Simout");
178  FunctionExit("LoadModules");
179 
180  fsicouplingpar fsiCouplerPar(*this);
181  fsiCouplerPar.SetRunMode(runMode);
182  fsiCouplerPar.SetVerbLevel(verblevel);
183  fsiCouplerPar.SetProbe(probProcId, probNdeId, probSolverName);
184 
185 
186  std::vector<std::string> componentInterfaceNames;
187  componentInterfaceNames.push_back("FluidsComponentInterface");
188  componentInterfaceNames.push_back("StructuresComponentInterface");
189  componentInterfaceNames.push_back("TransferInterface");
190  componentInterfaceNames.push_back("SurfUtil");
191  componentInterfaceNames.push_back("Simpal");
192  componentInterfaceNames.push_back("Simout");
193 
194 
195  FunctionEntry("fsiCouplerPar.Initialize");
196  fsiCouplerPar.Initialize(componentInterfaceNames, time_final, timestep);
197  FunctionExit("fsiCouplerPar.Initialize");
198  _communicator.Barrier();
199 
200  FunctionEntry("fsiCouplerPar.Run");
201  fsiCouplerPar.Run();
202  FunctionExit("fsiCouplerPar.Run");
203  _communicator.Barrier();
204 
205  FunctionEntry("fsiCouplerPar.Finalize");
206  fsiCouplerPar.Finalize();
207  FunctionExit("fsiCouplerPar.Finalize");
208  _communicator.Barrier();
209 
210  COM_unload_module(fluidSolverName.c_str(),"FluidsComponentInterface");
211  COM_unload_module(solidSolverName.c_str(),"StructuresComponentInterface");
212  _communicator.Barrier();
213 
214  outString << "Before Running FunctionExit" << std::endl;
215  StdOut(outString.str(),2,true);
216  outString.clear();
217  outString.str("");
218 
219  FunctionExit("Run");
220  outString << "After Running FunctionExit" << std::endl;
221  StdOut(outString.str(),2,true);
222  outString.clear();
223  outString.str("");
224 
225  // return 0 for success
226  return(0);
227  };
228 };
Example serial program.
Example program interface.
COM_EXTERN_MODULE(OpenFoamFSI)