48 #if !defined(HAVE_MUELU_MATLAB) || !defined(HAVE_MUELU_EPETRA)
49 #error "Muemex types require MATLAB, Epetra and Tpetra."
53 #if (defined(MX_API_VER) && MX_API_VER >= 0x07030000)
59 using namespace Teuchos;
64 template class MuemexData<RCP<Xpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
65 template class MuemexData<RCP<Xpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
66 template class MuemexData<RCP<Xpetra::Matrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
67 template class MuemexData<RCP<Xpetra::Matrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
68 template class MuemexData<RCP<MAggregates> >;
69 template class MuemexData<RCP<MAmalInfo> >;
70 template class MuemexData<int>;
71 template class MuemexData<bool>;
72 template class MuemexData<complex_t>;
73 template class MuemexData<string>;
74 template class MuemexData<double>;
75 template class MuemexData<RCP<Tpetra::CrsMatrix<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
76 template class MuemexData<RCP<Tpetra::CrsMatrix<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
77 template class MuemexData<RCP<Epetra_MultiVector> >;
78 template class MuemexData<RCP<Tpetra::MultiVector<double, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
79 template class MuemexData<RCP<Tpetra::MultiVector<complex_t, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
80 template class MuemexData<RCP<Xpetra::Vector<mm_LocalOrd, mm_LocalOrd, mm_GlobalOrd, mm_node_t> > >;
88 for (
int i = 0; i < N; i++)
89 rv[i] = (
int)mwi_array[i];
99 return mxCreateSparse(numRows, numCols, nnz, mxREAL);
104 return mxCreateSparse(numRows, numCols, nnz, mxCOMPLEX);
109 memcpy(mxGetPr(mxa), array, n *
sizeof(
double));
114 double* pr = mxGetPr(mxa);
115 double* pi = mxGetPi(mxa);
116 for (
int i = 0; i < n; i++) {
117 pr[i] = std::real<double>(array[i]);
118 pi[i] = std::imag<double>(array[i]);
127 int result = mexEvalString(
function.c_str());
129 mexPrintf(
"An error occurred while running a MATLAB command.");
133 using Teuchos::rcp_static_cast;
134 mxArray** matlabArgs =
new mxArray*[args.size()];
135 mxArray** matlabOutput =
new mxArray*[numOutputs];
136 std::vector<RCP<MuemexArg> > output;
138 for (
int i = 0; i < int(args.size()); i++) {
140 switch (args[i]->type) {
145 matlabArgs[i] = rcp_static_cast<
MuemexData<int>, MuemexArg>(args[i])->convertToMatlab();
148 matlabArgs[i] = rcp_static_cast<
MuemexData<double>, MuemexArg>(args[i])->convertToMatlab();
200 #ifdef HAVE_MUELU_INTREPID2
201 case FIELDCONTAINER_ORDINAL:
206 }
catch (std::exception& e) {
207 mexPrintf(
"An error occurred while converting arg #%d to MATLAB:\n", i);
208 std::cout << e.what() << std::endl;
209 mexPrintf(
"Passing 0 instead.\n");
210 matlabArgs[i] = mxCreateDoubleScalar(0);
214 int result = mexCallMATLAB(numOutputs, matlabOutput, args.size(), matlabArgs,
function.c_str());
216 mexPrintf(
"Matlab encountered an error while running command through muemexCallbacks.\n");
218 for (
int i = 0; i < numOutputs; i++) {
221 }
catch (std::exception& e) {
222 mexPrintf(
"An error occurred while converting output #%d from MATLAB:\n", i);
223 std::cout << e.what() << std::endl;
226 delete[] matlabOutput;
237 return mxCreateDoubleMatrix(numRows, numCols, mxREAL);
242 return mxCreateDoubleMatrix(numRows, numCols, mxCOMPLEX);
246 throw runtime_error(
"AmalgamationInfo not supported in MueMex yet.");
247 return mxCreateDoubleScalar(0);
251 bool isValidAggregates =
true;
252 if (!mxIsStruct(mxa))
254 int numFields = mxGetNumberOfFields(mxa);
256 isValidAggregates =
false;
257 if (isValidAggregates) {
258 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
259 if (mem1 == NULL || strcmp(mem1,
"nVertices") != 0)
260 isValidAggregates =
false;
261 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
262 if (mem2 == NULL || strcmp(mem2,
"nAggregates") != 0)
263 isValidAggregates =
false;
264 const char* mem3 = mxGetFieldNameByNumber(mxa, 2);
265 if (mem3 == NULL || strcmp(mem3,
"vertexToAggID") != 0)
266 isValidAggregates =
false;
267 const char* mem4 = mxGetFieldNameByNumber(mxa, 3);
268 if (mem3 == NULL || strcmp(mem4,
"rootNodes") != 0)
269 isValidAggregates =
false;
270 const char* mem5 = mxGetFieldNameByNumber(mxa, 4);
271 if (mem4 == NULL || strcmp(mem5,
"aggSizes") != 0)
272 isValidAggregates =
false;
274 return isValidAggregates;
278 bool isValidGraph =
true;
279 if (!mxIsStruct(mxa))
281 int numFields = mxGetNumberOfFields(mxa);
283 isValidGraph =
false;
285 const char* mem1 = mxGetFieldNameByNumber(mxa, 0);
286 if (mem1 == NULL || strcmp(mem1,
"edges") != 0)
287 isValidGraph =
false;
288 const char* mem2 = mxGetFieldNameByNumber(mxa, 1);
289 if (mem2 == NULL || strcmp(mem2,
"boundaryNodes") != 0)
290 isValidGraph =
false;
297 vector<string> rlist;
298 const char* delims =
",";
299 char* copy = (
char*)malloc(params.length() + 1);
300 strcpy(copy, params.c_str());
301 char* mark = (
char*)strtok(copy, delims);
302 while (mark != NULL) {
304 char* tail = mark + strlen(mark) - 1;
307 while (*tail ==
' ' && tail > mark)
312 rlist.push_back(tok);
313 mark = strtok(NULL, delims);
320 using namespace Teuchos;
325 validParamList->set<
RCP<const FactoryBase> >(
"Ptent", Teuchos::null,
"Factory for the tentative (unsmoothed) prolongator.");
326 validParamList->set<
RCP<const FactoryBase> >(
"Coordinates", Teuchos::null,
"Factory for the node coordinates.");
329 validParamList->set<
RCP<const FactoryBase> >(
"UnamalgamationInfo", Teuchos::null,
"Factory for amalgamation.");
330 #ifdef HAVE_MUELU_INTREPID2
331 validParamList->set<
RCP<const FactoryBase> >(
"pcoarsen: element to node map", Teuchos::null,
"Generating factory of the element to node map");
333 return validParamList;
337 switch (mxGetClassID(mxa)) {
342 case mxLOGICAL_CLASS:
347 if (mxGetM(mxa) == 1 && mxGetN(mxa) == 1)
350 else if (mxGetM(mxa) != 1 || mxGetN(mxa) != 1)
354 throw std::runtime_error(
"Error: Don't know what to do with integer array.\n");
357 if (mxGetM(mxa) == 1 && mxGetN(mxa) == 1) {
358 if (mxIsComplex(mxa))
364 }
else if (mxIsSparse(mxa))
367 if (mxIsComplex(mxa))
375 if (mxIsComplex(mxa))
381 case mxSTRUCT_CLASS: {
390 throw runtime_error(
"Invalid aggregates or graph struct passed in from MATLAB.");
391 return Teuchos::null;
396 throw std::runtime_error(
"MATLAB returned an unsupported type as a function output.\n");
397 return Teuchos::null;
421 template RCP<MAggregates> loadDataFromMatlab<RCP<MAggregates> >(
const mxArray* mxa);
422 template RCP<MAmalInfo> loadDataFromMatlab<RCP<MAmalInfo> >(
const mxArray* mxa);
443 template vector<RCP<MuemexArg> >
processNeeds<double>(
const Factory* factory,
string& needsParam, Level& lvl);
444 template vector<RCP<MuemexArg> >
processNeeds<complex_t>(
const Factory* factory,
string& needsParam, Level& lvl);
445 template void processProvides<double>(vector<RCP<MuemexArg> >& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
446 template void processProvides<complex_t>(vector<RCP<MuemexArg> >& mexOutput,
const Factory* factory,
string& providesParam, Level& lvl);
449 #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)
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)