12 #if !defined(HAVE_MUELU_MATLAB) || !defined(HAVE_MUELU_TPETRA)
13 #error "Muemex types require MATLAB and Tpetra."
17 #if (defined(MX_API_VER) && MX_API_VER >= 0x07030000)
23 using namespace Teuchos;
28 template class MuemexData<RCP<Xpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
29 template class MuemexData<RCP<Xpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
30 template class MuemexData<RCP<Xpetra::Matrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
31 template class MuemexData<RCP<Xpetra::Matrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
32 template class MuemexData<RCP<MAggregates> >;
33 template class MuemexData<RCP<MAmalInfo> >;
34 template class MuemexData<int>;
35 template class MuemexData<bool>;
36 template class MuemexData<complex_t>;
37 template class MuemexData<string>;
38 template class MuemexData<double>;
39 template class MuemexData<RCP<Tpetra::CrsMatrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
40 template class MuemexData<RCP<Tpetra::CrsMatrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
41 #ifdef HAVE_MUELU_EPETRA
42 template class MuemexData<RCP<Epetra_MultiVector> >;
44 template class MuemexData<RCP<Tpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
45 template class MuemexData<RCP<Tpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
46 template class MuemexData<RCP<Xpetra::Vector<mm_LocalOrd, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
54 for (
int i = 0; i < N; i++)
55 rv[i] = (
int)mwi_array[i];
65 return mxCreateSparse(numRows, numCols, nnz, mxREAL);
70 return mxCreateSparse(numRows, numCols, nnz, mxCOMPLEX);
75 memcpy(mxGetPr(mxa), array, n *
sizeof(
double));
80 double* pr = mxGetPr(mxa);
81 double* pi = mxGetPi(mxa);
82 for (
int i = 0; i < n; i++) {
83 pr[i] = std::real<double>(array[i]);
84 pi[i] = std::imag<double>(array[i]);
93 int result = mexEvalString(
function.c_str());
95 mexPrintf(
"An error occurred while running a MATLAB command.");
99 using Teuchos::rcp_static_cast;
102 std::vector<RCP<MuemexArg> > output;
104 for (
int i = 0; i < int(args.size()); i++) {
106 switch (args[i]->type) {
111 matlabArgs[i] = rcp_static_cast<
MuemexData<int>, MuemexArg>(args[i])->convertToMatlab();
114 matlabArgs[i] = rcp_static_cast<
MuemexData<double>, MuemexArg>(args[i])->convertToMatlab();
152 #ifdef HAVE_MUELU_EPETRA
168 #ifdef HAVE_MUELU_INTREPID2
169 case FIELDCONTAINER_ORDINAL:
174 }
catch (std::exception& e) {
175 mexPrintf(
"An error occurred while converting arg #%d to MATLAB:\n", i);
176 std::cout << e.what() << std::endl;
177 mexPrintf(
"Passing 0 instead.\n");
178 matlabArgs[i] = mxCreateDoubleScalar(0);
182 int result = mexCallMATLAB(numOutputs, matlabOutput, args.size(), matlabArgs,
function.c_str());
184 mexPrintf(
"Matlab encountered an error while running command through muemexCallbacks.\n");
186 for (
int i = 0; i < numOutputs; i++) {
189 }
catch (std::exception& e) {
190 mexPrintf(
"An error occurred while converting output #%d from MATLAB:\n", i);
191 std::cout << e.what() << std::endl;
194 delete[] matlabOutput;
205 return mxCreateDoubleMatrix(numRows, numCols, mxREAL);
210 return mxCreateDoubleMatrix(numRows, numCols, mxCOMPLEX);
214 throw runtime_error(
"AmalgamationInfo not supported in MueMex yet.");
215 return mxCreateDoubleScalar(0);
219 bool isValidAggregates =
true;
220 if (!mxIsStruct(mxa))
222 int numFields = mxGetNumberOfFields(mxa);
224 isValidAggregates =
false;
225 if (isValidAggregates) {
226 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
227 if (mem1 == NULL || strcmp(mem1,
"nVertices") != 0)
228 isValidAggregates =
false;
229 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
230 if (mem2 == NULL || strcmp(mem2,
"nAggregates") != 0)
231 isValidAggregates =
false;
232 const char* mem3 = mxGetFieldNameByNumber(mxa, 2);
233 if (mem3 == NULL || strcmp(mem3,
"vertexToAggID") != 0)
234 isValidAggregates =
false;
235 const char* mem4 = mxGetFieldNameByNumber(mxa, 3);
236 if (mem3 == NULL || strcmp(mem4,
"rootNodes") != 0)
237 isValidAggregates =
false;
238 const char* mem5 = mxGetFieldNameByNumber(mxa, 4);
239 if (mem4 == NULL || strcmp(mem5,
"aggSizes") != 0)
240 isValidAggregates =
false;
242 return isValidAggregates;
246 bool isValidGraph =
true;
247 if (!mxIsStruct(mxa))
249 int numFields = mxGetNumberOfFields(mxa);
251 isValidGraph =
false;
253 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
254 if (mem1 == NULL || strcmp(mem1,
"edges") != 0)
255 isValidGraph =
false;
256 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
257 if (mem2 == NULL || strcmp(mem2,
"boundaryNodes") != 0)
258 isValidGraph =
false;
265 vector<string> rlist;
266 const char* delims =
",";
267 char* copy = (
char*)malloc(params.length() + 1);
268 strcpy(copy, params.c_str());
269 char* mark = (
char*)strtok(copy, delims);
270 while (mark != NULL) {
272 char* tail = mark + strlen(mark) - 1;
275 while (*tail ==
' ' && tail > mark)
280 rlist.push_back(tok);
281 mark = strtok(NULL, delims);
288 using namespace Teuchos;
293 validParamList->set<
RCP<const FactoryBase> >(
"Ptent", Teuchos::null,
"Factory for the tentative (unsmoothed) prolongator.");
294 validParamList->set<
RCP<const FactoryBase> >(
"Coordinates", Teuchos::null,
"Factory for the node coordinates.");
297 validParamList->set<
RCP<const FactoryBase> >(
"UnamalgamationInfo", Teuchos::null,
"Factory for amalgamation.");
298 #ifdef HAVE_MUELU_INTREPID2
299 validParamList->set<
RCP<const FactoryBase> >(
"pcoarsen: element to node map", Teuchos::null,
"Generating factory of the element to node map");
301 return validParamList;
305 switch (mxGetClassID(mxa)) {
310 case mxLOGICAL_CLASS:
315 if (mxGetM(mxa) == 1 && mxGetN(mxa) == 1)
318 else if (mxGetM(mxa) != 1 || mxGetN(mxa) != 1)
322 throw std::runtime_error(
"Error: Don't know what to do with integer array.\n");
325 if (mxGetM(mxa) == 1 && mxGetN(mxa) == 1) {
326 if (mxIsComplex(mxa))
332 }
else if (mxIsSparse(mxa))
335 if (mxIsComplex(mxa))
343 if (mxIsComplex(mxa))
349 case mxSTRUCT_CLASS: {
358 throw runtime_error(
"Invalid aggregates or graph struct passed in from MATLAB.");
359 return Teuchos::null;
364 throw std::runtime_error(
"MATLAB returned an unsupported type as a function output.\n");
365 return Teuchos::null;
387 #ifdef HAVE_MUELU_EPETRA
408 #ifdef HAVE_MUELU_EPETRA
415 template vector<RCP<MuemexArg> >
processNeeds<double>(
const Factory* factory,
string& needsParam, Level& lvl);
416 template vector<RCP<MuemexArg> >
processNeeds<complex_t>(
const Factory* factory,
string& needsParam, Level& lvl);
417 template void processProvides<double>(vector<RCP<MuemexArg> >& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
418 template void processProvides<complex_t>(vector<RCP<MuemexArg> >& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
421 #endif // HAVE_MUELU_MATLAB
bool isValidMatlabAggregates(const mxArray *mxa)
template mxArray * saveDataToMatlab(bool &data)
std::vector< std::string > tokenizeList(const std::string ¶ms)
mxArray * createMatlabSparse< complex_t >(int numRows, int numCols, int nnz)
template vector< RCP< MuemexArg > > processNeeds< complex_t >(const Factory *factory, string &needsParam, Level &lvl)
template void processProvides< complex_t >(vector< RCP< MuemexArg > > &mexOutput, const Factory *factory, string &providesParam, Level &lvl)
mxArray * saveAmalInfo(RCP< MAmalInfo > &amalInfo)
std::vector< RCP< MuemexArg > > callMatlab(std::string function, int numOutputs, std::vector< RCP< MuemexArg > > args)
template vector< RCP< MuemexArg > > processNeeds< double >(const Factory *factory, string &needsParam, Level &lvl)
void fillMatlabArray< double >(double *array, const mxArray *mxa, int n)
bool isValidMatlabGraph(const mxArray *mxa)
template string loadDataFromMatlab< string >(const mxArray *mxa)
Teuchos::RCP< Teuchos::ParameterList > getInputParamList()
template void processProvides< double >(vector< RCP< MuemexArg > > &mexOutput, const Factory *factory, string &providesParam, Level &lvl)
struct mxArray_tag mxArray
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void fillMatlabArray< complex_t >(complex_t *array, const mxArray *mxa, int n)
mxArray * createMatlabSparse< double >(int numRows, int numCols, int nnz)
int * mwIndex_to_int(int N, mwIndex *mwi_array)
template complex_t loadDataFromMatlab< complex_t >(const mxArray *mxa)
template int loadDataFromMatlab< int >(const mxArray *mxa)
template bool loadDataFromMatlab< bool >(const mxArray *mxa)
mxArray * createMatlabMultiVector< complex_t >(int numRows, int numCols)
void callMatlabNoArgs(std::string function)
std::complex< double > complex_t
mxArray * createMatlabMultiVector< double >(int numRows, int numCols)
template double loadDataFromMatlab< double >(const mxArray *mxa)
Teuchos::RCP< MuemexArg > convertMatlabVar(const mxArray *mxa)