Tutorial:Creating an Utility

From KratosWiki
Jump to: navigation, search

Utilities are useful tools to perform arbitrary actions. Furthermore, it is possible to create the solver to your specific problems and avoid using the builder and solver, for example to create a new edgebased builder. For educational porpuses, we will create here a simple utility that calculates the mean temperature inside the domain once the solution has been obtained.

Modifying add_custom_utilites_to_python.cpp

To begin with we have to tell Kratos that we have an Utility. Doing so is straightforward since we only need to edit the file custom_python/add_custom_utilities_to_python.cpp , including the source of our utility and some lines that serve as an interfase between the C++ functions and python. Once you've done this Kratos knows you have a new utility and the functions you've declared in this file can be called directly from python. Have in mind that in the constructor you must write the input arguments, while in all the other functions you just type the name of them. Below is the file, with the new lines in bold.

// Project includes
#include "includes/define.h"
#include "processes/process.h"
#include "custom_python/add_custom_utilities_to_python.h"

#include "spaces/ublas_space.h"
#include "linear_solvers/linear_solver.h"

#include "custom_utilities/calculate_mean_temp.h"

namespace Kratos
namespace Python

  void  AddCustomUtilitiesToPython()
	using namespace boost::python;

		typedef UblasSpace<double, CompressedMatrix, Vector> SparseSpaceType;
		typedef UblasSpace<double, Matrix, Vector> LocalSpaceType;
		typedef LinearSolver<SparseSpaceType, LocalSpaceType > LinearSolverType;
		class_<CalculateMeanTemperature > ("CalculateMeanTemperature", init<ModelPart& >())  //the input parameters is a model part 
                   .def("Execute", &CalculateMeanTemperature::Calculate)  //when we call "Execute" in python, Calculate is called in C++. Notice we don't write the input parameters here 


}  // namespace Python.

} // Namespace Kratos

Creating custom_utilities/calculate_mean_temp.h


// System includes
#include <string>
#include <iostream> 
#include <algorithm>

// Project includes 
#include "includes/define.h"
#include "purediffusion_application.h"
#include "utilities/math_utils.h"
#include "utilities/geometry_utilities.h" 
#include "includes/ublas_interface.h"
#include "includes/variables.h" 
#include "includes/model_part.h"
#include "includes/node.h"
#include "includes/element.h"

namespace Kratos
	//this class is to be modified by the user to customize the interpolation process
	//template< unsigned int TDim>
	class CalculateMeanTemperature 

		CalculateMeanTemperature(ModelPart& model_part)
			: mr_model_part(model_part)              //mr_model_part is saved as private variable (declared at the end of the file)  
			std::cout << "Hello, I am the constructor of the Utility" << std::endl; 


 		void Calculate()  
			double area;                    //we create the needed variables
			double sum_areas=0.0;
			double sum_temperatures=0.0;
			double one_third=1.0/3.0;

			//getting data for the given geometry
			for(ModelPart::ElementsContainerType::iterator ielem = mr_model_part.ElementsBegin(); //looping the elements
				ielem!=mr_model_part.ElementsEnd(); ielem++)
				Geometry<Node<3> >& geom = ielem->GetGeometry(); 
				area=CalculateArea(geom);                            //we call CalculateArea (private function)  
				for (unsigned int k = 0; k < 3; k++)
					sum_temperatures += geom[k].FastGetSolutionStepValue(TEMPERATURE)*one_third*area;
			const double mean_temperature = sum_temperatures / sum_areas;
			std::cout << "Finished, the mean temperature is" << mean_temperature << std::endl;   //we print the result  

		double CalculateArea(Element::GeometryType& geom)
			return 0.5 * ((geom[1].X() - geom[0].X())*(geom[2].Y() - geom[0].Y())- (geom[1].Y() - geom[0].Y())*(geom[2].X() - geom[0].X()));
		ModelPart& mr_model_part;


}  // namespace Kratos.


And that's it, this is all you need, to use it you must first call the constructor and then execute it.

back to Kratos For Dummies

Personal tools